CUDA内核能调用cublas函数吗? [英] Could a CUDA kernel call a cublas function?

查看:734
本文介绍了CUDA内核能调用cublas函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来很奇怪,但这里是我的场景:

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

我需要做一个矩阵 - 矩阵乘法(A(n * k)* B * n)),但是我只需要为输出矩阵评估对角元素。我搜索cublas库,没有找到任何2级或3级功能可以做到这一点。
所以,我决定分配A的每一行和B的每一列到CUDA线程。对于每个线程(idx),我需要计算点积A [idx,:] * B [:,idx]并将其保存为对应的对角线输出。现在,因为这个点产品也需要一些时间,我想知道我是否可以在这里调用cublas函数(说cublasSdot)来实现它。

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.

如果我错过了一些cublas函数可以直接实现我的目标(只计算矩阵矩阵乘法的对角元素),这个问题可以被丢弃。

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.

推荐答案

是的,可以。

CUDA C / C ++中提供的语言接口和Device Runtime API是主机上可用的CUDA Runtime API的一个子集CUDA Runtime的语法和语义API已保留在设备上,以便于可以在主机或设备环境中运行的API例程的代码重用。内核也可以直接调用GPU库(如CUBLAS ),而无需返回到CPU。 来源

"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

在这里您可以看到和使用cuda和CUBLAS库函数cublasSgemv的Matrix矢量乘法。

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

这篇关于CUDA内核能调用cublas函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