Does each thread stay the same number when using openMP multiple times throughout program?

In the beginning of my program let's say I get the number of threads and assign an ID to them so I can easily assign each of them a task like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma omp parallel
        {
          if(omp_get_thread_num()==0){
            numThreads=omp_get_num_threads();
          }
        }
    
    #pragma omp parallel num_threads(numThreads)
      {
           int myID=omp_get_thread_num();
           //do stuff
      }
    #pragma omp barrier 


After using the omp barrier I need the cpu to do some work so I don't want to be in openMP anymore. Then a different part of the program comes up where I need each individual thread.

Do I need to reassign an ID to each thread again like this:
1
2
3
4
5
6
#pragma omp parallel num_threads(numThreads)
      {
           int myID=omp_get_thread_num();
           //do stuff
      }
    #pragma omp barrier 


or does each thread still remember which ID it was so I can simply do this:
1
2
3
4
5
#pragma omp parallel num_threads(numThreads)
      {
           //do stuff
      }
    #pragma omp barrier 
Last edited on
myID will not exist the second time you enter a parallel block, because it was declared in a different block. So yes, you have to call omp_get_thread_num() again, but it has nothing to do with the thread not "remembering" its ID. Every time you enter a new parallel block you're creating a new group of threads, independent of earlier ones.
Topic archived. No new replies allowed.