3

私はスラストを使い始めたばかりですが、これまでの最大の問題の1つは、メモリ操作に必要な量に関するドキュメントがないように見えることです。したがって、ソートしようとしたときに以下のコードがbad_allocをスローする理由がわかりません(ソートする前に、GPUメモリの50%以上が使用可能であり、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
4

1 に答える 1

1

Robert Crovella のコメントを考慮して、cudaMemGetInfo() を使用して GPU RAM の 39% を使用するコードがどのように機能するかを示します (これは、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;
}
于 2012-11-09T19:10:06.263 に答える