问题
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 regionDescription: 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