Home | History | Annotate | Download | only in gpu
      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 namespace gpu = ::perftools::gputools;
     27 
     28 // Utility methods for translation between Tensorflow GPU ids and CUDA GPU ids.
     29 class GpuIdUtil {
     30  public:
     31   // Convenient methods for getting the associated executor given a TfGpuId or
     32   // CudaGpuId.
     33   static gpu::port::StatusOr<gpu::StreamExecutor*> ExecutorForCudaGpuId(
     34       gpu::Platform* gpu_manager, CudaGpuId cuda_gpu_id) {
     35     return gpu_manager->ExecutorForDevice(cuda_gpu_id.value());
     36   }
     37   static gpu::port::StatusOr<gpu::StreamExecutor*> ExecutorForCudaGpuId(
     38       CudaGpuId cuda_gpu_id) {
     39     return ExecutorForCudaGpuId(GPUMachineManager(), cuda_gpu_id);
     40   }
     41   static gpu::port::StatusOr<gpu::StreamExecutor*> ExecutorForTfGpuId(
     42       TfGpuId tf_gpu_id) {
     43     return ExecutorForCudaGpuId(GpuIdManager::TfToCudaGpuId(tf_gpu_id));
     44   }
     45 
     46   // Verify that the cuda_gpu_id associated with a TfGpuId is legitimate.
     47   static void CheckValidTfGpuId(TfGpuId tf_gpu_id) {
     48     const CudaGpuId cuda_gpu_id = GpuIdManager::TfToCudaGpuId(tf_gpu_id);
     49     const int visible_device_count = GPUMachineManager()->VisibleDeviceCount();
     50     CHECK_LT(cuda_gpu_id.value(), visible_device_count)
     51         << "cuda_gpu_id is outside discovered device range."
     52         << " TF GPU id: " << tf_gpu_id << " CUDA GPU id: " << cuda_gpu_id
     53         << " visible device count: " << visible_device_count;
     54   }
     55 };
     56 
     57 }  // namespace tensorflow
     58 
     59 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_ID_UTILS_H_
     60