- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[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!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[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]
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page