VR-Link API Documentation for HLA Evolved
 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 
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 
18 #ifndef DtVRL_TBB_UNSUPPORTED
19 #include <tbb/tbb.h>
20 #else
21 #pragma message("Warning, DtParallelExecutionContainer parallel execution not supported on VC7 or earlier due to missing TBB dependency.")
22 #endif
23 
24 #include <vector>
25 #include <algorithm>
31 
32 template <class Texec>
34 {
35 public:
36 
37  typedef typename std::vector<Texec *> PecContainerType;
38  typedef typename PecContainerType::iterator PecIteratorType;
39 
40 private:
46  {
47  private:
50 
52  void (Texec::*myMethodPtr)();
53 
54  public:
55 
56 #ifndef DtVRL_TBB_UNSUPPORTED
57 
58  void operator()( const tbb::blocked_range<size_t>& r ) const
59  {
60  for( size_t i=r.begin(); i!=r.end(); ++i )
61  {
62  ((*myCV[i]).*myMethodPtr)();
63  }
64  }
65 #endif
66 
67  ExecutableItem(const PecContainerType& theCV, void (Texec::*theMethodPtr)())
68  : myCV(theCV)
69  , myMethodPtr(theMethodPtr)
70  {
71  }
72  };
73 
74 public:
75 
77  DtParallelExecutionContainer(bool parallel = true) : useParallel(parallel), myExecVec() {}
79 
81  virtual void add(Texec *item) { myExecVec.push_back(item); }
82 
84  virtual void remove(Texec *item) { myExecVec.erase(std::remove(begin(), end(), item), end()); }
85 
87  virtual void clear() { myExecVec.clear(); }
88 
90  virtual bool empty() { return myExecVec.empty(); }
91 
93  virtual void deleteAndClear();
94 
96  virtual PecIteratorType begin() { return myExecVec.begin(); }
97  virtual PecIteratorType end() { return myExecVec.end(); }
98 
100 
102  virtual void executeMethod(void (Texec::*theMethodPtr)());
103 
105  virtual void setParallel(bool parallel) { useParallel = parallel; }
106  virtual bool isParallel() const { return useParallel; }
107 
108 protected:
110  virtual void executeMethodParallel(void (Texec::*theMethodPtr)());
111 
113  virtual void executeMethodSerial(void (Texec::*theMethodPtr)());
114 
117 
118 private:
121 };
122 
123 // Delete all referenced objects and then clear the list.
124 template<class Texec>
126 {
127  for (PecIteratorType item = begin(); item != end(); ++item)
128  {
129  delete (*item);
130  }
131  clear();
132 }
133 
134 // Execute method on all contained objects
135 template<class Texec>
136 inline void DtParallelExecutionContainer<Texec>::executeMethod(void (Texec::*theMethodPtr)())
137 {
138  if (useParallel)
139  {
140  executeMethodParallel(theMethodPtr);
141  }
142  else
143  {
144  executeMethodSerial(theMethodPtr);
145  }
146 }
147 
148 // Execute method on all contained objects in parallel
149 template<class Texec>
150 inline void DtParallelExecutionContainer<Texec>::executeMethodParallel(void (Texec::*theMethodPtr)())
151 {
152 #ifndef DtVRL_TBB_UNSUPPORTED
153  size_t size = myExecVec.size();
154  tbb::blocked_range<size_t> range(0, size);
155  ExecutableItem execST(myExecVec, theMethodPtr);
156  tbb::parallel_for(range, execST);
157 #else
158  executeMethodSerial(theMethodPtr);
159 #endif
160 }
161 
162 // Execute method on all contained objects serially
163 template<class Texec>
164 inline void DtParallelExecutionContainer<Texec>::executeMethodSerial(void (Texec::*theMethodPtr)())
165 {
166  for (PecIteratorType item = begin(); item != end(); ++item)
167  {
168  ((**item).*theMethodPtr)();
169  }
170 }

Document ID: Generated on Wed Jan 7 13:31:29 EST 2015 from SVN revision 149162
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)