gpt4 book ai didi

c++ - Clock_gettime() 函数输出不正确的时间

转载 作者:行者123 更新时间:2023-11-30 02:37:28 25 4
gpt4 key购买 nike

我正在尝试使用 clock_gettime 函数获取以下代码的运行时间。但是,当我运行代码时,每次运行时我都会收到 0.0000 的时间。我也分别输出了开始和停止时间,我收到了完全相同的答案。

struct timespec start, stop;
double accum;

if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}

int src = 1, final_ret = 0;
for (int t = 0; t < rows - 1; t += pyramid_height)
{
int temp = src;
src = final_ret;
final_ret = temp;

// Calculate this for the kernel argument...
int arg0 = MIN(pyramid_height, rows-t-1);
int theHalo = HALO;

// Set the kernel arguments.
clSetKernelArg(cl.kernel(kn), 0, sizeof(cl_int), (void*) &arg0);
clSetKernelArg(cl.kernel(kn), 1, sizeof(cl_mem), (void*) &d_gpuWall);
clSetKernelArg(cl.kernel(kn), 2, sizeof(cl_mem), (void*) &d_gpuResult[src]);
clSetKernelArg(cl.kernel(kn), 3, sizeof(cl_mem), (void*) &d_gpuResult[final_ret]);
clSetKernelArg(cl.kernel(kn), 4, sizeof(cl_int), (void*) &cols);
clSetKernelArg(cl.kernel(kn), 5, sizeof(cl_int), (void*) &rows);
clSetKernelArg(cl.kernel(kn), 6, sizeof(cl_int), (void*) &t);
clSetKernelArg(cl.kernel(kn), 7, sizeof(cl_int), (void*) &borderCols);
clSetKernelArg(cl.kernel(kn), 8, sizeof(cl_int), (void*) &theHalo);
clSetKernelArg(cl.kernel(kn), 9, sizeof(cl_int) * (cl.localSize()), 0);
clSetKernelArg(cl.kernel(kn), 10, sizeof(cl_int) * (cl.localSize()), 0);
clSetKernelArg(cl.kernel(kn), 11, sizeof(cl_mem), (void*) &d_outputBuffer);
cl.launch(kn);
}

if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}
printf( "%lf\n", stop.tv_sec );
printf( "%lf\n", start.tv_sec );
accum = ( stop.tv_sec - start.tv_sec )
+ ( stop.tv_nsec - start.tv_nsec )
/ BILLION;
printf( "%lf\n", accum );

非常感谢任何关于我做错的建议

最佳答案

timespec::tv_nsec 是一个整数类型,所以如果 BILLION 也是一个整数类型那么:

( stop.tv_nsec - start.tv_nsec )
/ BILLION;

将截断为零。如果 tv_sec 值相同,则差异为零。

尝试:

double( stop.tv_nsec - start.tv_nsec )
/ BILLION;

这将执行 double 类型的除法。

关于c++ - Clock_gettime() 函数输出不正确的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31567131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com