Roland Deschain
Roland Deschain

Reputation: 2880

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. Therefore:

Intput (dev_ptr_keys): { 1, 4, 4, 4, 2, 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. 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.

How can I get the information of the size of my output in this particular case?

Upvotes: 0

Views: 304

Answers (1)

Roland Deschain
Roland Deschain

Reputation: 2880

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?

Upvotes: 1

Related Questions