【发布时间】:2020-12-25 05:30:36
【问题描述】:
我尝试将数据从主机复制到设备并返回,但不是使用 CUDA API 而是推力库。我在thrust::host_vector 中分配了内存,并尝试将其复制到thrust::device_vector。但是,当使用thrust::copy 和thrust::host 执行策略从主机 设备传输任何数据时,程序会因分段错误而崩溃。 cuda-memcheck 提供以下错误消息:
Error: process didn't terminate successfully
The application may have hit an error when dereferencing Unified Memory from the host.
关于thrust::host 和thrust::device 执行策略的实际作用以及使用它们时需要考虑哪些限制的文档非常少。
thrust::copy 不能与thrust::host 执行策略一起工作的潜在原因是什么? 请注意,不明确指定参数可以正常工作。我正在使用的机器是POWER9 机器。
这是一个可重现的小例子:
使用nvcc -O3 -std=c++11 -Xcompiler -fopenmp test.cu -o test构建
#include <vector>
#include <omp.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#define NUM_GPUS 4
int main(int argc, char *argv[]) {
size_t num_elements = 10000;
size_t block_size = num_elements/4;
thrust::host_vector<int> hvec(num_elements);
std::vector<thrust::device_vector<int>*> dvecs(NUM_GPUS);
#pragma omp parallel for
for (size_t i = 0; i < NUM_GPUS; ++i)
{
cudaSetDevice(i);
dvecs[i] = new thrust::device_vector<int>(block_size);
thrust::copy( thrust::host,
hvec.begin() + (block_size * i),
hvec.begin() + (block_size * (i + 1)),
dvecs[i]->begin());
}
return 0;
}
nvcc: NVIDIA (R) Cuda compiler driver
Cuda compilation tools, release 10.2, V10.2.89
gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
【问题讨论】:
-
如果没有看到minimal reproducible example,您的问题将无法得到合理的回答。发生这种情况的潜在原因有很多。请发布一些将您的问题重现到您的问题中的代码。
-
@πάνταῥεῖ 你说得对,我现在更新了问题以包含这样的示例代码。