Xing Shi Xing Shi - 1 year ago 117
C++ Question

How to convert dense vector to sparse vector in CUDA ?

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


and want to convert it into sparse format:

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

I know I can call
, 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

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download