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