Search code examples
cudagpgputhrust

How to fill missing values in array with next non-missing value using CUDA Thrust?


By using CUDA Thrust's inclusive scan with a max operator, I'm able to fill missing values with the previous non-missing value (i.e. last non-missing value to the left).

But how to fill using next non-missing value (to the right)? Thus, for example, using 0 as my missing value marker:

                  Input: [0 1 0 0 4 0 6 0]
 Fill missing from left: [0 1 1 1 4 4 6 6]
Fill missing from right: [0 1 4 4 4 6 6 6]   <- want

(Note if the last element is missing then revert to filling final 0s from left.)

I've tried a inclusive scan in reverse, which yields [0 6 6 6 6 6 6 6] for max, not as desired.

Many thanks.


Solution

  • The reason max() works on ascending values when scanning from left to right is that the current maximum value will always be higher than your 0 missing element, so it becomes the correct value for filling in the missing elements even though it has a "memory" coming all the way from the beginning.

    If you simply scan from right to left, max() no longer works, because you then have a descending range.

    So, it seems like you need to use rbegin() and rend() to scan from right to left and, in addition, use MAX_INT as your place-holder and min() as your operator.

    Input: [MAX_INT 1 MAX_INT MAX_INT 4 MAX_INT 6 MAX_INT]
    Fill missing from right: [1 1 4 4 4 6 6 MAX_INT]
    

    Then, you need to fudge things for your special cases on the left and right.