使用 CPU 个计时器对内核执行进行计时

Timing Kernel Execution with CPU Timers

我一直在尝试使用 nvidia 网站上显示的 cpu 指标来测量 cuda 内核执行时间,但是我在使用 myCPUTimer() 函数时遇到了问题

  T1=myCPUtimer();
  vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, numElements);
  cudaDeviceSynchronize();
  T2=myCPUTimer();

编译后出现此错误 undefined reference to 'myCPUTimer' 而且我似乎无法在网上找到有关如何使用此功能的任何文档。

我猜你指的是 this

那里的文字说:

the generic host time-stamp function myCPUTimer()

没有为您提供该功能,您不能按原样使用它。那里的“通用”意味着它是您将使用和提供的某些功能,这可能是特定于平台(即OS)的。

你必须自己提供这样的功能。在这种情况下,它是一个虚构的函数。现实世界中是不存在的,一模一样

您可以在 SO 上找到许多关于如何对 CUDA 内核进行主机计时的问题,例如 this one

例如,在 linux 上,您可以这样做:

#include <time.h>
#include <sys/time.h>
#define USECPSEC 1000000ULL

unsigned long long myCPUTimer(unsigned long long start=0){

  timeval tv;
  gettimeofday(&tv, 0);
  return ((tv.tv_sec*USECPSEC)+tv.tv_usec)-start;
}

这将 return 以微秒为单位的“时间戳”转换为 unsigned long long 变量,使用基于 CPU 的相当高分辨率的计时器。