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 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM 17 18 #define EIGEN_USE_GPU 19 20 #include "tensorflow/core/common_runtime/gpu/gpu_device.h" 21 #include "tensorflow/core/common_runtime/gpu/gpu_id.h" 22 #include "tensorflow/core/common_runtime/gpu/gpu_process_state.h" 23 #include "tensorflow/core/common_runtime/threadpool_device.h" 24 #include "tensorflow/core/platform/numa.h" 25 26 namespace tensorflow { 27 28 class GPUDevice : public BaseGPUDevice { 29 public: GPUDevice(const SessionOptions & options,const string & name,Bytes memory_limit,const DeviceLocality & locality,TfGpuId tf_gpu_id,const string & physical_device_desc,Allocator * gpu_allocator,Allocator * cpu_allocator)30 GPUDevice(const SessionOptions& options, const string& name, 31 Bytes memory_limit, const DeviceLocality& locality, 32 TfGpuId tf_gpu_id, const string& physical_device_desc, 33 Allocator* gpu_allocator, Allocator* cpu_allocator) 34 : BaseGPUDevice(options, name, memory_limit, locality, tf_gpu_id, 35 physical_device_desc, gpu_allocator, cpu_allocator, 36 false /* sync every op */, 1 /* max_streams */) { 37 if (options.config.has_gpu_options()) { 38 force_gpu_compatible_ = 39 options.config.gpu_options().force_gpu_compatible(); 40 } 41 } 42 GetAllocator(AllocatorAttributes attr)43 Allocator* GetAllocator(AllocatorAttributes attr) override { 44 CHECK(cpu_allocator_) << "bad place 1"; 45 if (attr.on_host()) { 46 if (attr.gpu_compatible() || force_gpu_compatible_) { 47 GPUProcessState* ps = GPUProcessState::singleton(); 48 return ps->GetGpuHostAllocator(0); 49 } else { 50 return cpu_allocator_; 51 } 52 } else { 53 return gpu_allocator_; 54 } 55 } 56 57 private: 58 bool force_gpu_compatible_ = false; 59 }; 60 61 class GPUDeviceFactory : public BaseGPUDeviceFactory { 62 private: CreateGPUDevice(const SessionOptions & options,const string & name,Bytes memory_limit,const DeviceLocality & locality,TfGpuId tf_gpu_id,const string & physical_device_desc,Allocator * gpu_allocator,Allocator * cpu_allocator)63 std::unique_ptr<BaseGPUDevice> CreateGPUDevice( 64 const SessionOptions& options, const string& name, Bytes memory_limit, 65 const DeviceLocality& locality, TfGpuId tf_gpu_id, 66 const string& physical_device_desc, Allocator* gpu_allocator, 67 Allocator* cpu_allocator) override { 68 return absl::make_unique<GPUDevice>(options, name, memory_limit, locality, 69 tf_gpu_id, physical_device_desc, 70 gpu_allocator, cpu_allocator); 71 } 72 }; 73 74 REGISTER_LOCAL_DEVICE_FACTORY("GPU", GPUDeviceFactory, 210); 75 76 //------------------------------------------------------------------------------ 77 // A CPUDevice that optimizes for interaction with GPUs in the 78 // process. 79 // ----------------------------------------------------------------------------- 80 class GPUCompatibleCPUDevice : public ThreadPoolDevice { 81 public: GPUCompatibleCPUDevice(const SessionOptions & options,const string & name,Bytes memory_limit,const DeviceLocality & locality,Allocator * allocator)82 GPUCompatibleCPUDevice(const SessionOptions& options, const string& name, 83 Bytes memory_limit, const DeviceLocality& locality, 84 Allocator* allocator) 85 : ThreadPoolDevice(options, name, memory_limit, locality, allocator), 86 numa_node_(locality.numa_node()) { 87 if (options.config.has_gpu_options()) { 88 force_gpu_compatible_ = 89 options.config.gpu_options().force_gpu_compatible(); 90 } 91 } ~GPUCompatibleCPUDevice()92 ~GPUCompatibleCPUDevice() override {} 93 GetAllocator(AllocatorAttributes attr)94 Allocator* GetAllocator(AllocatorAttributes attr) override { 95 GPUProcessState* ps = GPUProcessState::singleton(); 96 if (attr.gpu_compatible() || force_gpu_compatible_) { 97 return ps->GetGpuHostAllocator(numa_node_); 98 } else { 99 // Call the parent's implementation. 100 return ThreadPoolDevice::GetAllocator(attr); 101 } 102 } 103 104 private: 105 bool force_gpu_compatible_ = false; 106 int numa_node_ = port::kNUMANoAffinity; 107 }; 108 109 // The associated factory. 110 class GPUCompatibleCPUDeviceFactory : public DeviceFactory { 111 public: CreateDevices(const SessionOptions & options,const string & name_prefix,std::vector<std::unique_ptr<Device>> * devices)112 Status CreateDevices(const SessionOptions& options, const string& name_prefix, 113 std::vector<std::unique_ptr<Device>>* devices) override { 114 int n = 1; 115 auto iter = options.config.device_count().find("CPU"); 116 if (iter != options.config.device_count().end()) { 117 n = iter->second; 118 } 119 int num_numa_nodes = options.config.experimental().use_numa_affinity() 120 ? port::NUMANumNodes() 121 : 1; 122 for (int i = 0; i < n; i++) { 123 string name = strings::StrCat(name_prefix, "/device:CPU:", i); 124 int numa_node = i % num_numa_nodes; 125 DeviceLocality locality; 126 locality.set_numa_node(numa_node); 127 devices->push_back(absl::make_unique<GPUCompatibleCPUDevice>( 128 options, name, Bytes(256 << 20), DeviceLocality(), 129 ProcessState::singleton()->GetCPUAllocator(numa_node))); 130 } 131 132 return Status::OK(); 133 } 134 }; 135 REGISTER_LOCAL_DEVICE_FACTORY("CPU", GPUCompatibleCPUDeviceFactory, 70); 136 137 } // namespace tensorflow 138 139 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM 140