简体   繁体   中英

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

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM