C OpenMp在并行区域之前检测嵌套并行中的线程数
在并行区域启动之前,如何检测OpenMp中的线程数?如果使用嵌套并行,环境变量C OpenMp在并行区域之前检测嵌套并行中的线程数,c,nested,openmp,C,Nested,Openmp,在并行区域启动之前,如何检测OpenMp中的线程数?如果使用嵌套并行,环境变量OMP\u NUM\u THREADS看起来像4,64 get_nested_num_threads(); #pragma omp parallel { // starting 4 threads #pragma omp parallel { // starting 64 threads for each of the 4 } } 导致我使用以下代码实现查询OMP\u NUM\u线程: #incl
OMP\u NUM\u THREADS
看起来像4,64
get_nested_num_threads();
#pragma omp parallel
{
// starting 4 threads
#pragma omp parallel
{
// starting 64 threads for each of the 4
}
}
导致我使用以下代码实现查询OMP\u NUM\u线程
:
#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;
}
#包括
#包括
int get_nested_num_threads(){
字符分隔符[]=“,”;
char*ptr=NULL;
char*num_threads=NULL;
num_threads=getenv(“OMP_num_threads”);
int线程=1,嵌套线程=0;
ptr=strtok(num_线程,分隔符);
while(ptr!=NULL){
线程*=atoi(ptr);
ptr=strtok(空,分隔符);
嵌套+=1;
}
assert(nested我已经解决了这个问题,通过打开一个嵌套的并行区域来查询所有线程:
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。如果您担心创建和释放并行区域资源的开销,您可以在intel机器上将KMP_BLOCKTIME
的值设置为200毫秒。