Mex:如何将稀疏矩阵正确分配给LHS(输出)以及如何正确清理内存

时间:2015-02-10 15:26:13

标签: c++ c matlab mex

我编写了一个mex代码,它从matlab代码向C代码发送标量和矩阵。我的代码应该是return the sparse matrix after putting it in TAUCS format(第12页;也是列主要格式)。

我担心我可能not be assigning LHS correctly并返回空指针或我可能not be cleaning the memory properly。如果我做错了,有人会告诉我吗?

我的matlab代码的一部分:

% Create system matrix (size 8448 x 3264) 
smA_System = ConstructSystemMatrix();

x = 9;
y = ones(3);

% This works fine
z = mexcallingmatlab(x, y);

% This gives error
z = mexcallingmatlab(x, smA_System);

我的代码:

#include "mex.h"
#include "matrix.h"   // definition of Matlab sparse matrix

#include<stdio.h>

//#include "/usr/local/lib/taucs_full/src/taucs.h"
#include "/home/dkumar/libtsnnls-2.3.3/tsnnls/taucs_basic/taucs.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *y, *z, x;

    /* Declare variable */
    mwSize mrows,ncols;
    int status;
    mwSize nzmax;
    mwIndex *irs,*jcs,j,k;

    /*  Check for proper number of arguments. */
    if (nrhs != 2) 
        mexErrMsgTxt("Two inputs required.");
    if (nlhs != 1) 
        mexErrMsgTxt("One output required.");

    /* Check to make sure the first input argument is a scalar. */
    if (!mxIsDouble(prhs[0]) ||     mxIsComplex(prhs[0]) || mxGetN(prhs[0])*mxGetM(prhs[0]) != 1) {
        mexErrMsgTxt("Input x must be a scalar.");
    }

    /* Get the scalar input x. */
    x = mxGetScalar(prhs[0]);

    /* Create a pointer to the input matrix y. */
    y = mxGetPr(prhs[1]);


     /* Check data type dimensions of the matrix input y. */
    if (!(mxIsDouble(prhs[1]))){
        mexErrMsgIdAndTxt( "MATLAB:DKU", "Input argument must be of type double.");
    }

    if (mxGetNumberOfDimensions(prhs[1]) != 2){
        mexErrMsgIdAndTxt( "MATLAB:DKU", "Input argument must be two dimensional\n");
    }

     /* Get the size and pointers to input data */
    mrows =mxGetM(prhs[1]);
    ncols = mxGetN(prhs[1]);

    // Verify the matrix y is infact sparse
    if (!(mxIsSparse(prhs[1]))){
        mexErrMsgIdAndTxt( "MATLAB: DKU", "Input matrix is not sparse\n");
    }else{
        //Work with irs and jcs directly instead of row-column pairs that are less compact.     
        sr  = mxGetPr(prhs[1]);
        jcs = mxGetJc(prhs[1]);     
        irs = mxGetIr(prhs[1]);
    }

   // Now converting sparse matrix to TAUCS format
    taucs_ccs_matrix *A;
    A = (taucs_ccs_matrix*)malloc(sizeof(taucs_ccs_matrix));
    A->n = ncols;
    A->m = mrows;   
    A->flags = TAUCS_DOUBLE;

    // Allocating spaces
    int nnz = jcs[ncols]; 
    A->colptr       = (int*)mxMalloc(sizeof(int)*(A->n+1));
    A->rowind   = (int*)mxMalloc(sizeof(int)*nnz);
    A->values.d     = (double*)mxMalloc(sizeof(taucs_double)*nnz);  

    int icolOffset = 0;  // Matlab "SPARSE" indices start with 0 vs 0 based in C    

    A->colptr = jcs-icolOffset;
    A->rowind = irs-icolOffset;
    A->values.d = sr;

    /* Create a C pointer to a copy of the output matrix. */
     memcpy(mxGetPr(plhs[0]), &A, sizeof(A)); 

    //Freeing spaces
    mxFree(A->colptr);
    mxFree(A->rowind);
    mxFree(A->values.d);        
}

此外,有人可以确认基本上写为“colptr”的内容是not a real pointer;但就像一个指针conceptually

0 个答案:

没有答案