Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Chris_Szalwinski
New Contributor I
343 Views

OpenMP WorkSharing No of Threads

Jump to solution

Hi,

Is there any way to determine the actual number of threads executing in a C++ program during an omp parallel for construct?

How can I tell if the number set by omp_set_num_threads()  differs from the number returned by omp_get_num_threads()?

Thanks,

Chris

0 Kudos
1 Solution
AbhishekD_Intel
Moderator
326 Views

Hi Chris,

 

Thanks for reaching out to us.

It is possible to determine the number of threads executing in omp parallel region. You can use the omp_get_num_threads() function in the parallel region to get the total number of threads your omp parallel region is using.

You can also assign the total number of threads to your parallel region using the num_threads(<value>) function.

 

The omp_set_num_thread(<value>) is runtime function to request certain number(value) of threads that you parallel region will use. Whereas the omp_get_num_threads() function will show you the number of threads used. So as long as you don't change the value of total thread the parallel region ( omp_get_num_threads() ) will always give the same value as that of omp_set_num_thread(<value>).

 

For more details, please refer to the below code.

 

#include <omp.h>

int main(){

        omp_set_num_threads(4);
        printf("%d\n",omp_get_num_threads());  //This will always return 1 as its not present in parallel region.
        #pragma omp parallel
        {
                int total_td = omp_get_num_threads();  // This will always give the total number of threads used in parallel region.
                #pragma omp for
                for(int i=0;i<5;i++){
                        printf("Iteration %d is processed by %d/%d thread\n", i, omp_get_thread_num(), total_td);
                }
        }
        printf("\n");
         #pragma omp parallel num_threads(3)
         {
                int total_td = omp_get_num_threads();  // This will always give the total number of threads used in parallel region.
                #pragma omp for
                for(int i=0;i<5;i++){
                        printf("Iteration %d is processed by %d/%d thread\n", i, omp_get_thread_num(), total_td);
                }
         }
        return 0;
}

 

Warm Regards,

Abhishek

 

 

View solution in original post

2 Replies
AbhishekD_Intel
Moderator
327 Views

Hi Chris,

 

Thanks for reaching out to us.

It is possible to determine the number of threads executing in omp parallel region. You can use the omp_get_num_threads() function in the parallel region to get the total number of threads your omp parallel region is using.

You can also assign the total number of threads to your parallel region using the num_threads(<value>) function.

 

The omp_set_num_thread(<value>) is runtime function to request certain number(value) of threads that you parallel region will use. Whereas the omp_get_num_threads() function will show you the number of threads used. So as long as you don't change the value of total thread the parallel region ( omp_get_num_threads() ) will always give the same value as that of omp_set_num_thread(<value>).

 

For more details, please refer to the below code.

 

#include <omp.h>

int main(){

        omp_set_num_threads(4);
        printf("%d\n",omp_get_num_threads());  //This will always return 1 as its not present in parallel region.
        #pragma omp parallel
        {
                int total_td = omp_get_num_threads();  // This will always give the total number of threads used in parallel region.
                #pragma omp for
                for(int i=0;i<5;i++){
                        printf("Iteration %d is processed by %d/%d thread\n", i, omp_get_thread_num(), total_td);
                }
        }
        printf("\n");
         #pragma omp parallel num_threads(3)
         {
                int total_td = omp_get_num_threads();  // This will always give the total number of threads used in parallel region.
                #pragma omp for
                for(int i=0;i<5;i++){
                        printf("Iteration %d is processed by %d/%d thread\n", i, omp_get_thread_num(), total_td);
                }
         }
        return 0;
}

 

Warm Regards,

Abhishek

 

 

View solution in original post

AbhishekD_Intel
Moderator
302 Views

Hi Chris,


Thank you for the confirmation. As your issue is resolved we will no longer monitor this thread.

Please post a new thread if you have any issues.



Warm Regards,

Abhishek


Reply