• 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_GPU_GPU_ID_UTILS_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_ID_UTILS_H_
18 
19 #include "tensorflow/core/common_runtime/gpu/gpu_id.h"
20 #include "tensorflow/core/common_runtime/gpu/gpu_id_manager.h"
21 #include "tensorflow/core/common_runtime/gpu/gpu_init.h"
22 #include "tensorflow/core/lib/gtl/int_type.h"
23 #include "tensorflow/core/platform/stream_executor.h"
24 
25 namespace tensorflow {
26 
27 // Utility methods for translation between Tensorflow GPU ids and platform GPU
28 // ids.
29 class GpuIdUtil {
30  public:
31   // Convenient methods for getting the associated executor given a TfGpuId or
32   // PlatformGpuId.
ExecutorForPlatformGpuId(se::Platform * gpu_manager,PlatformGpuId platform_gpu_id)33   static se::port::StatusOr<se::StreamExecutor*> ExecutorForPlatformGpuId(
34       se::Platform* gpu_manager, PlatformGpuId platform_gpu_id) {
35     return gpu_manager->ExecutorForDevice(platform_gpu_id.value());
36   }
ExecutorForPlatformGpuId(PlatformGpuId platform_gpu_id)37   static se::port::StatusOr<se::StreamExecutor*> ExecutorForPlatformGpuId(
38       PlatformGpuId platform_gpu_id) {
39     return ExecutorForPlatformGpuId(GPUMachineManager(), platform_gpu_id);
40   }
ExecutorForTfGpuId(TfGpuId tf_gpu_id)41   static se::port::StatusOr<se::StreamExecutor*> ExecutorForTfGpuId(
42       TfGpuId tf_gpu_id) {
43     PlatformGpuId platform_gpu_id;
44     TF_RETURN_IF_ERROR(
45         GpuIdManager::TfToPlatformGpuId(tf_gpu_id, &platform_gpu_id));
46     return ExecutorForPlatformGpuId(platform_gpu_id);
47   }
48 
49   // Verify that the platform_gpu_id associated with a TfGpuId is legitimate.
CheckValidTfGpuId(TfGpuId tf_gpu_id)50   static void CheckValidTfGpuId(TfGpuId tf_gpu_id) {
51     PlatformGpuId platform_gpu_id;
52     TF_CHECK_OK(GpuIdManager::TfToPlatformGpuId(tf_gpu_id, &platform_gpu_id));
53     const int visible_device_count = GPUMachineManager()->VisibleDeviceCount();
54     CHECK_LT(platform_gpu_id.value(), visible_device_count)
55         << "platform_gpu_id is outside discovered device range."
56         << " TF GPU id: " << tf_gpu_id
57         << " platform GPU id: " << platform_gpu_id
58         << " visible device count: " << visible_device_count;
59   }
60 };
61 
62 }  // namespace tensorflow
63 
64 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_ID_UTILS_H_
65