【问题标题】:How to link Cublas library with CMake CUDA 10.0 Ubuntu 18如何将 Cublas 库与 CMake CUDA 10.0 Ubuntu 18 链接
【发布时间】:2019-06-18 05:46:51
【问题描述】:

以下代码是关于我的 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!

【问题讨论】:

  • 根据documenation,变量CUDA_LIBRARIES 仅包含core CUDA 库,不适用于Cublas。在您之前的(已删除)question 中,您尝试过 CUDA_CUBLAS_LIBRARIES 变量,这似乎是正确的方向。该文档还建议将 CUDA_ADD_CUBLAS_TO_TARGET 宏用于链接 cublas。
  • 请注意,您对CMAKE_*_FLAGS 变量的设置无效,因为它们位于之前 project() 调用之前,这会更改这些变量的内容。
  • @Tsyvarev 好吧,谢谢你的解释。我尝试在我的 CMakeList 文件中添加“CUDA_CUBLAS_LIBRARIES”,但是,我无法正确找到 Cublas。我也尝试使用“CUDA_ADD_CUBLAS_TO_TARGET”,但我得到了目标变量已被使用的错误。
  • @Tsyvarev 虽然我尝试了“CUDA_CUBLAS_LIBRARIES”,但我无法正确链接 cublas。
  • “我无法正确找到 Cublas”、“我收到目标变量已被使用的错误”、“我无法正确链接 cublas” - 很难帮助处理这种模糊的描述.显示确切的错误消息,就像您在已删除的帖子中所做的那样。

标签: cmake cuda linker cublas


【解决方案1】:

用于最新 CMake 版本(例如 13.0)的脚本 FindCUDA.cmake 知道,自 CUDA 9.2 起不再使用库 cublas_device,并且脚本不会搜索该库。

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

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

【讨论】:

  • 谢谢。那么对我来说,包含 cublas 和其他库(如 GSL)的最简单方法是什么?
  • 如果您不想更新 CMake,您可以在 target_link_libraries() 调用中显式添加所需的库(如 cublas)。
  • 当我将 cublas 添加到我的“target_link_libraries()”时,我得到以下 error.main.cpp:32:5: error: ‘cublasHandle_t’ is not declared in this scope cublasHandle_t handle; ^~~~~~~~~~~~~~
  • 嗯?链接库不应影响编译阶段。看起来你的 C++ 代码有问题。
  • 我的 C++ 代码没有错误。它可以很好地与 Ubuntu 14 和 CUDA 9.2 配合使用。谢谢
猜你喜欢
  • 2018-12-16
  • 2021-07-21
  • 2018-07-09
相关资源
最近更新 更多