简体   繁体   中英

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. I am using CUSP library. I don't know how to return cusp::csr_matrix back to 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. Now, I have to return it to Matlab. Something like this:

plhs[0] = At;

But, of course, it doesn't work like that, firstly because At is on GPU. I guess I should use At.values and methods for indexes. 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. All you have to do is to allocate the sparse matrix using mxArray *mxCreateSparse(mwSize m, mwSize n, mwSize nzmax, mxComplexity ComplexFlag); and then setting the pr, ir, jc arrays (using mxGetPr , mxGetIr , mxGetJc ). Pr corresponds to the values array in cusp, ir to column_indices and jc to row_offsets. If the matrix is in device memory, copy it using cudaMemcpy with cudaMemcpyDeviceToHost . Here is some examples using sparse matrices (its for Octave, but should work for Matlab as well).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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