Add CUDA to ROS Package

假装没事ソ 提交于 2020-02-21 13:05:40

问题


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!


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/25748039/add-cuda-to-ros-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!