CUDA/CPP Problems with the compilation

Hello everybody,

It’s the first time that I use CUDA and I’ve some problems with program compilation. First, I want to explain my program structure : I’ve two files octree.hpp and octree.cpp that contains several methods in order to create octree and manipulate its. precision : I define octree with the basic struct not a class.

I’ve a function that transforms my octree into a 1D array (In this function I use certain types of CUDA like float4).

My goal is to load my octree ( octree after transformation into a float4* ) and create octree manipulation functions.

I think I must create a file octree_kernel.cu contain my different octree manipulation functions but when I call the kernel in my main.cpp, I’ve a message :

/home/nicolas/Programmation/ter/src/main.cpp: In function ?int main()?:

/home/nicolas/Programmation/ter/src/main.cpp:27: erreur: ?display? was not declared in 

this scope

/home/nicolas/Programmation/ter/src/main.cpp:27: erreur: expected primary-expression 

before ?<? token

/home/nicolas/Programmation/ter/src/main.cpp:27: erreur: expected primary-expression 

before ?>? token

/home/nicolas/Programmation/ter/src/main.cpp:27: attention : left-hand operand of comma 

n'a pas d'effet

display is a little kernel in my octree_kernel.cu file.

I used CMake in order to compile my project :

cmake_minimum_required( VERSION 2.8 )

project( Octree )

set(EXECUTABLE_OUTPUT_PATH bin/)

set (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

find_package( CUDA REQUIRED )

set( CUDA_NVCC_FLAGS "--compiler-options")

set( CUDA_VERBOSE_BUILD ON )

set( CUDA_PROPAGATE_HOST_FLAGS OFF )

CUDA_BUILD_CLEAN_TARGET()

ADD_DEFINITIONS("-O2 -Wall -ansi -pedantic")

include_directories (

  headers

)

file (

  GLOB_RECURSE

  source_files

  ./src/*.cpp

  ./kernels/*.cu

)

CUDA_ADD_EXECUTABLE (

   Octree

   ${source_files}

)

My project is divided into several folders containing my files :

bin : binary

headers : octree.hpp

kernels : octree_kernel.cu

src : octree.cpp ; main.cpp

So my question is : How to include my C++ code with my different kernel CUDA and my function that transform octree (vector) into a 1D array (float4*) => this function is a host cuda function.

I hope to be clear because I’m very beginners with CUDA and sorry for my English I’m a French and my English is rather approximative.

PS : If it’s not clear, I do my best to answer your questions.

Thanks.

I’m not exactly sure what you are asking, but I may be able to help you with some of your problems.

First off let’s address the compilation errors you are seeing. These error messages you are seeing are likely caused from launching a CUDA kernel from a C file: run_kernel<<<gridsize, blocksize>>>(). C isn’t set up (as you have found) to digest this new syntax. This code needs to be in a CUDA file. I suggest you do the following.

  1. Create a new function called lauchMyKernel(). This can be forward declared in your main.cpp file and then defined in a cuda file.

  2. Define launchMyKernel() in a CUDA file to launch your kernel:

void launchMyKernel(Prams params)

{

  run_kernel<<<params.gridsize, params.blocksize>>>();

}
  1. Add the new cuda file to your list of files.

Alternatively, you could rename main.cpp as main.cu, and have nvcc process the file.

As far as the CMake code goes there are a couple of modifications that would help it work better.

  1. Move your call to CUDA_BUILD_CLEAN_TARGET() to after CUDA_ADD_EXECUTABLE. CUDA_BUILD_CLEAN_TARGET references a list of generated files that are populated by CUDA_ADD_EXECUTABLE. If you call CUDA_BUILD_CLEAN_TARGET before CUDA_ADD_EXECUTABLE that list will be empty.

  2. set( CUDA_NVCC_FLAGS “–compiler-options”): --compiler-options is an argument to nvcc that expects additional arguments. In other words, by specifying this flag without additional flags whatever happens to be next will be treated as a compiler option for the host compiler, and the host compiler will be given wrong arguments.

  3. ADD_DEFINITIONS(“-O2 -Wall -ansi -pedantic”): you need to remove the quotes, otherwise it will treat the entire string as a single argument. Try doing this: ADD_DEFINITIONS(-O2 -Wall -ansi -pedantic)

There is a sample project online you can use as a reference: svn co https://code.sci.utah.edu/svn/findcuda/trunk FindCUDA-test

Thanks for your reply, it’s to late for try what did you say (It’s 1AM) but I test it tomorrow and I come back if I’ve an over problem.
I think you have understand my problem ;)

I’m back after several test.

Everything works properly now. I created .cu file with my kernel and my function callKernel and I added extern int callKernel(); in my main.cpp. For my CMakeLists, I changed several things like you have suggested me.

cmake_minimum_required( VERSION 2.8 )

project( Octree )

set( EXECUTABLE_OUTPUT_PATH bin/ )

set( CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" )

find_package( CUDA REQUIRED )

#set( CUDA_NVCC_FLAGS "--compiler-options")

set( CUDA_VERBOSE_BUILD ON )

set( CUDA_PROPAGATE_HOST_FLAGS OFF )

set( CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE OFF )

ADD_DEFINITIONS( -O2 -Wall -ansi -pedantic )

include_directories(

  headers

)

file(

  GLOB_RECURSE

  source_files

  ./src/*.cpp

)

file(

   GLOB_RECURSE

   cuda_files

   ./kernels/*.cu

)

CUDA_COMPILE( CUDA_FILES ${cuda_files} )

CUDA_ADD_EXECUTABLE(

   Octree

   ${CUDA_FILES}

   ${source_files}

)

TARGET_LINK_LIBRARIES(

   Octree

   ${CUDA_LIBRARIES}

)

CUDA_BUILD_CLEAN_TARGET()

Now, my program compile without errors and I can run it without problems. Thanks a lot for your help and I would return if I’ve other questions.

I’m glad you got it working!

I would just do a couple more modifications (and only #2 is for correctness).

  1. I would list the source files explicitly instead of doing GLOB_RECURSE. The reason for this has to do when you want to change the list of source files. If you add a new source file and run ‘make’, CMake won’t run automatically. You would have to run cmake manually. If you listed the source files explicitly editing the CMakeLists.txt file to add a new source file will cause CMake to run and regenerate your build scripts automatically.

  2. You really shouldn’t call CUDA_COMPILE and CUDA_ADD_EXECUTABLE if you want your project to work in more than GNU Makefiles. The output of CUDA_COMPILE is designed to be used with add_executable or add_library and not with cuda_add_executable or cuda_add_library, because you need to add both ${CUDA_FILES} and ${cuda_files} for correct behavior with all build generators.

  3. You shouldn’t need to call target_link_libraries with the CUDA_LIBRARIES as this should already be done by cudea_add_executable.

In summary your CMakeLists.txt file could look like the following:

cmake_minimum_required( VERSION 2.8 )

project( Octree )

set( EXECUTABLE_OUTPUT_PATH bin/ )

set( CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" )

find_package( CUDA REQUIRED )

ADD_DEFINITIONS( -O2 -Wall -ansi -pedantic )

include_directories(

  headers

)

cuda_add_executable(

  Octree

  src/main.cpp

  src/octree.cpp

  headers/octree.hpp

  kernels/octree_kernel.cu

  kernels/launch.cu

)