Thrust :: exclusive_scan_by_key unexpected behavior

int data[ 10 ] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int keys[ 10 ] = { 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 };
thrust::exclusive_scan_by_key( keys, keys + 10, data, data );

      

With examples in Thrust Site I expected 0,0,1,1,2,2,3,3,4,4

, but got instead 0,0,0,0,0,0,0,0,0

; Is this a bug, or is there something somewhere that determines this behavior?

More importantly, assuming it's not a bug, can this effect be easily achieved?

+3


source to share


1 answer


I don't think you understand what scan_by_key does. From the doc:

"In particular, consecutive iterators i and i + 1 in the range [first1, last1] belong to the same segment if binary_pred (* i, * (i + 1)) is true and belong to different segments, otherwise"

scan_by_key requires your key array to mark individual segments using contiguous values:

keys: 0 0 0 1 1 1 0 0 0 1 1 1
seg#: 0 0 0 1 1 1 2 2 2 3 3 3

      

traction compares adjacent keys to determine segments.



Your keys create a segment map like this:

int keys[ 10 ] = { 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 };
          seg#:    0  1  2  3  4  5  6  7  8  9

      

Since you are doing an exclusive scan, the correct answer for such a segment map (regardless of the data) is all zeros.

It is not entirely clear what the "effect" you want to achieve is, but you can do stable sorting by key operations by changing the meaning of keys and values ​​by rearranging these data into a group of segments (ie keys 1 and 2) together.

+3


source







All Articles