0

So far I have been using omp only for large cycles as it is very easy to write and these cycles consumes most of the time. However, sometimes I need to do some I/O which can be efficiently done only in one thread but usualy this I/O is independent on (next) loop.

I need to do something like this:

print_something(); // independet
print_something_else(); // independent
for(...){...}; // large cycle independent on previous printing

How to use omp to execute print_something in one thread, print_something_else in second thread and use all the remaining threads to compute the loop? And because the loop will very probably take more time than execution of print functions, how to add the two threads which were doing the I/O into the loop after they are finished?

Would something like this work?

#pragma omp parallel
{
    #pragma omp sections
    {
        #pragma omp section
        {
            print_something();
        }
        #pragma omp section
        {
            print_something_else();
        }
    }
    #pragma omp for
    for(...){...};
}
Michal
  • 671
  • 3
  • 9
  • 22

1 Answers1

2

The #pragma omp section has an implicit barrier, so the application will wait for the sections to finish before running the #pragma omp for. One alternative would be to add the nowait clause in the pragma omp section. So this would be an alternative to run the code:

#pragma omp parallel
{
    #pragma omp sections nowait
    {
        #pragma omp section
        {
            print_something();
        }
        #pragma omp section
        {
            print_something_else();
        }
    }
    #pragma omp for
    for(...){...};
}

This approach, has a problem, though. The #pragma omp for may use the static scheduling and that means that it will distribute the work on the for-loop among the team of threads, and that includes those that are running the sections. In that case, the parallel for will have to wait for the sections to complete. One way to address is to use a different scheduling on the for loop (e.g. dynamic or guided).

If your compiler supports OpenMP task constructs, I believe that tasks would allow you expressing better this irregular parallelism you're looking for. This other question in SO has a good answer on the difference between the sections and tasks.

Harald
  • 3,110
  • 1
  • 24
  • 35
  • Can you please explain more how to use task constrcuts? I`m having difficulty in understanding them properly – Michal Oct 26 '17 at 14:51