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

How to convert dense vector to sparse vector in CUDA ?

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

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

并想将其转换为稀疏格式:

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

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

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