1

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
     }

}

2 Answers 2

3

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;
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Does num_threads(2) guarantee that you will get thread 0 and 1, not thread 2 and 3?
Threads in OpenMP parallel regions are always numbered starting from 0.
0

For assigning to the master thread you can use

#pragma omp master

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.