How can I get the maximum number of OpenMP threads that may be created during the whole execution of the program?

依然范特西╮ 提交于 2019-12-10 22:58:51

问题


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.

How can I get the maximum number of OpenMP threads that may be created during the whole execution of the program?

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 – Maximum number of threads of parallel region

Description: Return the maximum number of threads used for the current parallel region that does not use the clause num_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

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.

Which one of them is correct?


回答1:


There is no maximum number.

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.

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.



来源:https://stackoverflow.com/questions/37307627/how-can-i-get-the-maximum-number-of-openmp-threads-that-may-be-created-during-th

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!