Xiaohe Cheng
Xiaohe Cheng

Reputation: 27

Should I make cublas handle global and reuse them in different (host) functions?

I think this saves some configuration time, but I am not sure whether this will cause unexpected behaviours.

Upvotes: 2

Views: 1456

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 152143

If you need to issue calls in any sort of thread concurrency scenario, its recommended to use independent handles:

https://docs.nvidia.com/cuda/cublas/index.html#thread-safety2

The library is thread safe and its functions can be called from multiple host threads, as long as threads do not share the same cuBLAS handle simultaneously.

Also note that the device associated with a particular cublas handle is expected to remain unchanged for duration of handle use:

https://docs.nvidia.com/cuda/cublas/index.html#cublas-context

The device associated with a particular cuBLAS context is assumed to remain unchanged between the corresponding cublasCreate() and cublasDestroy() calls.

Otherwise, using a single handle should be fine amongst cublas calls belonging to the same device and host thread, even if shared amongst multiple streams.

An example of using a single "global" handle with multiple streamed CUBLAS calls (from the same host thread, on the same GPU device) is given in the CUDA batchCUBLAS sample code.

Upvotes: 2

Related Questions