推力排序示例中的崩溃

时间:2014-08-18 09:00:38

标签: c++ thrust

我正在尝试官方网站示例https://developer.nvidia.com/thrust的第一个示例,并将矢量大小更改为32 <&lt; 23。代码如下:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <algorithm>
#include <cstdlib>
#include <time.h>

using namespace std;

int main(void){
  // generate random numbers serially
  thrust::host_vector<int> h_vec(32 << 23);
  std::generate(h_vec.begin(), h_vec.end(), rand);
  std::cout << "1." << time(NULL) << endl;

  // transfer data to the device
  thrust::device_vector<int> d_vec = h_vec;
  cout << "2." << time(NULL) << endl;
  // sort data on the device (846M keys per second on GeForce GTX 480)
  thrust::sort(d_vec.begin(), d_vec.end());
  // transfer data back to host
  thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
  std::cout << "3." << time(NULL) << endl;

  return 0;
}

但是当程序运行到thrust :: sort时,程序崩溃了。我尝试使用std :: vector和std:sort,它运行良好。

这是一个推力的错误?我正在使用Thrust 1.7 + Cuda 6.5 + Visual Studio 2013 Update 2。

我使用的是GeForce GT 740M,总内存为2048M。

我使用processexplorer来监控进程并看到它分配了1.0G内存。但我有2G GPU内存,16G主CPU内存。

错误消息是“导致程序无法正常工作的问题.Windows将关闭程序并在解决方案可用时通知您。[Debug] [Close Program]”。单击[Debug]后,我可以看到调用堆栈。问题出在这一行:

thrust::device_vector<int> d_vec = h_vec;

cuda的最后一个来源是:

testcuda.exe!thrust::system::cuda::detail::malloc<thrust::system::cuda::detail::tag>(thrust::system::cuda::detail::execution_policy<thrust::system::cuda::detail::tag> & __formal, unsigned __int64 n) Line 48  C++

这似乎是内存分配问题。但我有2G GPU内存,16G主CPU内存。为什么?

罗伯特:

原始示例效果很好,即使对于32 <&lt; 21,32&lt;&lt; 22。是否有GPU内存的虚拟内存管理系统?这里的连续是指物理连续还是虚拟?在这种情况下是否有任何例外,我可以抓住它?

我的测试代码在这里:https://github.com/henrywoo/wufuheng/blob/master/testcuda.cu

在我的测试中,没有异常,但是运行时错误。

1 个答案:

答案 0 :(得分:1)

sizeof(int) * 32<<23 = 4* 2^28
I.e.你正在分配大约1 GB的GPU RAM。最有可能的是,你的卡无法处理那么多元素。这可能是因为:

  • 一般没有足够的GPU RAM
  • 没有足够的连续免费GPU RAM(这是必需的,因为矢量必须适合连续的内存)