2017-03-09 88 views
1

我在GPU存储器大的致密的载体(未矩阵):如何将稠密矢量转换为CUDA中的稀疏矢量?

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

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

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

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

+0

向量可以看作是只有1列(或1行)矩阵,所以无法与您通话“列数”设置为1'dense2csc'? –

+1

此转换可以通过在[GPU Gems 3](http://http.developer.nvidia.com/GPUGems3/gpugems3_ch39.html)中描述的流压缩算法中稍作修改来实现。 – sgarizvi

+1

修改是,不只是写矢量值,我们可以在最后阶段写入值和索引。 – sgarizvi

回答

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]; 
相关问题