如何获得整个程序执行过程中可能创建的最大OpenMP线程数? [英] How can I get the maximum number of OpenMP threads that may be created during the whole execution of the program?
问题描述
我想创建一个对象的全局数组(每个可能的线程由OpenMP
生成一个对象)并在整个程序中重用它.每个线程将使用omp_get_thread_num
读取其编号,并将其用于索引数组.
I want to create one global array of objects (One object per possible thread spawned by OpenMP
) and reuse it throughout the program. Each thread will read its number using omp_get_thread_num
and use it to index into the array.
如何获取整个程序执行期间可能创建的最大OpenMP
个线程?
How can I get the maximum number of OpenMP
threads that may be created during the whole execution of the program?
omp_get_max_threads
的文档说返回一个特定于调用它的特定并行区域的值
The documentation of omp_get_max_threads
says that this function is specified to return a value which is specific to the particular parallel region where it was invoked
omp_get_max_threads
–并行区域的最大线程数
omp_get_max_threads
– Maximum number of threads of parallel region
描述:返回不使用子句num_threads的当前并行区域使用的最大线程数.
Description: Return the maximum number of threads used for the current parallel region that does not use the clause num_threads.
MSDN文档的措辞暗示由omp_get_max_threads
在并行区域之外的值与在其他任何点处返回的值相同.
Whereas the wording of MSDN documentation implies that the value returned by omp_get_max_threads
outside a parallel region is the same value that would be returned in any other point.
omp_get_max_threads
返回一个整数,该整数等于或大于如果在代码中的那个点定义了没有num_threads的并行区域时可用的线程数.
Returns an integer that is equal to or greater than the number of threads that would be available if a parallel region without num_threads were defined at that point in the code.
其中哪一个是正确的?
推荐答案
没有最大数量.
从技术上讲,OpenMP定义了称为nthreads-var
的内部控制变量(请参见 OpenMP 4.5 2.3.3),它是默认的线程数.您可以使用omp_get_max_threads
进行阅读,使用omp_set_num_threads
进行设置(不幸的命名错误),然后使用显式的num_threads
子句覆盖它.
Technically OpenMP defines an internal control variable called nthreads-var
(see OpenMP 4.5 2.3.3) that is sort of the default number of threads. You read it with omp_get_max_threads
, you set it with omp_set_num_threads
(an unfortunate naming glitch), and you override it with an explicit num_threads
clause.
因此,您将必须编写代码,使其能够处理意外数量的线程,例如通过将数组预定义为omp_get_num_threads()
并在有更多线程到达时延迟调整大小.或者进行合理的猜测并检查每次访问的索引范围.
So you will have to write your code such that it can cope with an unexpectedly number of threads, e.g. by predefining the array up to omp_get_num_threads()
and lazily resizing it if more threads arrive. Or take a reasonable guess and check the index bounds on each access.
这篇关于如何获得整个程序执行过程中可能创建的最大OpenMP线程数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!