NvBlastTkTaskImpl.h
Go to the documentation of this file.
1 // This code contains NVIDIA Confidential Information and is disclosed to you
2 // under a form of NVIDIA software license agreement provided separately to you.
3 //
4 // Notice
5 // NVIDIA Corporation and its licensors retain all intellectual property and
6 // proprietary rights in and to this software and related documentation and
7 // any modifications thereto. Any use, reproduction, disclosure, or
8 // distribution of this software and related documentation without an express
9 // license agreement from NVIDIA Corporation is strictly prohibited.
10 //
11 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
12 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
13 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
14 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // Information and code furnished is believed to be accurate and reliable.
17 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
18 // information or for any infringement of patents or other rights of third parties that may
19 // result from its use. No license is granted by implication or otherwise under any patent
20 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
21 // This code supersedes and replaces all information previously supplied.
22 // NVIDIA Corporation products are not authorized for use as critical
23 // components in life support devices or systems without express written approval of
24 // NVIDIA Corporation.
25 //
26 // Copyright (c) 2016-2020 NVIDIA Corporation. All rights reserved.
27 
28 
29 #ifndef NVBLASTTKTASKIMPL_H
30 #define NVBLASTTKTASKIMPL_H
31 
32 #include "NvBlast.h"
33 
34 #include "NvBlastTkFrameworkImpl.h"
35 #include "NvBlastTkEventQueue.h"
36 #include "NvBlastArray.h"
37 
38 #include <atomic>
39 #include <mutex>
40 #include <condition_variable>
41 
42 #include "NvBlastAssert.h"
43 
44 #include "NvBlastTkGroup.h" // TkGroupStats
45 
46 
47 namespace Nv
48 {
49 namespace Blast
50 {
51 
52 class TkGroupImpl;
53 class TkActorImpl;
54 class TkFamilyImpl;
55 
56 
61 {
64  uint32_t m_newActorsCount;
65 };
66 
67 
68 
69 
70 
74 template<typename T>
76 {
77 public:
78 
79  SharedBlock() : m_numElementsPerBlock(0), m_numBlocks(0), m_buffer(nullptr) {}
80 
84  void allocate(uint32_t elementsPerBlock, uint32_t numBlocks)
85  {
86  NVBLAST_ASSERT(elementsPerBlock > 0 && numBlocks > 0);
87 
88  m_buffer = reinterpret_cast<T*>(NVBLAST_ALLOC_NAMED(elementsPerBlock*numBlocks*sizeof(T), "SharedBlock"));
89  m_numElementsPerBlock = elementsPerBlock;
90  m_numBlocks = numBlocks;
91  }
92 
96  T* getBlock(uint32_t id)
97  {
98  NVBLAST_ASSERT(id < m_numBlocks || 0 == m_numElementsPerBlock);
99  return &m_buffer[id*m_numElementsPerBlock];
100  }
101 
105  uint32_t numElementsPerBlock() const
106  {
107  return m_numElementsPerBlock;
108  }
109 
113  void release()
114  {
115  m_numBlocks = 0;
116  m_numElementsPerBlock = 0;
117  NVBLAST_FREE(m_buffer);
118  m_buffer = nullptr;
119  }
120 
121 private:
122  uint32_t m_numElementsPerBlock;
123  uint32_t m_numBlocks;
124  T* m_buffer;
125 };
126 
127 
134 template<typename T>
136 {
137 public:
138  SharedBuffer() : m_capacity(0), m_used(0), m_buffer(nullptr) {}
139 
143  T* reserve(size_t n)
144  {
145  NVBLAST_ASSERT(m_used + n <= m_capacity);
146  size_t start = m_used.fetch_add(n);
147  return &m_buffer[start];
148  }
149 
153  void allocate(size_t capacity)
154  {
155  NVBLAST_ASSERT(m_buffer == nullptr);
156  m_buffer = reinterpret_cast<T*>(NVBLAST_ALLOC_NAMED(capacity*sizeof(T), "SplitMemory"));
157  m_capacity = capacity;
158  }
159 
163  void reset()
164  {
165  m_used = 0;
166  }
167 
171  void release()
172  {
173  NVBLAST_ASSERT(m_buffer != nullptr);
174  NVBLAST_FREE(m_buffer);
175  m_buffer = nullptr;
176  m_capacity = m_used = 0;
177  }
178 
179 private:
180  size_t m_capacity;
181  std::atomic<size_t> m_used;
182  T* m_buffer;
183 };
184 
185 
190 template<typename T>
192 {
193 public:
198  T* allocate(size_t n)
199  {
200  if (m_used + n > m_capacity)
201  {
202  allocateNewBlock(n > m_capacity ? n : m_capacity);
203  }
204 
205  size_t index = m_used;
206  m_used += n;
207  return &m_currentBlock[index];
208  }
209 
214  void clear()
215  {
216  for (void* block : m_memoryBlocks)
217  {
218  NVBLAST_FREE(block);
219  }
220  m_memoryBlocks.clear();
221  }
222 
227  void initialize(T* block, size_t capacity)
228  {
229  m_currentBlock = block;
230  m_capacity = capacity;
231  m_used = 0;
232  }
233 
234 private:
238  void allocateNewBlock(size_t capacity)
239  {
240  BLAST_PROFILE_SCOPE_L("Local Buffer allocation");
241  m_capacity = capacity;
242  m_currentBlock = static_cast<T*>(NVBLAST_ALLOC_NAMED(capacity*sizeof(T), "Blast LocalBuffer"));
243  m_memoryBlocks.pushBack(m_currentBlock);
244  m_used = 0;
245  }
246 
247  InlineArray<void*, 4>::type m_memoryBlocks;
248  T* m_currentBlock;
249  size_t m_used;
250  size_t m_capacity;
251 };
252 
253 
258 {
259 public:
260  SharedMemory() : m_eventsMemory(0), m_eventsCount(0), m_refCount(0) {}
261 
266  {
267  return m_newActorBuffers.reserve(n);
268  }
269 
274  {
275  return m_newTkActorBuffers.reserve(n);
276  }
277 
281  void allocate(TkFamilyImpl&);
282 
287  void reset()
288  {
289  m_newActorBuffers.reset();
290  m_newTkActorBuffers.reset();
291  }
292 
296  void addReference() { m_refCount++; }
297 
301  void addReference(size_t n) { m_refCount += n; }
302 
308  {
309  m_refCount--;
310  return !isUsed();
311  }
312 
316  bool isUsed()
317  {
318  return m_refCount > 0;
319  }
320 
324  void release()
325  {
326  m_newActorBuffers.release();
327  m_newTkActorBuffers.release();
328  }
329 
331  uint32_t m_eventsMemory;
332  uint32_t m_eventsCount;
333 
334 private:
335  size_t m_refCount;
336 
337  SharedBuffer<NvBlastActor*> m_newActorBuffers;
338  SharedBuffer<TkActor*> m_newTkActorBuffers;
339 };
340 
341 
346 class TkWorker final : public TkGroupWorker
347 {
348 public:
349  TkWorker() : m_id(~(uint32_t)0), m_group(nullptr), m_isBusy(false) {}
350 
351  void process(uint32_t jobID);
352  void initialize();
353 
354  void process(TkWorkerJob& job);
355 
356  uint32_t m_id;
357  TkGroupImpl* m_group;
358 
361 
364  bool m_isBusy;
365 
366 #if NV_PROFILE
367  TkGroupStats m_stats;
368 #endif
369 };
370 }
371 }
372 
373 #endif // NVBLASTTKTASKIMPL_H
uint32_t m_eventsMemory
expected memory size for event data
Definition: NvBlastTkTaskImpl.h:331
SharedBlock()
Definition: NvBlastTkTaskImpl.h:79
SharedMemory()
Definition: NvBlastTkTaskImpl.h:260
LocalBuffer< NvBlastBondFractureData > m_bondBuffer
memory manager for bonds event data
Definition: NvBlastTkTaskImpl.h:360
Definition: NvBlastTkActorImpl.h:75
Definition: NvBlastTkTaskImpl.h:135
Definition: NvBlastArray.h:58
bool m_isBusy
Definition: NvBlastTkTaskImpl.h:364
bool isUsed()
Definition: NvBlastTkTaskImpl.h:316
void reset()
Definition: NvBlastTkTaskImpl.h:287
Definition: NvBlastTkActor.h:57
void * m_splitScratch
Definition: NvBlastTkTaskImpl.h:362
void initialize(T *block, size_t capacity)
Definition: NvBlastTkTaskImpl.h:227
NvBlastActor ** reserveNewActors(size_t n)
Definition: NvBlastTkTaskImpl.h:265
void addReference(size_t n)
Definition: NvBlastTkTaskImpl.h:301
TkGroupImpl * m_group
the group owning this worker
Definition: NvBlastTkTaskImpl.h:357
LocalBuffer< NvBlastChunkFractureData > m_chunkBuffer
memory manager for chunk event data
Definition: NvBlastTkTaskImpl.h:359
Definition: NvBlastTkGroup.h:58
TkActorImpl ** m_newActors
list of child actors created by splitting
Definition: NvBlastTkTaskImpl.h:63
Definition: NvBlastTkTaskImpl.h:346
void clear()
Definition: NvBlastTkTaskImpl.h:214
#define NVBLAST_ASSERT(exp)
Definition: NvBlastAssert.h:37
void addReference()
Definition: NvBlastTkTaskImpl.h:296
void release()
Definition: NvBlastTkTaskImpl.h:171
uint32_t m_newActorsCount
the number of child actors created
Definition: NvBlastTkTaskImpl.h:64
TkActorImpl * m_tkActor
the actor to process
Definition: NvBlastTkTaskImpl.h:62
void release()
Definition: NvBlastTkTaskImpl.h:324
Definition: NvBlastTkTaskImpl.h:60
#define BLAST_PROFILE_SCOPE_L(name)
Definition: NvBlastProfilerInternal.h:85
uint32_t m_id
this worker&#39;s id
Definition: NvBlastTkTaskImpl.h:356
Definition: NvBlastTkEventQueue.h:61
void allocate(size_t capacity)
Definition: NvBlastTkTaskImpl.h:153
T * allocate(size_t n)
Definition: NvBlastTkTaskImpl.h:198
#define NVBLAST_FREE(_mem)
Definition: NvBlastGlobals.h:222
Definition: NvBlastTkTaskImpl.h:75
Definition: NvBlastTkTaskImpl.h:257
uint32_t numElementsPerBlock() const
Definition: NvBlastTkTaskImpl.h:105
void release()
Definition: NvBlastTkTaskImpl.h:113
TkEventQueue m_events
event queue shared across a group&#39;s actors of the same family
Definition: NvBlastTkTaskImpl.h:330
Definition: NvBlastTypes.h:468
#define NVBLAST_ALLOC_NAMED(_size, _name)
Definition: NvBlastGlobals.h:221
Definition: NvBlastTkGroup.h:74
void reset()
Definition: NvBlastTkTaskImpl.h:163
T * reserve(size_t n)
Definition: NvBlastTkTaskImpl.h:143
T * getBlock(uint32_t id)
Definition: NvBlastTkTaskImpl.h:96
uint32_t m_eventsCount
expected number of events
Definition: NvBlastTkTaskImpl.h:332
Definition: NvBlastArray.h:37
TkActor ** reserveNewTkActors(size_t n)
Definition: NvBlastTkTaskImpl.h:273
NvBlastFractureBuffers m_tempBuffer
Definition: NvBlastTkTaskImpl.h:363
Definition: NvBlastTypes.h:387
SharedBuffer()
Definition: NvBlastTkTaskImpl.h:138
void allocate(uint32_t elementsPerBlock, uint32_t numBlocks)
Definition: NvBlastTkTaskImpl.h:84
Definition: NvBlastTkTaskImpl.h:191
bool removeReference()
Definition: NvBlastTkTaskImpl.h:307
TkWorker()
Definition: NvBlastTkTaskImpl.h:349