Home > Software design >  what happens when omp num_threads (more than 1) and parallel for with only 1 loop is present
what happens when omp num_threads (more than 1) and parallel for with only 1 loop is present

Time:03-31

l_thread = 4;
max = 1; //4
#pragma omp parallel for num_threads(l_thread) private(i)
 for(i=0;i<max;i  )
   ;//some operation

in this case, 4 threads will be created by omp. I want to know if the for loop which loops only 1 time(in the case), will be taken only by one of 4 threads right? and other threads will be idle state ? and in this case i am seeing cpu usage of 4 threads are nearly same. What might be the reason? only one thread should be high and others must be low right?

CodePudding user response:

Your take on this is correct. If max=1 and you have more than one thread, thread 0 will execute the single loop iteration and the other threads will wait at the end of the parallel region for thread 0 to catch up. The reason you're seeing the n-1 other threads causing load on the system is because they spin-wait at the end of regions, because that is much faster when the threads have to wake up and notice that the parallel work (or in your case: not so parallel work :-)) is completed.

You can change this behavior via the OMP_WAIT_POLICY environment variable. See the OpenMP specification for a full description.

  • Related