Main Page Class List Class Members

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
60 struct TkWorkerJob
61 {
62 TkActorImpl* m_tkActor;
63 TkActorImpl** m_newActors;
64 uint32_t m_newActorsCount;
65 };
66
67
68
69
70
74 template<typename T>
75 class SharedBlock
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>
135 class SharedBuffer
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>
191 class LocalBuffer
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
257 class SharedMemory
258 {
259 public:
260 SharedMemory() : m_eventsMemory(0), m_eventsCount(0), m_refCount(0) {}
261
265 NvBlastActor** reserveNewActors(size_t n)
266 {
267 return m_newActorBuffers.reserve(n);
268 }
269
273 TkActor** reserveNewTkActors(size_t n)
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
307 bool removeReference()
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
330 TkEventQueue m_events;
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
359 LocalBuffer<NvBlastChunkFractureData> m_chunkBuffer;
360 LocalBuffer<NvBlastBondFractureData> m_bondBuffer;
361
362 void* m_splitScratch;
363 NvBlastFractureBuffers m_tempBuffer;
364 bool m_isBusy;
365
366 #if NV_PROFILE
367 TkGroupStats m_stats;
368 #endif
369 };
370 }
371 }
372
373 #endif // NVBLASTTKTASKIMPL_H
Nv::Blast::SharedMemory::m_eventsMemory
uint32_t m_eventsMemory
expected memory size for event data
Definition: NvBlastTkTaskImpl.h:331
NvBlastArray.h
Nv::Blast::SharedBlock::SharedBlock
SharedBlock()
Definition: NvBlastTkTaskImpl.h:79
Nv::Blast::SharedMemory::SharedMemory
SharedMemory()
Definition: NvBlastTkTaskImpl.h:260
NvBlast.h
NvBlastTkEventQueue.h
Nv::Blast::TkWorker::m_bondBuffer
LocalBuffer< NvBlastBondFractureData > m_bondBuffer
memory manager for bonds event data
Definition: NvBlastTkTaskImpl.h:360
Nv::Blast::TkActorImpl
Definition: NvBlastTkActorImpl.h:75
Nv::Blast::SharedBuffer
Definition: NvBlastTkTaskImpl.h:135
Nv::Blast::InlineArray
Definition: NvBlastArray.h:58
Nv::Blast::TkWorker::m_isBusy
bool m_isBusy
Definition: NvBlastTkTaskImpl.h:364
Nv::Blast::SharedMemory::isUsed
bool isUsed()
Definition: NvBlastTkTaskImpl.h:316
Nv::Blast::SharedMemory::reset
void reset()
Definition: NvBlastTkTaskImpl.h:287
Nv::Blast::TkActor
Definition: NvBlastTkActor.h:57
Nv::Blast::TkWorker::m_splitScratch
void * m_splitScratch
Definition: NvBlastTkTaskImpl.h:362
Nv::Blast::LocalBuffer::initialize
void initialize(T *block, size_t capacity)
Definition: NvBlastTkTaskImpl.h:227
Nv::Blast::SharedMemory::reserveNewActors
NvBlastActor ** reserveNewActors(size_t n)
Definition: NvBlastTkTaskImpl.h:265
Nv::Blast::SharedMemory::addReference
void addReference(size_t n)
Definition: NvBlastTkTaskImpl.h:301
Nv::Blast::TkWorker::m_group
TkGroupImpl * m_group
the group owning this worker
Definition: NvBlastTkTaskImpl.h:357
Nv::Blast::TkWorker::m_chunkBuffer
LocalBuffer< NvBlastChunkFractureData > m_chunkBuffer
memory manager for chunk event data
Definition: NvBlastTkTaskImpl.h:359
Nv::Blast::TkGroupStats
Definition: NvBlastTkGroup.h:58
Nv::Blast::TkWorkerJob::m_newActors
TkActorImpl ** m_newActors
list of child actors created by splitting
Definition: NvBlastTkTaskImpl.h:63
Nv::Blast::TkWorker
Definition: NvBlastTkTaskImpl.h:346
Nv::Blast::LocalBuffer::clear
void clear()
Definition: NvBlastTkTaskImpl.h:214
NVBLAST_ASSERT
#define NVBLAST_ASSERT(exp)
Definition: NvBlastAssert.h:37
Nv::Blast::SharedMemory::addReference
void addReference()
Definition: NvBlastTkTaskImpl.h:296
Nv::Blast::SharedBuffer::release
void release()
Definition: NvBlastTkTaskImpl.h:171
Nv::Blast::TkWorkerJob::m_newActorsCount
uint32_t m_newActorsCount
the number of child actors created
Definition: NvBlastTkTaskImpl.h:64
Nv::Blast::TkWorkerJob::m_tkActor
TkActorImpl * m_tkActor
the actor to process
Definition: NvBlastTkTaskImpl.h:62
Nv::Blast::SharedMemory::release
void release()
Definition: NvBlastTkTaskImpl.h:324
Nv::Blast::TkWorkerJob
Definition: NvBlastTkTaskImpl.h:60
BLAST_PROFILE_SCOPE_L
#define BLAST_PROFILE_SCOPE_L(name)
Definition: NvBlastProfilerInternal.h:85
Nv::Blast::TkWorker::m_id
uint32_t m_id
this worker&#39;s id
Definition: NvBlastTkTaskImpl.h:356
Nv::Blast::TkEventQueue
Definition: NvBlastTkEventQueue.h:61
Nv::Blast::SharedBuffer::allocate
void allocate(size_t capacity)
Definition: NvBlastTkTaskImpl.h:153
Nv::Blast::LocalBuffer::allocate
T * allocate(size_t n)
Definition: NvBlastTkTaskImpl.h:198
NVBLAST_FREE
#define NVBLAST_FREE(_mem)
Definition: NvBlastGlobals.h:222
Nv::Blast::SharedBlock
Definition: NvBlastTkTaskImpl.h:75
Nv::Blast::SharedMemory
Definition: NvBlastTkTaskImpl.h:257
Nv::Blast::SharedBlock::numElementsPerBlock
uint32_t numElementsPerBlock() const
Definition: NvBlastTkTaskImpl.h:105
Nv::Blast::SharedBlock::release
void release()
Definition: NvBlastTkTaskImpl.h:113
Nv::Blast::SharedMemory::m_events
TkEventQueue m_events
event queue shared across a group&#39;s actors of the same family
Definition: NvBlastTkTaskImpl.h:330
NvBlastFractureBuffers
Definition: NvBlastTypes.h:468
NVBLAST_ALLOC_NAMED
#define NVBLAST_ALLOC_NAMED(_size, _name)
Definition: NvBlastGlobals.h:221
NvBlastTkGroup.h
NvBlastTkFrameworkImpl.h
Nv::Blast::TkGroupWorker
Definition: NvBlastTkGroup.h:74
Nv::Blast::SharedBuffer::reset
void reset()
Definition: NvBlastTkTaskImpl.h:163
Nv::Blast::SharedBuffer::reserve
T * reserve(size_t n)
Definition: NvBlastTkTaskImpl.h:143
Nv::Blast::SharedBlock::getBlock
T * getBlock(uint32_t id)
Definition: NvBlastTkTaskImpl.h:96
Nv::Blast::SharedMemory::m_eventsCount
uint32_t m_eventsCount
expected number of events
Definition: NvBlastTkTaskImpl.h:332
Nv
Definition: NvBlastArray.h:37
Nv::Blast::SharedMemory::reserveNewTkActors
TkActor ** reserveNewTkActors(size_t n)
Definition: NvBlastTkTaskImpl.h:273
Nv::Blast::TkWorker::m_tempBuffer
NvBlastFractureBuffers m_tempBuffer
Definition: NvBlastTkTaskImpl.h:363
NvBlastAssert.h
NvBlastActor
Definition: NvBlastTypes.h:387
Nv::Blast::SharedBuffer::SharedBuffer
SharedBuffer()
Definition: NvBlastTkTaskImpl.h:138
Nv::Blast::SharedBlock::allocate
void allocate(uint32_t elementsPerBlock, uint32_t numBlocks)
Definition: NvBlastTkTaskImpl.h:84
Nv::Blast::LocalBuffer
Definition: NvBlastTkTaskImpl.h:191
Nv::Blast::SharedMemory::removeReference
bool removeReference()
Definition: NvBlastTkTaskImpl.h:307
Nv::Blast::TkWorker::TkWorker
TkWorker()
Definition: NvBlastTkTaskImpl.h:349