如何在 CUDA 中获取内核启动时间?

How can I get kernel start time in CUDA?

我正在尝试使用以下代码获取内核启动时间,但此代码的输出不正确。

我想要内核函数的启动时间相对于这个程序的启动时间。也就是说,如果我们认为这个程序的开始时间为零时间,那么内核函数的开始时间是多少。

我的错误是什么?

//headers
#include <sys/time.h>

//kernel function defined here

int main(){
struct timeval kernelStartTime;

//memcpys and ....

gettimeofday(&kernelStartTime, 0);
MatrixMulCUDA<32><<<matrixMulgrid, matrixMulthreads>>>(C, A, B, dimsA.x, dimsB.x);
cudaDeviceSynchronize();

float startTime = (1000000.0 * kernelStartTime.tv_sec + kernelStartTime.tv_usec)/1000.0;

//some codes

printf("kernel start time = %f", startTime);
return 0;
}

输出:

内核启动时间=1.63786e+12

谢谢。

//headers
#include <sys/time.h>

//kernel function defined here

int main(){
struct timeval kernelStartTime, baseTime;
gettimeofday(&baseTime, 0);

//memcpys and ....

gettimeofday(&kernelStartTime, 0);
MatrixMulCUDA<32><<<matrixMulgrid, matrixMulthreads>>>(C, A, B, dimsA.x, dimsB.x);
cudaDeviceSynchronize();

float startTime = (1000000.0 * (kernelStartTime.tv_sec - baseTime.tv_sec) + (kernelStartTime.tv_usec - baseTime.tv_usec))/1000.0;

//some codes

printf("kernel start time = %f", startTime);
return 0;
}