sorting - cuda/thrust : Trying to sort_by_key 2. 6GB GPU RAM 中的 8GB 数据抛出 bad_alloc

标签 sorting cuda thrust bad-alloc

我刚刚开始使用推力,到目前为止我遇到的最大问题之一是似乎没有关于需要多少内存操作的文档。所以我不确定为什么下面的代码在尝试排序时会抛出 bad_alloc(在排序之前我仍然有 >50% 的 GPU 内存可用,并且我在 CPU 上有 70GB 的 RAM 可用)——任何人都可以阐明一下这个?

#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/random.h>

void initialize_data(thrust::device_vector<uint64_t>& data) {
  thrust::fill(data.begin(), data.end(), 10);
}

int main(void) {
  size_t N = 120 * 1024 * 1024;
  char line[256];
  try {
    std::cout << "device_vector" << std::endl;
    typedef thrust::device_vector<uint64_t>  vec64_t;

    // Each buffer is 900MB

    vec64_t c[3] = {vec64_t(N), vec64_t(N), vec64_t(N)};
    initialize_data(c[0]);
    initialize_data(c[1]);
    initialize_data(c[2]);

    std::cout << "initialize_data finished... Press enter";
    std::cin.getline(line, 0);

    // nvidia-smi reports 48% memory usage at this point (2959MB of                 
    // 6143MB)

    std::cout << "sort_by_key col 0" << std::endl;

    // throws bad_alloc

    thrust::sort_by_key(c[0].begin(), c[0].end(),
      thrust::make_zip_iterator(thrust::make_tuple(c[1].begin(),
      c[2].begin())));

    std::cout << "sort_by_key col 1" << std::endl;
    thrust::sort_by_key(c[1].begin(), c[1].end(),
        thrust::make_zip_iterator(thrust::make_tuple(c[0].begin(),
        c[2].begin())));
  } catch(thrust::system_error &e) {
    std::cerr << "Error: " << e.what() << std::endl;
    exit(-1);
  }
  return 0;
}

这是我编译代码的方式

nvcc -o ./bad_alloc ./bad_alloc.cu

最佳答案

考虑到 Robert Crovella 的评论,这就是我使用 cudaMemGetInfo() 使用 39% 的 GPU RAM 的代码(这是在禁用了 ECC 的 nvidia tesla 卡上,否则该值需要更低).

#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/random.h>

void initialize_data(thrust::device_vector<uint64_t>& data) {
  thrust::fill(data.begin(), data.end(), 10); }

#define BUFFERS 3

int main(void) {                                                                  
  size_t total_gpu_bytes;
  cudaMemGetInfo(0, &total_gpu_bytes);
  size_t N = (total_gpu_bytes * .39) / sizeof(uint64_t) / BUFFERS;
  try {
    std::cout << "device_vector " << (N/1024.0/1024.0) << std::endl;
    typedef thrust::device_vector<uint64_t>  vec64_t;
    vec64_t c[BUFFERS] = {vec64_t(N), vec64_t(N), vec64_t(N)};
    initialize_data(c[0]);
    initialize_data(c[1]);
    initialize_data(c[2]);
    thrust::sort_by_key(c[0].begin(), c[0].end(),
        thrust::make_zip_iterator(thrust::make_tuple(c[1].begin(),
        c[2].begin())));
    thrust::sort_by_key(c[1].begin(), c[1].end(),
        thrust::make_zip_iterator(thrust::make_tuple(c[0].begin(),
        c[2].begin())));
  } catch(thrust::system_error &e) {
    std::cerr << "Error: " << e.what() << std::endl;
    exit(-1);
  }
  return 0;
}

关于sorting - cuda/thrust : Trying to sort_by_key 2. 6GB GPU RAM 中的 8GB 数据抛出 bad_alloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13306793/

相关文章:

algorithm - 在 log(n) 时间内搜索元素

arrays - 具有 max(abs(x),abs(y)) 的元素 x(i) 或 y(i) 的 MATLAB 向量

textures - 在 CUDA 中不灵活地使用纹理引用是否有解决方法

performance - Cuda Bayer/CFA 去马赛克示例

memory - 合并是否触发以相反的顺序访问内存?

c++ - 从 C++ 访问 device_vector 的最佳方式

c++ - Thrust:对另一个数组索引的数组元素求和【Matlab的语法 sum(x(indices))】

python - 为什么我的 csv 排序功能不起作用?

javascript - 使用仅显示唯一值的 D3 对列表进行排序

c++ - 为什么我的 CUDA 程序的初始执行时间比后续执行时间长?