C OMP OMP_get_wtime()返回时间0.00

C OMP OMP_get_wtime()返回时间0.00,c,multithreading,openmp,C,Multithreading,Openmp,我使用了omp_get_wtime(),但是当我想要打印时间时,我总是得到0.00,问题出在哪里 #define SIZE 500 #define nthreads 10 (...) void sumTab(int mX[][SIZE], int mY[][SIZE], int mZ[][SIZE]) { int i,k; double start = omp_get_wtime(); #pragma omp parallel for schedule(dynamic,3) private(

我使用了omp_get_wtime(),但是当我想要打印时间时,我总是得到0.00,问题出在哪里

#define SIZE 500
#define nthreads 10

(...)

void sumTab(int mX[][SIZE], int mY[][SIZE], int mZ[][SIZE]) {
int i,k;
double start = omp_get_wtime();
#pragma omp parallel for schedule(dynamic,3) private(i) num_threads(nthreads)
for(i=0 ; i<SIZE ; i++)
{

   for(k=0 ; k<SIZE ; k++)  
   {

     mZ[i][k]=mX[i][k]+mY[i][k];
     printf("Thread no %d \t  [%d] [%d] result: %d\n", omp_get_thread_num(),i,k, mZ[i][k]); 
     }
}

printf("Time: \t %f \n", omp_get_wtime()-start); 
}
#定义尺寸500
#定义第10行
(...)
void sumTab(int mX[][SIZE]、int mY[][SIZE]、int mZ[][SIZE]){
int i,k;
双启动=omp_get_wtime();
#用于调度(动态,3)私有(i)num_线程(n线程)的pragma omp并行

对于(i=0;i声明程序结束的时间,然后取开始时间和结束时间之间的差值,输出差值。这应该可以解决问题,就像我几个月前做的类似操作一样

声明程序结束的时间,然后取开始时间和结束时间之间的差值,输出这应该可以解决问题,就像我几个月前做的一样

 THis is what  your code should look like:
 double  dif;
 double start = omp_get_wtime( ); //start the timer
 //beginning of computation
 ..
 ...
//end of computation
    double end = omp_get_wtime();// end the timer
   dif = end - start // stores the difference in dif
  printf("the time of dif is %f", dif);
 //this should point you in the way

确保在文件头中包含omp.h库

#include <omp.h>

double start_time = omp_get_wtime();
#pragma omp parallel [...]
// code
double time = omp_get_wtime() - start_time;

时间会显示正确。

我有同样的问题,而StCudio在C++中做了这个技巧。 您可以在c中使用以下代码。为了查看差异,您必须以高精度打印结果

double exec_time;
double start = omp_get_wtime();
//beginning of computation
...
//end of computation
double end = omp_get_wtime();
exec_time = end - start;
printf("the time difference is %15.15f", exec_time);

您的例程可能太快,无法解析
omp\u get\u wtime
。如果您只想测量时间,而不关心mZ的最终内容,您可以重复多次测试,并将最终数字除以重复次数:

#define REPS 1024
...
...

double acumtime = 0.0;
for (rep = 0; rep < REPS; rep++)
{
  double start = omp_get_wtime();
  #pragma omp parallel for schedule(dynamic,3) private(i) num_threads(nthreads)
  for(i=0 ; i<SIZE ; i++)
  {
    for(k=0 ; k<SIZE ; k++)  
    {
      mZ[i][k]=mX[i][k]+mY[i][k];
      printf("Thread no %d \t  [%d] [%d] result: %d\n", omp_get_thread_num(),i,k, mZ[i][k]); 
    }
  }
  acumtime += omp_get_wtime()-start; 
}
printf ("Elapsed time is: %f\n", acumtime/REPS);
#定义代表1024
...
...
双acumtime=0.0;
对于(rep=0;rep因为(i=0;i@mcleod_ideafix在写printf的抑制时是正确的。

您肯定应该从循环中删除对printf函数的调用,因为它可能会严重扭曲结果。请记住,对printf的调用在某些阶段会涉及到转换到内核模式,这本身就CPU周期而言是一项代价高昂的操作。

尝试使用“%g”打印这就剩下了科学的表示法。

这是因为将双精度转换为浮点数时的精度损失

尝试使用格式说明符%ld而不是“%f”来打印时间


程序执行所需的时间以毫秒为单位,或者可能少于毫秒。

可能是
omp\u get\u wtime
的精度不够高?您使用了哪些命令来编译和运行此程序?我使用的是make命令和OPT=-O3-fopenmpt。您编写的和他说的没有任何区别。它们是相等的。
#define REPS 1024
...
...

double acumtime = 0.0;
for (rep = 0; rep < REPS; rep++)
{
  double start = omp_get_wtime();
  #pragma omp parallel for schedule(dynamic,3) private(i) num_threads(nthreads)
  for(i=0 ; i<SIZE ; i++)
  {
    for(k=0 ; k<SIZE ; k++)  
    {
      mZ[i][k]=mX[i][k]+mY[i][k];
      printf("Thread no %d \t  [%d] [%d] result: %d\n", omp_get_thread_num(),i,k, mZ[i][k]); 
    }
  }
  acumtime += omp_get_wtime()-start; 
}
printf ("Elapsed time is: %f\n", acumtime/REPS);
printf("the time of dif is %lf", dif);