| 42 | // Release()'ing, Free()'ing, and other actions can deactive an active object. |
| 43 | template <typename ElemT> |
| 44 | class ScopedDeviceMemory { |
| 45 | public: |
| 46 | // Default construction initializes the internal state to nullptr. This |
| 47 | // mirrors the std::unique_ptr<> functionality, where default construction |
| 48 | // produces a nullptr unique_ptr, which can be assigned later. |
| 49 | ScopedDeviceMemory() : device_ordinal_(-1), allocator_(nullptr) {} |
| 50 | |
| 51 | // Construct a ScopedDeviceMemory from a custom allocator. |
| 52 | // |
| 53 | // Parameters: |
| 54 | // mem: Already-allocated device memory value for this scoped mechanism to |
| 55 | // deallocate. This memory must have been allocated by parent. |
| 56 | // device_ordinal: Device on which the memory was allocated. |
| 57 | // allocator: Allocator used to deallocate memory when this instance goes |
| 58 | // out of scope. |
| 59 | ScopedDeviceMemory(DeviceMemoryBase mem, int device_ordinal, |
| 60 | DeviceMemoryAllocator *allocator) |
| 61 | : wrapped_(mem), device_ordinal_(device_ordinal), allocator_(allocator) {} |
| 62 | |
| 63 | // A helper constructor to generate a scoped device memory given an already |
| 64 | // allocated memory and a stream executor. |
| 65 | // |
| 66 | // Precondition: memory was allocated by the stream executor `parent`. |
| 67 | ScopedDeviceMemory(StreamExecutor *parent, DeviceMemoryBase value); |
| 68 | |
| 69 | // Constructor overload that places a literal array into device memory. |
| 70 | // |
| 71 | // Relies on the allocation function exposed by the stream executor `parent`, |
| 72 | // which will be also used for deallocating the memory |
| 73 | ScopedDeviceMemory(StreamExecutor *parent, |
| 74 | std::initializer_list<ElemT> values); |
| 75 | |
| 76 | // Moves ownership of the memory from other to the constructed |
| 77 | // object. |
| 78 | // |
| 79 | // Postcondition: other == nullptr. |
| 80 | ScopedDeviceMemory(ScopedDeviceMemory &&other) |
| 81 | : ScopedDeviceMemory(other.Release(), other.device_ordinal_, |
| 82 | other.allocator_) {} |
| 83 | |
| 84 | // Releases the memory that was provided in the constructor, through the |
| 85 | // "parent" StreamExecutor. |
| 86 | ~ScopedDeviceMemory() { TF_CHECK_OK(Free()); } |
| 87 | |
| 88 | // Moves ownership of the memory from other to this object. |
| 89 | // |
| 90 | // Postcondition: other == nullptr. |
| 91 | ScopedDeviceMemory &operator=(ScopedDeviceMemory &&other) { |
| 92 | TF_CHECK_OK(Free()); |
| 93 | wrapped_ = other.Release(); |
| 94 | allocator_ = other.allocator_; |
| 95 | device_ordinal_ = other.device_ordinal_; |
| 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | // Returns the memory that backs this scoped allocation converted to |
| 100 | // DeviceMemory<T> apparent type. This is useful for cases where the |
| 101 | // DeviceMemory must be passed by const-ref, as the ScopedDeviceMemory doesn't |