NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
ScopedMemoryAllocation.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 
26 #ifndef NVNEURAL_SCOPEDMEMORYALLOCATION_H
27 #define NVNEURAL_SCOPEDMEMORYALLOCATION_H
28 
29 namespace nvneural {
30 
40  {
41  public:
47  ScopedMemoryAllocation(INetworkBackend* pBackend, size_t bufferSize)
48  : m_pBackend{pBackend}
49  , m_hBuffer{nullptr}
50  {
51  const NeuralResult status = pBackend->allocateMemoryBlock(&m_hBuffer, bufferSize);
52  if (failed(status))
53  {
54  m_hBuffer = nullptr;
55  }
56  }
57 
65  ScopedMemoryAllocation(INetworkBackend* pBackend, size_t bufferSize, const char* pTrackingKey)
66  : m_pBackend{pBackend}
67  , m_hBuffer{nullptr}
68  {
69  NeuralResult status;
71  if (pBackend2)
72  {
73  status = pBackend2->allocateMemoryBlock(&m_hBuffer, bufferSize, pTrackingKey);
74  }
75  else
76  {
77  status = pBackend->allocateMemoryBlock(&m_hBuffer, bufferSize);
78  }
79  if (failed(status))
80  {
81  m_hBuffer = nullptr;
82  }
83  }
84 
85  ScopedMemoryAllocation(const ScopedMemoryAllocation& copyFrom) = delete;
86 
89  : m_pBackend{moveFrom.m_pBackend}
90  , m_hBuffer{moveFrom.m_hBuffer}
91  {
92  }
93 
96  {
97  if (m_hBuffer)
98  {
99  m_pBackend->freeMemoryBlock(m_hBuffer);
100  }
101  }
102 
103  ScopedMemoryAllocation& operator=(const ScopedMemoryAllocation& copyFrom) = delete;
104 
109  {
110  std::swap(m_pBackend, moveFrom.m_pBackend);
111  std::swap(m_hBuffer, moveFrom.m_hBuffer);
112  return *this;
113  }
114 
116  explicit operator bool() const
117  {
118  return nullptr != m_hBuffer;
119  }
120 
125  {
126  return m_hBuffer;
127  }
128 
132  void* asPointer()
133  {
134  return m_pBackend->getAddressForMemoryBlock(m_hBuffer);
135  }
136 
137  private:
138  INetworkBackend* m_pBackend;
139  MemoryHandle m_hBuffer;
140  };
141 
142 } // namespace nvneural
143 
144 #endif // NVNEURAL_SCOPEDMEMORYALLOCATION_H
constexpr bool failed(NeuralResult result) noexcept
Helper function akin to COM's FAILED() macro.
Definition: CoreTypes.h:289
MemoryHandle__type * MemoryHandle
Opaque typedef used to represent INetworkBackend memory handles.
Definition: CoreTypes.h:626
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:275
INetworkBackend2 is a revision of INetworkBackend.
Definition: CoreTypes.h:958
INetworkBackend is a runtime-specific interface for CUDA, DirectX, or other system- specific operatio...
Definition: CoreTypes.h:643
virtual NeuralResult allocateMemoryBlock(MemoryHandle *pHandle, size_t byteCount) noexcept=0
Allocates a memory block of the requested size.
virtual NeuralResult freeMemoryBlock(MemoryHandle handle) noexcept=0
Frees a memory block that was allocated with allocateMemoryBlock.
virtual void * getAddressForMemoryBlock(MemoryHandle handle) noexcept=0
Retrieves the raw address corresponding to a MemoryHandle.
Intrusive pointer using IRefObject's reference counting system.
Definition: RefPtr.h:46
RefPtr< TDestinationObject > as() const noexcept
Creates a new reference to the object using the specified interface.
Definition: RefPtr.h:221
static RefPtr fromPointer(TObject *pObject)
Initializes a RefPtr pointing to a specific object.
Definition: RefPtr.h:65
ScopedMemoryAllocation is a helper function for allocating memory blocks from the backend while addin...
Definition: ScopedMemoryAllocation.h:40
MemoryHandle handle() const
Returns the memory handle of the allocated memory block.
Definition: ScopedMemoryAllocation.h:124
void * asPointer()
Returns the pointer to the allocated memory block.
Definition: ScopedMemoryAllocation.h:132
~ScopedMemoryAllocation()
Frees the allocation.
Definition: ScopedMemoryAllocation.h:95
ScopedMemoryAllocation(INetworkBackend *pBackend, size_t bufferSize, const char *pTrackingKey)
Creates a temporary memory allocation, using a network backend, of the requested size and allows trac...
Definition: ScopedMemoryAllocation.h:65
ScopedMemoryAllocation & operator=(ScopedMemoryAllocation &&moveFrom)
Takes ownership of an allocation from another ScopedMemoryAllocation.
Definition: ScopedMemoryAllocation.h:108
ScopedMemoryAllocation(INetworkBackend *pBackend, size_t bufferSize)
Creates a temporary memory allocation using a network backend.
Definition: ScopedMemoryAllocation.h:47
ScopedMemoryAllocation(ScopedMemoryAllocation &&moveFrom)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: ScopedMemoryAllocation.h:88