Search code examples
cmakecudalinkercublas

How to link Cublas library with CMake CUDA 10.0 Ubuntu 18


The following code is about my CMakeList file and it can work well on CUDA 9.2 Ubuntu 14. However, when I try to run it on our new server, I get error message.

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 log information is as the following.

-- 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

However, when I run my program, I the the following error message.

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'

I make sure that I have installed the Cublas on server, because I can reproduce the official Cublas demo. When I add the CUDA_CUBLAS_LIBRARIES, I get the following error.

-- 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!

Solution

  • Script FindCUDA.cmake for recent CMake versions (e.g., 13.0) knows, that the library cublas_device is no longer used since CUDA 9.2, and the script does not search that library.

    As your error message notes cublas_device library, it means that your FindCUDA.cmake script is too old, and it cannot work with newest CUDA versions.

    For correctly use find_package(CUDA) with newest CUDA version you need to update your CMake.