VR-Link API Documentation for HLA 1516
 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) 2014-2015 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 #include <vl/executionContainer.h>
19 #include <vl/entityPublisher.h>
20 
21 #ifndef DtVRL_TBB_UNSUPPORTED
22 #include <tbb/tbb.h>
23 #include <tbb/task_arena.h>
24 #else
25 #pragma message("Warning, DtParallelExecutionContainer parallel execution not supported on VC7 or earlier due to missing TBB dependency.")
26 #endif
27 
28 #include <vector>
29 #include <algorithm>
30 
35 
38 
41 
44 
47 
50 public:
53 
56 
57 private:
61 };
62 
68 
69 template <class Texec>
71 {
72 public:
73 
74  typedef typename std::vector<Texec *> PecContainerType;
75  typedef typename PecContainerType::iterator PecIteratorType;
76 
77 private:
82  template <typename T_ReturnValue>
84  {
85  private:
88 
90  T_ReturnValue(Texec::*myMethodPtr)();
91 
92  public:
93 
94 #ifndef DtVRL_TBB_UNSUPPORTED
95  void operator()( const tbb::blocked_range<size_t>& r ) const
97  {
98  for( size_t i=r.begin(); i!=r.end(); ++i )
99  {
100  ((*myCV[i]).*myMethodPtr)();
101  }
102  }
103 #endif
104  ExecutableItem(const PecContainerType& theCV, T_ReturnValue (Texec::*theMethodPtr)())
106  : myCV(theCV)
107  , myMethodPtr(theMethodPtr)
108  {
109  }
110  };
111 
112 public:
113 
115  DtParallelExecutionContainer(bool parallel = true) : useParallel(parallel), myNumThreads(-1), myExecVec() {}
117 
119  virtual void add(Texec *item) { myExecVec.push_back(item); }
120 
122  virtual void remove(Texec *item) { myExecVec.erase(std::remove(begin(), end(), item), end()); }
123 
125  virtual void clear() { myExecVec.clear(); }
126 
128  virtual bool empty() { return myExecVec.empty(); }
129 
131  virtual unsigned int count() { return myExecVec.size(); }
132 
134  virtual void deleteAndClear();
135 
137  virtual PecIteratorType begin() { return myExecVec.begin(); }
138  virtual PecIteratorType end() { return myExecVec.end(); }
139 
141 
143  virtual void executeMethod(void (Texec::*theMethodPtr)());
144  virtual void executeMethod(bool (Texec::*theMethodPtr)());
145 
147  virtual void setParallel(bool parallel) { useParallel = parallel; }
148  virtual bool isParallel() const { return useParallel; }
149 
151  virtual void setMaxNumberThreads( int numThreads ) { myNumThreads = numThreads; };
152  virtual int maxNumberThreads() const { return myNumThreads; };
153 
154 protected:
156  virtual void executeMethodParallel(void (Texec::*theMethodPtr)());
157  virtual void executeMethodParallel(bool (Texec::*theMethodPtr)());
158 
160  virtual void executeMethodSerial(void (Texec::*theMethodPtr)());
161  virtual void executeMethodSerial(bool (Texec::*theMethodPtr)());
162 
165 
168 
171 
172 private:
175 
178 };
179 
180 // Delete all referenced objects and then clear the list.
181 template<class Texec>
183 {
184  for (PecIteratorType item = begin(); item != end(); ++item)
185  {
186  delete (*item);
187  }
188  clear();
189 }
190 
191 // Execute method on all contained objects
192 template<class Texec>
193 inline void DtParallelExecutionContainer<Texec>::executeMethod(void (Texec::*theMethodPtr)())
194 {
195  if (useParallel)
196  {
197  executeMethodParallel(theMethodPtr);
198  }
199  else
200  {
201  executeMethodSerial(theMethodPtr);
202  }
203 }
204 
205 template<class Texec>
206 inline void DtParallelExecutionContainer<Texec>::executeMethod(bool (Texec::*theMethodPtr)())
207 {
208  if (useParallel)
209  {
210  executeMethodParallel(theMethodPtr);
211  }
212  else
213  {
214  executeMethodSerial(theMethodPtr);
215  }
216 }
217 
218 // Execute method on all contained objects in parallel
219 template<class Texec>
220 inline void DtParallelExecutionContainer<Texec>::executeMethodParallel(void (Texec::*theMethodPtr)())
221 {
222 #ifndef DtVRL_TBB_UNSUPPORTED
223  size_t size = myExecVec.size();
224  tbb::blocked_range<size_t> range(0, size);
225  ExecutableItem<void> execST(myExecVec, theMethodPtr);
226 #ifndef DtVRL_TBB_ARENA_UNSUPPORTED
227  tbb::task_arena limited_arena(myNumThreads);
228  beginParallel();
229  limited_arena.execute([&] { tbb::parallel_for(range, execST); });
230  endParallel();
231 #else
232  beginParallel();
233  tbb::parallel_for(range, execST);
234  endParallel();
235 #endif
236 #else
237  executeMethodSerial(theMethodPtr);
238 #endif
239 }
240 
241 // Execute method on all contained objects in parallel, only for DtObjectPublisher Tick
242 template<class Texec>
243 inline void DtParallelExecutionContainer<Texec>::executeMethodParallel(bool (Texec::*theMethodPtr)())
244 {
245 #ifndef DtVRL_TBB_UNSUPPORTED
246  size_t size = myExecVec.size();
247  tbb::blocked_range<size_t> range(0, size);
248  ExecutableItem<bool> execST(myExecVec, theMethodPtr);
249  tbb::task_arena limited_arena(myNumThreads);
250  beginParallel();
251  limited_arena.execute([&] { tbb::parallel_for(range, execST); });
252  endParallel();
253 #else
254  executeMethodSerial(theMethodPtr);
255 #endif
256 }
257 
258 // Execute method on all contained objects serially
259 template<class Texec>
260 inline void DtParallelExecutionContainer<Texec>::executeMethodSerial(void (Texec::*theMethodPtr)())
261 {
262  for (PecIteratorType item = begin(); item != end(); ++item)
263  {
264  ((**item).*theMethodPtr)();
265  }
266 }
267 
268 // Execute method on all contained objects serially, only for DtObjectPublisher tick
269 template<class Texec>
270 inline void DtParallelExecutionContainer<Texec>::executeMethodSerial(bool (Texec::*theMethodPtr)())
271 {
272  for (PecIteratorType item = begin(); item != end(); ++item)
273  {
274  ((**item).*theMethodPtr)();
275  }
276 }
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:96
void DtLeaveParallelExecutionSection()
End publishing in parallel.
virtual void endParallel()
executed after leaving a parallel section
Definition: parallelExecutionContainer.h:167
void DtEnterParallelExecutionSection()
Begin publishing in parallel.
virtual bool isParallel() const
Definition: parallelExecutionContainer.h:148
~DtScopedParallelExecutionLock()
Destructor, which will unlock if locked.
Definition: parallelExecutionContainer.h:55
Contains the DtExecutionContainer class declarations.
virtual bool empty()
Returns true if the container is empty.
Definition: parallelExecutionContainer.h:128
T_ReturnValue(Texec::* myMethodPtr)()
Member method to call in parallel.
Definition: parallelExecutionContainer.h:90
ExecutableItem(const PecContainerType &theCV, T_ReturnValue(Texec::*theMethodPtr)())
ctor with vector of objects and method to call
Definition: parallelExecutionContainer.h:105
bool useParallel
true if using parallel execution, false for serial
Definition: parallelExecutionContainer.h:174
#define DT_DLL_VL
Definition: vlMachineTypes.h:108
const PecContainerType & myCV
Publishers worked upon.
Definition: parallelExecutionContainer.h:87
virtual void executeMethodParallel(void(Texec::*theMethodPtr)())
Execute method on all contained objects in parallel using multiple threads.
Definition: parallelExecutionContainer.h:220
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:131
virtual void deleteAndClear()
Delete all referenced objects and then clear the list.
Definition: parallelExecutionContainer.h:182
std::vector< Texec * > PecContainerType
Definition: parallelExecutionContainer.h:74
virtual PecIteratorType begin()
Iterators over the collection.
Definition: parallelExecutionContainer.h:137
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:52
DtParallelExecutionContainer(bool parallel=true)
Default to parallel execution.
Definition: parallelExecutionContainer.h:115
Convenience class for scoped locking.
Definition: parallelExecutionContainer.h:49
virtual void clear()
Clear the list.
Definition: parallelExecutionContainer.h:125
PecContainerType::iterator PecIteratorType
Definition: parallelExecutionContainer.h:75
virtual PecIteratorType end()
Definition: parallelExecutionContainer.h:138
virtual int maxNumberThreads() const
Definition: parallelExecutionContainer.h:152
int myNumThreads
default is -1 for maximum concurrency
Definition: parallelExecutionContainer.h:177
PecContainerType myExecVec
Contains pointers to published entities.
Definition: parallelExecutionContainer.h:170
virtual void add(Texec *item)
Add a reference to the list.
Definition: parallelExecutionContainer.h:119
virtual void executeMethod(void(Texec::*theMethodPtr)())
worker methods
Definition: parallelExecutionContainer.h:193
void DtLockParallelExecutionAccess()
Lock a resource only if parallel section is active.
virtual void beginParallel()
executed before entering a parallel section
Definition: parallelExecutionContainer.h:164
This class is used by TBB to manage the work units to perform.
Definition: parallelExecutionContainer.h:83
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:147
DtParallelExecutionContainer is implementation of DtExecutionContainer which executes methods on all ...
Definition: parallelExecutionContainer.h:70
virtual ~DtParallelExecutionContainer()
Definition: parallelExecutionContainer.h:116
virtual void executeMethodSerial(void(Texec::*theMethodPtr)())
For test and debug, a convenience to execute methods serially.
Definition: parallelExecutionContainer.h:260
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:151
This file provides protocol independence for DtEntityPublisher.

Document ID: Generated on Wed Mar 27 02:04:30 EDT 2024 from SVN revision 264570
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)