使用推力device_vector作为全局变量

时间:2014-08-28 10:50:51

标签: cuda thrust

为什么以下代码在main的末尾崩溃?

#include <thrust/device_vector.h>

thrust::device_vector<float4> v;

int main(){
    v.resize(1000);
    return 0;
}

错误是:

terminate called after throwing an instance of 'thrust::system::system_error'
what():  unspecified driver error

如果我使用host_vector代替device_vector,代码运行正常。

你认为它是一个推力错误,还是我在这里做错了什么?

我在使用cuda 4.0的ubuntu 10.10和使用cuda 6.5的Windows 7上尝试过它。 在两种情况下,Thrust版本都是1.7。

感谢

1 个答案:

答案 0 :(得分:4)

这个问题既不是Thrust中的错误,也不是你做错了什么。相反,这是CUDA运行时API设计的限制。

崩溃的根本原因是当变量超出范围时调用thrust::vector的析构函数,这在CUDA运行时API上下文被拆除后发生。这将产生运行时错误(可能是cudaErrorCudartUnloading),因为该进程在已经与CUDA驱动程序断开连接后尝试调用cudaFree

我不知道除了不使用在main()翻译单元范围内声明的Thrust设备容器之外的解决方法。

相关问题