BodneyC BodneyC - 6 months ago 42
C Question

OpenMP - Serialise for-loop in Parallel Region which Contains Parallel for-loop

I'm asking a very similar question to this chap but there is only one answer there and it hasn't proved useful for me.

I have a parallel region in

main()
with a for-loop which calls the function
new_value()
:

int main(int argc, char** argv)
{
omp_set_num_threads(4);

#pragma omp parallel default(shared) private(...)
{
for(int i = 0; i < MAX_VALUE; i++)
new_value(...);
}

return 0;
}


And
new_value()
looks like:

void new_value(...)
{
#pragma omp for schedule(static, chunk_width)
for(int i = 0; i < N; i++)
arr[i] = update();
}


The issue I am having is that I do not want the for-loop in
main()
to be parallelised (i.e. ran
MAX_VALUE * 4
times in total).

I essentially want that one loop to be ran serially but the for-loop in
new_value()
to be ran with four threads.

I tried
#pragma omp single
before the loop but it caused the program to deadlock or hang on something. I also tried the nested parallelism approach as suggest by the already linked thread but I got the same result.

Any solutions would be appreciated.

Answer Source

Well, you kind of overcomplicated it a little. Try this:

void new_value(...){
#pragma omp parallel for num_threads(4)
    for(int i = 0; i < N; i++)
        arr[i] = update();
}

int main(int argc, char** argv){
    for(int i = 0; i < MAX_VALUE; i++)
        new_value(...);
    return 0;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download