Search code examples
cudacmakeopenmpthrust

Compilation error using FindCUDA.cmake and Thrust with THRUST_DEVICE_SYSTEM_OMP


I recently discovered that Thrust was able to handle automatic OMP and TBB parallelisation in addition to its classic cuda capability.

Although I was able to use this extremely verstile feature on a simple example, my cmake configuration generated compilation error, maybe I am using FindCUDA.cmake the wrong way, or maybe this module cannot be used with Thrust this way ?

Here is my Test.cu file:

#include <thrust/device_vector.h>
#include <cstdio>

struct cuda_hello
{
  __host__ __device__
  void operator()(int x)
  {
    printf("Hello, world from Cuda!\n");
  }
};

int main()
{
    thrust::device_vector<int> cuda_vec(1, 0);
    thrust::for_each(cuda_vec.begin(),cuda_vec.end(),cuda_hello());
}

And, the working compile line:

nvcc Test.cu -lgomp -DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_OMP -Xcompiler -fopenmp -gencode arch=compute_30,code=sm_30 -o Executable.exe

Now, the cmakefile that cannot generate a Makefiles that compiles properly under linux when using THRUST_DEVICE_SYSTEM_OMP (compile for device of CC 3.0):

PROJECT(ExecutableCmake)
set (OUTPUT_NAME ExecutableCmake)

cmake_minimum_required (VERSION 2.8)

#test variable
#set(THRUST_DEVICE_SYSTEM THRUST_DEVICE_SYSTEM_CUDA)
set(THRUST_DEVICE_SYSTEM THRUST_DEVICE_SYSTEM_OMP)
#set(THRUST_DEVICE_SYSTEM THRUST_DEVICE_SYSTEM_TBB)

########################################
####            Cuda Part           ####
########################################
find_package(CUDA REQUIRED)

list( APPEND CUDA_NVCC_FLAGS -gencode arch=compute_30,code=sm_30 -DTHRUST_DEVICE_SYSTEM=${THRUST_DEVICE_SYSTEM} )

set (sources_gpu_cuda
    Test.cu
)

########################################
####         /Cuda Part             ####
########################################

########################################
####           OMP Part             ####
########################################
set(omp_deps gomp)
########################################
####           /OMP Part            ####
########################################

set (sources
#cuda source files
${sources_gpu_cuda}
)

cuda_add_executable(${OUTPUT_NAME} ${sources} ${headers})
target_link_libraries (${OUTPUT_NAME} ${omp_deps})

The compile errors are of type:

/usr/local/cuda/include/thrust/system/omp/detail/for_each.inl(53): error: incomplete type is not allowed
          detected during:
            instantiation of "RandomAccessIterator thrust::system::omp::detail::for_each_n(thrust::system::omp::detail::execution_policy<DerivedPolicy> &, RandomAccessIterator, Size, UnaryFunction) [with DerivedPolicy=thrust::system::omp::detail::tag, RandomAccessIterator=thrust::device_ptr<int>, Size=unsigned long, UnaryFunction=thrust::detail::host_generate_functor<thrust::detail::fill_functor<int>>]" 
/usr/local/cuda/include/thrust/detail/for_each.inl(69): here
            instantiation of "InputIterator thrust::for_each_n(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, Size, UnaryFunction) [with DerivedPolicy=thrust::system::omp::detail::tag, InputIterator=thrust::device_ptr<int>, Size=unsigned long, UnaryFunction=thrust::detail::host_generate_functor<thrust::detail::fill_functor<int>>]" 
/usr/local/cuda/include/thrust/system/detail/generic/generate.inl(52): here
            instantiation of "OutputIterator thrust::system::detail::generic::generate_n(thrust::execution_policy<ExecutionPolicy> &, OutputIterator, Size, Generator) [with ExecutionPolicy=thrust::system::omp::detail::tag, OutputIterator=thrust::device_ptr<int>, Size=unsigned long, Generator=thrust::detail::fill_functor<int>]" 
/usr/local/cuda/include/thrust/detail/generate.inl(56): here
            instantiation of "OutputIterator thrust::generate_n(const thrust::detail::execution_policy_base<DerivedPolicy> &, OutputIterator, Size, Generator) [with DerivedPolicy=thrust::system::omp::detail::tag, OutputIterator=thrust::device_ptr<int>, Size=unsigned long, Generator=thrust::detail::fill_functor<int>]" 
