Thanks for the help, the stencil is a nice idea but the results are not what I wanted.
I need to copy the values in an offset (of 1…x)
So in your output the values are:
// {-1, 2, 3, 4, -1, 6, 7, 8}
whereas the output I need is this:
// {2, 3, 4, -1, 6, 7, 8, -1}
I guess this is a general thrust question.
How I can iterate/manipulate/read values from an underlying device pointer in a different
offset than the one thrust is currently looking at.
I solved it like this, but I wanted to know if there’s a more elegant way:
template <typename T>
struct LagWithNulls
{
size_t m_rows;
unsigned int m_offset;
unsigned int *m_partitions;
T *m_aggregated;
T *m_out;
LagWithNulls(size_t rows, unsigned int offset, unsigned int *partitions, T *aggergated, T *out)
: m_rows(rows), m_offset(offset), m_partitions(partitions), m_aggregated(aggergated),
m_out(out) {}
__device__ void operator()(unsigned int index)
{
unsigned int partition = m_partitions[index];
int lag_index = index - m_offset;
if (lag_index < 0) {
m_out[index] = 0;
return;
}
unsigned int lag_partition = m_partitions[lag_index];
if (partition == lag_partition)
{
m_out[index] = m_aggregated[lag_index];
}
else
{
m_out[index] = 0;
}
}
};
thrust::counting_iterator<unsigned int> index(0);
thrust::for_each(index, index + N,
LagWithNulls<T>(N, offset,
thrust::raw_pointer_cast(d_partitions.data()),
thrust::raw_pointer_cast(d_aggregated.data()),
thrust::raw_pointer_cast(d_sum_out.data()))