【发布时间】:2020-11-04 12:39:10
【问题描述】:
我正在编写一个使用多个并发 CUDA 流的应用程序。当我的thrust::reduce_by_key 调用似乎写入可分页内存时,我的其他流被阻塞。我认为返回值是问题。
如何防止返回值被写入可分页内存?
我将包含演示我尝试的解决方案的代码。
#include <thrust/system/cuda/vector.h>
#include <thrust/host_vector.h>
#include <thrust/pair.h>
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <thrust/random.h>
int main(void)
{
int N = 20;
thrust::default_random_engine rng;
thrust::uniform_int_distribution<int> dist(10, 99);
// initialize data
thrust::device_vector<int> array(N);
for (size_t i = 0; i < array.size(); i++)
array[i] = dist(rng);
// allocate storage for sums and indices
thrust::device_vector<int> sums(N);
thrust::device_vector<int> indices(N);
// make a pinned memory location for the returned pair of iterators
typedef thrust::device_vector<int>::iterator dIter;
thrust::pair<dIter, dIter>* new_end;
const unsigned int bytes = sizeof(thrust::pair<dIter, dIter>);
cudaMallocHost((void**)&new_end, bytes);
for(int i = 0 ; i< 20; i++){ // you can see in the profiler each operator writes 4 bytes to pageable memory
*new_end = thrust::reduce_by_key
(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(N),
array.begin(),
indices.begin(),
sums.begin(),
thrust::equal_to<int>(),
thrust::plus<int>());
}
std::cout << "done \n";
return 0;
}
【问题讨论】:
-
只是一个提示 -- Stack Overflow 是一个问答网站。 “我需要帮助”不是问题。这个地方的整个想法是您提出可能与更广阔的世界相关且有用的问题。有人回答了这个问题,并且问答对添加到每个人都可以使用的潜在有用信息的语料库中
-
使用 cub::DeviceReduce::ReduceByKey 代替。然后你需要自己管理复制到主机,并且可以为它使用固定内存。
-
我更改了标题和文字,希望更贴切地反映 Stack Overflow 的目的。我希望这个问题的答案能帮助更多的开发者,而不仅仅是我。
-
补充讨论here