1 /* Copyright 2019 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_DEVICE_DEVICE_MEM_ALLOCATOR_H_ 17 #define TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_DEVICE_MEM_ALLOCATOR_H_ 18 19 #include "tensorflow/core/common_runtime/device/device_id.h" 20 #include "tensorflow/core/framework/allocator.h" 21 #include "tensorflow/core/platform/stream_executor.h" 22 23 namespace tensorflow { 24 25 // Suballocator for StreamExecutor-based device memory. 26 class DeviceMemAllocator : public SubAllocator { 27 public: 28 // 'platform_device_id' refers to the ID of the device within 29 // the process and must reference a valid ID in the process. 30 // Note: stream_exec cannot be null. DeviceMemAllocator(se::StreamExecutor * stream_exec,PlatformDeviceId device_id,bool use_unified_memory,const std::vector<Visitor> & alloc_visitors,const std::vector<Visitor> & free_visitors)31 explicit DeviceMemAllocator(se::StreamExecutor* stream_exec, 32 PlatformDeviceId device_id, 33 bool use_unified_memory, 34 const std::vector<Visitor>& alloc_visitors, 35 const std::vector<Visitor>& free_visitors) 36 : SubAllocator(alloc_visitors, free_visitors), 37 stream_exec_(stream_exec), 38 device_id_(device_id), 39 use_unified_memory_(use_unified_memory) { 40 CHECK(stream_exec_ != nullptr); 41 } ~DeviceMemAllocator()42 ~DeviceMemAllocator() override {} 43 Alloc(size_t alignment,size_t num_bytes,size_t * bytes_received)44 void* Alloc(size_t alignment, size_t num_bytes, 45 size_t* bytes_received) override { 46 void* ptr = nullptr; 47 *bytes_received = num_bytes; 48 if (num_bytes > 0) { 49 if (use_unified_memory_) { 50 ptr = stream_exec_->UnifiedMemoryAllocate(num_bytes); 51 } else { 52 ptr = stream_exec_->AllocateArray<char>(num_bytes).opaque(); 53 } 54 VisitAlloc(ptr, device_id_.value(), num_bytes); 55 } 56 return ptr; 57 } 58 Free(void * ptr,size_t num_bytes)59 void Free(void* ptr, size_t num_bytes) override { 60 if (ptr != nullptr) { 61 VisitFree(ptr, device_id_.value(), num_bytes); 62 if (use_unified_memory_) { 63 stream_exec_->UnifiedMemoryDeallocate(ptr); 64 } else { 65 se::DeviceMemoryBase device_ptr(ptr); 66 stream_exec_->Deallocate(&device_ptr); 67 } 68 } 69 } 70 SupportsCoalescing()71 bool SupportsCoalescing() const override { return false; } 72 73 private: 74 se::StreamExecutor* stream_exec_; // not owned, non-null 75 const PlatformDeviceId device_id_; 76 const bool use_unified_memory_ = false; 77 78 TF_DISALLOW_COPY_AND_ASSIGN(DeviceMemAllocator); 79 }; 80 81 } // namespace tensorflow 82 83 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_DEVICE_MEM_ALLOCATOR_H_ 84