process_util.cc revision 0f70b8e4b72109a2f99a6235da2f17ec142040ad
1/* Copyright 2016 Google Inc. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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/host_info.h"
22#include "tensorflow/core/platform/logging.h"
23#include "tensorflow/core/platform/tracing.h"
24#include "tensorflow/core/platform/types.h"
25
26namespace tensorflow {
27
28namespace {
29
30static 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
44thread::ThreadPool* ComputePool(const SessionOptions& options) {
45  static thread::ThreadPool* compute_pool = InitComputePool(options);
46  return compute_pool;
47}
48
49void 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 = [closure, id]() {
55      port::Tracing::ScopedActivity region(
56          port::Tracing::EventCategory::kRunClosure, id);
57      closure();
58    };
59    Env::Default()->SchedClosure(wrapper);
60  } else {
61    Env::Default()->SchedClosure(closure);
62  }
63}
64
65void SchedNonBlockingClosureAfter(int micros, std::function<void()> closure) {
66  Env::Default()->SchedClosureAfter(micros, closure);
67}
68
69}  // namespace tensorflow
70