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 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_MGR_H_ 17 #define TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_MGR_H_ 18 19 #include <memory> 20 #include <string> 21 #include <unordered_map> 22 #include <unordered_set> 23 #include <vector> 24 25 #include "tensorflow/core/common_runtime/device.h" 26 #include "tensorflow/core/lib/core/arena.h" 27 #include "tensorflow/core/lib/core/status.h" 28 #include "tensorflow/core/lib/core/stringpiece.h" 29 #include "tensorflow/core/lib/gtl/inlined_vector.h" 30 #include "tensorflow/core/platform/macros.h" 31 32 namespace tensorflow { 33 34 class DeviceAttributes; 35 36 class DeviceMgr { 37 public: 38 // Constructs a DeviceMgr from a list of devices. 39 // TODO(zhifengc): Other initialization information. 40 explicit DeviceMgr(std::vector<std::unique_ptr<Device>> devices); 41 42 // Constructs a DeviceMgr managing a single device. 43 explicit DeviceMgr(std::unique_ptr<Device> device); 44 45 // Returns attributes of all devices. 46 void ListDeviceAttributes(std::vector<DeviceAttributes>* devices) const; 47 48 // Returns raw pointers to the underlying devices. 49 std::vector<Device*> ListDevices() const; 50 51 // Returns a string listing all devices. 52 string DebugString() const; 53 54 // Returns a string of all the device mapping. 55 string DeviceMappingString() const; 56 57 // Assigns *device with pointer to Device of the given name. 58 // Accepts either a full device name, or just the replica-local suffix. 59 Status LookupDevice(StringPiece name, Device** device) const; 60 61 // Clears given containers of all devices if 'container' is 62 // non-empty. Otherwise, clears default containers of all devices. 63 void ClearContainers(gtl::ArraySlice<string> containers) const; 64 65 int NumDeviceType(const string& type) const; 66 67 private: 68 const std::vector<std::unique_ptr<Device>> devices_; 69 70 StringPiece CopyToBackingStore(StringPiece s); 71 72 std::unordered_map<StringPiece, Device*, StringPieceHasher> device_map_; 73 core::Arena name_backing_store_; // Storage for keys in device_map_ 74 std::unordered_map<string, int> device_type_counts_; 75 76 TF_DISALLOW_COPY_AND_ASSIGN(DeviceMgr); 77 }; 78 79 } // namespace tensorflow 80 81 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_MGR_H_ 82