You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by jx...@apache.org on 2018/02/26 18:09:50 UTC

[incubator-mxnet] branch master updated: Set worker thread to use OMP when necessary (and not to when not nece… (#9801)

This is an automated email from the ASF dual-hosted git repository.

jxie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new 4bcca8c  Set worker thread to use OMP when necessary (and not to when not nece… (#9801)
4bcca8c is described below

commit 4bcca8cc00986b23116cc7841d162ed2111ded83
Author: Chris Olivier <cj...@gmail.com>
AuthorDate: Mon Feb 26 10:09:45 2018 -0800

    Set worker thread to use OMP when necessary (and not to when not nece… (#9801)
    
    * Set worker thread to use OMP when necessary (and not to when not necessary)
    
    * No change to omp config if environment var is set
    
    * Unit test for omp changes
---
 src/engine/openmp.cc                     | 14 ++++++--
 src/engine/openmp.h                      |  9 ++++-
 src/engine/threaded_engine_perdevice.cc  | 10 ++++++
 tests/cpp/engine/threaded_engine_test.cc | 59 ++++++++++++++++++++++++++++++++
 tests/cpp/include/test_util.h            | 15 ++++++++
 5 files changed, 103 insertions(+), 4 deletions(-)

diff --git a/src/engine/openmp.cc b/src/engine/openmp.cc
index bd7f7fd..8fe3939 100644
--- a/src/engine/openmp.cc
+++ b/src/engine/openmp.cc
@@ -39,13 +39,13 @@ OpenMP *OpenMP::Get() {
 }
 
 OpenMP::OpenMP()
-  : omp_num_threads_set_in_environment(is_env_set("OMP_NUM_THREADS")) {
+  : omp_num_threads_set_in_environment_(is_env_set("OMP_NUM_THREADS")) {
 #ifdef _OPENMP
   const int max = dmlc::GetEnv("MXNET_OMP_MAX_THREADS", INT_MIN);
   if (max != INT_MIN) {
     omp_thread_max_ = max;
   } else {
-    if (!omp_num_threads_set_in_environment) {
+    if (!omp_num_threads_set_in_environment_) {
       omp_thread_max_ = omp_get_num_procs();
 #ifdef ARCH_IS_INTEL_X86
       omp_thread_max_ >>= 1;
@@ -61,6 +61,14 @@ OpenMP::OpenMP()
 #endif
 }
 
+void OpenMP::on_start_worker_thread(bool use_omp) {
+#ifdef _OPENMP
+  if (!omp_num_threads_set_in_environment_) {
+    omp_set_num_threads(use_omp ? GetRecommendedOMPThreadCount(true) : 1);
+  }
+#endif
+}
+
 void OpenMP::set_reserve_cores(int cores) {
   CHECK_GE(cores, 0);
   reserve_cores_ = cores;
@@ -75,7 +83,7 @@ void OpenMP::set_reserve_cores(int cores) {
 
 int OpenMP::GetRecommendedOMPThreadCount(bool exclude_reserved) const {
 #ifdef _OPENMP
-  if (omp_num_threads_set_in_environment) {
+  if (omp_num_threads_set_in_environment_) {
     return omp_get_max_threads();
   }
   if (enabled_) {
diff --git a/src/engine/openmp.h b/src/engine/openmp.h
index 02e73c0..800ea2f 100644
--- a/src/engine/openmp.h
+++ b/src/engine/openmp.h
@@ -68,6 +68,13 @@ class OpenMP {
   int reserve_cores() const { return reserve_cores_; }
 
   /*!
+   * \brief Call at the beginning of a worker thread's life.  This will set the omp_num_threads
+   *        for omp regions created by this thread
+   * \param use_omp true if this thread plans to utilize parallel omp regions
+   */
+  void on_start_worker_thread(bool use_omp);
+
+  /*!
    * \brief Get the OpenMP object's singleton pointer
    * \return Singleton OpenMP object pointer
    */
@@ -91,7 +98,7 @@ class OpenMP {
    * \brief Whether OMP_NUM_THREADS was set in the environment.  If it is, we fall back to
    *        the OMP's implementation's handling of that environment variable
    */
-  const bool omp_num_threads_set_in_environment;
+  const bool omp_num_threads_set_in_environment_;
 };
 
 }  // namespace engine
diff --git a/src/engine/threaded_engine_perdevice.cc b/src/engine/threaded_engine_perdevice.cc
index 3b4c4fc..a822788 100644
--- a/src/engine/threaded_engine_perdevice.cc
+++ b/src/engine/threaded_engine_perdevice.cc
@@ -247,6 +247,11 @@ class ThreadedEnginePerDevice : public ThreadedEngine {
     OprBlock* opr_block;
     RunContext run_ctx{ctx, stream};
     auto* task_queue = &(block->task_queue);
+
+    // Don't eat up omp threads for GPU jobs.  They're probably best used elsewhere,
+    // for example for image decoding or the optimizer pass
+    OpenMP::Get()->on_start_worker_thread(false);
+
     while (task_queue->Pop(&opr_block)) {
       this->ExecuteOprBlock(run_ctx, opr_block);
     }
@@ -267,9 +272,14 @@ class ThreadedEnginePerDevice : public ThreadedEngine {
     this->is_worker_ = true;
     auto* task_queue = &(block->task_queue);
     RunContext run_ctx{ctx, nullptr};
+
     // execute task
     OprBlock* opr_block;
     ready_event->signal();
+
+    // Set default number of threads for OMP parallel regions initiated by this thread
+    OpenMP::Get()->on_start_worker_thread(true);
+
     while (task_queue->Pop(&opr_block)) {
       this->ExecuteOprBlock(run_ctx, opr_block);
     }
diff --git a/tests/cpp/engine/threaded_engine_test.cc b/tests/cpp/engine/threaded_engine_test.cc
index 3007285..945c083 100644
--- a/tests/cpp/engine/threaded_engine_test.cc
+++ b/tests/cpp/engine/threaded_engine_test.cc
@@ -24,6 +24,8 @@
 */
 #include <time.h>
 #include <dmlc/logging.h>
+#include <dmlc/thread_group.h>
+#include <dmlc/omp.h>
 #include <gtest/gtest.h>
 #include <mxnet/engine.h>
 #include <dmlc/timer.h>
@@ -33,6 +35,7 @@
 #include <vector>
 
 #include "../src/engine/engine_impl.h"
+#include "../include/test_util.h"
 
 /**
  * present the following workload
@@ -254,3 +257,59 @@ TEST(Engine, basics) {
   oprs.clear();
   LOG(INFO) << "All pass";
 }
+
+#ifdef _OPENMP
+
+struct TestSaveAndRestoreOMPState {
+  TestSaveAndRestoreOMPState() {
+    omp_set_dynamic(false);
+  }
+  ~TestSaveAndRestoreOMPState() {
+    omp_set_num_threads(nthreads_);
+    omp_set_dynamic(dynamic_);
+  }
+  const int nthreads_ = omp_get_max_threads();
+  const int dynamic_ = omp_get_dynamic();
+};
+
+/*!
+ * \brief This test checks that omp_set_num_threads implementation has thread-scope
+ */
+TEST(Engine, omp_threading_count_scope) {
+  TestSaveAndRestoreOMPState omp_state;
+  const int THREAD_COUNT = 10;
+  std::shared_ptr<dmlc::ManualEvent> ready = std::make_shared<dmlc::ManualEvent>();
+  std::shared_ptr<dmlc::ThreadGroup> threads = std::make_shared<dmlc::ThreadGroup>();
+  std::atomic<int> counter(0), correct(0);
+  omp_set_dynamic(0);
+  for (int x = 0; x < THREAD_COUNT; ++x) {
+    std::string name = "thread: ";
+    name += std::to_string(x + 1);
+    ++counter;
+    threads->create(name, false,
+                    [x, &counter, &correct](std::shared_ptr<dmlc::ManualEvent> ready_ptr) -> int {
+                      const int thread_count = x + 1;
+                      omp_set_num_threads(thread_count);
+                      --counter;
+                      ready_ptr->wait();
+                      CHECK_EQ(omp_get_max_threads(), thread_count);
+                      #pragma omp parallel for
+                      for (int i = 0; i < 100; ++i) {
+                        if (i == 50) {
+                          const int current_threads = omp_get_num_threads();
+                          if (current_threads == thread_count) {
+                            ++correct;
+                          }
+                        }
+                      }
+                      return 0;
+                    }, ready);
+  }
+  while (counter.load() > 0) {
+    usleep(100);
+  }
+  ready->signal();
+  threads->join_all();
+  GTEST_ASSERT_EQ(correct.load(), THREAD_COUNT);
+}
+#endif  // _OPENMP
diff --git a/tests/cpp/include/test_util.h b/tests/cpp/include/test_util.h
index a755698..dee8903 100644
--- a/tests/cpp/include/test_util.h
+++ b/tests/cpp/include/test_util.h
@@ -792,4 +792,19 @@ struct ScopeSet {
 }  // namespace test
 }  // namespace mxnet
 
+#if defined(_MSC_VER)
+inline void usleep(__int64 usec) {
+  HANDLE timer;
+  LARGE_INTEGER ft;
+
+  // Convert to 100 nanosecond interval, negative value indicates relative time
+  ft.QuadPart = -(10*usec);
+
+  timer = CreateWaitableTimer(NULL, TRUE, NULL);
+  SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
+  WaitForSingleObject(timer, INFINITE);
+  CloseHandle(timer);
+}
+#endif  // _WIN32
+
 #endif  // TEST_UTIL_H_

-- 
To stop receiving notification emails like this one, please contact
jxie@apache.org.