使用FindCUDA.cmake和Thrust以THRUST_DEVICE_SYSTEM_OMP编译时出现错误

4

最近我发现,Thrust除了经典的CUDA功能外,还能处理自动OMP和TBB并行化。

虽然我在一个简单的示例中成功使用了这个极为灵活的功能,但我的cmake配置生成了编译错误。也许我没有正确使用FindCUDA.cmake,或者这个模块不能以这种方式与Thrust一起使用?

这是我的Test.cu文件:

#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());
}

同时,下面是可行的编译命令:

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

现在,这个CMake文件不能生成适用于Linux的Makefile,当使用THRUST_DEVICE_SYSTEM_OMP(设备编译为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})

编译错误有以下类型:
/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

这些错误看起来和我在非 .cu 文件中放置 CUDA 代码时遇到的错误完全相同,但是我不太了解 cmake,无法理解为什么会出现这个问题。

提前感谢你的帮助。

1个回答

2

看起来您在nvcc中缺少一些标志。添加以下内容对我有用:

list(APPEND CUDA_NVCC_FLAGS -Xcompiler -fopenmp)

太棒了,它运行了!确实,我忘记添加与命令行中使用的完全相同的标志。 - Tobbey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接