Ivan
Ivan

Reputation: 409

Openmp thread control in C

If I have a parallel section code, is it possible to assign a specific thread to each section, i.e. something of the sort

#pragma omp sections num_threads(2)
{
   #pragma omp section  //<---assigned to master thread
    {
      //do something
     }
    #pragma omp section  //<---assigned to the other thread
    {
      //do something
     }

}

Upvotes: 1

Views: 615

Answers (2)

Hristo Iliev
Hristo Iliev

Reputation: 74485

Assignment of OpenMP sections to threads is done in an implementation-dependent manner. The only directive that results in specific execution thread is master.

If you really need to give thread-specific work to each thread, use conditionals over the return value of omp_get_thread_num():

#pragma omp parallel num_threads(2)
{
   switch (omp_get_thread_num())
   {
      case 0:
         // Code for thread 0
         break;
      case 1:
         // Code for thread 1
         break;
      default:
         break;
   }
}

Upvotes: 3

Aizen
Aizen

Reputation: 581

For assigning to the master thread you can use

#pragma omp master

Upvotes: 0

Related Questions