如何 link Cublas 库与 CMake CUDA 10.0 Ubuntu 18

How to link Cublas library with CMake CUDA 10.0 Ubuntu 18

以下代码是关于我的 CMakeList 文件的,它可以在 CUDA 9.2 Ubuntu 14 上运行良好。但是,当我尝试在我们的新服务器上 运行 它时,我收到错误消息。

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -std=c++11")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2")
cmake_minimum_required(VERSION 3.0)

project(LMM)
set(DYLD_LIBRARY_PATH /usr/local/include)
find_package(GSL REQUIRED)
find_package(BLAS REQUIRED)
find_package(CUDA)
if (CUDA_FOUND)
    message("CUDA found")
else()
    message("CUDA not found, doing something alternatively")
endif()

include_directories(test_cuda PRIVARE
                    ${GSL_INCLUDE_DIRS}
                    ${BLAS_INCLUDE_DIRS}
                    ${CUDA_INCLUDE_DIRS}
                    ${CUDA_CUBLAS_DIRS}
                    ${PROJECT_SOURCE_DIR})

add_executable(GPU_LMM main.cpp aux.cpp)
target_link_libraries( GPU_LMM  PRIVATE
                        ${GSL_LIBRARY}
                        ${BLAS_LIBRARIES}
                        ${CUDA_LIBRARIES})

日志信息如下

-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Found GSL: /usr/include (found version "2.4") 
-- Looking for sgemm_
-- Looking for sgemm_ - found
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- A library with BLAS API found.
-- Found CUDA: /usr/local/cuda (found version "10.0") 
CUDA found
-- Configuring done
-- Generating done

但是,当我运行我的程序时,出现以下错误信息。

aux.cpp:(.text+0x28d1): undefined reference to `cublasSetVector'
aux.cpp:(.text+0x290e): undefined reference to `cublasSetMatrix'
aux.cpp:(.text+0x2942): undefined reference to `cublasSetMatrix'
aux.cpp:(.text+0x2994): undefined reference to `cublasSgemv_v2'
aux.cpp:(.text+0x29e0): undefined reference to `cublasSgemv_v2'
aux.cpp:(.text+0x2a32): undefined reference to `cublasSgemv_v2'
aux.cpp:(.text+0x2a6c): undefined reference to `cublasSaxpy_v2'
aux.cpp:(.text+0x2b4a): undefined reference to `cublasSetMatrix'
aux.cpp:(.text+0x2bed): undefined reference to `cublasSgemv_v2'
aux.cpp:(.text+0x2c3c): undefined reference to `cublasSgemv_v2'
aux.cpp:(.text+0x2c7c): undefined reference to `cublasScopy_v2'
aux.cpp:(.text+0x2cb4): undefined reference to `cublasSaxpy_v2'
aux.cpp:(.text+0x2d1e): undefined reference to `cublasSgemv_v2'
aux.cpp:(.text+0x2d6a): undefined reference to `cublasSgemv_v2'
aux.cpp:(.text+0x2dbc): undefined reference to `cublasSgemv_v2'
aux.cpp:(.text+0x2df6): undefined reference to `cublasSaxpy_v2'
aux.cpp:(.text+0x2e25): undefined reference to `cublasGetVector'
aux.cpp:(.text+0x2e50): undefined reference to `cublasGetVector'

我确定我已经在服务器上安装了 Cublas,因为我可以重现官方的 Cublas 演示。当我添加 CUDA_CUBLAS_LIBRARIES 时,出现以下错误。

-- A library with BLAS API found.
CUDA found
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_cublas_device_LIBRARY (ADVANCED)
    linked by target "GPU_LMM" in directory /home/szhangcj/C++/LMM

-- Configuring incomplete, errors occurred!

最近的 CMake 版本(例如 13.0)的脚本 FindCUDA.cmake 知道,库 cublas_device 自 CUDA 9.2 以来不再使用,脚本不会搜索该库。

由于您的错误消息指出 cublas_device 库,这意味着您的 FindCUDA.cmake 脚本 太旧 ,并且它不能与最新的 CUDA 版本一起使用。

为了正确使用 find_package(CUDA) 最新的 CUDA 版本,您需要更新您的 CMake。