• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SET_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_SET_H_
18 
19 #include <memory>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "tensorflow/core/common_runtime/device.h"
24 #include "tensorflow/core/platform/macros.h"
25 #include "tensorflow/core/platform/types.h"
26 #include "tensorflow/core/util/device_name_utils.h"
27 
28 namespace tensorflow {
29 
30 // DeviceSet is a container class for managing the various types of
31 // devices used by a model.
32 class DeviceSet {
33  public:
34   DeviceSet();
35   ~DeviceSet();
36 
37   // Does not take ownership of 'device'.
38   void AddDevice(Device* device);
39 
40   // Set the device designated as the "client".  This device
41   // must also be registered via AddDevice().
set_client_device(Device * device)42   void set_client_device(Device* device) {
43     DCHECK(client_device_ == nullptr);
44     client_device_ = device;
45   }
46 
47   // Returns a pointer to the device designated as the "client".
client_device()48   Device* client_device() const { return client_device_; }
49 
50   // Return the list of devices in this set.
devices()51   const std::vector<Device*>& devices() const { return devices_; }
52 
53   // Given a DeviceNameUtils::ParsedName (which may have some
54   // wildcards for different components), fills "*devices" with all
55   // devices in "*this" that match "spec".
56   void FindMatchingDevices(const DeviceNameUtils::ParsedName& spec,
57                            std::vector<Device*>* devices) const;
58 
59   // Finds the device with the given "fullname". Returns nullptr if
60   // not found.
61   Device* FindDeviceByName(const string& fullname) const;
62 
63   // Return the list of unique device types in this set, ordered
64   // with more preferable devices earlier.
65   std::vector<DeviceType> PrioritizedDeviceTypeList() const;
66 
67   // An order to sort by device types according to system-determined
68   // priority.
69   //
70   // Higher result implies higher priority.
71   static int DeviceTypeOrder(const DeviceType& d);
72 
73  private:
74   // Not owned.
75   std::vector<Device*> devices_;
76 
77   // Fullname -> device* for device in devices_.
78   std::unordered_map<string, Device*> device_by_name_;
79 
80   // client_device_ points to an element of devices_ that we consider
81   // to be the client device (in this local process).
82   Device* client_device_ = nullptr;
83 
84   TF_DISALLOW_COPY_AND_ASSIGN(DeviceSet);
85 };
86 
87 }  // namespace tensorflow
88 
89 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_SET_H_
90