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 "tensorflow/core/common_runtime/device_factory.h"
17
18 #include <memory>
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22
23 #include "tensorflow/core/common_runtime/device.h"
24 #include "tensorflow/core/lib/core/errors.h"
25 #include "tensorflow/core/lib/strings/strcat.h"
26 #include "tensorflow/core/platform/logging.h"
27 #include "tensorflow/core/platform/mutex.h"
28 #include "tensorflow/core/platform/types.h"
29 #include "tensorflow/core/public/session_options.h"
30
31 namespace tensorflow {
32
33 namespace {
34
get_device_factory_lock()35 static mutex* get_device_factory_lock() {
36 static mutex device_factory_lock(LINKER_INITIALIZED);
37 return &device_factory_lock;
38 }
39
40 struct FactoryItem {
41 std::unique_ptr<DeviceFactory> factory;
42 int priority;
43 };
44
device_factories()45 std::unordered_map<string, FactoryItem>& device_factories() {
46 static std::unordered_map<string, FactoryItem>* factories =
47 new std::unordered_map<string, FactoryItem>;
48 return *factories;
49 }
50
51 } // namespace
52
53 // static
DevicePriority(const string & device_type)54 int32 DeviceFactory::DevicePriority(const string& device_type) {
55 mutex_lock l(*get_device_factory_lock());
56 std::unordered_map<string, FactoryItem>& factories = device_factories();
57 auto iter = factories.find(device_type);
58 if (iter != factories.end()) {
59 return iter->second.priority;
60 }
61
62 return -1;
63 }
64
65 // static
Register(const string & device_type,DeviceFactory * factory,int priority)66 void DeviceFactory::Register(const string& device_type, DeviceFactory* factory,
67 int priority) {
68 mutex_lock l(*get_device_factory_lock());
69 std::unique_ptr<DeviceFactory> factory_ptr(factory);
70 std::unordered_map<string, FactoryItem>& factories = device_factories();
71 auto iter = factories.find(device_type);
72 if (iter == factories.end()) {
73 factories[device_type] = {std::move(factory_ptr), priority};
74 } else {
75 if (iter->second.priority < priority) {
76 iter->second = {std::move(factory_ptr), priority};
77 } else if (iter->second.priority == priority) {
78 LOG(FATAL) << "Duplicate registration of device factory for type "
79 << device_type << " with the same priority " << priority;
80 }
81 }
82 }
83
GetFactory(const string & device_type)84 DeviceFactory* DeviceFactory::GetFactory(const string& device_type) {
85 mutex_lock l(*get_device_factory_lock()); // could use reader lock
86 auto it = device_factories().find(device_type);
87 if (it == device_factories().end()) {
88 return nullptr;
89 }
90 return it->second.factory.get();
91 }
92
AddDevices(const SessionOptions & options,const string & name_prefix,std::vector<std::unique_ptr<Device>> * devices)93 Status DeviceFactory::AddDevices(
94 const SessionOptions& options, const string& name_prefix,
95 std::vector<std::unique_ptr<Device>>* devices) {
96 // CPU first. A CPU device is required.
97 auto cpu_factory = GetFactory("CPU");
98 if (!cpu_factory) {
99 return errors::NotFound(
100 "CPU Factory not registered. Did you link in threadpool_device?");
101 }
102 size_t init_size = devices->size();
103 TF_RETURN_IF_ERROR(cpu_factory->CreateDevices(options, name_prefix, devices));
104 if (devices->size() == init_size) {
105 return errors::NotFound("No CPU devices are available in this process");
106 }
107
108 // Then the rest (including GPU).
109 mutex_lock l(*get_device_factory_lock());
110 for (auto& p : device_factories()) {
111 auto factory = p.second.factory.get();
112 if (factory != cpu_factory) {
113 TF_RETURN_IF_ERROR(factory->CreateDevices(options, name_prefix, devices));
114 }
115 }
116
117 return Status::OK();
118 }
119
NewDevice(const string & type,const SessionOptions & options,const string & name_prefix)120 std::unique_ptr<Device> DeviceFactory::NewDevice(const string& type,
121 const SessionOptions& options,
122 const string& name_prefix) {
123 auto device_factory = GetFactory(type);
124 if (!device_factory) {
125 return nullptr;
126 }
127 SessionOptions opt = options;
128 (*opt.config.mutable_device_count())[type] = 1;
129 std::vector<std::unique_ptr<Device>> devices;
130 TF_CHECK_OK(device_factory->CreateDevices(opt, name_prefix, &devices));
131 int expected_num_devices = 1;
132 auto iter = options.config.device_count().find(type);
133 if (iter != options.config.device_count().end()) {
134 expected_num_devices = iter->second;
135 }
136 DCHECK_EQ(devices.size(), static_cast<size_t>(expected_num_devices));
137 return std::move(devices[0]);
138 }
139
140 } // namespace tensorflow
141