Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could a CUDA kernel call a cublas function?

I know it sound weird, but here is my scenario:

I need to do a matrix-matrix multiplication (A(n*k)*B(k*n)), but I only needs the diagonal elements to be evaluated for the output matrix. I searched cublas library and didn't find any level 2 or 3 functions that can do that. So, I decided to distribute each row of A and each column of B into CUDA threads. For each thread (idx), I need to calculate the dot product "A[idx,:]*B[:,idx]" and save it as the corresponding diagonal output. Now since this dot product also takes some time, and I wonder whether I could somehow call cublas function here (say cublasSdot) to achieve it.

If I missed some cublas function that can achieve my goal directly (only calculate the diagonal elements for a matrix-matrix multiplication), this question could be discarded.

like image 872
Hailiang Zhang Avatar asked Nov 14 '12 00:11

Hailiang Zhang


People also ask

Can a CUDA kernel call another kernel?

Basically, a child CUDA kernel can be called from within a parent CUDA kernel and then optionally synchronize on the completion of that child CUDA Kernel. The parent CUDA kernel can consume the output produced from the child CUDA kernel, all without CPU involvement [136].

What is cuBLAS?

The cuBLAS Library provides a GPU-accelerated implementation of the basic linear algebra subroutines (BLAS). cuBLAS accelerates AI and HPC applications with drop-in industry standard BLAS APIs highly optimized for NVIDIA GPUs.


2 Answers

Yes, it can (until (and excluding) version CUDA 10).

"The language interface and Device Runtime API available in CUDA C/C++ is a subset of the CUDA Runtime API available on the Host. The syntax and semantics of the CUDA Runtime API have been retained on the device in order to facilitate ease of code reuse for API routines that may run in either the host or device environments. A kernel can also call GPU libraries such as CUBLAS directly without needing to return to the CPU." Source

Here you can see and Matrix-Vector Multiplication using cuda and CUBLAS library function cublasSgemv.

Bear in mind, however that there is no longer a device CUBLAS capability in CUDA 10.. From Robert_Crovella one can cite:

The current recommendation would be to see if CUTLASS 2 will help (it is mostly focused on GEMM related activities). If not, write your own code to perform the function, or call cublas from host code.

Nonetheless, currently there are several implementation online of Matrix-Vector Multiplication, for instance 1, 2, among others.

like image 89
dreamcrash Avatar answered Nov 30 '22 06:11

dreamcrash


Make sure you are using the device library to call the cublas. You can't use the same library that you used to call it from the host; details about using the cuda device library can be found on cuda toolkit: http://docs.nvidia.com/cuda/cublas/index.html#device-api

Look at the cuda 5 samples under 7_CUDALibraries/ .

like image 40
Sameer Asal Avatar answered Nov 30 '22 06:11

Sameer Asal