从 std::vector 复制到 thrust::device_vector 时出现未处理的异常

Unhandled exception when copying from a std::vector to a thrust::device_vector

我在尝试使用 thrust::copy 将数据从 std::vector 复制到 thrust::device_vector 时遇到以下错误:

Unhandled exception at 0x00007FFD7FF43E49 in tryThrustCopy.exe: Microsoft C++ exception: thrust::system::system_error at memory location 0x0000002CB3B9C8B0. occurred

我正在使用 Windows 10 和 Visual Studio 16,Visual C++ 2019(版本 14),CUDA 11.0,我的 GPU 驱动程序版本是 455.41。

在Visual Studio的调试模式下报错。发布配置中的程序从命令行运行,但会在复制步骤终止。

这是我生成错误的代码。

main.cpp:

#include <vector>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <iostream>
#include "particle.h"

int main(int argc, char** argv)
{   

    std::vector<particle> particles(5);

    particles.at(0).x += 1; // Do something to the host vector.

    thrust::device_vector<particle> dParticles;

    dParticles.resize(particles.size());

    //Here comes the error.
    thrust::copy(particles.begin(), particles.end(), dParticles.begin());

    std::cout << "test 2 ends" << std::endl;

    return 0;
}

particle.h:

#ifndef __particle_h__
#define __particle_h__

class particle
{
private:
    

public:
    particle() : x(0), y(0) {}
    int x;
    int y;

};

#endif

一般来说,我试图将粒子对象的宿主向量复制到设备向量。我还发现使用上述代码将整数向量 (vector<int>) 从主机复制到设备工作正常。

如果有人能指出我的错误,我将不胜感激。我是CUDA的新手,所以也欢迎任何关于如何检查错误的建议。

当我在 CUDA 10.2 上编译您的代码时,我收到以下警告(截断):

.../targets/x86_64-linux/include/thrust/detail/allocator/allocator_traits.inl(97): warning: calling a __host__ function from a __host__ __device__ function is not allowed

在进行 CUDA 或 Thrust 编程时,您不应该忽略这样的警告

不幸的是,出于某种原因,我在 CUDA 11 上没有收到该警告。尽管如此,它是实际问题的重要线索。

它可能指的是什么功能?它指的是你的构造函数:

particle() : x(0), y(0) {}

显然,该构造函数以某种方式在设备代码中被调用(在失败的情况下)

因此,正确的做法是用以下内容装饰该构造函数:

__host__ __device__
particle() : x(0), y(0) {}

这会使警告消失(CUDA 10.2),对我而言,您的代码随后可以在 CUDA 11 上正常运行。

(在 CUDA 10.2 上,我收到警告的行为是这样的,但即使有警告,代码运行时也没有错误。在 CUDA 11.0 上,我没有收到警告,但代码运行时错误,除非你正确地装饰了构造函数。所以我假设引擎盖下的推力行为存在显着差异。)

不过,我给出的一般 CUDA 编程建议是:

Any objects that you use in device code should have any possible methods that are callable in device code, decorated with __host__ __device__.