如何在并行区域启动之前检测OpenMp中的线程数?如果我使用嵌套并行性,则环境变量OMP_NUM_THREADS
看起来像4,64
。
get_nested_num_threads();
#pragma omp parallel
{
// starting 4 threads
#pragma omp parallel
{
// starting 64 threads for each of the 4
}
}
This answer导致我使用以下代码查询OMP_NUM_THREADS
:
#include <string.h>
#include <stdlib.h>
int get_nested_num_threads(){
char delimiter[] = ",";
char *ptr = NULL;
char *num_threads = NULL;
num_threads = getenv("OMP_NUM_THREADS");
int threads=1, nested=0;
ptr = strtok(num_threads, delimiter);
while ( ptr != NULL ){
threads *= atoi(ptr);
ptr = strtok(NULL,delimiter);
nested += 1;
}
assert( nested <= 2 );
return threads;
}
不幸的是,如果我拨打getenv("OMP_NUM_THREADS")
,我会发现4,4
而不是4,64
的嵌套并行性。这对我来说真的很奇怪。你对此有解释吗?
答案 0 :(得分:0)
我已经通过打开一个嵌套的并行区域来查询所有线程来解决它:
int get_nested_num_threads(){
int threads=1;
#pragma omp parallel shared(threads)
{
#pragma omp single
{
threads = omp_get_num_threads();
#pragma omp parallel shared(threads)
{
#pragma omp single
{
threads *= omp_get_num_threads();
}
}
}
}
return threads;
}
据我所知,在这种情况下,您无需在C中使用firstprivate
和lastprivate
。但你必须在Fortran中做到这一点。