gpt4 book ai didi

Cuda 重用事件来确定多个部分的执行时间

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:18 24 4
gpt4 key购买 nike

我想计算我的 cuda 代码的两个不同部分的执行时间。为此,我尝试使用 CudaEvent_t 启动、停止。我的问题是,我可以使用相同的两个事件“开始”和“停止”来计算其他部分的执行时间吗?

例如:

cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);

// SECTION 1
cudaEventRecord(start, 0);
cudaMemcpy(..., ..., ..., cudaMemcpyHostToDevice);
cudaEventRecord(stop, 0);

cudaEventSynchronize(stop);
cudaEventElapsedTime(&executionTime, start, stop);
printf("SECTION 1 executionTime: %f", executionTime);
// SECTION 1

// SECTION 2
cudaEventRecord(start, 0); // Reusing start event
cudaMemcpy(..., ..., ..., cudaMemcpyDeviceToHost);
cudaEventRecord(stop, 0); // Reusing stop event

cudaEventSynchronize(stop);
cudaEventElapsedTime(&executionTime, start, stop);
printf("SECTION 2 executionTime: %f", executionTime);
// SECTION 2

此代码能否准确估计第 1 节和第 2 节的执行时间因为我正在尝试恢复 cudaEvent_t 开始,所以第 2 节也停止?或者我是否需要为第二部分创建两个额外的事件 start1 和 stop1?

最佳答案

您可以重复使用事件。这样做的能力是由您可以 record an event more than once 提供的。 :

If cudaEventRecord() has previously been called on event, then this call will overwrite any existing state in event. Any subsequent calls which examine the status of event will only examine the completion of this most recent call to cudaEventRecord().

因此,这意味着使用事件测量的任何时间将仅与最近记录的事件有关。在您的案例/示例中,这基本上是不言而喻的,但在更复杂的案例中,应该注意只有最近记录的事件“版本”才是“事件的”。

关于Cuda 重用事件来确定多个部分的执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34005870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com