/usr/local/cuda/include/thrust/system/detail/generic/fill.h(45): here
            instantiation of "OutputIterator thrust::system::detail::generic::fill_n(thrust::execution_policy<DerivedPolicy> &, OutputIterator, Size, const T &) [with DerivedPolicy=thrust::system::omp::detail::tag, OutputIterator=thrust::device_ptr<int>, Size=unsigned long, T=int]" 
/usr/local/cuda/include/thrust/detail/fill.inl(50): here
            [ 6 instantiation contexts not shown ]
            instantiation of "void thrust::detail::contiguous_storage<T, Alloc>::uninitialized_fill_n(thrust::detail::contiguous_storage<T, Alloc>::iterator, thrust::detail::contiguous_storage<T, Alloc>::size_type, const thrust::detail::contiguous_storage<T, Alloc>::value_type &) [with T=int, Alloc=thrust::device_malloc_allocator<int>]" 
/usr/local/cuda/include/thrust/detail/vector_base.inl(164): here
            instantiation of "void thrust::detail::vector_base<T, Alloc>::fill_init(thrust::detail::vector_base<T, Alloc>::size_type, const T &) [with T=int, Alloc=thrust::device_malloc_allocator<int>]" 
/usr/local/cuda/include/thrust/detail/vector_base.inl(139): here
            instantiation of "void thrust::detail::vector_base<T, Alloc>::init_dispatch(IteratorOrIntegralType, IteratorOrIntegralType, thrust::detail::true_type) [with T=int, Alloc=thrust::device_malloc_allocator<int>, IteratorOrIntegralType=int]" 
/usr/local/cuda/include/thrust/detail/vector_base.inl(224): here
            instantiation of "thrust::detail::vector_base<T, Alloc>::vector_base(InputIterator, InputIterator) [with T=int, Alloc=thrust::device_malloc_allocator<int>, InputIterator=int]" 
/usr/local/cuda/include/thrust/device_vector.h(148): here
            instantiation of "thrust::device_vector<T, Alloc>::device_vector(InputIterator, InputIterator) [with T=int, Alloc=thrust::device_malloc_allocator<int>, InputIterator=int]" 


/usr/local/cuda/include/thrust/system/omp/detail/for_each.inl(53): error: incomplete type is not allowed
          detected during:
            instantiation of "RandomAccessIterator thrust::system::omp::detail::for_each_n(thrust::system::omp::detail::execution_policy<DerivedPolicy> &, RandomAccessIterator, Size, UnaryFunction) [with DerivedPolicy=thrust::system::omp::detail::tag, RandomAccessIterator=thrust::detail::normal_iterator<thrust::device_ptr<int>>, Size=long, UnaryFunction=cuda_hello]" 
(89): here
            instantiation of "RandomAccessIterator thrust::system::omp::detail::for_each(thrust::system::omp::detail::execution_policy<DerivedPolicy> &, RandomAccessIterator, RandomAccessIterator, UnaryFunction) [with DerivedPolicy=thrust::system::omp::detail::tag, RandomAccessIterator=thrust::detail::normal_iterator<thrust::device_ptr<int>>, UnaryFunction=cuda_hello]" 
/usr/local/cuda/include/thrust/detail/for_each.inl(43): here
            instantiation of "InputIterator thrust::for_each(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, UnaryFunction) [with DerivedPolicy=thrust::system::omp::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<int>>, UnaryFunction=cuda_hello]" 
/usr/local/cuda/include/thrust/detail/for_each.inl(57): here
            instantiation of "InputIterator thrust::for_each(InputIterator, InputIterator, UnaryFunction) [with InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<int>>, UnaryFunction=cuda_hello]" 


2 errors detected in the compilation of "/tmp/tmpxft_00002d3a_00000000-6_Test.cpp1.ii".
CMake Error at ExecutableCmake_generated_Test.cu.o.cmake:264 (message):
  Error generating file



make[2]: *** [CMakeFiles/ExecutableCmake.dir/./ExecutableCmake_generated_Test.cu.o] Erreur 1
make[1]: *** [CMakeFiles/ExecutableCmake.dir/all] Erreur 2
make: *** [all] Erreur 2

Those errors look exactly like the ones I get when I put cuda code in non .cu files, but I dont know cmake well enough to understand why this problem arise.

Thank you in advance for your help


Solution

  • It looks like you are missing a few flags for nvcc. Adding this worked for me:

    list(APPEND CUDA_NVCC_FLAGS -Xcompiler -fopenmp)