From 68401de6f246b494b14d4b1eecb0f9b5d1850ffd Mon Sep 17 00:00:00 2001
From: Vishakha Agrawal <vishakha.agrawal@intel.com>
Date: Tue, 4 Aug 2020 15:28:53 -0700
Subject: [PATCH 1/7] Removes unnecessary omp_set_num() calls

Signed-off-by: Vishakha Agrawal <vishakha.agrawal@intel.com>
---
 tensorflow/core/common_runtime/threadpool_device.cc | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/tensorflow/core/common_runtime/threadpool_device.cc b/tensorflow/core/common_runtime/threadpool_device.cc
index 44fa5bf2d3a..4e308cd09b5 100644
--- a/tensorflow/core/common_runtime/threadpool_device.cc
+++ b/tensorflow/core/common_runtime/threadpool_device.cc
@@ -60,13 +60,8 @@ ThreadPoolDevice::ThreadPoolDevice(const SessionOptions& options,
     // Default to available physical cores
     const int mkl_intra_op = port::NumSchedulableCPUs();
     const int ht = port::NumHyperthreadsPerCore();
-    omp_set_num_threads((mkl_intra_op + ht - 1) / ht);
-  } else {
-    uint64 user_val = 0;
-    if (strings::safe_strtou64(user_omp_threads, &user_val)) {
-      // Superflous but triggers OpenMP loading
-      omp_set_num_threads(user_val);
-    }
+    std::call_once(omp_setting_flag, omp_set_num_threads,
+		     (mkl_intra_op + ht - 1) / ht);
   }
 #endif  // _OPENMP
 #endif  // !defined(ENABLE_MKLDNN_THREADPOOL) && defined(INTEL_MKL)

From d2dfb6850d73ff63543314b6af09f10732bdbb69 Mon Sep 17 00:00:00 2001
From: Vishakha Agrawal <vishakha.agrawal@intel.com>
Date: Wed, 5 Aug 2020 09:42:39 -0700
Subject: [PATCH 2/7] Apply code format

Signed-off-by: Vishakha Agrawal <vishakha.agrawal@intel.com>
---
 tensorflow/core/common_runtime/threadpool_device.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow/core/common_runtime/threadpool_device.cc b/tensorflow/core/common_runtime/threadpool_device.cc
index 4e308cd09b5..0b955d63aa6 100644
--- a/tensorflow/core/common_runtime/threadpool_device.cc
+++ b/tensorflow/core/common_runtime/threadpool_device.cc
@@ -61,7 +61,7 @@ ThreadPoolDevice::ThreadPoolDevice(const SessionOptions& options,
     const int mkl_intra_op = port::NumSchedulableCPUs();
     const int ht = port::NumHyperthreadsPerCore();
     std::call_once(omp_setting_flag, omp_set_num_threads,
-		     (mkl_intra_op + ht - 1) / ht);
+                   (mkl_intra_op + ht - 1) / ht);
   }
 #endif  // _OPENMP
 #endif  // !defined(ENABLE_MKLDNN_THREADPOOL) && defined(INTEL_MKL)

From 769fdabfa4d3f90e914fe8a8b710fc39543ea268 Mon Sep 17 00:00:00 2001
From: Vishakha Agrawal <vishakha.agrawal@intel.com>
Date: Wed, 5 Aug 2020 11:58:27 -0700
Subject: [PATCH 3/7] Added call once flag, by got deleted by mistake

Signed-off-by: Vishakha Agrawal <vishakha.agrawal@intel.com>
---
 tensorflow/core/common_runtime/threadpool_device.cc | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tensorflow/core/common_runtime/threadpool_device.cc b/tensorflow/core/common_runtime/threadpool_device.cc
