Home | History | Annotate | Download | only in common_runtime
      1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/common_runtime/process_util.h"
     17 
     18 #include <string.h>
     19 
     20 #include "tensorflow/core/lib/core/threadpool.h"
     21 #include "tensorflow/core/platform/cpu_info.h"
     22 #include "tensorflow/core/platform/logging.h"
     23 #include "tensorflow/core/platform/tracing.h"
     24 #include "tensorflow/core/platform/types.h"
     25 
     26 namespace tensorflow {
     27 
     28 namespace {
     29 
     30 static thread::ThreadPool* InitComputePool(const SessionOptions& options) {
     31   int32 inter_op_parallelism_threads =
     32       options.config.inter_op_parallelism_threads();
     33   if (inter_op_parallelism_threads == 0) {
     34     // Default to using the number of cores available in the process.
     35     inter_op_parallelism_threads = port::NumSchedulableCPUs();
     36   }
     37 
     38   return new thread::ThreadPool(Env::Default(), "Compute",
     39                                 inter_op_parallelism_threads);
     40 }
     41 
     42 }  // namespace
     43 
     44 thread::ThreadPool* ComputePool(const SessionOptions& options) {
     45   static thread::ThreadPool* compute_pool = InitComputePool(options);
     46   return compute_pool;
     47 }
     48 
     49 void SchedClosure(std::function<void()> closure) {
     50   if (port::Tracing::IsActive()) {
     51     const uint64 id = port::Tracing::UniqueId();
     52     port::Tracing::RecordEvent(port::Tracing::EventCategory::kScheduleClosure,
     53                                id);
     54     std::function<void()> wrapper = std::bind(
     55         [id](std::function<void()> closure) {
     56           port::Tracing::ScopedActivity region(
     57               port::Tracing::EventCategory::kRunClosure, id);
     58           closure();
     59         },
     60         std::move(closure));
     61     Env::Default()->SchedClosure(std::move(wrapper));
     62   } else {
     63     Env::Default()->SchedClosure(std::move(closure));
     64   }
     65 }
     66 
     67 void SchedNonBlockingClosureAfter(int64 micros, std::function<void()> closure) {
     68   Env::Default()->SchedClosureAfter(micros, std::move(closure));
     69 }
     70 
     71 }  // namespace tensorflow
     72