使用thrust device_vector作为全局变量

3
为什么下面的代码在 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,代码就可以正常运行。你觉得这是一个Thrust的bug,还是我做错了什么?我在Ubuntu 10.10和CUDA 4.0以及Windows 7和CUDA 6.5上尝试过。在这两种情况下,Thrust版本都是1.7。谢谢。

3
这里发生的不是那样的情况。原因是向量超出了作用域范围,在CUDA上下文被撤销后被销毁。因此,实际上cudaFree在没有有效的运行时API连接的情况下被调用,导致运行时错误。 - talonmies
1个回答

5
问题既不是Thrust中的错误,也不是你做错了什么。相反,这是CUDA运行时API设计的限制。
崩溃的根本原因是在变量超出范围后,thrust::vector的析构函数被调用,而这是在CUDA运行时API上下文被拆除之后发生的。这将产生一个运行时错误(可能是cudaErrorCudartUnloading),因为进程试图在已经断开与CUDA驱动程序的连接后调用cudaFree。
我不知道有什么解决方法,除了不使用在main()函数翻译单元作用域中声明的Thrust设备容器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接