简体   繁体   English

如何在CUDA中将密集向量转换为稀疏向量?

[英]How to convert dense vector to sparse vector in CUDA ?

I have a large dense vector(not matrix) in GPU memory: 我在GPU内存中有一个较大的密集向量(不是矩阵):

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

and want to convert it into sparse format: 并希望将其转换为稀疏格式:

values = [1,3,4]; 值= [1,3,4]; index = [0,1,4] 索引= [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. 我知道我可以在cuSPARSE调用cusparse<t>dense2csc() ,但这是为矩阵设计的,对于矢量而言可能不是有效的。 Is there any other way to do this ? 还有其他方法吗? Or maybe a CUDA kernel. 也许是CUDA内核。 Thanks 谢谢

Use thrust::copy_if 使用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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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