AGX Dynamics 2.42.3.1
Loading...
Searching...
No Matches
GlobalResult.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_GLOBALRESULT_H
18#define AGX_GLOBALRESULT_H
19
20#ifdef _MSC_VER
21# pragma warning(push)
22# pragma warning( disable : 4290 ) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
23#endif
24
25#include <agx/agx.h>
26#include <agx/agxCore_export.h>
28#include <agx/Object.h>
29
30#include <atomic>
31
32namespace agxData
33{
34 class Buffer;
35 class EntityStorage;
36}
37
38namespace agx
39{
40 class Task;
41 class Parameter;
42 class ArrayParameter;
43 class EntityDataParameter;
44
52 {
53 public:
55
57 {
58 // Mode
59 TRUNCATE = 0x1,
60 APPEND = 0x2,
61
62 // Ordering
63 ORDER = 0x4,
64 SORT = 0x8
65 };
66
67
73 {
74 public:
78 const IndexRange& range() const;
79
87 bool commit(Index sortIndex);
88
92 bool commit();
93
94
98 BufferSet *getBufferSet();
99
103 GlobalResult *getTarget();
104
105 private:
106 friend class agx::GlobalResult;
107 Transaction(GlobalResult *target, const IndexRange& range, BufferSet *bufferSet);
108
109 private:
110 GlobalResult *m_target;
111 IndexRange m_range;
112 BufferSet *m_bufferSet;
113 };
114
115
121 Transaction allocateResult(size_t numElements);
122
135 size_t commit(const agxData::AbstractArray& target, size_t numElements, const void *localResult, Index sortIndex);
136
137 template <typename T>
138 size_t commit(const agxData::AbstractArray& target, const T& vector, Index sortIndex);
139
140 size_t commit(const agxData::AbstractArray& target, size_t numElements, const void *localResult);
141
142 template <typename T>
143 size_t commit(const agxData::AbstractArray& target, const T& vector);
144
145
149 bool didOverflow() const;
150
154 agx::UInt getNumOverflows() const;
155
156 agx::Mutex& getMutex();
157
158 public:
159 GlobalResult(UInt settings, const Path& bindPath);
160
161 BufferSet *getActiveSet();
162
163 void prepareResultSorting(size_t numJobs);
164
165 protected:
167 {
170
172 void* ptr;
173 size_t size;
174 };
175
176 protected:
177 virtual ~GlobalResult();
178
179 virtual void handleOverflow(size_t overflowSize) = 0;
180
181 template <typename T>
182 size_t calculateInitialCapacity(const T *target);
183
184 size_t calculateIncreasedCapacity(size_t currentCapacity, size_t overflowSize);
185
188 virtual void sortResult(const agxData::IndexArray& permutation) = 0;
189
190 void init(const IndexRange& range);
191 BufferSet *createNewSet(size_t capacity);
192 BufferSet *reportFailedTransaction(Transaction *transaction);
193 void reportFailedTransaction(const IndexRange& transactionRange, BufferSet *startSet);
195
196 protected:
200
201 std::atomic<size_t> m_capacity;
204
205 std::atomic<agx::Int32> m_head;
208 std::atomic<agx::Int32> m_numTransactions;
209 std::atomic<agx::Int32> m_numCompletedTransactions;
211
214 };
215
216 //---------------------------------------------------------------
217
221 class AGXCORE_EXPORT GlobalResult::BufferSet : public Referenced
222 {
223 public:
224 BufferSet(size_t index, size_t capacity);
225
227 void init(size_t index, size_t capacity);
228
230 size_t getIndex() const;
231
233 size_t getCapacity() const;
234
236 void addOverflow(const OverflowBuffer& buffer);
237
239 void addFailedTransaction(const IndexRange& transactionRange);
240
243
246
248 void commitRanges(const IndexRangeVector& ranges);
249
250 protected:
251 virtual ~BufferSet() {}
252
253 private:
254 VectorPOD<OverflowBuffer> m_overflows;
255 IndexRangeVector m_failedTransactions;
256 size_t m_index;
257 size_t m_capacity;
258 };
259
260 //---------------------------------------------------------------
261
264 {
265 public:
266
268
269 void prepare(Task *task);
270 void finalize();
271
272 // agxData::EntityStorage *getStorage();
273
274 protected:
276
277 virtual void handleOverflow(size_t overflowSize) override;
278 virtual void sortResult(const agxData::IndexArray& permutation) override;
279
280 private:
281 void locateTargets(Task *task);
282
284 void locateTargets(EntityDataParameter *parameter);
285
286 template <typename ParameterVectorT>
287 void locateTargets(const ParameterVectorT& parameters);
288
289 private:
290 agxData::EntityStorage *m_targetStorage;
292 VectorPOD<agxData::Buffer *> m_implicitBuffers;
293 bool m_targetInstanceBuffer;
294 };
295
296 //---------------------------------------------------------------
297
300 {
301 public:
303
304 void prepare(Task *task);
305 void finalize();
306
307 bool isActive() const;
308
309 protected:
310 virtual void handleOverflow(size_t overflowSize) override;
311 virtual void sortResult(const agxData::IndexArray& permutation) override;
312
314
315 private:
316 agxData::Buffer *m_target;
317 bool m_isActive;
318 };
319
320
321
322
323 /* Implementation */
324 AGX_FORCE_INLINE GlobalResult::Transaction::Transaction(GlobalResult *target, const IndexRange& range, BufferSet *bufferSet)
325 : m_target(target)
326 , m_range(range)
327 , m_bufferSet(bufferSet)
328 {
329 agxAssert(target);
330 agxAssert(bufferSet);
331 }
332
333
337
338
340 {
341 m_target->getMutex().lock();
342 auto currentActiveSet = m_target->getActiveSet();
343 m_target->getMutex().unlock();
344
345 if (m_bufferSet == currentActiveSet)
346 {
347 #ifdef AGX_DEBUG
348 m_target->reportCompletedTransaction(this);
349 #endif
350
351 agxAssert(sortIndex < m_target->m_sortedRanges.size());
352 m_target->m_sortedRanges[sortIndex] = m_range;
353
354 return true;
355 }
356
357 m_bufferSet = m_target->reportFailedTransaction(this);
358 return false;
359 }
360
362 {
363 agxAssertN(!(m_target->m_settings & (SORT|ORDER)) || m_target->m_sortedRanges.empty(), "%s: Must use extended commit method if SORT or ORDER is specified", m_target->getPath().c_str());
364
365 m_target->getMutex().lock();
366 BufferSet *currentActiveSet = m_target->getActiveSet();
367 m_target->getMutex().unlock();
368
369 if (m_bufferSet == currentActiveSet)
370 {
371 #ifdef AGX_DEBUG
372 m_target->reportCompletedTransaction(this);
373 #endif
374
375 return true;
376 }
377
378 m_bufferSet = m_target->reportFailedTransaction(this);
379 return false;
380 }
381
382
383 //---------------------------------------------------------------
384
388
389 //---------------------------------------------------------------
390
392 {
393 return m_mutex;
394 }
395
396 template <typename T>
397 AGX_FORCE_INLINE size_t GlobalResult::commit(const agxData::AbstractArray& target, const T& vector, Index sortIndex)
398 {
399 return this->commit(target, vector.size(), vector.ptr(), sortIndex);
400 }
401
402 template <typename T>
403 AGX_FORCE_INLINE size_t GlobalResult::commit(const agxData::AbstractArray& target, const T& vector)
404 {
405 return this->commit(target, vector.size(), vector.ptr());
406 }
407
408 AGX_FORCE_INLINE bool GlobalResultBuffer::isActive() const { return m_isActive; }
409
410
411
412}
413
414#ifdef _MSC_VER
415# pragma warning(pop)
416#endif
417#endif /* AGX_GLOBALRESULT_H */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXCORE_EXPORT
#define m_capacity
Definition: agx/Vector.h:430
Arrays are fast accessors into the data, or a portion of it, held by a Buffer.
Definition: Array.h:37
size_t size() const
Definition: Buffer.h:708
Abstract representation of a data buffer.
Definition: Buffer.h:56
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:73
size_t size() const
Definition: Container.h:134
Entity parameter.
Definition: Parameter.h:483
void prepare(Task *task)
virtual void handleOverflow(size_t overflowSize) override
GlobalResultBuffer(agxData::Buffer *target, UInt settings)
virtual void sortResult(const agxData::IndexArray &permutation) override
GlobalResultStorage(agxData::EntityStorage *targetStorage, UInt settings)
virtual void handleOverflow(size_t overflowSize) override
virtual void sortResult(const agxData::IndexArray &permutation) override
void prepare(Task *task)
Transactions are used to reserve exclusive memory areas in which the threads can write their data.
Definition: GlobalResult.h:73
const IndexRange & range() const
Definition: GlobalResult.h:334
bool commit()
Same as above but without deterministic sorting.
Definition: GlobalResult.h:361
GlobalResult is used by kernels to generate result data from different threads.
Definition: GlobalResult.h:52
VectorPOD< OverflowBuffer > & getOverflows()
@ SORT
Build a permutation table for deterministic ordering of result data.
Definition: GlobalResult.h:64
@ ORDER
Append to the existing data.
Definition: GlobalResult.h:63
virtual void handleOverflow(size_t overflowSize)=0
std::atomic< agx::Int32 > m_numCompletedTransactions
Definition: GlobalResult.h:209
size_t commit(const agxData::AbstractArray &target, size_t numElements, const void *localResult, Index sortIndex)
Convenience wrapper to perform a complete transaction, this consists of:
Transaction allocateResult(size_t numElements)
Allocate a transaction.
BufferSet * createNewSet(size_t capacity)
void commitRanges(const IndexRangeVector &ranges)
Commit partial result to final buffers.
bool didOverflow() const
Definition: GlobalResult.h:386
size_t getCapacity() const
void finalizeTransactions()
void init(const IndexRange &range)
virtual void sortResult(const agxData::IndexArray &permutation)=0
virtual ~BufferSet()
Definition: GlobalResult.h:251
BufferSet * getActiveSet()
Definition: GlobalResult.h:385
agxData::Buffer * m_resultOrderBuffer
Definition: GlobalResult.h:213
size_t calculateInitialCapacity(const T *target)
VectorPOD< Transaction > m_transactions
Definition: GlobalResult.h:207
Vector< BufferSetRef > m_bufferSets
Definition: GlobalResult.h:199
size_t calculateIncreasedCapacity(size_t currentCapacity, size_t overflowSize)
void addOverflow(const OverflowBuffer &buffer)
Report an overflown buffer.
std::atomic< size_t > m_capacity
Definition: GlobalResult.h:201
BufferSetRef m_activeSet
Definition: GlobalResult.h:198
void finalizeResultSorting(IndexRange range)
agx::UInt getNumOverflows() const
Definition: GlobalResult.h:387
BufferSet(size_t index, size_t capacity)
GlobalResult(UInt settings, const Path &bindPath)
size_t commit(const agxData::AbstractArray &target, size_t numElements, const void *localResult)
size_t getIndex() const
std::atomic< agx::Int32 > m_numTransactions
Definition: GlobalResult.h:208
void init(size_t index, size_t capacity)
Initialize a buffer set.
agx::Mutex m_mutex
Definition: GlobalResult.h:206
VectorPOD< IndexRange > m_sortedRanges
Definition: GlobalResult.h:212
std::atomic< agx::Int32 > m_head
Definition: GlobalResult.h:205
void reportCompletedTransaction(Transaction *transaction)
void reportFailedTransaction(const IndexRange &transactionRange, BufferSet *startSet)
agx::Mutex & getMutex()
Definition: GlobalResult.h:391
IndexRangeVector & getFailedTransactions()
virtual ~GlobalResult()
void prepareResultSorting(size_t numJobs)
void addFailedTransaction(const IndexRange &transactionRange)
Report a failed transaction.
BufferSet * reportFailedTransaction(Transaction *transaction)
agx::Object is a refcounted object with a name.
Definition: Object.h:59
Representation of a path, a list of name components.
Definition: Path.h:33
Base class providing referencing counted objects.
Definition: Referenced.h:120
A representation of a generic task.
Definition: Task.h:57
Vector containing 'raw' data.
Definition: agx/Vector.h:246
Templated vector class.
Definition: agx/Vector.h:53
#define agxAssertN(expr, format,...)
Definition: debug.h:145
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
Contains classes for low level data storage for AGX.
Definition: Container.h:23
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
std::mutex Mutex
Definition: Referenced.h:99
uint64_t UInt
Definition: Integer.h:27
double Real
Definition: Real.h:41
UInt32 Index
Definition: Integer.h:44
OverflowBuffer(agxData::Buffer *buffer_)