OpenMP® Forum

Discussion on the OpenMP specification run by the OpenMP ARB. OpenMP and the OpenMP logo are registered trademarks of the OpenMP Architecture Review Board in the United States and other countries. All rights reserved.

nivaldo wrote:sorry, I'm learning OpenMP on my own, and wanted to see an example.

There's no way to do this directly without modifying the code. A simple solution would be to split the loop into two, the first loop calling calculateValue() and storing the results in a temporary array, and a second loop using these values to compute array[]. The first loop can then be parallelised easily with a #pragma omp parallel for directive. The second loop has to remain sequential. This would be a reasonable solution if calculateValue() is expensive and n is not too large. A more sophisticated solution would be to implement a parallel prefix sum algorithm.

Thanks for the help, I'm new to openmp and at my university do not know anyone who can parallel programming. I'm working on an application that identifies the faces of a figure from a 2D sketch to take to 3d. The identification algorithm that implements faces a maximum weight clique but the execution time of some cases is very large. So I want to parallelize the code using openmp, and I have a part of code that is similar to that cycle, when using openmp considerably low time, but since there is no data dependency executes the code correctly. I was reading about the prefix sum algorithm, but do not really understand, you believe that with this algorithm can lower the execution time of this cycle although n is a value big enough?

For sufficiently large N, the parallel prefix algorithm will be faster that the sequential version. The benefit of parallelism has to outweigh the overheads of starting a parallel region and synchronising the threads: a very rough estimate suggests that sufficiently large probably means N > 10^6 for a typical OpenMP implementation.