简体   繁体   English

如何从MEX中的CUSP :: csr_matrix返回稀疏矩阵到Matlab?

[英]How to return sparse matrix from CUSP::csr_matrix in MEX to Matlab?

I am developing my MEX file for sparse matrix computing with CUDA. 我正在开发用于CUDA的稀疏矩阵计算的MEX文件。 I am using CUSP library. 我正在使用CUSP库。 I don't know how to return cusp::csr_matrix back to Matlab. 我不知道如何将cusp :: csr_matrix返回到Matlab。 For example, I have 例如,我有

cusp::csr_matrix<mwIndex,double,cusp::host_memory> At(m,n,N);

So, it is At matrix in CSR format, which, lets say, I have computed. 因此,它是CSR格式的At矩阵,可以说,我已经计算过了。 Now, I have to return it to Matlab. 现在,我必须将其返回给Matlab。 Something like this: 像这样:

plhs[0] = At;

But, of course, it doesn't work like that, firstly because At is on GPU. 但是,当然,它不是那样工作的,首先是因为At在GPU上。 I guess I should use At.values and methods for indexes. 我想我应该对索引使用At.values和方法。 But, also, how to assign them to host data? 但是,又如何将它们分配给主机数据?

Could somebody suggest how to do all that? 有人可以建议怎么做吗? :) :)

Matlab stores sparse matrices in CSR format too, so it's not complicated. Matlab也以CSR格式存储稀疏矩阵,因此并不复杂。 All you have to do is to allocate the sparse matrix using mxArray *mxCreateSparse(mwSize m, mwSize n, mwSize nzmax, mxComplexity ComplexFlag); 您要做的就是使用mxArray *mxCreateSparse(mwSize m, mwSize n, mwSize nzmax, mxComplexity ComplexFlag);分配稀疏矩阵mxArray *mxCreateSparse(mwSize m, mwSize n, mwSize nzmax, mxComplexity ComplexFlag); and then setting the pr, ir, jc arrays (using mxGetPr , mxGetIr , mxGetJc ). 然后设置pr,ir,jc数组(使用mxGetPrmxGetIrmxGetJc )。 Pr corresponds to the values array in cusp, ir to column_indices and jc to row_offsets. Pr对应于cusp中的值数组,ir对应于column_indices,jc对应于row_offsets。 If the matrix is in device memory, copy it using cudaMemcpy with cudaMemcpyDeviceToHost . 如果矩阵在设备内存中,请使用cudaMemcpycudaMemcpyDeviceToHost复制它。 Here is some examples using sparse matrices (its for Octave, but should work for Matlab as well). 是一些使用稀疏矩阵的示例(它适用于Octave,但也适用于Matlab)。

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

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