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/byte_order.h" 20 #include "tensorflow/core/platform/cpu_info.h" 21 22 #if GOOGLE_CUDA 23 #include "tensorflow/core/common_runtime/gpu/gpu_init.h" 24 #include "tensorflow/core/platform/stream_executor.h" 25 #endif // GOOGLE_CUDA 26 27 namespace tensorflow { 28 namespace grappler { 29 GetNumAvailableGPUs()30int GetNumAvailableGPUs() { 31 int num_eligible_gpus = 0; 32 #if GOOGLE_CUDA 33 if (ValidateGPUMachineManager().ok()) { 34 se::Platform* gpu_manager = GPUMachineManager(); 35 if (gpu_manager != nullptr) { 36 int num_gpus = gpu_manager->VisibleDeviceCount(); 37 for (int i = 0; i < num_gpus; i++) { 38 auto exec_status = gpu_manager->ExecutorForDevice(i); 39 if (exec_status.ok()) { 40 se::StreamExecutor* se = exec_status.ValueOrDie(); 41 const se::DeviceDescription& desc = 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 LOG(INFO) << "Number of eligible GPUs (core count >= 8): " 51 << num_eligible_gpus; 52 #else 53 LOG(INFO) << "Number of eligible GPUs (core count >= 8): " 54 << num_eligible_gpus 55 << " (Note: TensorFlow was not compiled with CUDA support)"; 56 #endif // GOOGLE_CUDA 57 return num_eligible_gpus; 58 } 59 AvailableGPUMemory(int gpu_id)60int64 AvailableGPUMemory(int gpu_id) { 61 #if GOOGLE_CUDA 62 // Look up the device, to see its attributes. 63 se::Platform* gpu_platform = GPUMachineManager(); 64 CHECK_LT(gpu_id, gpu_platform->VisibleDeviceCount()); 65 se::StreamExecutor* se = gpu_platform->ExecutorForDevice(gpu_id).ValueOrDie(); 66 int64 total_memory, available_memory; 67 CHECK(se->DeviceMemoryUsage(&available_memory, &total_memory)); 68 69 return available_memory; 70 #else 71 return 0; 72 #endif 73 } 74 GetNumAvailableLogicalCPUCores()75int GetNumAvailableLogicalCPUCores() { return port::NumSchedulableCPUs(); } 76 77 } // end namespace grappler 78 } // end namespace tensorflow 79