Home | History | Annotate | Download | only in grappler
      1 /* Copyright 2017 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 #include <memory>
     17 
     18 #include "tensorflow/core/grappler/devices.h"
     19 #include "tensorflow/core/platform/cpu_info.h"
     20 
     21 #if GOOGLE_CUDA
     22 #include "tensorflow/core/common_runtime/gpu/gpu_init.h"
     23 #include "tensorflow/core/platform/stream_executor.h"
     24 #endif  // GOOGLE_CUDA
     25 
     26 namespace tensorflow {
     27 namespace grappler {
     28 
     29 int GetNumAvailableGPUs() {
     30   int num_eligible_gpus = 0;
     31 #if GOOGLE_CUDA
     32   if (ValidateGPUMachineManager().ok()) {
     33     perftools::gputools::Platform* gpu_manager = GPUMachineManager();
     34     if (gpu_manager != nullptr) {
     35       int num_gpus = gpu_manager->VisibleDeviceCount();
     36       for (int i = 0; i < num_gpus; i++) {
     37         auto exec_status = gpu_manager->ExecutorForDevice(i);
     38         if (exec_status.ok()) {
     39           perftools::gputools::StreamExecutor* se = exec_status.ValueOrDie();
     40           const perftools::gputools::DeviceDescription& desc =
     41               se->GetDeviceDescription();
     42           int min_gpu_core_count = 8;
     43           if (desc.core_count() >= min_gpu_core_count) {
     44             num_eligible_gpus++;
     45           }
     46         }
     47       }
     48     }
     49   }
     50 #endif  // GOOGLE_CUDA
     51   LOG(INFO) << "Number of eligible GPUs (core count >= 8): "
     52             << num_eligible_gpus;
     53   return num_eligible_gpus;
     54 }
     55 
     56 int64 AvailableGPUMemory(int gpu_id) {
     57 #if GOOGLE_CUDA
     58   // Look up the device, to see its attributes.
     59   perftools::gputools::Platform* gpu_platform = GPUMachineManager();
     60   CHECK_LT(gpu_id, gpu_platform->VisibleDeviceCount());
     61   perftools::gputools::StreamExecutor* se =
     62       gpu_platform->ExecutorForDevice(gpu_id).ValueOrDie();
     63   int64 total_memory, available_memory;
     64   CHECK(se->DeviceMemoryUsage(&available_memory, &total_memory));
     65 
     66   return available_memory;
     67 #else
     68   return 0;
     69 #endif
     70 }
     71 
     72 int GetNumAvailableLogicalCPUCores() { return port::NumSchedulableCPUs(); }
     73 
     74 }  // end namespace grappler
     75 }  // end namespace tensorflow
     76