AstSharedMemoryParallelProcessing.h

Go to the documentation of this file.
00001 // Author: Gergo Barany
00002 // $Id: AstSharedMemoryParallelProcessing.h,v 1.1 2008/01/08 02:56:39 dquinlan Exp $
00003 
00004 // Classes for shared-memory (multithreaded) parallel AST traversals.
00005 
00006 #ifndef ASTSHAREDMEMORYPARALLELPROCESSING_H
00007 #define ASTSHAREDMEMORYPARALLELPROCESSING_H
00008 
00009 // tps (01/08/2010) Added sage3basic since this doesnt compile under gcc4.1.2
00010 //#include "sage3basic.h"
00011 //#include "sage3.h"
00012 
00013 #include "AstProcessing.h"
00014 
00015 // Class containing all the information needed to synchronize parallelizable traversals. All traversals running
00016 // synchronously must have a shallow copy of this (they all need the same pointers!), the default copy mechanism takes
00017 // care of that.
00018 struct AstSharedMemoryParallelProcessingSynchronizationInfo
00019 {
00020 #ifdef _MSC_VER
00021 #pragma message ("Error: pthread.h is unavailable on MSVC, we might want to use boost.thread library.")
00022 #else
00023     // mutex that controls access to the global stuff here
00024     pthread_mutex_t *mutex;
00025     // signal broadcast by last worker thread to arrive at a synchronization point
00026     pthread_cond_t *synchronizationEvent;
00027     // signal sent by workers when they exit
00028     pthread_cond_t *threadFinishedEvent;
00029 #endif
00030 
00031     // global counter of the number of threads that are still working (i.e.
00032     // have not sent a synchronizationEvent)
00033     size_t *workingThreads;
00034     // global counter of the number of threads that have completely finished
00035     // (i.e. have traversed the whole AST)
00036     size_t *finishedThreads;
00037     // number of nodes to be visited by each traversal before they are
00038     // supposed to synchronize
00039     size_t synchronizationWindowSize;
00040 
00041     AstSharedMemoryParallelProcessingSynchronizationInfo(size_t numberOfThreads, size_t synchronizationWindowSize);
00042     ~AstSharedMemoryParallelProcessingSynchronizationInfo();
00043 
00044 #if 1
00045  // We can't implement this as private since it is required.  So we have a potential double free error here.
00046  // private:
00047  // DQ (9/13/2011): This copy constructor was built because static analysis tools (made it private to force compile time error if used).
00048     AstSharedMemoryParallelProcessingSynchronizationInfo( const AstSharedMemoryParallelProcessingSynchronizationInfo & X );
00049 #endif
00050 };
00051 
00052 // Class containing the code needed for synchronization of parallelizable traversals. The parallelizable processing
00053 // classes inherit privately from this because the code is the same for all of them.
00054 class AstSharedMemoryParallelProcessingSynchronizationBase
00055 {
00056 protected:
00057     AstSharedMemoryParallelProcessingSynchronizationBase(const AstSharedMemoryParallelProcessingSynchronizationInfo &);
00058     // called when threads want to synchronize
00059     void synchronize();
00060     // called when threads are about to finish
00061     void signalFinish();
00062 
00063 private:
00064     AstSharedMemoryParallelProcessingSynchronizationInfo syncInfo;
00065     size_t numberOfThreads;
00066 };
00067 
00068 // TOP DOWN BOTTOM UP parallel traversals
00069 
00070 // Class representing a traversal that can run in parallel with some other instances of the same type. It is basically a
00071 // combined processing class with a thin synchronization layer. The user will probably never need to instantiate this
00072 // class, they should use the AstSharedMemoryParallel*Processing classes instead.
00073 template <class InheritedAttributeType, class SynthesizedAttributeType>
00074 class AstSharedMemoryParallelizableTopDownBottomUpProcessing
00075     : public AstCombinedTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType>,
00076       private AstSharedMemoryParallelProcessingSynchronizationBase
00077 {
00078 public:
00079     typedef AstCombinedTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType> Superclass;
00080     typedef typename Superclass::TraversalType TraversalType;
00081     typedef typename Superclass::TraversalPtr TraversalPtr;
00082     typedef typename Superclass::TraversalPtrList TraversalPtrList;
00083     typedef typename Superclass::InheritedAttributeTypeList InheritedAttributeTypeList;
00084     typedef typename Superclass::SynthesizedAttributeTypeList SynthesizedAttributeTypeList;
00085     typedef typename Superclass::SynthesizedAttributesList SynthesizedAttributesList;
00086 
00087     AstSharedMemoryParallelizableTopDownBottomUpProcessing(
00088             const AstSharedMemoryParallelProcessingSynchronizationInfo &,
00089             const TraversalPtrList &);
00090 
00091     void set_runningParallelTraversal(bool val);
00092 
00093 protected:
00094     virtual InheritedAttributeTypeList *evaluateInheritedAttribute(
00095             SgNode *astNode,
00096             InheritedAttributeTypeList *inheritedValues);
00097     virtual void atTraversalEnd();
00098 
00099 private:
00100     size_t visitedNodes;
00101     bool runningParallelTraversal;
00102     size_t synchronizationWindowSize;
00103 };
00104 
00105 // Class for parallel execution of a number of traversals. This is a drop-in replacement for the corresponding
00106 // AstCombined*Processing class, the usage is identical except that you call traverseInParallel() instead of traverse().
00107 // (Calling traverse() is identical to AstCombined*Processing, i.e. it will not run in parallel.)
00108 template <class InheritedAttributeType, class SynthesizedAttributeType>
00109 class AstSharedMemoryParallelTopDownBottomUpProcessing
00110     : public AstCombinedTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType>
00111 {
00112 public:
00113     typedef AstCombinedTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType> Superclass;
00114     typedef typename Superclass::InheritedAttributeTypeList InheritedAttributeTypeList;
00115     typedef typename Superclass::SynthesizedAttributeTypeList SynthesizedAttributeTypeList;
00116     typedef typename Superclass::TraversalPtr TraversalPtr;
00117     typedef typename Superclass::TraversalPtrList TraversalPtrList;
00118 
00119     typedef AstSharedMemoryParallelizableTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType> *ParallelizableTraversalPtr;
00120     typedef std::vector<ParallelizableTraversalPtr> ParallelizableTraversalPtrList;
00121 
00122     SynthesizedAttributeTypeList *traverseInParallel(SgNode *basenode,
00123             InheritedAttributeTypeList *inheritedValue);
00124 
00125     AstSharedMemoryParallelTopDownBottomUpProcessing();
00126     AstSharedMemoryParallelTopDownBottomUpProcessing(const TraversalPtrList &);
00127 
00128     void set_numberOfThreads(size_t threads) const;
00129     void set_synchronizationWindowSize(size_t windowSize) const;
00130 
00131 private:
00132     size_t numberOfThreads;
00133     size_t synchronizationWindowSize;
00134 };
00135 
00136 // TOP DOWN parallel traversals
00137 
00138 // Class representing a traversal that can run in parallel with some other instances of the same type. It is basically a
00139 // combined processing class with a thin synchronization layer. The user will probably never need to instantiate this
00140 // class, they should use the AstSharedMemoryParallel*Processing classes instead.
00141 template <class InheritedAttributeType>
00142 class AstSharedMemoryParallelizableTopDownProcessing
00143     : public AstCombinedTopDownProcessing<InheritedAttributeType>,
00144       private AstSharedMemoryParallelProcessingSynchronizationBase
00145 {
00146 public:
00147     typedef AstCombinedTopDownProcessing<InheritedAttributeType> Superclass;
00148     typedef typename Superclass::TraversalType TraversalType;
00149     typedef typename Superclass::TraversalPtr TraversalPtr;
00150     typedef typename Superclass::TraversalPtrList TraversalPtrList;
00151     typedef typename Superclass::InheritedAttributeTypeList InheritedAttributeTypeList;
00152 
00153     AstSharedMemoryParallelizableTopDownProcessing(
00154             const AstSharedMemoryParallelProcessingSynchronizationInfo &,
00155             const TraversalPtrList &);
00156 
00157     void set_runningParallelTraversal(bool val);
00158 
00159 protected:
00160     virtual InheritedAttributeTypeList *evaluateInheritedAttribute(
00161             SgNode *astNode,
00162             InheritedAttributeTypeList *inheritedValues);
00163     virtual void atTraversalEnd();
00164 
00165 private:
00166     size_t visitedNodes;
00167     bool runningParallelTraversal;
00168     size_t synchronizationWindowSize;
00169 };
00170 
00171 // Class for parallel execution of a number of traversals. This is a drop-in replacement for the corresponding
00172 // AstCombined*Processing class, the usage is identical except that you call traverseInParallel() instead of traverse().
00173 // (Calling traverse() is identical to AstCombined*Processing, i.e. it will not run in parallel.)
00174 template <class InheritedAttributeType>
00175 class AstSharedMemoryParallelTopDownProcessing
00176     : public AstCombinedTopDownProcessing<InheritedAttributeType>
00177 {
00178 public:
00179     typedef AstCombinedTopDownProcessing<InheritedAttributeType> Superclass;
00180     typedef typename Superclass::InheritedAttributeTypeList InheritedAttributeTypeList;
00181     typedef typename Superclass::TraversalPtr TraversalPtr;
00182     typedef typename Superclass::TraversalPtrList TraversalPtrList;
00183 
00184     typedef AstSharedMemoryParallelizableTopDownProcessing<InheritedAttributeType> *ParallelizableTraversalPtr;
00185     typedef std::vector<ParallelizableTraversalPtr> ParallelizableTraversalPtrList;
00186 
00187     void traverseInParallel(SgNode *basenode, InheritedAttributeTypeList *inheritedValue);
00188 
00189     AstSharedMemoryParallelTopDownProcessing();
00190     AstSharedMemoryParallelTopDownProcessing(const TraversalPtrList &);
00191 
00192     void set_numberOfThreads(size_t threads) const;
00193     void set_synchronizationWindowSize(size_t windowSize) const;
00194 
00195 private:
00196     size_t numberOfThreads;
00197     size_t synchronizationWindowSize;
00198 };
00199 
00200 // BOTTOM UP parallel traversals
00201 
00202 // Class representing a traversal that can run in parallel with some other instances of the same type. It is basically a
00203 // combined processing class with a thin synchronization layer. The user will probably never need to instantiate this
00204 // class, they should use the AstSharedMemoryParallel*Processing classes instead.
00205 template <class SynthesizedAttributeType>
00206 class AstSharedMemoryParallelizableBottomUpProcessing
00207     : public AstCombinedBottomUpProcessing<SynthesizedAttributeType>,
00208       private AstSharedMemoryParallelProcessingSynchronizationBase
00209 {
00210 public:
00211     typedef AstCombinedBottomUpProcessing<SynthesizedAttributeType> Superclass;
00212     typedef typename Superclass::TraversalType TraversalType;
00213     typedef typename Superclass::TraversalPtr TraversalPtr;
00214     typedef typename Superclass::TraversalPtrList TraversalPtrList;
00215     typedef typename Superclass::SynthesizedAttributeTypeList SynthesizedAttributeTypeList;
00216     typedef typename Superclass::SynthesizedAttributesList SynthesizedAttributesList;
00217 
00218     AstSharedMemoryParallelizableBottomUpProcessing(
00219             const AstSharedMemoryParallelProcessingSynchronizationInfo &,
00220             const TraversalPtrList &);
00221 
00222     void set_runningParallelTraversal(bool val);
00223 
00224 protected:
00225     virtual SynthesizedAttributeTypeList *evaluateSynthesizedAttribute(
00226             SgNode *astNode,
00227             SynthesizedAttributesList synthesizedAttributes);
00228     virtual void atTraversalEnd();
00229 
00230 private:
00231     size_t visitedNodes;
00232     bool runningParallelTraversal;
00233     size_t synchronizationWindowSize;
00234 };
00235 
00236 // Class for parallel execution of a number of traversals. This is a drop-in replacement for the corresponding
00237 // AstCombined*Processing class, the usage is identical except that you call traverseInParallel() instead of traverse().
00238 // (Calling traverse() is identical to AstCombined*Processing, i.e. it will not run in parallel.)
00239 template <class SynthesizedAttributeType>
00240 class AstSharedMemoryParallelBottomUpProcessing
00241     : public AstCombinedBottomUpProcessing<SynthesizedAttributeType>
00242 {
00243 public:
00244     typedef AstCombinedBottomUpProcessing<SynthesizedAttributeType> Superclass;
00245     typedef typename Superclass::SynthesizedAttributeTypeList SynthesizedAttributeTypeList;
00246     typedef typename Superclass::TraversalPtr TraversalPtr;
00247     typedef typename Superclass::TraversalPtrList TraversalPtrList;
00248 
00249     typedef AstSharedMemoryParallelizableBottomUpProcessing<SynthesizedAttributeType> *ParallelizableTraversalPtr;
00250     typedef std::vector<ParallelizableTraversalPtr> ParallelizableTraversalPtrList;
00251 
00252     SynthesizedAttributeTypeList *traverseInParallel(SgNode *basenode);
00253 
00254     AstSharedMemoryParallelBottomUpProcessing();
00255     AstSharedMemoryParallelBottomUpProcessing(const TraversalPtrList &);
00256 
00257     void set_numberOfThreads(size_t threads) const;
00258     void set_synchronizationWindowSize(size_t windowSize) const;
00259 
00260 private:
00261     size_t numberOfThreads;
00262     size_t synchronizationWindowSize;
00263 };
00264 
00265 #include "AstSharedMemoryParallelProcessingImpl.h"
00266 
00267 #include "AstSharedMemoryParallelSimpleProcessing.h"
00268 
00269 #endif

Generated on Tue Jan 31 05:31:20 2012 for ROSE by  doxygen 1.4.7