首页 > 解决方案 > 从 std::vector 复制到推力::device_vector 时出现未处理的异常

问题描述

尝试将数据从 a 复制std::vector到 a thrust::device_vectorusing时遇到以下错误thrust::copy

tryThrustCopy.exe 中 0x00007FFD7FF43E49 处的未处理异常:Microsoft C++ 异常:内存位置 0x0000002CB3B9C8B0 处的推力::system::system_error。发生了

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

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

这是我生成错误的代码。

主.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;
}

粒子.h:

#ifndef __particle_h__
#define __particle_h__

class particle
{
private:
    

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

};

#endif

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

如果有人能指出我在哪里犯了错误,我真的很感激。我是 CUDA 的新手,因此也欢迎任何有关如何检查错误的建议。

标签: c++visual-studiocudathrust

解决方案


当我在 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 编程建议是:

您在设备代码中使用的任何对象都应具有在设备代码中可调用的任何可能方法,并用__host__ __device__.


推荐阅读