AGXDynamics  2.35.0.3
GlobalResult.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx 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 
32 namespace agxData
33 {
34  class Buffer;
35  class EntityStorage;
36 }
37 
38 namespace agx
39 {
40  class Task;
41  class Parameter;
42  class ArrayParameter;
43  class EntityDataParameter;
44 
52  {
53  public:
55 
56  enum Settings
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 
162 
163  void prepareResultSorting(size_t numJobs);
164 
165  protected:
167  {
168  OverflowBuffer();
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 
186  void finalizeTransactions();
187  void finalizeResultSorting(IndexRange range);
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);
194  void reportCompletedTransaction(Transaction *transaction);
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 
242  VectorPOD<OverflowBuffer>& getOverflows();
243 
245  IndexRangeVector& getFailedTransactions();
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 
267  GlobalResultStorage(agxData::EntityStorage *targetStorage, UInt settings);
268 
269  void prepare(Task *task);
270  void finalize();
271 
272  // agxData::EntityStorage *getStorage();
273 
274  protected:
275  virtual ~GlobalResultStorage();
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 
283  friend class EntityDataParameter;
284  void locateTargets(EntityDataParameter *parameter);
285 
286  template <typename ParameterVectorT>
287  void locateTargets(const ParameterVectorT& parameters);
288 
289  private:
290  agxData::EntityStorage *m_targetStorage;
291  VectorPOD<ArrayParameter *> m_targets;
292  VectorPOD<agxData::Buffer *> m_implicitBuffers;
293  bool m_targetInstanceBuffer;
294  };
295 
296  //---------------------------------------------------------------
297 
300  {
301  public:
302  GlobalResultBuffer(agxData::Buffer *target, UInt settings);
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 
313  virtual ~GlobalResultBuffer();
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 */
agx::GlobalResult::Transaction::commit
bool commit()
Same as above but without deterministic sorting.
Definition: GlobalResult.h:361
AGXCORE_EXPORT
#define AGXCORE_EXPORT
agx::GlobalResult::SORT
@ SORT
Build a permutation table for deterministic ordering of result data.
Definition: GlobalResult.h:64
agx::Real
double Real
Definition: Real.h:42
AttributeContainer.h
agx::GlobalResult::m_initialSize
size_t m_initialSize
Definition: GlobalResult.h:202
agxData::AbstractArray
Arrays are fast accessors into the data, or a portion of it, held by a Buffer.
Definition: Array.h:36
agx::Vector
Templated vector class.
Definition: Vector.h:52
agx::EntityDataParameter
Entity parameter.
Definition: Parameter.h:482
agx::Path
Representation of a path, a list of name components.
Definition: Path.h:33
agx::Container::size
size_t size() const
Definition: Container.h:134
agx::GlobalResult::getActiveSet
BufferSet * getActiveSet()
Definition: GlobalResult.h:385
agx::GlobalResult::commit
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:
agx::GlobalResult::m_numTransactions
std::atomic< agx::Int32 > m_numTransactions
Definition: GlobalResult.h:208
agx::GlobalResult::getMutex
agx::Mutex & getMutex()
Definition: GlobalResult.h:391
agxAssert
#define agxAssert(expr)
Definition: debug.h:143
agx::GlobalResult::m_numCompletedTransactions
std::atomic< agx::Int32 > m_numCompletedTransactions
Definition: GlobalResult.h:209
m_capacity
#define m_capacity
Definition: Vector.h:430
agx::GlobalResult::BufferSet
BufferSet(size_t index, size_t capacity)
agxData::EntityStorage
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:72
agx::GlobalResult::m_bufferSets
Vector< BufferSetRef > m_bufferSets
Definition: GlobalResult.h:199
agx::VectorPOD
Vector containing 'raw' data.
Definition: Vector.h:245
agx::GlobalResultBuffer
Definition: GlobalResult.h:299
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agxData
Contains classes for low level data storage for AGX.
Definition: Container.h:23
agxData::Array< agx::Index >
agx::GlobalResult::m_numOverflows
UInt m_numOverflows
Definition: GlobalResult.h:210
agx::IndexRangeT< agx::UInt >
agx::GlobalResult::m_head
std::atomic< agx::Int32 > m_head
Definition: GlobalResult.h:205
agx::Task
A representation of a generic task.
Definition: Task.h:57
agx::GlobalResult::m_transactions
VectorPOD< Transaction > m_transactions
Definition: GlobalResult.h:207
agx::GlobalResult::m_activeSet
BufferSetRef m_activeSet
Definition: GlobalResult.h:198
agx::GlobalResult::Transaction::range
const IndexRange & range() const
Definition: GlobalResult.h:334
agx::GlobalResult::getNumOverflows
agx::UInt getNumOverflows() const
Definition: GlobalResult.h:387
agx::ref_ptr< BufferSet >
agx::GlobalResultStorage
Definition: GlobalResult.h:263
agx::GlobalResult::m_smoothed
Real m_smoothed
Definition: GlobalResult.h:203
agx::Index
UInt32 Index
Definition: Integer.h:52
agx::GlobalResult::OverflowBuffer
Definition: GlobalResult.h:166
agx::Mutex
std::mutex Mutex
Definition: Referenced.h:93
agxData::Buffer
Abstract representation of a data buffer.
Definition: Buffer.h:55
agx::GlobalResult::m_sortedRanges
VectorPOD< IndexRange > m_sortedRanges
Definition: GlobalResult.h:212
agxCore_export.h
Object.h
agx::Object
agx::Object is a refcounted object with a name.
Definition: Object.h:59
agx::GlobalResult::ORDER
@ ORDER
Append to the existing data.
Definition: GlobalResult.h:63
agx::GlobalResult::Settings
Settings
Definition: GlobalResult.h:56
agx.h
agx::GlobalResult::m_mutex
agx::Mutex m_mutex
Definition: GlobalResult.h:206
agx::GlobalResult::GlobalResult
GlobalResult(UInt settings, const Path &bindPath)
agx::GlobalResult::Transaction
Transactions are used to reserve exclusive memory areas in which the threads can write their data.
Definition: GlobalResult.h:72
agx::Referenced
Base class providing referencing counted objects.
Definition: Referenced.h:113
AGX_DECLARE_POINTER_TYPES
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
agx::GlobalResult::m_settings
UInt m_settings
Definition: GlobalResult.h:197
agx::GlobalResultBuffer::isActive
bool isActive() const
Definition: GlobalResult.h:408
agx::UInt
uint32_t UInt
Definition: Integer.h:34
agx::GlobalResult::prepareResultSorting
void prepareResultSorting(size_t numJobs)
agx::GlobalResult::Transaction::getTarget
GlobalResult * getTarget()
Definition: GlobalResult.h:336
agx::GlobalResult::m_resultOrderBuffer
agxData::Buffer * m_resultOrderBuffer
Definition: GlobalResult.h:213
agxAssertN
#define agxAssertN(expr, format,...)
Definition: debug.h:145
agx::init
void AGXPHYSICS_EXPORT init()
Initialize agx.
agx::GlobalResult::OverflowBuffer::buffer
agxData::Buffer * buffer
Definition: GlobalResult.h:171
agx::GlobalResult::allocateResult
Transaction allocateResult(size_t numElements)
Allocate a transaction.
agx::GlobalResult::m_capacity
std::atomic< size_t > m_capacity
Definition: GlobalResult.h:201
agx::GlobalResult::~BufferSet
virtual ~BufferSet()
Definition: GlobalResult.h:251
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58
agxData::AbstractArray::size
size_t size() const
Definition: Buffer.h:708
agx::GlobalResult::OverflowBuffer::ptr
void * ptr
Definition: GlobalResult.h:172
agx::GlobalResult::didOverflow
bool didOverflow() const
Definition: GlobalResult.h:386
agx::GlobalResult::OverflowBuffer::size
size_t size
Definition: GlobalResult.h:173
agx::GlobalResult
GlobalResult is used by kernels to generate result data from different threads.
Definition: GlobalResult.h:51
agx::GlobalResult::Transaction::getBufferSet
BufferSet * getBufferSet()
Definition: GlobalResult.h:335