gpt4 book ai didi

c++ - 推力::主机执行策略的段错误

转载 作者:行者123 更新时间:2023-12-02 09:52:02 31 4
gpt4 key购买 nike

我尝试将数据从主机复制到设备并返回,但不是使用 CUDA API,而是使用推力库。我在 thrust::host_vector 中分配了内存,并尝试将其复制到 thrust::device_vector .但是,当使用 thrust::copythrust::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 执行策略的实际作用以及使用它们时要考虑哪些约束的文档非常稀缺。
什么是推力::复制不能与推力::主机执行策略一起工作的潜在原因是什么? 请注意,不明确指定参数可以正常工作。我正在使用的机器是 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)

最佳答案

您不应该使用 thrust::host 的执行策略。或 thrust::device使用 thrust::copy 时在主机和设备之间复制数据。
通过阅读 thrust::device 的文档,其原因是相当明显的。 (例如)。

Instead of relying on implicit algorithm dispatch through iterator system tags, users may directly target algorithm dispatch at Thrust's device system by providing thrust::device as an algorithm parameter.


thrust::host 提供了类似的措辞和意图
当然,这不是您在主机和设备之间复制数据时想要的。您依赖于对迭代器的检查来确定传输方向等。路过 thrust::host意味着推力可以解释两个地址(最终迭代器被减少为复制操作使用的地址),就好像它们是有效的主机地址一样,因此执行主机->主机复制。如果这些地址之一是设备地址,则会导致段错误。
路过 thrust::device意味着推力可以将两个地址都解释为有效的设备地址,因此执行设备->设备复制。如果其中一个地址是主机地址,则将导致无效参数错误或非法地址错误(如果拷贝是通过内核实现的。在我的测试中,我碰巧看到了非法地址错误)。
以上当然是我希望在非 Power9 系统上看到的行为。如果您认为您应该在 Power9 系统上看到不同的东西,您可能希望提交一个推力问题。然而,不管平台如何,为这个算法传递一个执行策略对我来说似乎是荒谬的。

关于c++ - 推力::主机执行策略的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63766633/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com