无法创建Thrust设备向量

3

所以我正在尝试开始使用GPU编程并使用Thrust库简化事情。 我创建了一个测试程序来与之一起工作并查看其工作原理,但是每当我尝试创建一个具有非零大小的thrust::device_vector时,程序就会崩溃,并显示“Run-time Check Failure #3 - The variable 'result' is being used without being initialized.”(此错误来自allocator_traits.inl文件),而且我不知道该如何解决。 以下是引起此错误所需的所有内容。

#include <thrust/device_vector.h>

int main()
{
    int N = 100;
    thrust::device_vector<int> d_a(N);
    return 0;
}

我怀疑这可能是环境设置的问题,因此在此提供细节...

使用Visual Studio 2019创建,CUDA 11.0运行时项目(打开该项目时给出的示例程序可以正常工作),Thrust版本为1.9,所使用的GPU为GTX 970。


如果将代码改为 { thrust::device_vector<int> d_a(N); } 会发生什么? - talonmies
1
你正在构建一个调试项目吗?尝试切换到发布项目。如果这对你来说是个问题,我建议按照这里的说明在developer.nvidia.com上提交一个错误报告。顺便说一句,作为一个简单的测试,我能够在对话框上点击“忽略”,并且运行/调试CUDA程序。你可能还会在执行结束时收到一个对话框,你也可以点击“忽略”。这似乎只出现在调试项目中。(也许只有在CUDA 11下才会出现这个问题。我无法在CUDA 10.1下重现这个问题) - Robert Crovella
切换到发布模式或忽略弹出窗口都可以解决问题。我没有预料到程序在此之后能够正常运行,但考虑到它被指定为调试错误,这有点合理。非常感谢! - Crock
2个回答

1
此问题似乎只出现在与CUDA 11.0相关的推力版本(1.9.x)中,仅在Windows/Visual Studio上的调试项目中出现。
一些解决方法是切换到构建发布项目,或者只需在运行时单击“忽略”弹出的对话框。根据我的测试,在这一点上,这允许普通运行或调试。
我没有确认过,但我相信最新的thrust(1.10.x)已经修复了这个问题(虽然目前还不是任何正式CUDA版本的一部分,但我希望它会成为未来某个CUDA版本的一部分)。

1

在Robert Crovella的回答之后,我通过使用GitHub上的代码更改了thrust库中相应的代码行来解决了这个问题。更确切地说,在文件...\CUDA\v11.1\include\thrust\detail\allocator\allocator_traits.inl中,我替换了以下函数。

template<typename Alloc>
__host__ __device__
  typename disable_if<
    has_member_system<Alloc>::value,
    typename allocator_system<Alloc>::type
  >::type
    system(Alloc &)
{
  // return a copy of a default-constructed system
  typename allocator_system<Alloc>::type result;
  return result;
}

by

template<typename Alloc>
__host__ __device__
  typename disable_if<
    has_member_system<Alloc>::value,
    typename allocator_system<Alloc>::type
  >::type
    system(Alloc &)
{
  // return a copy of a default-constructed system
  return typename allocator_system<Alloc>::type();
}

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