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