Xing Shi
Xing Shi

Reputation: 2230

How to convert dense vector to sparse vector in CUDA ?

I have a large dense vector(not matrix) in GPU memory:

[1,3,0,0,4,0,0]

and want to convert it into sparse format:

values = [1,3,4]; index = [0,1,4]

I know I can call cusparse<t>dense2csc() in cuSPARSE, but that's designed for matrix, and may not be efficient for vector. Is there any other way to do this ? Or maybe a CUDA kernel. Thanks

Upvotes: 1

Views: 722

Answers (1)

Xing Shi
Xing Shi

Reputation: 2230

Use thrust::copy_if

int * d_index = [1,3,0,0,4,0,0];
int * d_index_compact;

struct non_negative
{
    __host__ __device__
    bool operator()(const int x)
    {
        return x >= 0;
    }
};


thrust::copy_if(thrust::cuda::par, d_index, d_index + this->vocab_size , d_index_compact, non_negative()); // d_index_compact = [1,3,4];

Upvotes: 2

Related Questions