cudaEventRecord() 在 Visual Studio CPU 代码上计时不正确

标签 c windows visual-studio-2013 time cuda

在做 NVIDIA 制作的一些 CUDA 基本示例时,我复制了一些代码来测试矩阵乘法从 CPU 到 GPU 计算的加速。

30 分钟后查看结果并看到我的 CPU(是的 CPU)的计算速度比我的 GPU 快 1000 倍,我意识到计时不正确。代码片段如下所示(这是来自 NVIDIA 的代码):

//Create timers
cudaEvent_t start;
cudaEvent_t stop;
float simpleKernelTime;
float optimisedKernelTime;

//start timer
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);

matrixMultKernel<<<grid, block >>>(a_d, b_d, c_d, N);

cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);

// Print time and do other things

cudaEventRecord(start, 0);

matrixMultCPU(a_h, b_h, d_, N);

cudaEventRecord(stop, 0)
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);

// Print time

这段代码在 Linux 机器上运行良好(我复制了与我旁边的人相同的代码,他的计时效果很好),但在装有 Visual Studio 2013 的 Windows 8 机器上,CPU 部分的计时(后半部分)的片段)不起作用(总是给出~0.003ms)。

为什么会发生这种情况?我使用 <time.h> 修复了它(删除 cudaEventRecord() 调用并使用标准 C 代码计时方法),所以我不想知道如何修复它,但更多的是为什么会发生这种情况。

最佳答案

据我了解,CUDA 事件本身并不是为了测量仅 CPU(仅主机)时间,而是为了测量内核执行和 CUDA API 调用。来自 CUDA C Programming Guide 3.2.5.6. 事件(强调我的):

The runtime also provides a way to closely monitor the device's progress, as well as perform accurate timing, by letting the application asynchronously record events at any point in the program and query when these events are completed.

我也很惊讶你有时间(内核启动是异步的),因为你的代码缺少cudaEventSynchronize():

cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);

另请参阅How to Implement Performance Metrics in CUDA C/C++ .

有关仅 CPU 的时间测量,请参阅 this thread .

编辑:

要获得 matrixMultCPU() 的正确时间,您需要为 start 事件添加同步:

cudaEventRecord(start, 0);
cudaEventSynchronize(start);

关于cudaEventRecord() 在 Visual Studio CPU 代码上计时不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31046158/

相关文章:

javascript - Visual Studio 2013 - AngularJS 基本问题

intellisense - Visual Studio 2013 智能感知错误

c - 检测 scanf 何时没有输入

python - 谁能解释shutil.rmtree和shutil.copytree的这种奇怪行为?

windows - 如何修复 ‘--go_out: protoc-gen-go: The system cannot find the file specified.’ 错误

node.js - webpack-dev-server错误: EPERM: operation not permitted,统计 'D:\System Volume Information'

.net - 如何在 F# 应用程序中嵌入应用程序 list

c - 在各种编译器和优化级别下,bool 在 c 中的存储

c - 冒泡和插入之间的时间复杂度

c - 如何使用 FFMPEG 加速时间线清理?