我使用了 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/