index 0b955d63aa6..f580aadd84f 100644
--- a/tensorflow/core/common_runtime/threadpool_device.cc
+++ b/tensorflow/core/common_runtime/threadpool_device.cc
@@ -41,6 +41,7 @@ limitations under the License.
 #endif
 
 namespace tensorflow {
+std::once_flag omp_setting_flag;
 
 ThreadPoolDevice::ThreadPoolDevice(const SessionOptions& options,
                                    const string& name, Bytes memory_limit,

From 9835aaae5c70bfbc864f09563bf5ab1736eba086 Mon Sep 17 00:00:00 2001
From: Vishakha Agrawal <vishakha.agrawal@intel.com>
Date: Thu, 6 Aug 2020 10:32:41 -0700
Subject: [PATCH 4/7] Removing unit test that depended on pre seting of OMP
 threads

Signed-off-by: Vishakha Agrawal <vishakha.agrawal@intel.com>
---
 .../core/common_runtime/mkl_threadpool_device_test.cc    | 9 ---------
 1 file changed, 9 deletions(-)

diff --git a/tensorflow/core/common_runtime/mkl_threadpool_device_test.cc b/tensorflow/core/common_runtime/mkl_threadpool_device_test.cc
index c29752d3c2c..1b64060bb02 100644
--- a/tensorflow/core/common_runtime/mkl_threadpool_device_test.cc
+++ b/tensorflow/core/common_runtime/mkl_threadpool_device_test.cc
@@ -37,15 +37,6 @@ TEST(MKLThreadPoolDeviceTest, TestOmpDefaults) {
   EXPECT_EQ(omp_get_max_threads(), (port::NumSchedulableCPUs() + ht - 1) / ht);
 }
 
-TEST(MKLThreadPoolDeviceTest, TestOmpPreSets) {
-  SessionOptions options;
-  setenv("OMP_NUM_THREADS", "314", 1);
-
-  ThreadPoolDevice* tp = new ThreadPoolDevice(
-      options, "/device:CPU:0", Bytes(256), DeviceLocality(), cpu_allocator());
-
-  EXPECT_EQ(omp_get_max_threads(), 314);
-}
 #endif  // defined(_OPENMP) && !defined(ENABLE_MKLDNN_THREADPOOL)
 
 }  // namespace tensorflow

From 508fddb3af9824680d1c594115f5b90e9a99fac9 Mon Sep 17 00:00:00 2001
From: Vishakha Agrawal <vishakha.agrawal@intel.com>
Date: Mon, 10 Aug 2020 14:43:27 -0700
Subject: [PATCH 5/7] replaced std with absl

Signed-off-by: Vishakha Agrawal <vishakha.agrawal@intel.com>
---
 tensorflow/core/common_runtime/threadpool_device.cc | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tensorflow/core/common_runtime/threadpool_device.cc b/tensorflow/core/common_runtime/threadpool_device.cc
index f580aadd84f..d709883441d 100644
--- a/tensorflow/core/common_runtime/threadpool_device.cc
+++ b/tensorflow/core/common_runtime/threadpool_device.cc
@@ -12,6 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 ==============================================================================*/
+#include "absl/base/call_once.h"
 
 #include "tensorflow/core/common_runtime/threadpool_device.h"
 
@@ -41,7 +42,7 @@ limitations under the License.
 #endif
 
 namespace tensorflow {
-std::once_flag omp_setting_flag;
+absl::once_flag omp_setting_flag;
 
 ThreadPoolDevice::ThreadPoolDevice(const SessionOptions& options,
                                    const string& name, Bytes memory_limit,
@@ -61,8 +62,8 @@ ThreadPoolDevice::ThreadPoolDevice(const SessionOptions& options,
     // Default to available physical cores
     const int mkl_intra_op = port::NumSchedulableCPUs();
     const int ht = port::NumHyperthreadsPerCore();
-    std::call_once(omp_setting_flag, omp_set_num_threads,
-                   (mkl_intra_op + ht - 1) / ht);
+    absl::call_once(omp_setting_flag, omp_set_num_threads,
+                    (mkl_intra_op + ht - 1) / ht);
   }
 #endif  // _OPENMP
 #endif  // !defined(ENABLE_MKLDNN_THREADPOOL) && defined(INTEL_MKL)

From f63da8dfc0a40765829e795d2327eb20aaf5203c Mon Sep 17 00:00:00 2001
From: Vishakha Agrawal <vishakha.agrawal@intel.com>
Date: Tue, 11 Aug 2020 16:27:54 -0700
Subject: [PATCH 6/7] Moving once_flag as requested by reviewer

Signed-off-by: Vishakha Agrawal <vishakha.agrawal@intel.com>
---
 tensorflow/core/common_runtime/threadpool_device.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow/core/common_runtime/threadpool_device.cc b/tensorflow/core/common_runtime/threadpool_device.cc
index d709883441d..b60d622c346 100644
--- a/tensorflow/core/common_runtime/threadpool_device.cc
+++ b/tensorflow/core/common_runtime/threadpool_device.cc
@@ -42,7 +42,6 @@ limitations under the License.
 #endif
 
 namespace tensorflow {
-absl::once_flag omp_setting_flag;
 
 ThreadPoolDevice::ThreadPoolDevice(const SessionOptions& options,
                                    const string& name, Bytes memory_limit,
@@ -57,6 +56,7 @@ ThreadPoolDevice::ThreadPoolDevice(const SessionOptions& options,
   if (DisableMKL()) return;
 #ifdef _OPENMP
   const char* user_omp_threads = getenv("OMP_NUM_THREADS");
+  static absl::once_flag omp_setting_flag;
   if (user_omp_threads == nullptr) {
     // OMP_NUM_THREADS controls MKL's intra-op parallelization
     // Default to available physical cores

From 88c613add6e6fa4fd4e0358c7f6a31c8773ea3d9 Mon Sep 17 00:00:00 2001
From: Vishakha Agrawal <vishakha.agrawal@intel.com>
Date: Wed, 12 Aug 2020 14:14:11 -0700
Subject: [PATCH 7/7] Removed lines as requested

Signed-off-by: Vishakha Agrawal <vishakha.agrawal@intel.com>
---
 tensorflow/core/common_runtime/threadpool_device.cc | 2 --
 1 file changed, 2 deletions(-)

diff --git a/tensorflow/core/common_runtime/threadpool_device.cc b/tensorflow/core/common_runtime/threadpool_device.cc
index b60d622c346..0470f4975b4 100644
--- a/tensorflow/core/common_runtime/threadpool_device.cc
+++ b/tensorflow/core/common_runtime/threadpool_device.cc
@@ -13,9 +13,7 @@ See the License for the specific language governing permissions and
 limitations under the License.
 ==============================================================================*/
 #include "absl/base/call_once.h"
-
 #include "tensorflow/core/common_runtime/threadpool_device.h"
-
 #include "tensorflow/core/common_runtime/local_device.h"
 #include "tensorflow/core/common_runtime/scoped_allocator.h"
 #include "tensorflow/core/common_runtime/scoped_allocator_mgr.h"