Home:ALL Converter>Assigning different thread numbers in OpenMP do-loops

Assigning different thread numbers in OpenMP do-loops

Ask Time:2013-07-28T01:47:21         Author:tiki

Json Formatter

I have two do-loops inside OpenMP parallel region as follows:

!$OMP PARALLEL
...
!$OMP DO
...
!$OMP END DO
...
!$OMP DO
...
!$OMP END DO
...
!$OMP END PARALLEL

Let's say OMP_NUM_THREADS=6. I wanted to run first do-loop with 4 threads, and the second do-loop with 3 threads. Can you show how to do it? I want them to be inside one parallel region though. Also is it possible to specify which thread numbers should do either of the do-loops, for example in case of first do-loop I could ask it to use thread numbers 1,2,4, and 5. Thanks.

Author:tiki,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17900751/assigning-different-thread-numbers-in-openmp-do-loops
High Performance Mark :

Well, you can add the num_threads clause to an OpenMP parallel directive but that applies to any directive inside the region. In your case you could split your program into two regions, like \n\n!$OMP PARALLEL DO num_threads(4)\n...\n!$OMP END PARALLEL DO\n...\n!$OMP PARALLEL DO num_threads(3)\n...\n!$OMP END PARALLEL DO\n\n\nThis, of course, is precisely what you say you don't want to do, have only one parallel region. But there is no mechanism for throttling the number of threads in use inside a parallel region. Personally I can't see why anyone would want to do that.\n\nAs for assigning parts of the computation to particular threads, again, no, OpenMP does not provide a mechanism for doing that and why would you want to ?\n\nI suppose that I am dreadfully conventional, but when I see signs of parallel programs where the programmer has tried to take precise control over individual threads, I usually see a program with one or more of the following characteristics:\n\n\nOpenMP directives are used to ensure that the code runs in serial with the result that run time exceeds that of the original serial code;\nthe program is incorrect because the programmer has failed to deal correctly with the subtleties of data races;\nit has been carefully arranged to run only on a specific number of threads.\n\n\nNone of these is desirable in a parallel program and if you want the level of control over numbers of threads and the allocation of work to individual threads you will have to use a lower-level approach than OpenMP provides. Such approaches abound so giving up OpenMP should not limit you.",
2013-07-27T19:53:29
yy