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_BFC_ALLOCATOR_H_ 17 #define TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_BFC_ALLOCATOR_H_ 18 19 #include <memory> 20 #include <string> 21 #include <unordered_map> 22 #include <vector> 23 24 #include "tensorflow/core/common_runtime/allocator_retry.h" 25 #include "tensorflow/core/common_runtime/bfc_allocator.h" 26 #include "tensorflow/core/common_runtime/gpu/gpu_id.h" 27 #include "tensorflow/core/platform/stream_executor.h" 28 #include "tensorflow/core/platform/thread_annotations.h" 29 #include "tensorflow/core/platform/types.h" 30 #include "tensorflow/core/protobuf/config.pb.h" 31 32 namespace tensorflow { 33 34 // Suballocator for GPU memory. 35 class GPUMemAllocator : public SubAllocator { 36 public: 37 // 'platform_gpu_id' refers to the ID of the GPU device within 38 // the process and must reference a valid ID in the process. 39 // Note: stream_exec cannot be null. GPUMemAllocator(se::StreamExecutor * stream_exec,PlatformGpuId gpu_id,bool use_unified_memory,const std::vector<Visitor> & alloc_visitors,const std::vector<Visitor> & free_visitors)40 explicit GPUMemAllocator(se::StreamExecutor* stream_exec, 41 PlatformGpuId gpu_id, bool use_unified_memory, 42 const std::vector<Visitor>& alloc_visitors, 43 const std::vector<Visitor>& free_visitors) 44 : SubAllocator(alloc_visitors, free_visitors), 45 stream_exec_(stream_exec), 46 gpu_id_(gpu_id), 47 use_unified_memory_(use_unified_memory) { 48 CHECK(stream_exec_ != nullptr); 49 } ~GPUMemAllocator()50 ~GPUMemAllocator() override {} 51 Alloc(size_t alignment,size_t num_bytes)52 void* Alloc(size_t alignment, size_t num_bytes) override { 53 void* ptr = nullptr; 54 if (num_bytes > 0) { 55 if (use_unified_memory_) { 56 ptr = stream_exec_->UnifiedMemoryAllocate(num_bytes); 57 } else { 58 ptr = stream_exec_->AllocateArray<char>(num_bytes).opaque(); 59 } 60 VisitAlloc(ptr, gpu_id_.value(), num_bytes); 61 } 62 return ptr; 63 } 64 Free(void * ptr,size_t num_bytes)65 void Free(void* ptr, size_t num_bytes) override { 66 if (ptr != nullptr) { 67 VisitFree(ptr, gpu_id_.value(), num_bytes); 68 if (use_unified_memory_) { 69 stream_exec_->UnifiedMemoryDeallocate(ptr); 70 } else { 71 se::DeviceMemoryBase gpu_ptr(ptr); 72 stream_exec_->Deallocate(&gpu_ptr); 73 } 74 } 75 } 76 77 private: 78 se::StreamExecutor* stream_exec_; // not owned, non-null 79 const PlatformGpuId gpu_id_; 80 const bool use_unified_memory_ = false; 81 82 TF_DISALLOW_COPY_AND_ASSIGN(GPUMemAllocator); 83 }; 84 85 // A GPU memory allocator that implements a 'best-fit with coalescing' 86 // algorithm. 87 class GPUBFCAllocator : public BFCAllocator { 88 public: 89 GPUBFCAllocator(GPUMemAllocator* sub_allocator, size_t total_memory, 90 const string& name); 91 GPUBFCAllocator(GPUMemAllocator* sub_allocator, size_t total_memory, 92 const GPUOptions& gpu_options, const string& name); ~GPUBFCAllocator()93 ~GPUBFCAllocator() override {} 94 95 TF_DISALLOW_COPY_AND_ASSIGN(GPUBFCAllocator); 96 97 private: 98 static bool GetAllowGrowthValue(const GPUOptions& gpu_options); 99 }; 100 101 } // namespace tensorflow 102 103 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_BFC_ALLOCATOR_H_ 104