openMP early loop exit workaround?

I’m currently in the process of trying to convert some scientific code from serial to parallel in openMP. The code is used to calculate infinite sums, and the problem is the number of loops that need to be done before the sum is accurate to the proper number of significant figures is variable. So the loop could do 60 or 60,000 iterations.

Now in serial this is trivial with an if check and exit statement. However in openMP I’m having some trouble.

It appears I can’t make the end bound in the do loop shared and set it equal to the starting bound when the condition is met so that all the threads will kick out after they finish their current loop. And the EXIT statement, as well as DO WHILE don’t seem to be supported in openMP either.

Is there a way to do this? Because looking at the openMP guide I’m not seeing any nice workarounds or features which support this offhand.

Hi vibrantcascade,

From what you describe, this wont parallelize due to a loop dependency. i.e., iteration 2 can’t happen till iteration 1 is complete since iteration 2 doesn’t know if iteration 1 has exited or not.

However, can you change the algorithm so that you only check the exit condition after a certain number of iterations? If so, then you can add an inner parallel loop which computes the partial sum for this interval. Something like:

! Parallelize this loop
   do ix=i, (i*INTERVAL)
      ! do the partial sum
   end do
   sum = sum + partialSum
   if (sum .eq. someCondition)
end do
  • Mat

Yep that will work. I’ll have to change the end bound for the inner do loop to (i + INTERVAL - 1) I think, but other than that it would work well enough.

It would be really nice if there was something like a:
directive for this though where if I have 64 cores, it would do iterations 1 to 64 and then wait until they all hit the enddo and then check the while statement against the last iteration before starting the next 64. (Or only let a thread start the next sequential iteration after all the threads before it are done so it can check itself against the while statement.) Granted I can do the first one of those 2 with the code you gave me, but it would be a nice feature if you guys are looking to implement something new.

Thanks Mat!