2#ifndef ASTSHAREDMEMORYPARALLELPROCESSING_C
3#define ASTSHAREDMEMORYPARALLELPROCESSING_C
5#include "rosePublicConfig.h"
8# ifdef ROSE_HAVE_PTHREAD_H
13# pragma message ("POSIX threads are unavailable on this platform.")
15# warning "POSIX threads are unavailable on this platform."
26#include "AstSharedMemoryParallelProcessing.h"
33template <
class I,
class S>
37 const typename AstSharedMemoryParallelizableTopDownBottomUpProcessing<I, S>::TraversalPtrList &t)
40 visitedNodes(0), runningParallelTraversal(false),
41 synchronizationWindowSize(syncInfo.synchronizationWindowSize)
45template <
class I,
class S>
49 runningParallelTraversal = val;
52template <
class I,
class S>
53typename AstSharedMemoryParallelizableTopDownBottomUpProcessing<I, S>::InheritedAttributeTypeList *
56 typename AstSharedMemoryParallelizableTopDownBottomUpProcessing<I, S>::InheritedAttributeTypeList *inheritedValues)
58 if (runningParallelTraversal && ++visitedNodes > synchronizationWindowSize)
65 return Superclass::evaluateInheritedAttribute(astNode, inheritedValues);
68template <
class I,
class S>
73 Superclass::atTraversalEnd();
76 if (runningParallelTraversal)
80 runningParallelTraversal =
false;
87template <
class I,
class S>
88struct AstSharedMemoryParallelTopDownBottomUpThreadArgs
92 typename AstSharedMemoryParallelizableTopDownBottomUpProcessing<I, S>::InheritedAttributeTypeList *inheritedValues;
94 AstSharedMemoryParallelTopDownBottomUpThreadArgs(
97 typename AstSharedMemoryParallelizableTopDownBottomUpProcessing<I, S>::InheritedAttributeTypeList *inheritedValues)
98 : traversal(traversal), basenode(basenode), inheritedValues(inheritedValues)
107template <
class I,
class S>
108void *parallelTopDownBottomUpProcessingThread(
void *p)
110 AstSharedMemoryParallelTopDownBottomUpThreadArgs<I, S> *threadArgs = (AstSharedMemoryParallelTopDownBottomUpThreadArgs<I, S> *) p;
113 SgNode *basenode = threadArgs->basenode;
114 typename AstSharedMemoryParallelizableTopDownBottomUpProcessing<I, S>::InheritedAttributeTypeList
115 *inheritedValues = threadArgs->inheritedValues;
120 traversal->set_runningParallelTraversal(
true);
122 return traversal->
traverse(basenode, inheritedValues);
125template <
class I,
class S>
126typename AstSharedMemoryParallelTopDownBottomUpProcessing<I, S>::SynthesizedAttributeTypeList *
129 typename AstSharedMemoryParallelTopDownBottomUpProcessing<I, S>::InheritedAttributeTypeList *inheritedValues)
132 const typename Superclass::TraversalPtrList &traversals = Superclass::traversals;
134 size_t numberOfTraversals = traversals.size();
141 ParallelizableTraversalPtrList parallelTraversals(numberOfThreads);
142 std::vector<InheritedAttributeTypeList *> parallelInheritedValues(numberOfThreads);
143 size_t begin = 0, end;
144 for (i = 0; i < numberOfThreads; i++)
146 end = begin + numberOfTraversals / numberOfThreads;
147 if (end > numberOfTraversals)
148 end = numberOfTraversals;
150 parallelTraversals[i]
152 std::vector<TraversalPtr>(traversals.begin() + begin, traversals.begin() + end));
153 parallelInheritedValues[i]
154 =
new InheritedAttributeTypeList(
155 inheritedValues->begin() + begin, inheritedValues->begin() + end);
159#if defined(_REENTRANT) && defined(ROSE_HAVE_PTHREAD_H)
162 pthread_t *threads =
new pthread_t[numberOfThreads];
163 for (i = 0; i < numberOfThreads; i++)
165 pthread_create(&threads[i], NULL,
166 parallelTopDownBottomUpProcessingThread<I, S>,
167 new AstSharedMemoryParallelTopDownBottomUpThreadArgs<I, S>(
168 parallelTraversals[i], basenode, parallelInheritedValues[i]));
174 pthread_mutex_lock(syncInfo.mutex);
175 while (*syncInfo.finishedThreads < numberOfThreads)
176 pthread_cond_wait(syncInfo.threadFinishedEvent, syncInfo.mutex);
177 pthread_mutex_unlock(syncInfo.mutex);
180 std::vector<SynthesizedAttributeTypeList *> finalResults(numberOfThreads);
181 for (i = 0; i < numberOfThreads; i++)
182 pthread_join(threads[i], (
void **) &finalResults[i]);
187 SynthesizedAttributeTypeList *flatFinalResults =
new SynthesizedAttributeTypeList;
188 for (i = 0; i < numberOfThreads; i++)
189 std::copy(finalResults[i]->begin(), finalResults[i]->end(), std::back_inserter(*flatFinalResults));
192 return flatFinalResults;
195template <
class I,
class S>
197 : numberOfThreads(2), synchronizationWindowSize(100000)
201template <
class I,
class S>
208template <
class I,
class S>
216 numberOfThreads = threads;
220template <
class I,
class S>
228 synchronizationWindowSize = windowSize;
238 const typename AstSharedMemoryParallelizableTopDownProcessing<I>::TraversalPtrList &t)
241 visitedNodes(0), runningParallelTraversal(false),
242 synchronizationWindowSize(syncInfo.synchronizationWindowSize)
250 runningParallelTraversal = val;
254typename AstSharedMemoryParallelizableTopDownProcessing<I>::InheritedAttributeTypeList *
257 typename AstSharedMemoryParallelizableTopDownProcessing<I>::InheritedAttributeTypeList *inheritedValues)
259 if (runningParallelTraversal && ++visitedNodes > synchronizationWindowSize)
266 return Superclass::evaluateInheritedAttribute(astNode, inheritedValues);
274 Superclass::atTraversalEnd();
277 if (runningParallelTraversal)
281 runningParallelTraversal =
false;
289struct AstSharedMemoryParallelTopDownThreadArgs
293 typename AstSharedMemoryParallelizableTopDownProcessing<I>::InheritedAttributeTypeList *inheritedValues;
295 AstSharedMemoryParallelTopDownThreadArgs(
298 typename AstSharedMemoryParallelizableTopDownProcessing<I>::InheritedAttributeTypeList *inheritedValues)
299 : traversal(traversal), basenode(basenode), inheritedValues(inheritedValues)
309void *parallelTopDownProcessingThread(
void *p)
311 AstSharedMemoryParallelTopDownThreadArgs<I> *threadArgs = (AstSharedMemoryParallelTopDownThreadArgs<I> *) p;
314 SgNode *basenode = threadArgs->basenode;
315 typename AstSharedMemoryParallelizableTopDownProcessing<I>::InheritedAttributeTypeList
316 *inheritedValues = threadArgs->inheritedValues;
321 traversal->set_runningParallelTraversal(
true);
323 traversal->
traverse(basenode, inheritedValues);
333 typename AstSharedMemoryParallelTopDownProcessing<I>::InheritedAttributeTypeList *inheritedValues)
335 const typename Superclass::TraversalPtrList &traversals = Superclass::traversals;
337 size_t numberOfTraversals = traversals.size();
344 ParallelizableTraversalPtrList parallelTraversals(numberOfThreads);
345 std::vector<InheritedAttributeTypeList *> parallelInheritedValues(numberOfThreads);
346 size_t begin = 0, end;
347 for (i = 0; i < numberOfThreads; i++)
349 end = begin + numberOfTraversals / numberOfThreads;
350 if (end > numberOfTraversals)
351 end = numberOfTraversals;
353 parallelTraversals[i]
355 std::vector<TraversalPtr>(traversals.begin() + begin, traversals.begin() + end));
356 parallelInheritedValues[i]
357 =
new InheritedAttributeTypeList(
358 inheritedValues->begin() + begin, inheritedValues->begin() + end);
364 pthread_t *threads =
new pthread_t[numberOfThreads];
365 for (i = 0; i < numberOfThreads; i++)
367 pthread_create(&threads[i], NULL,
368 parallelTopDownProcessingThread<I>,
369 new AstSharedMemoryParallelTopDownThreadArgs<I>(
370 parallelTraversals[i], basenode, parallelInheritedValues[i]));
376 pthread_mutex_lock(syncInfo.mutex);
377 while (*syncInfo.finishedThreads < numberOfThreads)
378 pthread_cond_wait(syncInfo.threadFinishedEvent, syncInfo.mutex);
379 pthread_mutex_unlock(syncInfo.mutex);
382 std::vector<void *> finalResults(numberOfThreads);
383 for (i = 0; i < numberOfThreads; i++)
384 pthread_join(threads[i], &finalResults[i]);
392 : numberOfThreads(2), synchronizationWindowSize(100000)
411 numberOfThreads = threads;
423 synchronizationWindowSize = windowSize;
433 const typename AstSharedMemoryParallelizableBottomUpProcessing<S>::TraversalPtrList &t)
436 visitedNodes(0), runningParallelTraversal(false),
437 synchronizationWindowSize(syncInfo.synchronizationWindowSize)
445 runningParallelTraversal = val;
449typename AstSharedMemoryParallelizableBottomUpProcessing<S>::SynthesizedAttributeTypeList *
452 typename AstSharedMemoryParallelizableBottomUpProcessing<S>::SynthesizedAttributesList synthesizedAttributes)
454 if (runningParallelTraversal && ++visitedNodes > synchronizationWindowSize)
461 return Superclass::evaluateSynthesizedAttribute(astNode, synthesizedAttributes);
469 Superclass::atTraversalEnd();
472 if (runningParallelTraversal)
476 runningParallelTraversal =
false;
484struct AstSharedMemoryParallelBottomUpThreadArgs
489 AstSharedMemoryParallelBottomUpThreadArgs(
492 : traversal(traversal), basenode(basenode)
502void *parallelBottomUpProcessingThread(
void *p)
504 AstSharedMemoryParallelBottomUpThreadArgs<S> *threadArgs = (AstSharedMemoryParallelBottomUpThreadArgs<S> *) p;
507 SgNode *basenode = threadArgs->basenode;
512 traversal->set_runningParallelTraversal(
true);
514 return traversal->
traverse(basenode);
518typename AstSharedMemoryParallelBottomUpProcessing<S>::SynthesizedAttributeTypeList *
521 const typename Superclass::TraversalPtrList &traversals = Superclass::traversals;
523 size_t numberOfTraversals = traversals.size();
530 ParallelizableTraversalPtrList parallelTraversals(numberOfThreads);
531 size_t begin = 0, end;
532 for (i = 0; i < numberOfThreads; i++)
534 end = begin + numberOfTraversals / numberOfThreads;
535 if (end > numberOfTraversals)
536 end = numberOfTraversals;
538 parallelTraversals[i]
540 std::vector<TraversalPtr>(traversals.begin() + begin, traversals.begin() + end));
545 pthread_t *threads =
new pthread_t[numberOfThreads];
546 for (i = 0; i < numberOfThreads; i++)
548 pthread_create(&threads[i], NULL,
549 parallelBottomUpProcessingThread<S>,
550 new AstSharedMemoryParallelBottomUpThreadArgs<S>(
551 parallelTraversals[i], basenode));
557 pthread_mutex_lock(syncInfo.mutex);
558 while (*syncInfo.finishedThreads < numberOfThreads)
559 pthread_cond_wait(syncInfo.threadFinishedEvent, syncInfo.mutex);
560 pthread_mutex_unlock(syncInfo.mutex);
563 std::vector<SynthesizedAttributeTypeList *> finalResults(numberOfThreads);
564 for (i = 0; i < numberOfThreads; i++)
565 pthread_join(threads[i], (
void **) &finalResults[i]);
569 SynthesizedAttributeTypeList *flatFinalResults =
new SynthesizedAttributeTypeList;
570 for (i = 0; i < numberOfThreads; i++)
571 std::copy(finalResults[i]->begin(), finalResults[i]->end(), std::back_inserter(*flatFinalResults));
574 return flatFinalResults;
579 : numberOfThreads(2), synchronizationWindowSize(100000)
598 numberOfThreads = threads;
610 synchronizationWindowSize = windowSize;
SynthesizedAttributeType traverse(SgNode *node)
evaluates attributes on the entire AST
virtual InheritedAttributeTypeList * evaluateInheritedAttribute(SgNode *astNode, InheritedAttributeTypeList *inheritedValues)
pure virtual function which must be implemented to compute the inherited attribute at a node
virtual InheritedAttributeTypeList * evaluateInheritedAttribute(SgNode *astNode, InheritedAttributeTypeList *inheritedValues)
pure virtual function which must be implemented to compute the inherited attribute at a node
SynthesizedAttributeType traverse(SgNode *node, InheritedAttributeType inheritedValue)
evaluates attributes on the entire AST
void traverse(SgNode *node, InheritedAttributeType inheritedValue)
evaluates attributes on the entire AST
This class represents the base class for all IR nodes within Sage III.