C OMP omp_get_wtime() 返回时间 0.00

标签 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(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); 
}

最佳答案

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

#include <omp.h>

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

这个库将在编译中删除这个警告:

warning: implicit declaration of function ‘omp_get_wtime’ [-Wimplicit-function-declaration]

时间会正确显示。

关于C OMP omp_get_wtime() 返回时间 0.00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16777810/

相关文章:

java - 对 AsyncTask 中的 UI 操作进行故障排除(OnProgressDialog、Toasts)

Java notify() 在 wait() 之前运行?

multithreading - C++11 用 clang 替代 OpenMP

c++ - 我无法通过程序的并行化进行优化

c - 匿名联盟的目的是什么?

c++ - 使用 fork() 的子和父 pid;

c++ - CUrl(MSVS 库)还是 libcurl?

c - C 中的字符串分词器

c++ - 多线程 galib247 遗传算法卡在局部最大值

c++ - 为什么ubuntu 12.04下的OpenMP比串口版慢