gpt4 book ai didi

c - 带有 C 和 gcc 的 OpenMP omp_set_num_threads() 没有效果

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:04 25 4
gpt4 key购买 nike

我正在学习 OpenMP 和 C,但在使用简单程序时遇到了一些问题。

我在我的 bashrc 中设置了以下环境变量:

define how many threads you want
export OMP_NUM_THREADS=4

#allow to switch number of threads
export OMP_DYNAMIC=true

#allow nested parallel regions
export OMP_NESTED=true

这是我要运行的程序:

#include <stdio.h>      /* input, output    */
#include <omp.h> /* openMP library */
#include <time.h> /* measure time */

#define N 100000000 // if sourcearray not static, I'll be overflowing the stack.
// > ~10^6 elements is a lot for most systems.



void forloop(void);


int
main(void)
{

/* worksharing: for loop */
forloop();

return(0);
}

/*=============================================================*/
/*=============================================================*/

void forloop(void){
/*do a for loop sequentially and in parallel; measure each times */


printf("=====================\n");
printf("FOR LOOP\n");
printf("=====================\n\n");

long i;
clock_t start, end;
double cpu_time_used;

static double sourcearray[N];


/*============*/
/*measure time*/
/*============*/

start=clock();

for (i=0; i<N; i++){
sourcearray[i] = ((double) (i)) * ((double) (i))/2.2034872;
}

end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

printf("Non-parallel needed %lf s\n", cpu_time_used);




/*===============*/
/*parallel region*/
/*===============*/

#pragma omp parallel
/*need to specify num_threads, when OMP_DYNAMIC=true to make sure 4 are used.*/
{
omp_set_num_threads(4);

double starttime_omp, endtime_omp;
/*time measurement*/
starttime_omp=omp_get_wtime();

int procs, maxt, nt, id;

procs = omp_get_num_procs(); // number of processors in use
maxt = omp_get_max_threads(); // max available threads
nt = omp_get_num_threads();
id = omp_get_thread_num();

printf("num threads forloop %d from id %d, procs: %d, maxthrds: %d\n", nt, id, procs, maxt);


#pragma omp for
for (i=0; i<N; i++){
sourcearray[i] = ((double) (i)) * ((double) (i))/2.2034872;
}


endtime_omp = omp_get_wtime();
cpu_time_used = ((endtime_omp - starttime_omp)) ;

} /* end parallel region */


}

我编译代码 gcc -g -Wall -fopenmp -o omp_worksharing.exe omp_worksharing.c

程序编译时出现一个我不太明白的警告:

omp_worksharing.c: In function ‘forloop’:
omp_worksharing.c:78:17: warning: variable ‘sourcearray’ set but not used [-Wunused-but-set-variable]
static double sourcearray[N];

但这不是主要问题:

问题是程序没有启动 4 个线程。这是输出:

=====================
FOR LOOP
=====================

Non-parallel needed 0.900340 s
num threads forloop 3 from id 0, procs: 8, maxthrds: 4
num threads forloop 3 from id 1, procs: 8, maxthrds: 4
num threads forloop 3 from id 2, procs: 8, maxthrds: 4

当我使用 #pragma omp num_threads(4) 而不是 omp_set_num_threads(4);

时也会发生同样的情况

更奇怪的是,当我同时省略 #pragma omp num_threads(4)omp_set_num_threads(4); 时,大多数时候会启动 3 个线程,但是 < strong>有时 4. 我找不到任何规律性,何时或为什么,但一项研究表明,OMP_DYNAMIC=true 允许 OpenMP 自行选择最佳线程数。

为什么我不能指定要使用的线程数?

最佳答案

在实际使用 #pragma omp parallel 之前调用 omp_set_num_threads(4);

关于c - 带有 C 和 gcc 的 OpenMP omp_set_num_threads() 没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46529242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com