【发布时间】:2026-01-19 13:25:01
【问题描述】:
我有一个调用一系列设备功能的 CUDA 内核。
获取每个设备功能的执行时间的最佳方法是什么?
在其中一个设备函数中获取一段代码的执行时间的最佳方法是什么?
【问题讨论】:
标签: optimization cuda benchmarking
我有一个调用一系列设备功能的 CUDA 内核。
获取每个设备功能的执行时间的最佳方法是什么?
在其中一个设备函数中获取一段代码的执行时间的最佳方法是什么?
【问题讨论】:
标签: optimization cuda benchmarking
在我自己的代码中,我使用clock() 函数来获取精确的时间。为方便起见,我有宏
enum {
tid_this = 0,
tid_that,
tid_count
};
__device__ float cuda_timers[ tid_count ];
#ifdef USETIMERS
#define TIMER_TIC clock_t tic; if ( threadIdx.x == 0 ) tic = clock();
#define TIMER_TOC(tid) clock_t toc = clock(); if ( threadIdx.x == 0 ) atomicAdd( &cuda_timers[tid] , ( toc > tic ) ? (toc - tic) : ( toc + (0xffffffff - tic) ) );
#else
#define TIMER_TIC
#define TIMER_TOC(tid)
#endif
这些可用于检测设备代码,如下所示:
__global__ mykernel ( ... ) {
/* Start the timer. */
TIMER_TIC
/* Do stuff. */
...
/* Stop the timer and store the results to the "timer_this" counter. */
TIMER_TOC( tid_this );
}
然后您可以读取主机代码中的cuda_timers。
一些注意事项:
#ifdef USETIMERS 中以便您可以轻松关闭它们。clock() 返回clock_t 类型的整数值,但我将累积值存储为float,否则对于耗时超过几秒的内核(在所有块上累积),这些值将回绕。李>
( toc > tic ) ? (toc - tic) : ( toc + (0xffffffff - tic) ) ) 是必要的,以防时钟计数器回绕。附:这是我对this question 的回复的副本,因为所需的时间是针对整个内核的,所以没有得到多少分。
【讨论】:
clock(),我发现还有一个clock64(),可能不需要溢出检查和转换为float。
clock64() 仅出现在 4.2 版的 CUDA 编程指南中。至于你的第一点,我已经相应地更新了我的答案。谢谢!