helloworld
helloworld

Reputation: 31

How to run a static parallel for loop without the main thread

I want to execute a funtion with multithreads, without using main thread. So this is what I want:

  # pragma omp parallel num_threads(9)
  {
     // do something
     # pragma omp for schedule(static,1)
     for(int i = 0; i < 10; i++)
        func(i); // random stuff
  }

So I want func() to be executed just by 8 threads, without main thread. Is that possible somehow?

Upvotes: 1

Views: 620

Answers (2)

paleonix
paleonix

Reputation: 3031

If it isn't important which thread doesn't do the loop, another option would be to combine sections with the loop. This means nesting parallelism, which one should be very careful with, but it should work:

#pragma omp parallel sections num_threads(2)
{
    #pragma omp section
    { /* work for one thread */ }
      #pragma omp section
      {
          #pragma omp parallel for num_threads(8) schedule(static, 1)
          for (int i = 0; i < N; ++i) { /* ... */ }
      }
}

The main problem here is, that most likely one of those sections will be taking much longer than the other one, meaning that in the worst case (loop faster than first section) all but one thread are doing nothing most of the time.

If you really need the master thread to be outside the parallel region this might work (not tested):

#pragma omp parallel num_threads(2)
{
    #pragma omp master
    { /* work for master thread, other thread is NOT waiting */ }
        #pragma omp single
        {
            #pragma omp parallel for num_threads(8) schedule(static, 1)
            for (int i = 0; i < N; ++i) { /* ... */ }
        }
}

There is no guarantee that the master thread wont be computing the single region as well, but if your cores aren't over-occupied it should at least be unlikely. One could even argue that if the second thread from the outer parallel region doesn't reach the single region in time, it is better that the master thread also has a chance of going in there, even if that means, that the second thread doesn't get anything to do.

As the single region should only have an implicit barrier at it's end, while the master region doesn't contain any implicit barriers, they should potentially be executed in parallel as longs as the master region is in front of the single region. This assumes that the single region is well-implemented, such that every thread has a chance of computing it. This isn't guaranteed by the standard, I think.

EDIT: These solutions require nested parallelism to work, which is disabled by default in most implementations. It can be activated via the environment variable OMP_NESTED or by calling omp_set_nested().

Upvotes: 1

dreamcrash
dreamcrash

Reputation: 51393

So I want func() to be executed just by 8 threads, without main thread. Is that possible somehow?

Yes, you can do it. However, you will have to implement the functionality of

#pragma omp for schedule(static,1) 

since, explicitly using the aforementioned clause will make the compiler automatically divide the iterations of the loop among the threads in the team, including the master thread of that team, which in your code example will be also the main thread. The code could look like the following:

# pragma omp parallel num_threads(9) 
{
     // do something
      int thread_id = omp_get_thread_num();
      int total_threads = omp_get_num_threads();
      if(thread_id != 0) // all threads but the master thread
      {
        thread_id--; // shift all the ids
        total_threads = total_threads - 1;
        for(int i = thread_id ; i < 10; i += total_threads)
            func(i); // random stuff
      }
      #pragma omp barrier
} 

First, we ensure that all threads except the master executed the loop to be parallelized (i.e., if(thread_id != 0)), then we divided the iterations of the loop among the remaining threads (i.e., for(int i = thread_id ; i < 10; i += total_threads)), and finally we ensure that all threads wait for each other at the end of the parallel region (i.e., #pragma omp barrier).

Upvotes: 1

Related Questions