不允许通过从__host__ __device__函数调用__host__函数来编译推力集差异

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不允许通过从__host__ __device__函数调用__host__函数来编译推力集差异相关的知识,希望对你有一定的参考价值。

我有两组A和B分别为20和10整数。 B是A的子集。我需要找到B的互补集。我使用thrust :: set_difference来查找集合差异,但是无法使用消息编译:warning: calling a __host__ function from a __host__ __device__ function is not allowed

我的代码如下。我不知道为什么这个简单的代码无法编译。

#include <thrust/sequence.h>
#include <thrust/execution_policy.h>
#include <thrust/set_operations.h>
#include <thrust/device_vector.h>


        thrust::device_vector<int> find_complimentary_set(thrust::device_vector<int> A, thrust::device_vector<int> B)
        {

        thrust::sort(thrust::device, A.begin(), A.end());
        thrust::sort(thrust::device, B.begin(), B.end());

        int N=A.size()-B.size();
        thrust::device_vector<int> complimentary_set(N);

        **// the following line causes the compilation error**
        thrust::set_difference(thrust::device, A.begin(), A.end(), B.begin(), B.end(), complimentary_set);

        return complimentary_set;

        }
            int main(int argc, char * argv[])
            {
            int N=20;
            thrust::device_vector<int> A(N);
            thrust::sequence(thrust::device, A.begin(), A.end(),0); 

            thrust::device_vector<int> B(10);
            B[0]=2;B[1]=4;B[2]=8;B[3]=10;B[4]=11;B[5]=13;B[6]=15;B[7]=17;B[8]=19;B[9]=6;

            find_complimentary_set(A, B);

            return 0;
            }

我的编译错误很大。我在其中找到了8个错误,我在下面列出:

    /usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/cuda/detail/detail/set_operation.inl(517): error: no operator "+" matches these operands
                operand types are: thrust::device_vector<int, thrust::device_malloc_allocator<int>> + thrust::reference<signed long, thrust::pointer<signed long, thrust::system::cuda::detail::par_t, thrust::use_default, thrust::use_default>, thrust::use_default>

    /usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/cuda/detail/detail/set_operation.inl(648): error: no operator "+" matches these operands
                operand types are: thrust::device_vector<int, thrust::device_malloc_allocator<int>> + signed long

/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/detail/sequential/set_operations.h(64): error: no operator "*" matches these operands
            operand types are: * thrust::device_vector<int, thrust::device_malloc_allocator<int>>

/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/detail/sequential/set_operations.h(66): error: no operator "++" matches these operands
            operand types are: ++ thrust::device_vector<int, thrust::device_malloc_allocator<int>>

/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/iterator/iterator_traits.h(49): error: class "thrust::device_vector<int, thrust::device_malloc_allocator<int>>" has no member "iterator_category"

/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/detail/sequential/general_copy.h(106): error: no operator "++" matches these operands
            operand types are: ++ thrust::device_vector<int, thrust::device_malloc_allocator<int>>

/usr/local/cuda/bin/../targets/x86_64-linux/include/thrust/system/detail/sequential/general_copy.h(76): error: no operator "*" matches these operands
            operand types are: * thrust::device_vector<int, thrust::device_malloc_allocator<int>>
答案

使用thrust :: set_difference时,参数是迭代器而不是device_vectors。 set_difference的最后一个参数必须是complementary_set.begin()而不是complimentary_set。

现在整个正确的陈述如下:

thrust::set_difference(thrust::device, A.begin(), A.end(), B.begin(), B.end(), complimentary_set.begin());

以上是关于不允许通过从__host__ __device__函数调用__host__函数来编译推力集差异的主要内容,如果未能解决你的问题,请参考以下文章

不允许从 __device__ 函数调用 __host__ 函数的 cuda::cub 错误

cuda nvcc 使 __device__ 有条件

__shared__ 变量可以封装在 __device__ __host__ 函数中吗?

推力 copy_if:不完整的类型是不允许的

CUDA C Programming Guide 在线教程学习笔记 Part 5

如何修复警告:函数范围的静态 __shared__ 变量不支持动态初始化?