[vtkusers] Writing CMake file for VTK+CUDA project

Chuck Atkins chuck.atkins at kitware.com
Mon Jun 27 14:20:45 EDT 2016


Hi Reza,


> find_package(CUDA REQUIRED)
> if (CUDA_FOUND)
>     message("CUDA found!")
> else()
>     message("CUDA not found, doing something alternatively")
> endif()
>

The if(CUDA_FOUND) is redundant since the REQUIRED argument to find_package
will generate a fatal error if not found.  If you want to be able to have
two different configurations, one found and one not found, then just remove
the REQUIRED from find_package(CUDA).



> set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}
>

Rather than explicitly force the CXX flags, there's more compiler agnostic
ways to address this:


-std=c++11
>

instead of forcing the flag, let CMake do the work for you.  Just set the
CXX_STANDARD=11 property on the resulting target and CMake will know how to
set the correct flag.



> -D_FORCE_INLINES
>

 Use add_definitions(-D_FORCE_INLINES).  The end effect is the same but it
does a better job of propagating target usage requirements under the hood.


CUDA_ADD_EXECUTABLE(Test source.cu)
>

Ideally you should keep the host and device code seperate.  That way the
CUDA compiler is only used for actual CUDA code and your host compiler
deals with the rest.



> I am using CUDA 7.5, VTK 7.0.0 and CMake 3.6.0. Here is the source.cu. It
> is basically combination of VTK Hello World example (Rendering a Cylinder)
> and vectorAdd.cu in CUDA samples and both are working separately.
>



> What are my mistakes that VS12 cannot find includes of VTK in my codes
>
You're missing a call to CUDA_INCLUDE_DIRECTORIES to pass them to the
separate CUDA compiler



> Generally, how do we write a CMake code when we want to combine CUDA and
> VTK?
>
Putting it all together, we get:

cmake_minimum_required(VERSION 3.3)
project(Test)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

find_package(CUDA REQUIRED)

add_definitions(-D_FORCE_INLINES)

set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} --gpu-architecture sm_20)

// The following is ONLY necessary if your CDA code is actually including
VTK
CUDA_INCLUDE_DIRECTORIES(${VTK_INCLUDE_DIRS})

CUDA_ADD_EXECUTABLE(Test cylinder.cxx vectorAdd.cu)
target_link_libraries(Test ${VTK_LIBRARIES})
set_target_properties(Test PROPERTIES CXX_STANDARD 11)

Hope that helps,
- Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160627/1e04f946/attachment.html>


More information about the vtkusers mailing list