AGX Dynamics  2.37.2.0
GlobalResult.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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 
334  AGX_FORCE_INLINE const IndexRange& GlobalResult::Transaction::range() const { return m_range; }
335  AGX_FORCE_INLINE GlobalResult::BufferSet *GlobalResult::Transaction::getBufferSet() { return m_bufferSet; }
336  AGX_FORCE_INLINE GlobalResult *GlobalResult::Transaction::getTarget() { return m_target; }
337 
338 
339  AGX_FORCE_INLINE bool GlobalResult::Transaction::commit(Index sortIndex)
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 
361  AGX_FORCE_INLINE bool GlobalResult::Transaction::commit()
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 
385  AGX_FORCE_INLINE GlobalResult::BufferSet *GlobalResult::getActiveSet() { return m_activeSet; }
386  AGX_FORCE_INLINE bool GlobalResult::didOverflow() const { return m_numOverflows > 0; }
387  AGX_FORCE_INLINE agx::UInt GlobalResult::getNumOverflows() const { return m_numOverflows; }
388 
389  //---------------------------------------------------------------
390 
391  AGX_FORCE_INLINE agx::Mutex& GlobalResult::getMutex()
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 */
Representation of a path, a list of name components.
Definition: Path.h:32
size_t size() const
Definition: Buffer.h:708
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
Vector< BufferSetRef > m_bufferSets
Definition: GlobalResult.h:199
std::atomic< agx::Int32 > m_numTransactions
Definition: GlobalResult.h:208
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:72
agx::Mutex & getMutex()
Definition: GlobalResult.h:391
agx::UInt getNumOverflows() const
Definition: GlobalResult.h:387
agx::Mutex m_mutex
Definition: GlobalResult.h:206
#define agxAssertN(expr, format,...)
Definition: debug.h:145
BufferSet(size_t index, size_t capacity)
Transactions are used to reserve exclusive memory areas in which the threads can write their data...
Definition: GlobalResult.h:72
virtual ~BufferSet()
Definition: GlobalResult.h:251
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
A representation of a generic task.
Definition: Task.h:57
#define AGX_FORCE_INLINE
Definition: Object.h:59
Arrays are fast accessors into the data, or a portion of it, held by a Buffer.
Definition: Array.h:36
VectorPOD< IndexRange > m_sortedRanges
Definition: GlobalResult.h:212
BufferSet * getActiveSet()
Definition: GlobalResult.h:385
Base class providing referencing counted objects.
Definition: Referenced.h:119
agxData::Buffer * m_resultOrderBuffer
Definition: GlobalResult.h:213
void prepareResultSorting(size_t numJobs)
Templated vector class.
Definition: Vector.h:52
uint32_t UInt
Definition: Integer.h:34
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
GlobalResult is used by kernels to generate result data from different threads.
Definition: GlobalResult.h:51
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
VectorPOD< Transaction > m_transactions
Definition: GlobalResult.h:207
Transaction allocateResult(size_t numElements)
Allocate a transaction.
std::atomic< agx::Int32 > m_head
Definition: GlobalResult.h:205
std::atomic< agx::Int32 > m_numCompletedTransactions
Definition: GlobalResult.h:209
Abstract representation of a data buffer.
Definition: Buffer.h:55
Contains classes for low level data storage for AGX.
Definition: Container.h:23
UInt32 Index
Definition: Integer.h:52
#define agxAssert(expr)
Definition: debug.h:143
Entity parameter.
Definition: Parameter.h:482
GlobalResult(UInt settings, const Path &bindPath)
std::mutex Mutex
Definition: Referenced.h:99
BufferSetRef m_activeSet
Definition: GlobalResult.h:198
bool didOverflow() const
Definition: GlobalResult.h:386
#define m_capacity
Definition: Vector.h:430
std::atomic< size_t > m_capacity
Definition: GlobalResult.h:201
double Real
Definition: Real.h:42
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:
#define AGXCORE_EXPORT
agx::Object is a refcounted object with a name.
Definition: Object.h:58