C语言 C OMP omp_get_wtime() 返回时间 0.00
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16777810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C OMP omp_get_wtime() returning time 0.00
提问by kxyz
I have used a omp_get_wtime() but when i want to print the time i always get 0.00, where is the problem ?
我使用了 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);
}
回答by zhalk
Make sure you include the omp.h library in the header of the file.
确保在文件头中包含 omp.h 库。
#include <omp.h>
double start_time = omp_get_wtime();
#pragma omp parallel [...]
// code
double time = omp_get_wtime() - start_time;
This library will remove this warning in compilation:
该库将在编译中删除此警告:
warning: implicit declaration of function ‘omp_get_wtime' [-Wimplicit-function-declaration]
And the time will show correctly.
并且时间会正确显示。
回答by rodolf
Try to print with "% g" that leaves it as scientific notation.
尝试使用 "% g" 打印,将其保留为科学记数法。
回答by Vilas Joshi
This is because of precision lossin converting the double to float.
这是因为在将双精度数转换为浮点数时精度损失。
Try to print time with format specifier "%ld"for doubleinsted of "%f".
尝试使用格式说明符“%ld”为“%f”的双重插入打印时间。
printf("the time of dif is %lf", dif);
The program execution takes time in milliseconds or maybe less than that.
程序执行需要以毫秒为单位的时间,或者可能少于这个时间。
回答by Oyinlade Olumide
Declare the time where the program ends, after which you take the difference between the start time and the end time, output the difference. that should solve it as i did something similar some months ago
声明程序结束的时间,之后取开始时间和结束时间的差值,输出差值。这应该可以解决它,因为我几个月前做了类似的事情
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
回答by Oyinlade Olumide
Declare the time where the program ends, after which you take the difference between the start time and the end time, output the difference. that should solve it as i did something similar some months ago
声明程序结束的时间,之后取开始时间和结束时间的差值,输出差值。这应该可以解决它,因为我几个月前做了类似的事情
回答by Sumith Yesudasan
I had this same problem and while setprecision did the trick in c++, you can use the following code in c. In order to see the difference you have to print the results with high precision.
我遇到了同样的问题,虽然 setprecision 在 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);
回答by mcleod_ideafix
Your routine could be too fast for the resolution of omp_get_wtime. If you only want to measure the time and don't care about the final contents of mZ, you can repeat the test a number of times and divide the final number by the number of repetitions:
您的例程对于omp_get_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);
You may also want to supress printf'sinside the parallel block, as this might be a severe cause of slowdown.
您可能还想printf's在并行块内部进行抑制,因为这可能是导致速度下降的严重原因。
回答by beniekg
@mcleod_ideafix was right when he wrote about the suppression of printf. You should definitely remove the call to printf function from within the loop as it may skew the result greatly. Bear in mind that call to printf will at some stage involve transition to kernel mode and this itself is costly operation in terms of CPU cycles.
@mcleod_ideafix 在写关于对 printf 的抑制时是对的。您绝对应该从循环中删除对 printf 函数的调用,因为它可能会极大地扭曲结果。请记住,对 printf 的调用将在某个阶段涉及到内核模式的转换,并且就 CPU 周期而言,这本身就是一项代价高昂的操作。

