Signaling in OpenMP
I am writing computational code that more-less has the following schematic:
#pragma omp parallel
{
#pragma omp for nowait
// Compute elements of some array A[i] in parallel
#pragma omp single
for (i = 0; i < N; ++i) {
// Do some operation with A[i].
// This time it is important that operations are sequential. e.g.:
result = compute_new_result(result, A[i]);
}
}
Both computing A[i] and compute_new_result are rather expensive. So my
idea is to compute the array elements in parallel and if any of the
threads gets free, it starts doing sequential operations. There is a good
chance that the starting array elements are already computed and the
others will be provided by the other threads doing still the first loop.
However, to make the concept work I have to achieve two things:
To make OpenMP split the loops in alternative way, i.e. for two threads:
thread 1 computing A[0], A[2], A[4] and thread 2: A[1], A[3], A[5], etc.
To provide some signaling system. I am thinking about an array of flags
indicating that A[i] has already been computed. Then compute_new_result
should wait for the flag for respective A[i] to be released before
proceeding.
I would be glad for any hints how to achieve both goals. I need the
solution to be portable across Linux, Windows and Mac. I am writing the
whole code in C++11.
No comments:
Post a Comment