简体   繁体   English

如何从 CUDA 中的推力 device_ptr 获得偏移量?

[英]How to get offset from thrust device_ptr in CUDA?

Consider the following code:考虑以下代码:

thrust::device_ptr<uint> dev_ptr_keys(ptrCellIDs);

thrust::device_ptr<uint> dev_ptr_offset(ptrOffset);
thrust::pair<thrust::discard_iterator<>, thrust::device_ptr<uint>> end;

end = thrust::reduce_by_key(dev_ptr_keys, dev_ptr_keys + 10,
    thrust::make_constant_iterator(1), 
    thrust::make_discard_iterator(), 
    dev_ptr_offset, binary_pred, binary_op);

The reduce_by_key function in this case should create an array of offsets of equal values in the array the device pointer dev_ptr_keys points to and save it to the array at dev_ptr_offset .在这种情况下, reduce_by_key function 应该在设备指针dev_ptr_keys指向的数组中创建一个相等值的偏移量数组,并将其保存到dev_ptr_offset的数组中。 Therefore:所以:

Intput (dev_ptr_keys): { 1, 4, 4, 4, 2, 2, 1 }输入 (dev_ptr_keys): { 1, 4, 4, 4, 2, 2, 1 }

Output (dev_ptr_offset): { 1, 3, 2, 1 } Output (dev_ptr_offset): { 1, 3, 2, 1 }

So far, so good - however I also need to know the size of the new valid output array, which should be saved in end.second .到目前为止,一切都很好 - 但是我还需要知道新的有效 output 数组的大小,它应该保存在end.second中。 But, I don't manage to get this information.但是,我无法获得这些信息。 I tried to play around with thrust::raw_pointer_cast() , but I don't manage to get the value neither within my kernel (by accessing the pointer wrapped by the thrust device pointer), nor in my host code.我试图玩弄thrust::raw_pointer_cast() ,但我既没有在我的 kernel 中(通过访问由推力设备指针包裹的指针),也没有在我的主机代码中获得该值。

How can I get the information of the size of my output in this particular case?在这种特殊情况下,如何获取 output 的尺寸信息?

Well, of all the things I tried the following was the last and it works the way I want:好吧,在我尝试的所有事情中,以下是最后一个,它按我想要的方式工作:

uint numOffset = thrust::raw_pointer_cast(&end.second[0]) - thrust::raw_pointer_cast(&dev_ptr_offset[0]);

On question remains: is this efficient at all, or does it even matter?问题仍然存在:这是否有效,或者它甚至重要吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM