c++ - 为 CUDA 编程计算设备和主机的执行时间

标签 c++ time cuda

我需要计算 GPU 运行时代码,以及总运行代码(主机和设备)。 在我的代码中,我有两个 gpu 内核在运行,并且在一个用于复制数据的主机 for 循环之间,下面的示例可以显示我的代码的样子

cuda event start

//FIRST kernel code call <<...>>

// cuda memory copy result back from device to host

CudadeviceSyncronize()

// copy host data to host array (CPU funtion loop)

// cuda memory copy from host to device

// SECOND Kernel call <<...>>

cuda event stop

//memory copy back from device to host

我所知道的是我使用事件来计算内核,事件精确地测量内核在 GPU 上花费的实际时间。所以我的问题和目标是:

1- 我把事件调用放在上面所示的方式是:将仅记录内核并忽略主机功能?

2- 主机循环调用会影响 cuda 事件计时吗?

3- 我的目标是只计算 GPU,还有 GPU+CPU 一起计算,上面会实现它还是我应该使用 clock_gettime(CLOCK_REALTIME, timer) 来计算主机?

最佳答案

像这样的序列:

float et;
cudaEvent_t start, stop;
cudaEventCreate(&start); cudaEventCreate(&stop);
cudaEventRecord(start);
kernel1<<<...>>>(...);
cudaDeviceSynchronize();
host_code_routine(...);
kernel2<<<...>>>(...);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&et, start, stop);

将在 et 中返回浮点运行时间(以毫秒为单位),即(大约)总和:

  1. kernel1 执行时间
  2. host_code_routine相关的(主机)执行时间
  3. kernel2 执行时间

如果您只想生成上面的 1 和 3 的总和,您需要(仅)用 cudaEvent 时序将每个内核括起来,然后在主机代码中手动对这两个值求和。

要回答你的问题,那么:

1- is my way i put the event calling above shown : will be recording the kernel Only and neglecting the host functions ?

不,您描述的记录将捕获序列中的主机和设备耗时。

2- will the host loop call affect the cuda events timing?

3- my goal is to calculate the GPU only , and also GPU+CPU together, the above will it achieve it or should i use clock_gettime(CLOCK_REALTIME, timer) to calculate the host ?

如果您想要单独的时间和各种总和,我建议您独立地为内核计时,并使用一些基于主机的方法为主机代码计时,然后以您希望的任何方式组合各种组件。

关于c++ - 为 CUDA 编程计算设备和主机的执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30439157/

相关文章:

c++ - 大型多应用程序 COM 项目中 Doxygen 的最佳设置

ios - 如何在swift中延迟for循环迭代

c - 避免 CUDA 字符串搜索中的分支发散

c++ - 在设备 vector 上设置每个主机 vector 的 int 数组类型的数据元素

CUDA 代码中的常量内存使用

c++ - LLVM IR 是否包含内置函数的代码

c++ - 在 C++ 中不使用括号类的原因可能是什么?

c++ - std::string 参数的右值

java - java支持GPS时间吗?

sql-server - 如何在 SQL Server 中将工作日中多个时间戳的时间分类为“入”和“出”