From b912cf140d51a16e7120e230c6ff8c8fc108113d Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Mon, 12 Nov 2018 11:45:31 +0100
Subject: [PATCH] BUG: #1761: make sure OpenMP runtime is loaded by the main
 executable

---
 .../Core/Common/include/otbConfigurationManager.h | 11 +++++++++--
 .../Core/Common/src/otbConfigurationManager.cxx   | 15 +++++++++++++++
 Modules/IO/TestKernel/include/otbTestMain.h       |  3 +++
 Modules/Visualization/Mapla/src/main.cxx          |  3 +++
 Modules/Visualization/Monteverdi/src/main.cxx     |  3 +++
 .../src/otbApplicationLauncherCommandLine.cxx     |  3 +++
 .../QtWidget/src/otbApplicationLauncherQt.cxx     |  3 +++
 7 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/Modules/Core/Common/include/otbConfigurationManager.h b/Modules/Core/Common/include/otbConfigurationManager.h
index 5dd079f012..33cb1c53b7 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 7a48580b5a..a04dfd56c8 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 852b3bf50c..83e8126cb5 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 612113459a..df01d6ef43 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 93e65f21ad..c8760ae691 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 a761881824..d367a0aba0 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 bb916d1f9a..105c4994d1 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;
-- 
GitLab