• 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_DEVICE_ID_UTILS_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_DEVICE_ID_UTILS_H_
18 
19 #include "tensorflow/core/common_runtime/device/device_id.h"
20 #include "tensorflow/core/common_runtime/device/device_id_manager.h"
21 #include "tensorflow/core/lib/gtl/int_type.h"
22 #include "tensorflow/core/platform/types.h"
23 #include "tensorflow/stream_executor/platform.h"
24 #include "tensorflow/stream_executor/stream_executor.h"
25 
26 namespace tensorflow {
27 
28 // Utility methods for translation between TensorFlow device ids and platform
29 // device ids.
30 class DeviceIdUtil {
31  public:
32   // Convenient methods for getting the associated executor given a TfDeviceId
33   // or PlatformDeviceId.
ExecutorForPlatformDeviceId(se::Platform * device_manager,PlatformDeviceId platform_device_id)34   static se::port::StatusOr<se::StreamExecutor*> ExecutorForPlatformDeviceId(
35       se::Platform* device_manager, PlatformDeviceId platform_device_id) {
36     return device_manager->ExecutorForDevice(platform_device_id.value());
37   }
ExecutorForTfDeviceId(const DeviceType & type,se::Platform * device_manager,TfDeviceId tf_device_id)38   static se::port::StatusOr<se::StreamExecutor*> ExecutorForTfDeviceId(
39       const DeviceType& type, se::Platform* device_manager,
40       TfDeviceId tf_device_id) {
41     PlatformDeviceId platform_device_id;
42     TF_RETURN_IF_ERROR(DeviceIdManager::TfToPlatformDeviceId(
43         type, tf_device_id, &platform_device_id));
44     return ExecutorForPlatformDeviceId(device_manager, platform_device_id);
45   }
46 
47   // Verify that the platform_device_id associated with a TfDeviceId is
48   // legitimate.
CheckValidTfDeviceId(const DeviceType & type,se::Platform * device_manager,TfDeviceId tf_device_id)49   static void CheckValidTfDeviceId(const DeviceType& type,
50                                    se::Platform* device_manager,
51                                    TfDeviceId tf_device_id) {
52     PlatformDeviceId platform_device_id;
53     TF_CHECK_OK(DeviceIdManager::TfToPlatformDeviceId(type, tf_device_id,
54                                                       &platform_device_id));
55     const int visible_device_count = device_manager->VisibleDeviceCount();
56     CHECK_LT(platform_device_id.value(), visible_device_count)
57         << "platform_device_id is outside discovered device range."
58         << " TF " << type << " id: " << tf_device_id << ", platform " << type
59         << " id: " << platform_device_id
60         << ", visible device count: " << visible_device_count;
61   }
62 };
63 
64 }  // namespace tensorflow
65 
66 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_DEVICE_ID_UTILS_H_
67