I’m new in cuda world.
I’ve got a Tesla2050 NVIDIA card, installed under Linux (Ubuntu).
I installed the driver, sdk and tool kit. I made the tests (by compiling the toolkit) and everything seems ok.
I now want to compile my first own program. To do so, I use cmake to produce a structure of a future project.
Folders are organised this way:
myproject → src → core (contain the main c++ files)
→ cuda (contain the .cu files)
→ bin
→ doc
in the root folder (myproject) the CMakeLists.txt contains:
cmake_minimum_required(VERSION 2.8)
project(myproject)
set(EXECUTABLE_OUTPUT_PATH ./bin)
set(CUDA_VERBOSE_BUILD ON)
add_subdirectory(src)
in src folder the CMakeLists.txt contains:
find_package(CUDA REQUIRED)
add_subdirectory(cuda)
add_subdirectory(core)
in core folder the CMakeLists.txt contains:
include_directories(…/lbm)
include_directories(…/cuda)
add_executable(
semoul
main.cpp
)
target_link_libraries(
semoul
cuda
cudart
)
in cuda folder the CMakeLists.txt contains:
CUDA_add_library(cuda cuda_test.cu)
My program is just a test wish aim to execute a cosine on the device and get the result
After a cmake . in the myproject folder, I tape make and I get:
I don’t actually use cmake (I just use GNU make. There is a simple example of a makefile in the nvcc documentation) so I don’t think I will be of much help.
Have you checked that you are able to compile the program manually without using cmake? I.e.
#compile object (.o) files
nvcc -c main.cu -o main.o
nvcc -c something.cu -o something.o
# etc...
#Now try to link
nvcc main.o something.o [other objects ..] -o program
#If the link fails try to manually add directory to search for libraries
nvcc -L/usr/local/cuda/lib64/ main.o something.o [other objects ..] -o program
You should note the extension of the source file is important. .cpp is interpreted as c++ and .cu is interpreted as a CUDA source file. I would advise having a read of the nvcc manual if you’re still having problems.
This error tells you exactly what is going wrong. The linker can’t find the cudart library. Setting the LD_LIBRARY_PATH is a run-time and not compile-time resolution for finding libraries. Now, I’ll explain how to get CMake to find cudart properly.
target_link_libraries( semoul
cuda
cudart
)
target_link_libraries() can handle several types of libraries. The first is if the library is a target within the CMake project. There are two libraries you with to link against semoul. When CMake sees ‘cuda’ it will try and find a target with the same name in your project. In your other directory you have created a library called cuda ( CUDA_add_library(cuda cuda_test.cu) ). CMake will then link the cuda library with semoul. The next library is cudart. CMake checks its list of targets and doesn’t find one. It then checks to see if cudart is a full path to a library (which it isn’t). At this point CMake assumes that you know what you are doing and adds -lcudart to the link line.
The best way to handle linking external libraries is to use the output of the find_library() command. This is what FindCUDA.cmake does in the CMake distribution. If you look at the documentation, you will notice that there are several variables that FindCUDA produces. One of these is the CUDA_LIBRARIES variables that contain all the libraries needed to link a cuda run time application. These libraries are automatically added with CUDA_ADD_LIBRARY and CUDA_ADD_EXECUTABLE. What remains is adding it to your regular executable:
target_link_libraries( semoul
cuda
${CUDA_LIBRARIES}
)
Try that and let me know.
Alternatively, you could have also added the path to cudart in the link command (see link_directories() documentation). This command adds the -L path to your link line. Note that this method isn’t as favored as the one I originally proposed.