diff --git a/Modules/Core/Common/include/otbConfigurationManager.h b/Modules/Core/Common/include/otbConfigurationManager.h index 5dd079f012f4c906615f06cd5a94916fdb807882..33cb1c53b741d11cbf7dcbc50a2776abecc54e54 100644 --- a/Modules/Core/Common/include/otbConfigurationManager.h +++ b/Modules/Core/Common/include/otbConfigurationManager.h @@ -103,8 +103,15 @@ public: * */ static itk::LoggerBase::PriorityLevelType GetLoggerLevel(); - - + + /** + * If OpenMP is enabled, the number of threads for openMP is set to the + * same number as in ITK (see GetGlobalDefaultNumberOfThreads()). This number + * of threads is returned. + * If OpenMP is disabled, this function does nothing + */ + static int InitOpenMPThreads(); + private: ConfigurationManager() = delete; ~ConfigurationManager() = delete; diff --git a/Modules/Core/Common/src/otbConfigurationManager.cxx b/Modules/Core/Common/src/otbConfigurationManager.cxx index 7a48580b5ac96801fe932c384fa21f1ef732095b..a04dfd56c8bad67fc0d0d6c5c499db86c7c564da 100644 --- a/Modules/Core/Common/src/otbConfigurationManager.cxx +++ b/Modules/Core/Common/src/otbConfigurationManager.cxx @@ -23,8 +23,13 @@ #include "otbMacro.h" #include "otbLogger.h" +#include "itkMultiThreader.h" #include "itksys/SystemTools.hxx" +#ifdef _OPENMP +#include <omp.h> +#endif + #include <cstdlib> #include <algorithm> #include <string> @@ -99,4 +104,14 @@ itk::LoggerBase::PriorityLevelType ConfigurationManager::GetLoggerLevel() return level; } +int ConfigurationManager::InitOpenMPThreads() +{ + int ret = 1; +#ifdef _OPENMP + ret = itk::MultiThreader::GetGlobalDefaultNumberOfThreads(); + omp_set_num_threads(ret); +#endif + return ret; +} + } diff --git a/Modules/IO/TestKernel/include/otbTestMain.h b/Modules/IO/TestKernel/include/otbTestMain.h index 852b3bf50cf3ff0f9706af1a84f5426bc2d0f28a..83e8126cb58e2ab61a548f492090cb74c8384373 100644 --- a/Modules/IO/TestKernel/include/otbTestMain.h +++ b/Modules/IO/TestKernel/include/otbTestMain.h @@ -22,6 +22,7 @@ #define otbTestMain_h #include "otbConfigure.h" +#include "otbConfigurationManager.h" #include <map> #include <string> @@ -72,6 +73,8 @@ int main(int ac, char* av[]) #ifdef OTB_USE_MPI otb::MPIConfig::Instance()->Init(ac,av); #endif + + otb::ConfigurationManager::InitOpenMPThreads(); bool lFlagRegression(false); double lToleranceDiffValue(0); diff --git a/Modules/Visualization/Mapla/src/main.cxx b/Modules/Visualization/Mapla/src/main.cxx index 612113459a3241160f2647ef5b16df4aa2c8e0ff..df01d6ef4387a10284c284aec71b5f1276ec0af9 100644 --- a/Modules/Visualization/Mapla/src/main.cxx +++ b/Modules/Visualization/Mapla/src/main.cxx @@ -44,6 +44,7 @@ // // OTB includes (sorted by alphabetic order) +#include "otbConfigurationManager.h" // // Monteverdi includes (sorted by alphabetic order) @@ -70,6 +71,8 @@ main( int argc, char* argv[] ) { QApplication qtApp( argc, argv ); + otb::ConfigurationManager::InitOpenMPThreads(); + // // 0. Splash-screen. #if !defined( OTB_DEBUG ) diff --git a/Modules/Visualization/Monteverdi/src/main.cxx b/Modules/Visualization/Monteverdi/src/main.cxx index 93e65f21add158b28de4b5b3239cef36e6a20c55..c8760ae691d752d2add6a3af5ca1a180c735199d 100644 --- a/Modules/Visualization/Monteverdi/src/main.cxx +++ b/Modules/Visualization/Monteverdi/src/main.cxx @@ -48,6 +48,7 @@ // // OTB includes (sorted by alphabetic order) +#include "otbConfigurationManager.h" // // Monteverdi includes (sorted by alphabetic order) @@ -97,6 +98,8 @@ main( int argc, char * argv[] ) { QApplication qtApp( argc, argv ); + otb::ConfigurationManager::InitOpenMPThreads(); + // // 0. Splash-screen. #if USE_SPLASH_SCREEN diff --git a/Modules/Wrappers/CommandLine/src/otbApplicationLauncherCommandLine.cxx b/Modules/Wrappers/CommandLine/src/otbApplicationLauncherCommandLine.cxx index a7618818242cf2f1ca3896ad9bf346b5f98afe52..d367a0aba094d0e53c9995b17ca04ca9d56db2f8 100644 --- a/Modules/Wrappers/CommandLine/src/otbApplicationLauncherCommandLine.cxx +++ b/Modules/Wrappers/CommandLine/src/otbApplicationLauncherCommandLine.cxx @@ -20,6 +20,7 @@ #include "otbWrapperCommandLineLauncher.h" +#include "otbConfigurationManager.h" #include "otb_tinyxml.h" #include <vector> @@ -267,6 +268,8 @@ int main(int argc, char* argv[]) otb::MPIConfig::Instance()->Init(argc,argv); #endif + otb::ConfigurationManager::InitOpenMPThreads(); + if (argc < 2) { ShowUsage(argv); diff --git a/Modules/Wrappers/QtWidget/src/otbApplicationLauncherQt.cxx b/Modules/Wrappers/QtWidget/src/otbApplicationLauncherQt.cxx index bb916d1f9a0069a6e90e25ed759a29fecd832c50..105c4994d18b1670ae5e00bdd869e35c236908f2 100644 --- a/Modules/Wrappers/QtWidget/src/otbApplicationLauncherQt.cxx +++ b/Modules/Wrappers/QtWidget/src/otbApplicationLauncherQt.cxx @@ -19,6 +19,7 @@ */ #include <QtWidgets> +#include "otbConfigurationManager.h" #include "otbWrapperApplicationRegistry.h" #include "otbWrapperQtWidgetView.h" #include "otbWrapperQtWidgetSimpleProgressReport.h" @@ -43,6 +44,8 @@ int main(int argc, char* argv[]) //////////////////////////////////////////////////////////////////*/ QtApplication qtApp(argc, argv); + otb::ConfigurationManager::InitOpenMPThreads(); + if (argc < 2) { std::cerr << "Usage : " << argv[0] << " module_name [module_path]" << std::endl;