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 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 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 }

Document ID: Generated on Fri Dec 2 02:42:08 EST 2016 from SVN revision 171416
Copyright © 2005-2016 VT MÄK. All Rights Reserved (www.mak.com)