VR-Link API Documentation for DIS
 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 VT MAK
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:
83  {
84  private:
87 
89  void (Texec::*myMethodPtr)();
90 
91  public:
92 
93 #ifndef DtVRL_TBB_UNSUPPORTED
94  void operator()( const tbb::blocked_range<size_t>& r ) const
96  {
97  for( size_t i=r.begin(); i!=r.end(); ++i )
98  {
99  ((*myCV[i]).*myMethodPtr)();
100  }
101  }
102 #endif
103  ExecutableItem(const PecContainerType& theCV, void (Texec::*theMethodPtr)())
105  : myCV(theCV)
106  , myMethodPtr(theMethodPtr)
107  {
108  }
109  };
110 
111 public:
112 
114  DtParallelExecutionContainer(bool parallel = true) : useParallel(parallel), myNumThreads(-1), myExecVec() {}
116 
118  virtual void add(Texec *item) { myExecVec.push_back(item); }
119 
121  virtual void remove(Texec *item) { myExecVec.erase(std::remove(begin(), end(), item), end()); }
122 
124  virtual void clear() { myExecVec.clear(); }
125 
127  virtual bool empty() { return myExecVec.empty(); }
128 
130  virtual unsigned int count() { return myExecVec.size(); }
131 
133  virtual void deleteAndClear();
134 
136  virtual PecIteratorType begin() { return myExecVec.begin(); }
137  virtual PecIteratorType end() { return myExecVec.end(); }
138 
140 
142  virtual void executeMethod(void (Texec::*theMethodPtr)());
143 
145  virtual void setParallel(bool parallel) { useParallel = parallel; }
146  virtual bool isParallel() const { return useParallel; }
147 
149  virtual void setMaxNumberThreads( int numThreads ) { myNumThreads = numThreads; };
150  virtual int maxNumberThreads() const { return myNumThreads; };
151 
152 protected:
154  virtual void executeMethodParallel(void (Texec::*theMethodPtr)());
155 
157  virtual void executeMethodSerial(void (Texec::*theMethodPtr)());
158 
161 
164 
167 
168 private:
171 
174 };
175 
176 // Delete all referenced objects and then clear the list.
177 template<class Texec>
179 {
180  for (PecIteratorType item = begin(); item != end(); ++item)
181  {
182  delete (*item);
183  }
184  clear();
185 }
186 
187 // Execute method on all contained objects
188 template<class Texec>
189 inline void DtParallelExecutionContainer<Texec>::executeMethod(void (Texec::*theMethodPtr)())
190 {
191  if (useParallel)
192  {
193  executeMethodParallel(theMethodPtr);
194  }
195  else
196  {
197  executeMethodSerial(theMethodPtr);
198  }
199 }
200 
201 // Execute method on all contained objects in parallel
202 template<class Texec>
203 inline void DtParallelExecutionContainer<Texec>::executeMethodParallel(void (Texec::*theMethodPtr)())
204 {
205 #ifndef DtVRL_TBB_UNSUPPORTED
206  size_t size = myExecVec.size();
207  tbb::blocked_range<size_t> range(0, size);
208  ExecutableItem execST(myExecVec, theMethodPtr);
209 #ifndef DtVRL_TBB_ARENA_UNSUPPORTED
210  tbb::task_arena limited_arena(myNumThreads);
211  beginParallel();
212  limited_arena.execute([&] { tbb::parallel_for(range, execST); });
213  endParallel();
214 #else
215  beginParallel();
216  tbb::parallel_for(range, execST);
217  endParallel();
218 #endif
219 #else
220  executeMethodSerial(theMethodPtr);
221 #endif
222 }
223 
224 // Execute method on all contained objects serially
225 template<class Texec>
226 inline void DtParallelExecutionContainer<Texec>::executeMethodSerial(void (Texec::*theMethodPtr)())
227 {
228  for (PecIteratorType item = begin(); item != end(); ++item)
229  {
230  ((**item).*theMethodPtr)();
231  }
232 }
void DtLeaveParallelExecutionSection()
End publishing in parallel.
virtual void endParallel()
executed after leaving a parallel section
Definition: parallelExecutionContainer.h:163
void DtEnterParallelExecutionSection()
Begin publishing in parallel.
virtual bool isParallel() const
Definition: parallelExecutionContainer.h:146
~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:127
bool useParallel
true if using parallel execution, false for serial
Definition: parallelExecutionContainer.h:170
#define DT_DLL_VL
Definition: vlMachineTypes.h:108
virtual void executeMethodParallel(void(Texec::*theMethodPtr)())
Execute method on all contained objects in parallel using multiple threads.
Definition: parallelExecutionContainer.h:203
void(Texec::* myMethodPtr)()
Member method to call in parallel.
Definition: parallelExecutionContainer.h:89
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:130
virtual void deleteAndClear()
Delete all referenced objects and then clear the list.
Definition: parallelExecutionContainer.h:178
ExecutableItem(const PecContainerType &theCV, void(Texec::*theMethodPtr)())
ctor with vector of objects and method to call
Definition: parallelExecutionContainer.h:104
std::vector< Texec * > PecContainerType
Definition: parallelExecutionContainer.h:74
virtual PecIteratorType begin()
Iterators over the collection.
Definition: parallelExecutionContainer.h:136
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:114
Convenience class for scoped locking.
Definition: parallelExecutionContainer.h:49
virtual void clear()
Clear the list.
Definition: parallelExecutionContainer.h:124
PecContainerType::iterator PecIteratorType
Definition: parallelExecutionContainer.h:75
virtual PecIteratorType end()
Definition: parallelExecutionContainer.h:137
virtual int maxNumberThreads() const
Definition: parallelExecutionContainer.h:150
const PecContainerType & myCV
Publishers worked upon.
Definition: parallelExecutionContainer.h:86
int myNumThreads
default is -1 for maximum concurrency
Definition: parallelExecutionContainer.h:173
PecContainerType myExecVec
Contains pointers to published entities.
Definition: parallelExecutionContainer.h:166
virtual void add(Texec *item)
Add a reference to the list.
Definition: parallelExecutionContainer.h:118
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:95
virtual void executeMethod(void(Texec::*theMethodPtr)())
worker methods
Definition: parallelExecutionContainer.h:189
void DtLockParallelExecutionAccess()
Lock a resource only if parallel section is active.
virtual void beginParallel()
executed before entering a parallel section
Definition: parallelExecutionContainer.h:160
This class is used by TBB to manage the work units to perform.
Definition: parallelExecutionContainer.h:82
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:145
DtParallelExecutionContainer is implementation of DtExecutionContainer which executes methods on all ...
Definition: parallelExecutionContainer.h:70
virtual ~DtParallelExecutionContainer()
Definition: parallelExecutionContainer.h:115
virtual void executeMethodSerial(void(Texec::*theMethodPtr)())
For test and debug, a convenience to execute methods serially.
Definition: parallelExecutionContainer.h:226
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:149
This file provides protocol independence for DtEntityPublisher.

Document ID: Generated on Mon Apr 25 03:39:01 EDT 2022 from SVN revision 242502
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)