cuBLAS 同步最佳实践

标签 c cuda cublas

我在 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/

相关文章:

cuda - CUBLAS 矩阵乘法

CUBLAS 通用矩阵点积

c - 可靠地以56K逐字节读取linux中的串行数据

CS50 Speller 可以编译,但根本无法运行

c++ - 使用 CUDA Thrust 确定每个矩阵列中的最小元素及其位置

CUDA 核心管道

c - 在 C 中包含数组索引是一种好习惯吗?

c - 在 cygwin 中使用 tr

c - 您是否应该尝试始终以最大线程数运行 CUDA C 硬件?

c++ - CUBLAS - 矩阵加法..怎么样?