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_mgr.h" 17 18 #include <vector> 19 #include "tensorflow/core/common_runtime/local_device.h" 20 #include "tensorflow/core/framework/device_attributes.pb.h" 21 #include "tensorflow/core/lib/core/errors.h" 22 #include "tensorflow/core/platform/logging.h" 23 #include "tensorflow/core/util/device_name_utils.h" 24 25 namespace tensorflow { 26 27 DeviceMgr::DeviceMgr(const std::vector<Device*>& devices) 28 : name_backing_store_(128) { 29 for (Device* d : devices) { 30 devices_.push_back(d); 31 32 // Register under the (1) full name and (2) canonical name. 33 for (const string& name : 34 DeviceNameUtils::GetNamesForDeviceMappings(d->parsed_name())) { 35 device_map_[CopyToBackingStore(name)] = d; 36 } 37 // Register under the (3) local name and (4) legacy local name. 38 for (const string& name : 39 DeviceNameUtils::GetLocalNamesForDeviceMappings(d->parsed_name())) { 40 device_map_[CopyToBackingStore(name)] = d; 41 } 42 device_type_counts_[d->device_type()]++; 43 } 44 } 45 46 DeviceMgr::~DeviceMgr() { 47 // TODO(b/37437134): Remove destructor after converting to std::unique_ptr. 48 for (Device* p : devices_) delete p; 49 } 50 51 StringPiece DeviceMgr::CopyToBackingStore(StringPiece s) { 52 size_t n = s.size(); 53 char* space = name_backing_store_.Alloc(n); 54 memcpy(space, s.data(), n); 55 return StringPiece(space, n); 56 } 57 58 void DeviceMgr::ListDeviceAttributes( 59 std::vector<DeviceAttributes>* devices) const { 60 devices->reserve(devices_.size()); 61 for (Device* dev : devices_) { 62 devices->emplace_back(dev->attributes()); 63 } 64 } 65 66 std::vector<Device*> DeviceMgr::ListDevices() const { 67 return std::vector<Device*>(devices_.begin(), devices_.end()); 68 } 69 70 string DeviceMgr::DebugString() const { 71 string out; 72 for (Device* dev : devices_) { 73 strings::StrAppend(&out, dev->name(), "\n"); 74 } 75 return out; 76 } 77 78 string DeviceMgr::DeviceMappingString() const { 79 string out; 80 for (Device* dev : devices_) { 81 if (!dev->attributes().physical_device_desc().empty()) { 82 strings::StrAppend(&out, dev->name(), " -> ", 83 dev->attributes().physical_device_desc(), "\n"); 84 } 85 } 86 return out; 87 } 88 89 Status DeviceMgr::LookupDevice(StringPiece name, Device** device) const { 90 Status s; 91 auto iter = device_map_.find(name); 92 if (iter == device_map_.end()) { 93 std::vector<StringPiece> device_names; 94 for (auto&& itr : device_map_) { 95 device_names.push_back(itr.first); 96 } 97 LOG(WARNING) << "Unknown device: " << name 98 << " all devices: " << str_util::Join(device_names, ", "); 99 return errors::InvalidArgument(name, " unknown device."); 100 } 101 *device = iter->second; 102 return Status::OK(); 103 } 104 105 void DeviceMgr::ClearContainers(gtl::ArraySlice<string> containers) const { 106 Status s; 107 for (Device* dev : devices_) { 108 if (containers.empty()) { 109 s.Update(dev->resource_manager()->Cleanup( 110 dev->resource_manager()->default_container())); 111 } else { 112 for (const string& c : containers) { 113 s.Update(dev->resource_manager()->Cleanup(c)); 114 } 115 } 116 if (!s.ok()) { 117 LOG(WARNING) << s; 118 } 119 } 120 } 121 122 int DeviceMgr::NumDeviceType(const string& type) const { 123 auto iter = device_type_counts_.find(type); 124 if (iter != device_type_counts_.end()) return iter->second; 125 return 0; 126 } 127 128 } // namespace tensorflow 129