VR-Link API Documentation for HLA Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parallelExecutionContainer.h
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (c) 1992-2025 MAK Technologies, Inc
3  * All rights reserved.
4  ****************************************************************************/
5 
9 
10 #pragma once
11 
12 #if defined(_WIN32) && _MSC_VER < 1400
13 #define DtVRL_TBB_UNSUPPORTED 1
14 #elif __rhentws7__
15 #define DtVRL_TBB_ARENA_UNSUPPORTED 1
16 #endif
17 
18 #define NOMINMAX
19 #include <vl/executionContainer.h>
20 #include <vl/entityPublisher.h>
21 
22 #ifndef DtVRL_TBB_UNSUPPORTED
23 #include <oneapi/tbb.h>
24 #include <tbb/task_arena.h>
25 #else
26 #pragma message("Warning, DtParallelExecutionContainer parallel execution not supported on VC7 or earlier due to missing TBB dependency.")
27 #endif
28 
29 #include <vector>
30 #include <algorithm>
31 
36 
39 
42 
45 
48 
51 public:
54 
57 
58 private:
62 };
63 
69 
70 template <class Texec>
72 {
73 public:
74 
75  typedef typename std::vector<Texec *> PecContainerType;
76  typedef typename PecContainerType::iterator PecIteratorType;
77 
78 private:
83  template <typename T_ReturnValue>
85  {
86  private:
89 
91  T_ReturnValue(Texec::*myMethodPtr)();
92 
93  public:
94 
95 #ifndef DtVRL_TBB_UNSUPPORTED
96  void operator()( const tbb::blocked_range<size_t>& r ) const
98  {
99  for( size_t i=r.begin(); i!=r.end(); ++i )
100  {
101  ((*myCV[i]).*myMethodPtr)();
102  }
103  }
104 #endif
105  ExecutableItem(const PecContainerType& theCV, T_ReturnValue (Texec::*theMethodPtr)())
107  : myCV(theCV)
108  , myMethodPtr(theMethodPtr)
109  {
110  }
111  };
112 
113 public:
114 
116  DtParallelExecutionContainer(bool parallel = true) : useParallel(parallel), myNumThreads(-1), myExecVec() {}
118 
120  virtual void add(Texec *item) { myExecVec.push_back(item); }
121 
123  virtual void remove(Texec *item) { myExecVec.erase(std::remove(begin(), end(), item), end()); }
124 
126  virtual void clear() { myExecVec.clear(); }
127 
129  virtual bool empty() { return myExecVec.empty(); }
130 
132  virtual unsigned int count() { return myExecVec.size(); }
133 
135  virtual void deleteAndClear();
136 
138  virtual PecIteratorType begin() { return myExecVec.begin(); }
139  virtual PecIteratorType end() { return myExecVec.end(); }
140 
142 
144  virtual void executeMethod(void (Texec::*theMethodPtr)());
145  virtual void executeMethod(bool (Texec::*theMethodPtr)());
146 
148  virtual void setParallel(bool parallel) { useParallel = parallel; }
149  virtual bool isParallel() const { return useParallel; }
150 
152  virtual void setMaxNumberThreads( int numThreads ) { myNumThreads = numThreads; };
153  virtual int maxNumberThreads() const { return myNumThreads; };
154 
155 protected:
157  virtual void executeMethodParallel(void (Texec::*theMethodPtr)());
158  virtual void executeMethodParallel(bool (Texec::*theMethodPtr)());
159 
161  virtual void executeMethodSerial(void (Texec::*theMethodPtr)());
162  virtual void executeMethodSerial(bool (Texec::*theMethodPtr)());
163 
166 
169 
172 
173 private:
176 
179 };
180 
181 // Delete all referenced objects and then clear the list.
182 template<class Texec>
184 {
185  for (PecIteratorType item = begin(); item != end(); ++item)
186  {
187  delete (*item);
188  }
189  clear();
190 }
191 
192 // Execute method on all contained objects
193 template<class Texec>
194 inline void DtParallelExecutionContainer<Texec>::executeMethod(void (Texec::*theMethodPtr)())
195 {
196  if (useParallel)
197  {
198  executeMethodParallel(theMethodPtr);
199  }
200  else
201  {
202  executeMethodSerial(theMethodPtr);
203  }
204 }
205 
206 template<class Texec>
207 inline void DtParallelExecutionContainer<Texec>::executeMethod(bool (Texec::*theMethodPtr)())
208 {
209  if (useParallel)
210  {
211  executeMethodParallel(theMethodPtr);
212  }
213  else
214  {
215  executeMethodSerial(theMethodPtr);
216  }
217 }
218 
219 // Execute method on all contained objects in parallel
220 template<class Texec>
221 inline void DtParallelExecutionContainer<Texec>::executeMethodParallel(void (Texec::*theMethodPtr)())
222 {
223 #ifndef DtVRL_TBB_UNSUPPORTED
224  size_t size = myExecVec.size();
225  tbb::blocked_range<size_t> range(0, size);
226  ExecutableItem<void> execST(myExecVec, theMethodPtr);
227 #ifndef DtVRL_TBB_ARENA_UNSUPPORTED
228  tbb::task_arena limited_arena(myNumThreads);
229  beginParallel();
230  limited_arena.execute([&] { tbb::parallel_for(range, execST); });
231  endParallel();
232 #else
233  beginParallel();
234  tbb::parallel_for(range, execST);
235  endParallel();
236 #endif
237 #else
238  executeMethodSerial(theMethodPtr);
239 #endif
240 }
241 
242 // Execute method on all contained objects in parallel, only for DtObjectPublisher Tick
243 template<class Texec>
244 inline void DtParallelExecutionContainer<Texec>::executeMethodParallel(bool (Texec::*theMethodPtr)())
245 {
246 #ifndef DtVRL_TBB_UNSUPPORTED
247  size_t size = myExecVec.size();
248  tbb::blocked_range<size_t> range(0, size);
249  ExecutableItem<bool> execST(myExecVec, theMethodPtr);
250  tbb::task_arena limited_arena(myNumThreads);
251  beginParallel();
252  limited_arena.execute([&] { tbb::parallel_for(range, execST); });
253  endParallel();
254 #else
255  executeMethodSerial(theMethodPtr);
256 #endif
257 }
258 
259 // Execute method on all contained objects serially
260 template<class Texec>
261 inline void DtParallelExecutionContainer<Texec>::executeMethodSerial(void (Texec::*theMethodPtr)())
262 {
263  for (PecIteratorType item = begin(); item != end(); ++item)
264  {
265  ((**item).*theMethodPtr)();
266  }
267 }
268 
269 // Execute method on all contained objects serially, only for DtObjectPublisher tick
270 template<class Texec>
271 inline void DtParallelExecutionContainer<Texec>::executeMethodSerial(bool (Texec::*theMethodPtr)())
272 {
273  for (PecIteratorType item = begin(); item != end(); ++item)
274  {
275  ((**item).*theMethodPtr)();
276  }
277 }
void operator()(const tbb::blocked_range< size_t > &r) const
Called by TBB to execute a method on a block of objects.
Definition: parallelExecutionContainer.h:97
void DtLeaveParallelExecutionSection()
End publishing in parallel.
virtual void endParallel()
executed after leaving a parallel section
Definition: parallelExecutionContainer.h:168
void DtEnterParallelExecutionSection()
Begin publishing in parallel.
virtual bool isParallel() const
Definition: parallelExecutionContainer.h:149
~DtScopedParallelExecutionLock()
Destructor, which will unlock if locked.
Definition: parallelExecutionContainer.h:56
Contains the DtExecutionContainer class declarations.
virtual bool empty()
Returns true if the container is empty.
Definition: parallelExecutionContainer.h:129
T_ReturnValue(Texec::* myMethodPtr)()
Member method to call in parallel.
Definition: parallelExecutionContainer.h:91
ExecutableItem(const PecContainerType &theCV, T_ReturnValue(Texec::*theMethodPtr)())
ctor with vector of objects and method to call
Definition: parallelExecutionContainer.h:106
bool useParallel
true if using parallel execution, false for serial
Definition: parallelExecutionContainer.h:175
#define DT_DLL_VL
Definition: vlMachineTypes.h:108
const PecContainerType & myCV
Publishers worked upon.
Definition: parallelExecutionContainer.h:88
virtual void executeMethodParallel(void(Texec::*theMethodPtr)())
Execute method on all contained objects in parallel using multiple threads.
Definition: parallelExecutionContainer.h:221
void DtUnlockParallelExecutionAccess()
Unlock a resource only if parallel section is active.
virtual unsigned int count()
Returns the number of items in the container.
Definition: parallelExecutionContainer.h:132
virtual void deleteAndClear()
Delete all referenced objects and then clear the list.
Definition: parallelExecutionContainer.h:183
std::vector< Texec * > PecContainerType
Definition: parallelExecutionContainer.h:75
virtual PecIteratorType begin()
Iterators over the collection.
Definition: parallelExecutionContainer.h:138
bool DtEnableParallelExecutionAccess(bool enable)
Enable locking a resource only if parallel section is active (default).
DtScopedParallelExecutionLock()
Constructs a DtScopedParallelExecutionLock and locks it.
Definition: parallelExecutionContainer.h:53
DtParallelExecutionContainer(bool parallel=true)
Default to parallel execution.
Definition: parallelExecutionContainer.h:116
Convenience class for scoped locking.
Definition: parallelExecutionContainer.h:50
virtual void clear()
Clear the list.
Definition: parallelExecutionContainer.h:126
PecContainerType::iterator PecIteratorType
Definition: parallelExecutionContainer.h:76
virtual PecIteratorType end()
Definition: parallelExecutionContainer.h:139
virtual int maxNumberThreads() const
Definition: parallelExecutionContainer.h:153
int myNumThreads
default is -1 for maximum concurrency
Definition: parallelExecutionContainer.h:178
PecContainerType myExecVec
Contains pointers to published entities.
Definition: parallelExecutionContainer.h:171
virtual void add(Texec *item)
Add a reference to the list.
Definition: parallelExecutionContainer.h:120
virtual void executeMethod(void(Texec::*theMethodPtr)())
worker methods
Definition: parallelExecutionContainer.h:194
void DtLockParallelExecutionAccess()
Lock a resource only if parallel section is active.
virtual void beginParallel()
executed before entering a parallel section
Definition: parallelExecutionContainer.h:165
This class is used by TBB to manage the work units to perform.
Definition: parallelExecutionContainer.h:84
DtExecutionContainer is only the definition of a container which executes methods on all contained ob...
Definition: executionContainer.h:19
virtual void setParallel(bool parallel)
The &#39;parallel&#39; property determines if the methods are executed in parallel or serial.
Definition: parallelExecutionContainer.h:148
DtParallelExecutionContainer is implementation of DtExecutionContainer which executes methods on all ...
Definition: parallelExecutionContainer.h:71
virtual ~DtParallelExecutionContainer()
Definition: parallelExecutionContainer.h:117
virtual void executeMethodSerial(void(Texec::*theMethodPtr)())
For test and debug, a convenience to execute methods serially.
Definition: parallelExecutionContainer.h:261
virtual void setMaxNumberThreads(int numThreads)
Set maximum number of threads created during executeMothod calls. Default is -1, which uses as many t...
Definition: parallelExecutionContainer.h:152
This file provides protocol independence for DtEntityPublisher.

Document ID: Generated on Thu Oct 16 00:12:25 EDT 2025 from SVN revision 280738
Copyright © 1992-2025 MAK Technologies. All Rights Reserved (www.mak.com)