首页 文章

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

提问于
浏览
1

我在GPU内存中有一个大的密集向量(不是矩阵):

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

并希望将其转换为稀疏格式:

values = [1,3,4];指数= [0,1,4]

我知道我可以在 cuSPARSE 中调用 cusparse<t>dense2csc() ,但这是为矩阵设计的,对于矢量可能效率不高 . 有没有其他方法可以做到这一点?或者也许是CUDA内核 . 谢谢

1 回答

  • 2

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

相关问题