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 
92  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 
101  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 void deleteAndClear();
128 
130  virtual PecIteratorType begin() { return myExecVec.begin(); }
131  virtual PecIteratorType end() { return myExecVec.end(); }
132 
134 
136  virtual void executeMethod(void (Texec::*theMethodPtr)());
137 
139  virtual void setParallel(bool parallel) { useParallel = parallel; }
140  virtual bool isParallel() const { return useParallel; }
141 
142 protected:
144  virtual void executeMethodParallel(void (Texec::*theMethodPtr)());
145 
147  virtual void executeMethodSerial(void (Texec::*theMethodPtr)());
148 
151 
154 
157 
158 private:
161 };
162 
163 // Delete all referenced objects and then clear the list.
164 template<class Texec>
166 {
167  for (PecIteratorType item = begin(); item != end(); ++item)
168  {
169  delete (*item);
170  }
171  clear();
172 }
173 
174 // Execute method on all contained objects
175 template<class Texec>
176 inline void DtParallelExecutionContainer<Texec>::executeMethod(void (Texec::*theMethodPtr)())
177 {
178  if (useParallel)
179  {
180  executeMethodParallel(theMethodPtr);
181  }
182  else
183  {
184  executeMethodSerial(theMethodPtr);
185  }
186 }
187 
188 // Execute method on all contained objects in parallel
189 template<class Texec>
190 inline void DtParallelExecutionContainer<Texec>::executeMethodParallel(void (Texec::*theMethodPtr)())
191 {
192 #ifndef DtVRL_TBB_UNSUPPORTED
193  size_t size = myExecVec.size();
194  tbb::blocked_range<size_t> range(0, size);
195  ExecutableItem execST(myExecVec, theMethodPtr);
196  beginParallel();
197  tbb::parallel_for(range, execST);
198  endParallel();
199 #else
200  executeMethodSerial(theMethodPtr);
201 #endif
202 }
203 
204 // Execute method on all contained objects serially
205 template<class Texec>
206 inline void DtParallelExecutionContainer<Texec>::executeMethodSerial(void (Texec::*theMethodPtr)())
207 {
208  for (PecIteratorType item = begin(); item != end(); ++item)
209  {
210  ((**item).*theMethodPtr)();
211  }
212 }

Document ID: Generated on Tue Mar 1 02:56:17 EST 2016 from SVN revision 162687
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)