PxCudaMemoryManager.h
Go to the documentation of this file.
1 //
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions
4 // are met:
5 // * Redistributions of source code must retain the above copyright
6 // notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright
8 // notice, this list of conditions and the following disclaimer in the
9 // documentation and/or other materials provided with the distribution.
10 // * Neither the name of NVIDIA CORPORATION nor the names of its
11 // contributors may be used to endorse or promote products derived
12 // from this software without specific prior written permission.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27 
28 #ifndef PXCUDACONTEXTMANAGER_PXCUDAMEMORYMANAGER_H
29 #define PXCUDACONTEXTMANAGER_PXCUDAMEMORYMANAGER_H
30 
32 
33 #if PX_SUPPORT_GPU_PHYSX
34 
35 #include "task/PxTaskDefine.h"
36 
37 // some macros to keep the source code more readable
38 #define PX_ALLOC_INFO(name, ID) __FILE__, __LINE__, name, physx::PxAllocId::ID
39 #define PX_ALLOC_INFO_PARAMS_DECL(p0, p1, p2, p3) const char* file = p0, int line = p1, const char* allocName = p2, physx::PxAllocId::Enum allocId = physx::PxAllocId::p3
40 #define PX_ALLOC_INFO_PARAMS_DEF() const char* file, int line, const char* allocName, physx::PxAllocId::Enum allocId
41 #define PX_ALLOC_INFO_PARAMS_INPUT() file, line, allocName, allocId
42 #define PX_ALLOC_INFO_PARAMS_INPUT_INFO(info) info.getFileName(), info.getLine(), info.getAllocName(), info.getAllocId()
43 
44 #ifndef NULL // don't want to include <string.h>
45 #define NULL 0
46 #endif
47 
48 namespace physx
49 {
50 
52 
57 struct PxAllocId
58 {
62  enum Enum
63  {
64  UNASSIGNED,
65  APEX,
66  PARTICLES,
67  GPU_UTIL,
68  CLOTH,
69  NUM_IDS
70  };
71 };
72 
74 struct PxCudaBufferMemorySpace
75 {
79  enum Enum
80  {
81  T_GPU,
82  T_PINNED_HOST,
83  T_WRITE_COMBINED,
84  T_HOST,
85  COUNT
86  };
87 };
88 
90 class PxAllocInfo
91 {
92 public:
96  PxAllocInfo() {}
97 
101  PxAllocInfo(const char* file, int line, const char* allocName, PxAllocId::Enum allocId)
102  : mFileName(file)
103  , mLine(line)
104  , mAllocName(allocName)
105  , mAllocId(allocId)
106  {}
107 
109  inline const char* getFileName() const
110  {
111  return mFileName;
112  }
113 
115  inline int getLine() const
116  {
117  return mLine;
118  }
119 
121  inline const char* getAllocName() const
122  {
123  return mAllocName;
124  }
125 
127  inline PxAllocId::Enum getAllocId() const
128  {
129  return mAllocId;
130  }
131 
132 private:
133  const char* mFileName;
134  int mLine;
135  const char* mAllocName;
136  PxAllocId::Enum mAllocId;
137 };
138 
140 struct PxAllocIdStats
141 {
142  size_t size;
143  size_t maxSize;
144  size_t elements;
145  size_t maxElements;
146 };
147 
148 class PxCudaMemoryManager;
149 typedef size_t PxCudaBufferPtr;
150 
152 struct PxCudaBufferFlags
153 {
155  enum Enum
156  {
157  F_READ = (1 << 0),
158  F_WRITE = (1 << 1),
159  F_READ_WRITE = F_READ | F_WRITE
160  };
161 };
162 
163 
165 struct PxCudaMemoryManagerStats
166 {
167 
168  size_t heapSize;
169  size_t totalAllocated;
170  size_t maxAllocated;
171  PxAllocIdStats allocIdStats[PxAllocId::NUM_IDS];
172 };
173 
174 
176 struct PxCudaBufferType
177 {
179  PX_INLINE PxCudaBufferType(const PxCudaBufferType& t)
180  : memorySpace(t.memorySpace)
181  , flags(t.flags)
182  {}
183 
185  PX_INLINE PxCudaBufferType(PxCudaBufferMemorySpace::Enum _memSpace, PxCudaBufferFlags::Enum _flags)
186  : memorySpace(_memSpace)
187  , flags(_flags)
188  {}
189 
190  PxCudaBufferMemorySpace::Enum memorySpace;
191  PxCudaBufferFlags::Enum flags;
192 };
193 
194 
196 class PxCudaBuffer
197 {
198 public:
200  virtual PxCudaMemoryManager* getCudaMemoryManager() const = 0;
201 
203  virtual bool free() = 0;
204 
209  virtual bool realloc(size_t size, PX_ALLOC_INFO_PARAMS_DECL(NULL, 0, NULL, UNASSIGNED)) = 0;
210 
212  virtual const PxCudaBufferType& getType() const = 0;
213 
215  virtual PxCudaBufferPtr getPtr() const = 0;
216 
218  virtual size_t getSize() const = 0;
219 
220 protected:
222  virtual ~PxCudaBuffer() {}
223 };
224 
225 
227 class PxCudaMemoryManager
228 {
229 public:
231  virtual PxCudaBuffer* alloc(const PxCudaBufferType& type, size_t size, PX_ALLOC_INFO_PARAMS_DECL(NULL, 0, NULL, UNASSIGNED)) = 0;
232 
234  virtual PxCudaBufferPtr alloc(PxCudaBufferMemorySpace::Enum memorySpace, size_t size, PX_ALLOC_INFO_PARAMS_DECL(NULL, 0, NULL, UNASSIGNED)) = 0;
235 
237  virtual bool free(PxCudaBufferMemorySpace::Enum memorySpace, PxCudaBufferPtr addr) = 0;
238 
240  virtual bool realloc(PxCudaBufferMemorySpace::Enum memorySpace, PxCudaBufferPtr addr, size_t size, PX_ALLOC_INFO_PARAMS_DECL(NULL, 0, NULL, UNASSIGNED)) = 0;
241 
243  virtual void getStats(const PxCudaBufferType& type, PxCudaMemoryManagerStats& outStats) = 0;
244 
247  virtual bool reserve(const PxCudaBufferType& type, size_t size) = 0;
248 
252  virtual bool setPageSize(const PxCudaBufferType& type, size_t size) = 0;
253 
256  virtual bool setMaxMemorySize(const PxCudaBufferType& type, size_t size) = 0;
257 
259  virtual size_t getBaseSize(const PxCudaBufferType& type) = 0;
260 
262  virtual size_t getPageSize(const PxCudaBufferType& type) = 0;
263 
265  virtual size_t getMaxMemorySize(const PxCudaBufferType& type) = 0;
266 
268  virtual PxCudaBufferPtr getMappedPinnedPtr(PxCudaBufferPtr hostPtr) = 0;
269 
270 protected:
272  virtual ~PxCudaMemoryManager() {}
273 };
274 
276 
277 
278 } // end physx namespace
279 
280 #endif // PX_SUPPORT_GPU_PHYSX
281 #endif // PXCUDACONTEXTMANAGER_PXCUDAMEMORYMANAGER_H
Definition: GuContactBuffer.h:37
#define PX_POP_PACK
Definition: PxPreprocessor.h:330
PxU16 flags
a set of Px1DConstraintFlags
Definition: PxConstraintDesc.h:110
PX_FORCE_INLINE PxGeometryType::Enum getType() const
Definition: PxGeometryHelpers.h:88
#define PX_PUSH_PACK_DEFAULT
Definition: PxPreprocessor.h:329
#define PX_INLINE
Definition: PxPreprocessor.h:336