1 | /* Copyright 2015 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 <vector> |
17 | |
18 | // Register a factory that provides CPU devices. |
19 | #include "absl/memory/memory.h" |
20 | #include "tensorflow/core/common_runtime/device_factory.h" |
21 | #include "tensorflow/core/common_runtime/process_state.h" |
22 | #include "tensorflow/core/common_runtime/threadpool_device.h" |
23 | #include "tensorflow/core/framework/allocator.h" |
24 | #include "tensorflow/core/platform/numa.h" |
25 | #include "tensorflow/core/public/session_options.h" |
26 | |
27 | namespace tensorflow { |
28 | |
29 | // TODO(zhifengc/tucker): Figure out the bytes of available RAM. |
30 | class ThreadPoolDeviceFactory : public DeviceFactory { |
31 | public: |
32 | Status ListPhysicalDevices(std::vector<string>* devices) override { |
33 | devices->push_back("/physical_device:CPU:0" ); |
34 | |
35 | return OkStatus(); |
36 | } |
37 | |
38 | Status CreateDevices(const SessionOptions& options, const string& name_prefix, |
39 | std::vector<std::unique_ptr<Device>>* devices) override { |
40 | int num_numa_nodes = port::NUMANumNodes(); |
41 | int n = 1; |
42 | auto iter = options.config.device_count().find("CPU" ); |
43 | if (iter != options.config.device_count().end()) { |
44 | n = iter->second; |
45 | } |
46 | for (int i = 0; i < n; i++) { |
47 | string name = strings::StrCat(name_prefix, "/device:CPU:" , i); |
48 | std::unique_ptr<ThreadPoolDevice> tpd; |
49 | if (options.config.experimental().use_numa_affinity()) { |
50 | int numa_node = i % num_numa_nodes; |
51 | if (numa_node != i) { |
52 | LOG(INFO) << "Only " << num_numa_nodes |
53 | << " NUMA nodes visible in system, " |
54 | << " assigning device " << name << " to NUMA node " |
55 | << numa_node; |
56 | } |
57 | DeviceLocality dev_locality; |
58 | dev_locality.set_numa_node(numa_node); |
59 | tpd = std::make_unique<ThreadPoolDevice>( |
60 | options, name, Bytes(256 << 20), dev_locality, |
61 | ProcessState::singleton()->GetCPUAllocator(numa_node)); |
62 | } else { |
63 | tpd = std::make_unique<ThreadPoolDevice>( |
64 | options, name, Bytes(256 << 20), DeviceLocality(), |
65 | ProcessState::singleton()->GetCPUAllocator(port::kNUMANoAffinity)); |
66 | } |
67 | devices->push_back(std::move(tpd)); |
68 | } |
69 | |
70 | return OkStatus(); |
71 | } |
72 | }; |
73 | |
74 | REGISTER_LOCAL_DEVICE_FACTORY("CPU" , ThreadPoolDeviceFactory, 60); |
75 | |
76 | } // namespace tensorflow |
77 | |