【问题标题】:OpenMP with C and gcc omp_set_num_threads() has no effectOpenMP 与 C 和 gcc omp_set_num_threads() 没有效果
【发布时间】:2018-03-13 17:52:22
【问题描述】:

我正在学习 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 个线程,但 有时 4. 我找不到任何规律性的时间或原因,但一项研究表明,OMP_DYNAMIC=true 允许 OpenMP 自行选择最佳线程数。

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

【问题讨论】:

    标签: c multithreading gcc openmp


    【解决方案1】:

    在实际使用 #pragma omp parallel 之前,请致电 omp_set_num_threads(4);

    【讨论】:

    • 确实有效,谢谢!知道为什么#pragma omp num_threads(4) 没有吗?是因为 OMP_DYNAMIC=true 吗?或者为什么我会收到未使用的变量警告?
    • 您是否也尝试将其放在#pragma omp parallel 之前?
    • 确实有效。对我来说似乎很奇怪,因为大多数教程都在同一行中进行...最后,您对我为什么收到未使用的变量警告有任何意见吗?我清楚地使用了数组,即使在顺序部分也是如此。
    • 其实我设置了OMP_DYNAMIC=false,现在代码不能正常工作,除非num_threads(4)#pragma omp parallel在同一行
    • 您收到未使用变量警告,因为数组元素值已分配,但从未在代码中读取。
    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多