c++ - c++ 和 cublas 代码的混合未编译

标签 c++ cuda nvcc cublas

所以我有这段代码,假设以不同的方式计算矩阵的点积(其中一种是在 C++ 中使用 blas),但是当我尝试使用 nvcc 编译代码时,它不会工作,它说我有一个对 ddot 的 undefined reference 。这很奇怪,因为我很确定我正在使用此处引用的 cublas 调用符号:http://www.sdsc.edu/us/training/assets/docs/NVIDIA-03-Toolkit.pdf

谁能帮帮我?这是我遇到问题的一段代码:

#include <cublas.h> //just some included files here. No problems with these
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

extern "C" //This is where I mention the cublas functions are external.
//I think this is necessary since I also have cuda pieces of code
{
    double cublasDDOT_(int *n, double *A, int *incA, double *B, int *incB);

    void cublasDAXPY_(int *n, double *a, double *A, int *incA, double *B, int *incB);
}

//Stuff happens here

C[i][t]=cublasDDOT_(&n, partA, &incA, partB, &incB); //This is a piece of my function and where the compiler chokes up

这对我来说很奇怪。我也试过删除“_”但没有成功。

这是我使用的编译命令:nvcc program

我是否需要在编译期间以某种方式提及 cublas 库?我安装了 cuda 工具包,但我不知道如何引用库,除了 with

#include <cublas.h>

新更新

事实证明,无论是否包含 cublas.h header ,我都会得到相同的输出

无论是否输入 -lcublas,我都会得到相同的输出

这是输出,对于所有编译都是垃圾(有/没有 cublas.h 和有/没有 -lcublas)

nvcc project4.cu -lcublas
/tmp/tmpxft_000051cb_00000000-14_project4.o: In function `ddot(int&, int&, int&, double**&, double**&, double**&, double*&, double*&, int&, int&, double&, double&, double*)':
tmpxft_000051cb_00000000-3_project4.cudafe1.cpp:(.text+0xda1): undefined reference to `cublasDDOT'
/tmp/tmpxft_000051cb_00000000-14_project4.o: In function `daxpy(int&, int&, int&, double**&, double**&, double**&, double**&, double*&, double*&, int&, int&, double&, double&, double*)':
tmpxft_000051cb_00000000-3_project4.cudafe1.cpp:(.text+0xff3): undefined reference to `cublasDAXPY'
collect2: ld returned 1 exit status

最佳答案

即使在使用 nvcc 编译时,您仍然需要指定 -lcublas 链接开关。

看起来你调用的函数名称不正确:

cublasDDOT_()

应该是:

cublasDdot()

和:

cublasDAXPY_()

应该是:

cublasDaxpy()

命名区分大小写。

如果您不确定正确的命名,请引用 cublas documentation并查看 sample codes 中的用法

是的,删除下划线。我不明白你为什么这样调用函数名。如果您破坏了名称,链接器就无法知道您打算将其链接到什么。

我也不确定是否需要任何“extern C”内容。这取决于您的项目中发生的其他事情,但我认为如果您正在使用 nvcc 进行编译/链接,则不应使用“extern C”包裹您打算与 cublas 库链接的函数

关于c++ - c++ 和 cublas 代码的混合未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16627304/

相关文章:

python - 在 C++ 中创建列表列表

c++ - 从 OpenCV 的 VideoCapture 获取的图像与标量之间的整数除法

c++ - 如何在Qt中拖动一个未装饰的窗口

c++ - 如何在 Cuda 设备函数中使用 C++11 中的闭包参数声明函数?

c - __internal_trig_reduction_slowpathd 的函数属性

cuda - 支持 CUDA 5 的 GPU 上不受支持的 GPU 架构计算_30

c++ - 插入器和提取器读取/写入二进制数据与文本

c++ - 将两个不同长度的 vector 根据各自包含全局地址的索引 vector 拼接到具有推力的共同长度的新 vector

c++ - 处理图像时出现 CUDA 错误

CUDA/PTX 32 位与 64 位