VR-Forces 4.3.1 Class Documentation
 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 VT MAK
3  * All rights reserved.
4  ****************************************************************************/
5 
8 
9 #pragma once
10 
12 #if _MSC_VER < 1400
13 #define TBB_SUPPORTED FALSE
14 #else
15 #define TBB_SUPPORTED TRUE
16 #endif
17 
19 
20 #if (TBB_SUPPORTED)
21 #pragma message("Using TBB for parallel execution.")
22 #include <tbb/tbb.h>
23 #else
24 #pragma message("MAK WARNING: DtParallelExecutionContainer parallel execution not supported on VC7 or earlier due to missing TBB dependency.")
25 #endif
26 
27 #include <vector>
28 
41 template <class Texec>
43 {
44 public:
45 
47  typedef typename std::vector<Texec *> PecContainerType;
48  typedef typename PecContainerType::iterator PecIteratorType;
49 
50 private:
56  {
57  private:
60 
62  void (Texec::*myMethodPtr)();
63 
64  public:
65 
66 #if (TBB_SUPPORTED)
67 
68  void operator()( const tbb::blocked_range<size_t>& r ) const
69  {
70  for( size_t i=r.begin(); i!=r.end(); ++i )
71  {
72  ((*myCV[i]).*myMethodPtr)();
73  }
74  }
75 #endif
76 
77  ExecutableItem(const PecContainerType& theCV, void (Texec::*theMethodPtr)())
78  : myCV(theCV)
79  , myMethodPtr(theMethodPtr)
80  {
81  }
82  };
83 
84 public:
85 
87  DtParallelExecutionContainer(bool parallel = true)
88  : myExecVec()
89  , myUseParallel(parallel)
90  , myGrainSize(10)
91  {
92  }
93 
97 
100  virtual void add(Texec *item) { myExecVec.push_back(item); }
101 
104  virtual void remove(Texec *item) { myExecVec.erase(std::remove(myExecVec.begin(), myExecVec.end(), item), myExecVec.end()); }
105 
107  virtual void clear() { myExecVec.clear(); }
108 
112  virtual void deleteAndClear();
113 
116  virtual bool empty() const { return myExecVec.empty(); }
117 
121  virtual bool contains(Texec *item) { return std::find(begin(), end(), item) != end(); }
122 
125  virtual PecIteratorType begin() { return myExecVec.begin(); }
127  virtual PecIteratorType end() { return myExecVec.end(); }
128 
132  virtual void executeMethod(void (Texec::*theMethodPtr)());
133 
136  virtual void setParallel(bool parallel) { myUseParallel = parallel; }
137 
140  virtual bool isParallel() const { return myUseParallel; }
141 
143  virtual void setGrainSize(int theGrainSize) { myGrainSize = theGrainSize; }
144 
146  virtual int getGrainSize() const { return myGrainSize; }
147 
148 protected:
151  virtual void executeMethodParallel(void (Texec::*theMethodPtr)());
152 
155  virtual void executeMethodSerial(void (Texec::*theMethodPtr)());
156 
159 
160 private:
163 
166 };
167 
168 template<class Texec>
170 {
173  for (PecIteratorType item = begin(); item != end(); ++item)
174  {
175  delete (*item);
176  }
177  clear();
178 }
179 
180 template<class Texec>
181 inline void DtParallelExecutionContainer<Texec>::executeMethod(void (Texec::*theMethodPtr)())
182 {
183  if (empty())
184  {
185  return;
186  }
187 
188  if (myUseParallel)
189  {
190  executeMethodParallel(theMethodPtr);
191  }
192  else
193  {
194  executeMethodSerial(theMethodPtr);
195  }
196 }
197 
198 template<class Texec>
199 inline void DtParallelExecutionContainer<Texec>::executeMethodParallel(void (Texec::*theMethodPtr)())
200 {
201 #if (TBB_SUPPORTED)
202  try
203  {
205  tbb::blocked_range<size_t> range(0, myExecVec.size(), myGrainSize);
207  ExecutableItem execST(myExecVec, theMethodPtr);
209  tbb::parallel_for(range, execST);
210  }
211  catch (tbb::captured_exception& ex)
212  {
213  DtTHROW_NEW(DtException, ex.what());
214  }
215 #else
216 
217  executeMethodSerial(theMethodPtr);
218 #endif
219 }
220 
221 template<class Texec>
222 inline void DtParallelExecutionContainer<Texec>::executeMethodSerial(void (Texec::*theMethodPtr)())
223 {
226  for (PecIteratorType item = begin(); item != end(); ++item)
227  {
228  ((**item).*theMethodPtr)();
229  }
230 }

Document ID: Generated on Wed Jul 29 00:55:00 EDT 2015 from SVN revision 155257
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)