gpt4 book ai didi

cuBLAS 同步最佳实践

转载 作者:太空狗 更新时间:2023-10-29 15:00:34 26 4
gpt4 key购买 nike

我在 Stack Overflow 上阅读了两篇文章,即 Will the cublas kernel functions automatically be synchronized with the host?CUDA Dynamic Parallelizm; stream synchronization from device他们建议在调用 cuBLAS 函数后使用一些同步 API,例如 cudaDeviceSynchronize()。我不确定使用这样一个通用函数是否有意义。

这样会不会更好? [如果我错了请纠正我]:

cublasHandle_t cublas_handle;
cudaStream_t stream;
// Initialize the matrices
CUBLAS_CALL(
cublasDgemm(cublas_handle, CUBLAS_OP_N, CUBLAS_OP_N, M, M,
M, &alpha, d_A, M, d_B, M, &beta, d_C, M));
// cublasDgemm is non-blocking!
cublasGetStream(cublas_handle, &stream);
cudaStreamSynchronize(stream);
// Now it is safe to copy the result (d_C) from the device
// to the host and use it

另一方面,如果使用大量流/句柄来执行并行 cuBLAS 操作,则可以优先使用 cudaDeviceSynchronize。 cuBLAS 句柄同步的“最佳实践”是什么? cuBLAS 句柄是否可以被视为流的包装器,因为它们从同步的角度来看具有相同的目的?

最佳答案

如果您使用单个流,那么无论您是同步该流还是使用 cudaDeviceSynchronize() 都没有区别。在性能和效果上应该是完全一样的。请注意,当使用事件对代码的一部分计时时(例如,一个 cublas 调用),调用 cudaDeviceSynchronize() 以获得有意义的测量值始终是一个好习惯。根据我的经验,它不会强加任何显着的开销,此外,用它来为你的内核计时更安全。

如果您的应用程序使用多个流,那么只对您想要的流进行同步是有意义的。我相信this question会对你有所帮助。此外,您还可以阅读 CUDA C 编程指南,Section 3.2.5.5 .

关于cuBLAS 同步最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22988733/

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