ROSE  0.11.145.0
AstSharedMemoryParallelProcessing.h
1 // Author: Gergo Barany
2 // $Id: AstSharedMemoryParallelProcessing.h,v 1.1 2008/01/08 02:56:39 dquinlan Exp $
3 
4 // Classes for shared-memory (multithreaded) parallel AST traversals.
5 
6 #ifndef ASTSHAREDMEMORYPARALLELPROCESSING_H
7 #define ASTSHAREDMEMORYPARALLELPROCESSING_H
8 
9 #include "rosePublicConfig.h"
10 
11 #include "AstProcessing.h"
12 
13 // Class containing all the information needed to synchronize parallelizable traversals. All traversals running
14 // synchronously must have a shallow copy of this (they all need the same pointers!), the default copy mechanism takes
15 // care of that.
17 {
18 #ifdef _REENTRANT // user wants mult-thread support? (e.g., g++ -pthread)?
19 # ifdef ROSE_HAVE_PTHREAD_H // POSIX threads are available?
20  // mutex that controls access to the global stuff here
21  pthread_mutex_t *mutex;
22  // signal broadcast by last worker thread to arrive at a synchronization point
23  pthread_cond_t *synchronizationEvent;
24  // signal sent by workers when they exit
25  pthread_cond_t *threadFinishedEvent;
26 # else
27 # ifdef _MSC_VER
28 # pragma message ("POSIX threads are unavailable on this platform");
29 # else
30 # warning "POSIX threads are unavailable on this platform."
31 # endif
32 # endif
33 #endif
34 
35  // global counter of the number of threads that are still working (i.e.
36  // have not sent a synchronizationEvent)
37  size_t *workingThreads;
38  // global counter of the number of threads that have completely finished
39  // (i.e. have traversed the whole AST)
40  size_t *finishedThreads;
41  // number of nodes to be visited by each traversal before they are
42  // supposed to synchronize
43  size_t synchronizationWindowSize;
44 
45  AstSharedMemoryParallelProcessingSynchronizationInfo(size_t numberOfThreads, size_t synchronizationWindowSize);
47 
48 #if 1
49  // We can't implement this as private since it is required. So we have a potential double free error here.
50  // private:
51  // DQ (9/13/2011): This copy constructor was built because static analysis tools (made it private to force compile time error if used).
53 #endif
54 };
55 
56 // Class containing the code needed for synchronization of parallelizable traversals. The parallelizable processing
57 // classes inherit privately from this because the code is the same for all of them.
59 {
60 protected:
62  // called when threads want to synchronize
63  void synchronize();
64  // called when threads are about to finish
65  void signalFinish();
66 
67 private:
69  size_t numberOfThreads;
70 };
71 
72 // TOP DOWN BOTTOM UP parallel traversals
73 
74 // Class representing a traversal that can run in parallel with some other instances of the same type. It is basically a
75 // combined processing class with a thin synchronization layer. The user will probably never need to instantiate this
76 // class, they should use the AstSharedMemoryParallel*Processing classes instead.
77 template <class InheritedAttributeType, class SynthesizedAttributeType>
79  : public AstCombinedTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType>,
81 {
82 public:
84  typedef typename Superclass::TraversalType TraversalType;
85  typedef typename Superclass::TraversalPtr TraversalPtr;
86  typedef typename Superclass::TraversalPtrList TraversalPtrList;
87  typedef typename Superclass::InheritedAttributeTypeList InheritedAttributeTypeList;
88  typedef typename Superclass::SynthesizedAttributeTypeList SynthesizedAttributeTypeList;
89  typedef typename Superclass::SynthesizedAttributesList SynthesizedAttributesList;
90 
93  const TraversalPtrList &);
94 
95  void set_runningParallelTraversal(bool val);
96 
97 protected:
98  virtual InheritedAttributeTypeList *evaluateInheritedAttribute(
99  SgNode *astNode,
100  InheritedAttributeTypeList *inheritedValues);
101  virtual void atTraversalEnd();
102 
103 private:
104  size_t visitedNodes;
105  bool runningParallelTraversal;
106  size_t synchronizationWindowSize;
107 };
108 
109 // Class for parallel execution of a number of traversals. This is a drop-in replacement for the corresponding
110 // AstCombined*Processing class, the usage is identical except that you call traverseInParallel() instead of traverse().
111 // (Calling traverse() is identical to AstCombined*Processing, i.e. it will not run in parallel.)
112 template <class InheritedAttributeType, class SynthesizedAttributeType>
114  : public AstCombinedTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType>
115 {
116 public:
118  typedef typename Superclass::InheritedAttributeTypeList InheritedAttributeTypeList;
119  typedef typename Superclass::SynthesizedAttributeTypeList SynthesizedAttributeTypeList;
120  typedef typename Superclass::TraversalPtr TraversalPtr;
121  typedef typename Superclass::TraversalPtrList TraversalPtrList;
122 
124  typedef std::vector<ParallelizableTraversalPtr> ParallelizableTraversalPtrList;
125 
126  SynthesizedAttributeTypeList *traverseInParallel(SgNode *basenode,
127  InheritedAttributeTypeList *inheritedValue);
128 
130  AstSharedMemoryParallelTopDownBottomUpProcessing(const TraversalPtrList &);
131 
132  void set_numberOfThreads(size_t threads);
133  void set_synchronizationWindowSize(size_t windowSize);
134 
135 private:
136  size_t numberOfThreads;
137  size_t synchronizationWindowSize;
138 };
139 
140 // TOP DOWN parallel traversals
141 
142 // Class representing a traversal that can run in parallel with some other instances of the same type. It is basically a
143 // combined processing class with a thin synchronization layer. The user will probably never need to instantiate this
144 // class, they should use the AstSharedMemoryParallel*Processing classes instead.
145 template <class InheritedAttributeType>
147  : public AstCombinedTopDownProcessing<InheritedAttributeType>,
149 {
150 public:
152  typedef typename Superclass::TraversalType TraversalType;
153  typedef typename Superclass::TraversalPtr TraversalPtr;
154  typedef typename Superclass::TraversalPtrList TraversalPtrList;
155  typedef typename Superclass::InheritedAttributeTypeList InheritedAttributeTypeList;
156 
159  const TraversalPtrList &);
160 
161  void set_runningParallelTraversal(bool val);
162 
163 protected:
164  virtual InheritedAttributeTypeList *evaluateInheritedAttribute(
165  SgNode *astNode,
166  InheritedAttributeTypeList *inheritedValues);
167  virtual void atTraversalEnd();
168 
169 private:
170  size_t visitedNodes;
171  bool runningParallelTraversal;
172  size_t synchronizationWindowSize;
173 };
174 
175 // Class for parallel execution of a number of traversals. This is a drop-in replacement for the corresponding
176 // AstCombined*Processing class, the usage is identical except that you call traverseInParallel() instead of traverse().
177 // (Calling traverse() is identical to AstCombined*Processing, i.e. it will not run in parallel.)
178 template <class InheritedAttributeType>
180  : public AstCombinedTopDownProcessing<InheritedAttributeType>
181 {
182 public:
184  typedef typename Superclass::InheritedAttributeTypeList InheritedAttributeTypeList;
185  typedef typename Superclass::TraversalPtr TraversalPtr;
186  typedef typename Superclass::TraversalPtrList TraversalPtrList;
187 
189  typedef std::vector<ParallelizableTraversalPtr> ParallelizableTraversalPtrList;
190 
191  void traverseInParallel(SgNode *basenode, InheritedAttributeTypeList *inheritedValue);
192 
194  AstSharedMemoryParallelTopDownProcessing(const TraversalPtrList &);
195 
196  void set_numberOfThreads(size_t threads);
197  void set_synchronizationWindowSize(size_t windowSize);
198 
199 private:
200  size_t numberOfThreads;
201  size_t synchronizationWindowSize;
202 };
203 
204 // BOTTOM UP parallel traversals
205 
206 // Class representing a traversal that can run in parallel with some other instances of the same type. It is basically a
207 // combined processing class with a thin synchronization layer. The user will probably never need to instantiate this
208 // class, they should use the AstSharedMemoryParallel*Processing classes instead.
209 template <class SynthesizedAttributeType>
211  : public AstCombinedBottomUpProcessing<SynthesizedAttributeType>,
213 {
214 public:
216  typedef typename Superclass::TraversalType TraversalType;
217  typedef typename Superclass::TraversalPtr TraversalPtr;
218  typedef typename Superclass::TraversalPtrList TraversalPtrList;
219  typedef typename Superclass::SynthesizedAttributeTypeList SynthesizedAttributeTypeList;
220  typedef typename Superclass::SynthesizedAttributesList SynthesizedAttributesList;
221 
224  const TraversalPtrList &);
225 
226  void set_runningParallelTraversal(bool val);
227 
228 protected:
229  virtual SynthesizedAttributeTypeList *evaluateSynthesizedAttribute(
230  SgNode *astNode,
231  SynthesizedAttributesList synthesizedAttributes);
232  virtual void atTraversalEnd();
233 
234 private:
235  size_t visitedNodes;
236  bool runningParallelTraversal;
237  size_t synchronizationWindowSize;
238 };
239 
240 // Class for parallel execution of a number of traversals. This is a drop-in replacement for the corresponding
241 // AstCombined*Processing class, the usage is identical except that you call traverseInParallel() instead of traverse().
242 // (Calling traverse() is identical to AstCombined*Processing, i.e. it will not run in parallel.)
243 template <class SynthesizedAttributeType>
245  : public AstCombinedBottomUpProcessing<SynthesizedAttributeType>
246 {
247 public:
249  typedef typename Superclass::SynthesizedAttributeTypeList SynthesizedAttributeTypeList;
250  typedef typename Superclass::TraversalPtr TraversalPtr;
251  typedef typename Superclass::TraversalPtrList TraversalPtrList;
252 
254  typedef std::vector<ParallelizableTraversalPtr> ParallelizableTraversalPtrList;
255 
256  SynthesizedAttributeTypeList *traverseInParallel(SgNode *basenode);
257 
259  AstSharedMemoryParallelBottomUpProcessing(const TraversalPtrList &);
260 
261  void set_numberOfThreads(size_t threads);
262  void set_synchronizationWindowSize(size_t windowSize);
263 
264 private:
265  size_t numberOfThreads;
266  size_t synchronizationWindowSize;
267 };
268 
269 #include "AstSharedMemoryParallelProcessingImpl.h"
270 
271 #include "AstSharedMemoryParallelSimpleProcessing.h"
272 
273 #endif
Attribute Evaluator for synthesized attributes.
Attribute Evaluator for inherited attributes.
virtual InheritedAttributeTypeList * evaluateInheritedAttribute(SgNode *astNode, InheritedAttributeTypeList *inheritedValues)
pure virtual function which must be implemented to compute the inherited attribute at a node ...
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9846
virtual InheritedAttributeTypeList * evaluateInheritedAttribute(SgNode *astNode, InheritedAttributeTypeList *inheritedValues)
pure virtual function which must be implemented to compute the inherited attribute at a node ...