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 #endif
15 
16 #include <vl/executionContainer.h>
17 #include <vl/entityPublisher.h>
18 
19 #ifndef DtVRL_TBB_UNSUPPORTED
20 #include <tbb/tbb.h>
21 #else
22 #pragma message("Warning, DtParallelExecutionContainer parallel execution not supported on VC7 or earlier due to missing TBB dependency.")
23 #endif
24 
25 #include <vector>
26 #include <algorithm>
27 
32 
35 
38 
41 
44 
47 public:
50 
53 
54 private:
58 };
59 
65 
66 template <class Texec>
68 {
69 public:
70 
71  typedef typename std::vector<Texec *> PecContainerType;
72  typedef typename PecContainerType::iterator PecIteratorType;
73 
74 private:
80  {
81  private:
84 
86  void (Texec::*myMethodPtr)();
87 
88  public:
89 
90 #ifndef DtVRL_TBB_UNSUPPORTED
91  void operator()( const tbb::blocked_range<size_t>& r ) const
93  {
94  for( size_t i=r.begin(); i!=r.end(); ++i )
95  {
96  ((*myCV[i]).*myMethodPtr)();
97  }
98  }
99 #endif
100  ExecutableItem(const PecContainerType& theCV, void (Texec::*theMethodPtr)())
102  : myCV(theCV)
103  , myMethodPtr(theMethodPtr)
104  {
105  }
106  };
107 
108 public:
109 
111  DtParallelExecutionContainer(bool parallel = true) : useParallel(parallel), myExecVec() {}
113 
115  virtual void add(Texec *item) { myExecVec.push_back(item); }
116 
118  virtual void remove(Texec *item) { myExecVec.erase(std::remove(begin(), end(), item), end()); }
119 
121  virtual void clear() { myExecVec.clear(); }
122 
124  virtual bool empty() { return myExecVec.empty(); }
125 
127  virtual unsigned int count() { return myExecVec.size(); }
128 
130  virtual void deleteAndClear();
131 
133  virtual PecIteratorType begin() { return myExecVec.begin(); }
134  virtual PecIteratorType end() { return myExecVec.end(); }
135 
137 
139  virtual void executeMethod(void (Texec::*theMethodPtr)());
140 
142  virtual void setParallel(bool parallel) { useParallel = parallel; }
143  virtual bool isParallel() const { return useParallel; }
144 
145 protected:
147  virtual void executeMethodParallel(void (Texec::*theMethodPtr)());
148 
150  virtual void executeMethodSerial(void (Texec::*theMethodPtr)());
151 
154 
157 
160 
161 private:
164 };
165 
166 // Delete all referenced objects and then clear the list.
167 template<class Texec>
169 {
170  for (PecIteratorType item = begin(); item != end(); ++item)
171  {
172  delete (*item);
173  }
174  clear();
175 }
176 
177 // Execute method on all contained objects
178 template<class Texec>
179 inline void DtParallelExecutionContainer<Texec>::executeMethod(void (Texec::*theMethodPtr)())
180 {
181  if (useParallel)
182  {
183  executeMethodParallel(theMethodPtr);
184  }
185  else
186  {
187  executeMethodSerial(theMethodPtr);
188  }
189 }
190 
191 // Execute method on all contained objects in parallel
192 template<class Texec>
193 inline void DtParallelExecutionContainer<Texec>::executeMethodParallel(void (Texec::*theMethodPtr)())
194 {
195 #ifndef DtVRL_TBB_UNSUPPORTED
196  size_t size = myExecVec.size();
197  tbb::blocked_range<size_t> range(0, size);
198  ExecutableItem execST(myExecVec, theMethodPtr);
199  beginParallel();
200  tbb::parallel_for(range, execST);
201  endParallel();
202 #else
203  executeMethodSerial(theMethodPtr);
204 #endif
205 }
206 
207 // Execute method on all contained objects serially
208 template<class Texec>
209 inline void DtParallelExecutionContainer<Texec>::executeMethodSerial(void (Texec::*theMethodPtr)())
210 {
211  for (PecIteratorType item = begin(); item != end(); ++item)
212  {
213  ((**item).*theMethodPtr)();
214  }
215 }
void DtLeaveParallelExecutionSection()
End publishing in parallel.
virtual void endParallel()
executed after leaving a parallel section
Definition: parallelExecutionContainer.h:156
void DtEnterParallelExecutionSection()
Begin publishing in parallel.
virtual bool isParallel() const
Definition: parallelExecutionContainer.h:143
~DtScopedParallelExecutionLock()
Destructor, which will unlock if locked.
Definition: parallelExecutionContainer.h:52
Contains the DtExecutionContainer class declarations.
virtual bool empty()
Returns true if the container is empty.
Definition: parallelExecutionContainer.h:124
bool useParallel
true if using parallel execution, false for serial
Definition: parallelExecutionContainer.h:163
#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:193
void(Texec::* myMethodPtr)()
Member method to call in parallel.
Definition: parallelExecutionContainer.h:86
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:127
virtual void deleteAndClear()
Delete all referenced objects and then clear the list.
Definition: parallelExecutionContainer.h:168
ExecutableItem(const PecContainerType &theCV, void(Texec::*theMethodPtr)())
ctor with vector of objects and method to call
Definition: parallelExecutionContainer.h:101
std::vector< Texec * > PecContainerType
Definition: parallelExecutionContainer.h:71
virtual PecIteratorType begin()
Iterators over the collection.
Definition: parallelExecutionContainer.h:133
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:49
DtParallelExecutionContainer(bool parallel=true)
Default to parallel execution.
Definition: parallelExecutionContainer.h:111
Convenience class for scoped locking.
Definition: parallelExecutionContainer.h:46
virtual void clear()
Clear the list.
Definition: parallelExecutionContainer.h:121
PecContainerType::iterator PecIteratorType
Definition: parallelExecutionContainer.h:72
virtual PecIteratorType end()
Definition: parallelExecutionContainer.h:134
const PecContainerType & myCV
Publishers worked upon.
Definition: parallelExecutionContainer.h:83
PecContainerType myExecVec
Contains pointers to published entities.
Definition: parallelExecutionContainer.h:159
virtual void add(Texec *item)
Add a reference to the list.
Definition: parallelExecutionContainer.h:115
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:92
virtual void executeMethod(void(Texec::*theMethodPtr)())
worker methods
Definition: parallelExecutionContainer.h:179
void DtLockParallelExecutionAccess()
Lock a resource only if parallel section is active.
virtual void beginParallel()
executed before entering a parallel section
Definition: parallelExecutionContainer.h:153
This class is used by TBB to manage the work units to perform.
Definition: parallelExecutionContainer.h:79
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:142
DtParallelExecutionContainer is implementation of DtExecutionContainer which executes methods on all ...
Definition: parallelExecutionContainer.h:67
virtual ~DtParallelExecutionContainer()
Definition: parallelExecutionContainer.h:112
virtual void executeMethodSerial(void(Texec::*theMethodPtr)())
For test and debug, a convenience to execute methods serially.
Definition: parallelExecutionContainer.h:209
This file provides protocol independence for DtEntityPublisher.

Document ID: Generated on Tue Feb 2 21:02:17 EST 2021 from SVN revision 223668
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)