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).