• 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_FRAMEWORK_DEVICE_FACTORY_H_
17 #define TENSORFLOW_CORE_FRAMEWORK_DEVICE_FACTORY_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "tensorflow/core/platform/status.h"
23 #include "tensorflow/core/platform/types.h"
24 
25 namespace tensorflow {
26 
27 class Device;
28 struct SessionOptions;
29 
30 class DeviceFactory {
31  public:
~DeviceFactory()32   virtual ~DeviceFactory() {}
33   static void Register(const std::string& device_type, DeviceFactory* factory,
34                        int priority);
35   static DeviceFactory* GetFactory(const std::string& device_type);
36 
37   // Append to "*devices" all suitable devices, respecting
38   // any device type specific properties/counts listed in "options".
39   //
40   // CPU devices are added first.
41   static Status AddDevices(const SessionOptions& options,
42                            const std::string& name_prefix,
43                            std::vector<std::unique_ptr<Device>>* devices);
44 
45   // Helper for tests.  Create a single device of type "type".  The
46   // returned device is always numbered zero, so if creating multiple
47   // devices of the same type, supply distinct name_prefix arguments.
48   static std::unique_ptr<Device> NewDevice(const string& type,
49                                            const SessionOptions& options,
50                                            const string& name_prefix);
51 
52   // Iterate through all device factories and build a list of all of the
53   // possible physical devices.
54   //
55   // CPU is are added first.
56   static Status ListAllPhysicalDevices(std::vector<string>* devices);
57 
58   // Get details for a specific device among all device factories.
59   // 'device_index' indexes into devices from ListAllPhysicalDevices.
60   static Status GetAnyDeviceDetails(
61       int device_index, std::unordered_map<string, string>* details);
62 
63   // For a specific device factory list all possible physical devices.
64   virtual Status ListPhysicalDevices(std::vector<string>* devices) = 0;
65 
66   // Get details for a specific device for a specific factory. Subclasses
67   // can store arbitrary device information in the map. 'device_index' indexes
68   // into devices from ListPhysicalDevices.
GetDeviceDetails(int device_index,std::unordered_map<string,string> * details)69   virtual Status GetDeviceDetails(int device_index,
70                                   std::unordered_map<string, string>* details) {
71     return Status::OK();
72   }
73 
74   // Most clients should call AddDevices() instead.
75   virtual Status CreateDevices(
76       const SessionOptions& options, const std::string& name_prefix,
77       std::vector<std::unique_ptr<Device>>* devices) = 0;
78 
79   // Return the device priority number for a "device_type" string.
80   //
81   // Higher number implies higher priority.
82   //
83   // In standard TensorFlow distributions, GPU device types are
84   // preferred over CPU, and by default, custom devices that don't set
85   // a custom priority during registration will be prioritized lower
86   // than CPU.  Custom devices that want a higher priority can set the
87   // 'priority' field when registering their device to something
88   // higher than the packaged devices.  See calls to
89   // REGISTER_LOCAL_DEVICE_FACTORY to see the existing priorities used
90   // for built-in devices.
91   static int32 DevicePriority(const std::string& device_type);
92 };
93 
94 namespace dfactory {
95 
96 template <class Factory>
97 class Registrar {
98  public:
99   // Multiple registrations for the same device type with different priorities
100   // are allowed.  Priorities are used in two different ways:
101   //
102   // 1) When choosing which factory (that is, which device
103   //    implementation) to use for a specific 'device_type', the
104   //    factory registered with the highest priority will be chosen.
105   //    For example, if there are two registrations:
106   //
107   //      Registrar<CPUFactory1>("CPU", 125);
108   //      Registrar<CPUFactory2>("CPU", 150);
109   //
110   //    then CPUFactory2 will be chosen when
111   //    DeviceFactory::GetFactory("CPU") is called.
112   //
113   // 2) When choosing which 'device_type' is preferred over other
114   //    DeviceTypes in a DeviceSet, the ordering is determined
115   //    by the 'priority' set during registration.  For example, if there
116   //    are two registrations:
117   //
118   //      Registrar<CPUFactory>("CPU", 100);
119   //      Registrar<GPUFactory>("GPU", 200);
120   //
121   //    then DeviceType("GPU") will be prioritized higher than
122   //    DeviceType("CPU").
123   //
124   // The default priority values for built-in devices is:
125   // GPU: 210
126   // GPUCompatibleCPU: 70
127   // ThreadPoolDevice: 60
128   // Default: 50
129   explicit Registrar(const std::string& device_type, int priority = 50) {
130     DeviceFactory::Register(device_type, new Factory(), priority);
131   }
132 };
133 
134 }  // namespace dfactory
135 
136 #define REGISTER_LOCAL_DEVICE_FACTORY(device_type, device_factory, ...) \
137   INTERNAL_REGISTER_LOCAL_DEVICE_FACTORY(device_type, device_factory,   \
138                                          __COUNTER__, ##__VA_ARGS__)
139 
140 #define INTERNAL_REGISTER_LOCAL_DEVICE_FACTORY(device_type, device_factory, \
141                                                ctr, ...)                    \
142   static ::tensorflow::dfactory::Registrar<device_factory>                  \
143       INTERNAL_REGISTER_LOCAL_DEVICE_FACTORY_NAME(ctr)(device_type,         \
144                                                        ##__VA_ARGS__)
145 
146 // __COUNTER__ must go through another macro to be properly expanded
147 #define INTERNAL_REGISTER_LOCAL_DEVICE_FACTORY_NAME(ctr) ___##ctr##__object_
148 
149 }  // namespace tensorflow
150 
151 #endif  // TENSORFLOW_CORE_FRAMEWORK_DEVICE_FACTORY_H_
152