使用工具包中提供的库编译我的 CUDA 程序

Compiling my CUDA program with libraries provided in toolkit

我编写了简单的 CUDA c++ 程序来模拟二维矩阵上的扩散。当我尝试使用 Toolkit 中提供的一些库时遇到了麻烦。我想用 cuBlas 的东西替换我效率极低的矩阵转置内核,并用 cuSolvers 解决线性系统的实现替换 implCU。问题是我不知道如何使用这些函数或编译它们。它与 Nvidia 提供的示例代码的 Makefiles 一起工作。如果有人能帮助我,最好是告诉我在编写 .cu 文件时应该如何使用这些函数,我将不胜感激。

代码如下:http://pastebin.com/UKhJZQBz

我在 Ubuntu 16.04 上,我已经按照官方指南中的说明导出了 PATH 变量(因此它们包括 /usr/local/cuda-8.0/bin)。

这是nvcc -I /usr/local/cuda-8.0/samples/common/inc/ difusion2d.cu

的输出
/tmp/tmpxft_00001c09_00000000-16_difusion2d.o: In function `csr_mat_norminf(int, int, int, cusparseMatDescr*, double const*, int const*, int const*)':
undefined reference to `cusparseGetMatIndexBase'
/tmp/tmpxft_00001c09_00000000-16_difusion2d.o: In function `display_matrix(int, int, int, cusparseMatDescr*, double const*, int const*, int const*)':
undefined reference to `cusparseGetMatIndexBase'
/tmp/tmpxft_00001c09_00000000-16_difusion2d.o: In function `main':
undefined reference to `cusolverDnCreate'
undefined reference to `cublasCreate_v2'
undefined reference to `cusolverDnSetStream'
undefined reference to `cublasSetStream_v2'
collect2: error: ld returned 1 exit status

您必须明确 link cublas 和 cusolver 库。像

nvcc -I /usr/local/cuda-8.0/samples/common/inc \
     -L/path/to/CUDA/libraries  difusion2d.cu -lcublas -lcusolver

应该可以。根据您的安装,提供库搜索路径的 -L 选项可能是必需的,也可能不是必需的。