Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.

omp_in_parallel() problem

unrue
Beginner
1,141 Views
Hi all,
i'm using Intel 11.1 with a simple openmp code. i've noted that omp_in_parallel returns 0 if i'm using just one thread:
[cpp]int in_parallel;


#pragma omp parallel num_threads(1)
{

in_parallel=omp_in_parallel();
printf(" in_parallel: %d \n", in_parallel);

}


[/cpp]
Why? I'm using one thread, but i'm in a parallel region!
Thanks.
0 Kudos
8 Replies
ClayB
New Contributor I
1,141 Views
I took a look at the OpenMP Specification 3.0 and found this explanation of the omp_in_parallel() function:

The omp_in_parallel routine returns true if the call to the routine is enclosed by an active parallel region; otherwise, it returns false.

No mention about the number of threads executing that parallel region.Your source code does have the call enclosed within a parallel region.

I tried my own test code with parallel regions usingthe default number of threads, 7 threads, and 1 thread. I also put a call to omp_in_parallel outside a parallel region. All results were as expected except the region with one thread.It reported FALSE from omp_in_parallel(). This is obviously a bug in the compiler. I'll submit a bug report.

(I can only guess that the number of threads are used to determine this return value. It must have been thought that no one would use a parallel region with one thread and need to know if the code was executing from a parallel region.)

0 Kudos
Olga_M_Intel
Employee
1,142 Views
I took a look at the OpenMP Specification 3.0 and found this explanation of the omp_in_parallel() function:

The omp_in_parallel routine returns true if the call to the routine is enclosed by an active parallel region; otherwise, it returns false.
...

OpenMP Specfication 3.1 contains the following definition of the active parallel region:
"parallel region that is executed by a team consisting of more than one thread".
So, in the example omp_in_parallel() is called from non-active parallel region and the result is correct.

0 Kudos
ClayB
New Contributor I
1,143 Views
Yep. The definition of an "active parallel region" appears earlier in the spec (1.2.2?). This interpretation is supported by the Intel compiler version 12.0 update #5, and g++ 4.6.0.

Even so, this seems an odd way to define active vs. inactive since the programmer can set a region to be executed by a single thread (based on work available) or multiple threads. Also, what happens if there should be a call to omp_in_parallel() from a master region? There would only be one thread executing the call, but there might be more threads running in the overall parallel region.

Maybe more to the point would be if nested parallelism resulted in a nested region running only a single thread in the team. Would that be an active or inactive parallel region?

Maybe a better function would be omp_in_active() with this functionality and omp_in_parallel() to return true if the call is made from an executing parallel region regardles of the number of threads?
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,143 Views
IMHO if a user declares a parallel region, regardless of number of threads, the in parallel test should report true. Reason being is you may be in nested regions and/or using the OpenMP 3.n task sets (one thread per task). These tasks may be invoked within a parallel region. Therefore imho, the number of members in a team is not a controlling factor as if you are in a parallel region or not. Also consider dynamic scheduling where the number of threads that actually execute the code (possibly 1) is not necessarily the anticipated number of threads.

As an alternative to calling a omp_in_parallel() you can call omp_get_num_threads() and if the return is .le. 1 then you are either outside parallel regions or in a region with but one thread in the team. Caution though since other teams may be running and you will not necessarily have exclusive use of all resources.

Jim Dempsey
0 Kudos
unrue
Beginner
1,142 Views
Do you know other way to have inside or not parallel region information not using OpenMP directives?
With ppid for example? Or others posix threads routines.
0 Kudos
unrue
Beginner
1,142 Views
Well,
I found the solution:
simply, I used omp_get_level(). By definition, it works with active and inatcive parallel region. So:
- if omp_get_num_threads=1 andomp_get_level()=0, I'm in a serial region.
- ifomp_get_num_threads=1 andomp_get_level()=1 I'm in a parallel region with only one thread.
- ifomp_in_parallel()=1 I'm in a parallel region with more than one thread.
It works well and it's portable.
0 Kudos
jose-jesus-ambriz-me
1,142 Views
unrue: please, could you write the final code with your solution?
thanks for all.
jam
0 Kudos
unrue
Beginner
1,142 Views
This is my solution:
[cpp]int my_omp_in_parallel(){

        int result = 0;

        // serial region
        if( ((omp_get_num_threads()==1)&&( omp_get_level()==0)))
                result = 0;

        // parallel region with one thread
        if( (omp_get_num_threads()==1)&&( omp_get_level()==1) )
                result = 1;

        // parallel region with more than one thread
        if( omp_in_parallel()==1 )
                result = 1;

        return result;

}
[/cpp]
But it works only with OpenMP 3.0, because omp_get_levels() is undefined in OpenMP 2.5.
0 Kudos
Reply