【问题标题】:Unhandled exception when copying from a std::vector to a thrust::device_vector从 std::vector 复制到推力::device_vector 时出现未处理的异常
【发布时间】:2020-12-09 06:29:45
【问题描述】:

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

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

我正在使用带有 Visual Studio 16、Visual C++ 2019(版本 14)、CUDA 11.0 的 Windows 10,我的 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;
}

粒子.h:

#ifndef __particle_h__
#define __particle_h__

class particle
{
private:
    

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

};

#endif

一般来说,我试图将粒子对象的宿主向量复制到设备向量。我还发现使用上述代码将整数向量 (vector&lt;int&gt;) 从主机复制到设备效果很好。

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

【问题讨论】:

    标签: c++ visual-studio cuda thrust


    【解决方案1】:

    当我在 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__ 装饰。

    【讨论】:

    • 非常感谢!是的,这解决了问题。我不知道在设备中调用了构造函数。 (初始化设备向量时是否调用?)谢谢您的建议。
    • 在 CUDA 11 上,我相信构造函数在这一行被调用:dParticles.resize(particles.size());
    • 或者,可以使用reserve 代替resize
    • reserve 是一个提示。回想起来,我认为constructor-on-resize是有道理的。
    猜你喜欢
    • 2013-06-08
    • 2019-08-29
    • 2013-01-04
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    相关资源
    最近更新 更多