I would like to use cuda within a ros package. Has anyone a simple example for me?
I tried to built a static library with the cuda function and add this library to my package, but I get always a linking error: Undefined reference cuda...
I have built a executable instead of the library and it works.
Please help!
I found a solution myself:
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
PROJECT (beginner_tutorials)
FIND_PACKAGE(CUDA REQUIRED)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
SET(CUDA_NVCC_FLAGS "-arch=sm_13" CACHE STRING "nvcc flags" FORCE)
SET (CUDA_VERBOSE_BUILD ON CACHE BOOL "nvcc verbose" FORCE)
SET(LIB_TYPE STATIC)
CUDA_ADD_LIBRARY(TestLib ${LIB_TYPE} src/helloWorld.cu)
catkin_package(
)
include_directories(
${catkin_INCLUDE_DIRS}
)
ADD_EXECUTABLE(beginner_tutorials_node src/main.cpp)
ADD_DEPENDENCIES(beginner_tutorials_node TestLib)
TARGET_LINK_LIBRARIES(beginner_tutorials_node
${catkin_LIBRARIES}
${PCL_LIBRARIES}
TestLib
)
main.cpp:
int testmain();
int main()
{
testmain();
return 0;
}
helloWorld.cu:
#include <stdio.h>
#include <cuda.h>
#include <cuda_runtime.h>
const int N = 7;
const int blocksize = 7;
__global__
void hello(char *a, int *b)
{
a[threadIdx.x] += b[threadIdx.x];
}
int testmain()
{
char a[N] = "Hello ";
int b[N] = {15, 10, 6, 0, -11, 1, 0};
char *ad;
int *bd;
const int csize = N*sizeof(char);
const int isize = N*sizeof(int);
printf("%s", a);
cudaMalloc( (void**)&ad, csize );
cudaMalloc( (void**)&bd, isize );
cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice );
cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice );
dim3 dimBlock( blocksize, 1 );
dim3 dimGrid( 1, 1 );
hello<<<dimGrid, dimBlock>>>(ad, bd);
cudaMemcpy( a, ad, csize, cudaMemcpyDeviceToHost );
cudaFree( ad );
printf("%s\n", a);
return EXIT_SUCCESS;
}
If you are using catkin-simple to make your CMake file, you can use this CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(cuda_test)
set(CMAKE_CUDA_COMPILER /usr/local/cuda-9.1/bin/nvcc)
find_package(catkin_simple REQUIRED)
find_package(CUDA REQUIRED)
catkin_simple()
#Here you can set any ncvv compiler flags, if you so wish
#SET(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -DMY_DEF=1")
#Here you can set any gcc/cmake compiler flags, if you so wish
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3")
#Add all of your sources here
cuda_add_executable(
cuda_test_cu
src/hello.cu
)
#Link the executable to the necessary libs
target_link_libraries(
cuda_test_cu
${catkin_LIBRARIES}
${CUDA_LIBRARIES}
)
# CMake Indexing
FILE(GLOB_RECURSE LibFiles "include/*")
add_custom_target(headers SOURCES ${LibFiles})
cs_install()
I use this and find that it works just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With