将CUDA添加到ROS程序包 [英] Add CUDA to ROS Package

查看:380
本文介绍了将CUDA添加到ROS程序包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to use cuda within a ros package. Has anyone a simple example for me?

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

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.

请帮助!

推荐答案

我自己找到了一个解决方案:

I found a solution myself:

CMakeLists.txt:

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:

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

这篇关于将CUDA添加到ROS程序包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