I have a for-loop to iterate over a rather large amount of points (ca. 20000), for every point it is checked whether or not the point is inside some cylinder (that cylinder is the same for every point). Furthermore, I would like to have the highest Y coordinate from the set of points. Since I have to do this calculation a lot, and it's quite slow, I want to use OpenMP to parallelize the loop. Currently I have (somewhat reduced):
#pragma omp parallel for default(shared) private(reducedCloudSize, reducedCloud, cylinderBottom, cylinderTop) reduction(+:ptsInside, ptsInsideLarger)
for (int i = 0; i < reducedCloudSize; i++){
highestYCoord = highestYCoord > testPt.y ? highestYCoord : testPt.y;
if (CylTest_CapsFirst(cylinderBottom,cylinderTop,cylinderHeight*cylinderHeight,cylinderRadius*cylinderRadius,testPt) != -1){
ptsInside++;
}
}
Where the CylTest_CapsFirst will check whether the point is inside of the cylinder. However, this code does not work. If I leave out the reduction(+:ptsInside, ptsInsideLarger) part it actually works, but is much slower than the non-parallelized version. If I include the reduction clause, the program never even seems to enter the for-loop!
What am I doing wrong?
Thanks!
highestYCoord. Fix it with critical or atomic. You also might want to rethink what variables you want shared and private. You only write tohighestYCoordandptsInsidein your loop. Those need to be shared. The others (except i) don't matter. Only i needs to be private (which it should be by construction). This assumes thatCylTest_CapsFirstonly read variables and does not write anything.reduction(max:highestYCoord)to avoid using a critical or atomic, resulting in cleaner and possibly faster code.