其他分享
首页 > 其他分享> > c – 将CUDA添加到ROS包中

c – 将CUDA添加到ROS包中

作者:互联网

我想在一个ros包中使用cuda.有没有人给我一个简单的例子?

我尝试使用cuda函数构建一个静态库并将此库添加到我的包中,但我总是得到一个链接错误:未定义的引用cuda …

我已经构建了一个可执行文件而不是库,它可以工作.

请帮忙!

解决方法:

我自己找到了解决方案:

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

标签:ros,c,cuda,static-libraries,linker-errors
来源: https://codeday.me/bug/20190825/1713743.html