Skip to content

Commit

Permalink
Quadtree: correct OpenMP parallelism
Browse files Browse the repository at this point in the history
The problem is that the STL vector 'array' is being used in multiple threads in
parallel. Give each thread its own vector and merge the result into 'array'.

Signed-off-by: Steven Noonan <steven@uplinklabs.net>
  • Loading branch information
tycho committed May 2, 2015
1 parent 98a558f commit dc55e2d
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions source/crisscross/quadtree.h
Expand Up @@ -112,41 +112,59 @@ namespace CrissCross
midY = (llPosition.Y() + trPosition.Y()) / 2.0f;

#ifdef _OPENMP
std::vector<T> v1, v2, v3, v4;
#pragma omp parallel sections
#endif
{
#ifdef _OPENMP
#pragma omp section
#endif
if (top > midY && left < midX) {
/* need to descend into top left quadtree */
tl->ObjectsInCircle(array, circle, radius);
tl->ObjectsInCircle(v1, circle, radius);
}

#ifdef _OPENMP
#pragma omp section
#endif
if (top > midY && right > midX) {
/* top right quadtree */
tr->ObjectsInCircle(array, circle, radius);
tr->ObjectsInCircle(v2, circle, radius);
}

#ifdef _OPENMP
#pragma omp section
#endif
if (bottom < midY && right > midX) {
/* lower right quadtree */
lr->ObjectsInCircle(array, circle, radius);
lr->ObjectsInCircle(v3, circle, radius);
}

#ifdef _OPENMP
#pragma omp section
#endif
if (bottom < midY && left < midX) {
/* lower left quadtree */
ll->ObjectsInCircle(array, circle, radius);
ll->ObjectsInCircle(v4, circle, radius);
}
}
/* Merge the resulting vectors. */
array.insert(array.end(), v1.begin(), v1.end());
array.insert(array.end(), v2.begin(), v2.end());
array.insert(array.end(), v3.begin(), v3.end());
array.insert(array.end(), v4.begin(), v4.end());
#else
if (top > midY && left < midX) {
/* need to descend into top left quadtree */
tl->ObjectsInCircle(array, circle, radius);
}

if (top > midY && right > midX) {
/* top right quadtree */
tr->ObjectsInCircle(array, circle, radius);
}

if (bottom < midY && right > midX) {
/* lower right quadtree */
lr->ObjectsInCircle(array, circle, radius);
}

if (bottom < midY && left < midX) {
/* lower left quadtree */
ll->ObjectsInCircle(array, circle, radius);
}
#endif
}

template <class T, int MaxDepth, int MaxNodesPerLevel>
Expand Down

0 comments on commit dc55e2d

Please sign in to comment.