0
$\begingroup$

Rosanswers logo

Hi, everyone.
I'd like to know how to write CMakesLists.txt for cuda code.
Are there any good references for it ??

Code is as the following, very simple.


// hello.cu
#include <cuda.h>
#include <cuda_runtime.h>

__global__ kernel()
{
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "hello");
  ros::NodeHandle nh;

  kernel<<<1, 1>>>()
  ROS_INFO_STREAM("Hello, world.");

  return 0;
}

Thanks in advance.


Originally posted by moyashi on ROS Answers with karma: 721 on 2014-05-07

Post score: 2

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

in your CMakeLists.txt:

    find_package( CUDA REQUIRED 
                                                
 # set CUDA_NVCC_FLAGS as you would do with CXX/C FLAGS                        
  set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -DMY_DEF=1")
  set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMY_DEF=1" )
  set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMY_DEF=1" )

 # only necessary if you wish to create a lib
  set(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE OFF)
  set(BUILD_SHARED_LIBS ON)

# cuda compile GPU_CODE for executable
  cuda_compile(  HELLO_CU_O src/hello.cu )

## create an executable
## can add other (none CUDA) cpp files here
  cuda_add_executable( hello_exec ${LINE_PROFILER_CU_O} )
  target_link_libraries( hello_exec ${catkin_LIBRARIES} )
  add_dependencies( hello_lib ${PROJECT_NAME}_generate_messages_cpp  ${LINE_PROFILER_CU_O} )

## if message headers needed 
  add_dependencies( hello_lib ${PROJECT_NAME}_generate_messages_cpp )

### or create a lib 
  cuda_add_library( ${PROJECT_NAME} src/lib_cuda_code.cu src/libe_none_cuda_code.cpp )
  target_link_libraries( ${PROJECT_NAME} ${catkin_LIBRARIES}  )

## if message headers needed 
  add_dependencies( ${PROJECT_NAME} ${PROJECT_NAME}_generate_messages_cpp )

Originally posted by Wolf with karma: 7555 on 2014-05-08

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by barovehicles on 2018-03-18:
The question is: if i want to add a ros.h this code don't work.. because i can't to include the ros.h in the compilation of hello.cu

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.