writeBinarySparseMat_mex_c

PURPOSE ^

C/C++ source

SYNOPSIS ^

C/C++ source

DESCRIPTION ^

C/C++ source

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 /*=================================================================
0002  * Mex function to write sparse matrix in PETSc binary format. 
0003  * USAGE: writePetscBinaryMat(filename,A'), where
0004  *          filename: string containing name of file to write to
0005  *          A: sparse matrix to write out.
0006  * IMPORTANT: To write out matrix A you MUST pass A' (A transpose)
0007  *            as the 2d argument. This is because matlab stores 
0008  *            sparse matrices internally in column major format. 
0009  *            This is exactly the opposite of what is needed for 
0010  *            writing out a PETSc binary file.
0011  *=================================================================*/
0012 
0013 #include <stdio.h>
0014 #include <string.h>
0015 #include "mex.h"
0016 
0017  
0018 /* mexFunction is the gateway routine for the MEX-file. */ 
0019 void
0020 mexFunction( int nlhs, mxArray *plhs[],
0021              int nrhs, const mxArray *prhs[] )
0022 {
0023  double  *pr, *pi;
0024   int  *ir, *jc;
0025   int   m,n,nnz;
0026   int i,j,*nnzr;
0027   FILE *io;
0028   size_t one = 1;
0029   int MAT_FILE_COOKIE=1211216;
0030 
0031   (void) nlhs;     /* unused parameters */
0032   (void) plhs;
0033 
0034   char *filename;
0035   int   buflen,status;
0036     
0037   /* Check for proper number of arguments. */
0038   if (nrhs != 2) 
0039     mexErrMsgTxt("Two inputs required.");
0040 
0041   /* First argument must be a string. */
0042   if (mxIsChar(prhs[0]) != 1)
0043     mexErrMsgTxt("First argument must be a string.");
0044 
0045   /* Second argument must be a sparse matrix. */
0046   if (mxIsSparse(prhs[1]) != 1)
0047     mexErrMsgTxt("Second argument must be a sparse matrix.");
0048 
0049   /* Get the length of the input string. */
0050   buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
0051 
0052   /* Allocate memory for input and output strings. */
0053   filename = mxCalloc(buflen, sizeof(char));
0054 
0055   /* Copy the string data from prhs[0] into a C string 
0056    * filename. */
0057   status = mxGetString(prhs[0], filename, buflen);
0058   if (status != 0) 
0059     mexWarnMsgTxt("Not enough space. String is truncated.");
0060 
0061   /* Get the starting positions of all four data arrays. */ 
0062   pr = mxGetPr(prhs[1]);
0063   pi = mxGetPi(prhs[1]);
0064   ir = mxGetIr(prhs[1]);
0065   jc = mxGetJc(prhs[1]);
0066   
0067   m = mxGetM(prhs[1]); /* number of rows of A' (columns of A) */
0068   n = mxGetN(prhs[1]); /* number of columns of A' (rows of A) */
0069 /*   nnz = mxGetNzmax(prhs[1]); */ /* this is only an upper bound! */
0070 
0071 /* Figure out number of nonzero elements */
0072   nnz=0;
0073   /*for (j=0;j<n;j++) {
0074     nnzr=jc[j+1]-jc[j]; /* number of nonzero elements in column j+1 of A' (row j+1 of A) */
0075     /*nnz=nnz+nnzr;
0076   }*/
0077   
0078 nnz=jc[n]-jc[0];
0079 
0080   io = fopen(filename,"w");
0081   fwrite (&MAT_FILE_COOKIE,sizeof(int),one,io);   
0082    /* mexPrintf("%s=%d\n","MAT_FILE_COOKIE",MAT_FILE_COOKIE); */
0083   fwrite (&n,sizeof(int),one,io); 
0084    /* mexPrintf("%s=%d\n","n",n); */
0085   fwrite (&m,sizeof(int),one,io); 
0086    /* mexPrintf("%s=%d\n","m",m);  */ 
0087   fwrite (&nnz,sizeof(int),one,io); 
0088    /* mexPrintf("%s=%d\n","nnz",nnz); */  
0089 nnzr=mxCalloc(n,sizeof(int));
0090   for (j=0;j/* mexPrintf("%s=%d\n","nnzr",nnzr); */
0093   }
0094   fwrite (jc,sizeof(int),n+1,io); 
0095   fwrite (ir,sizeof(int),(size_t) nnz,io); /* row indices of of nonzero elements of A' (column indices of A) */
0096   /*fwrite (pr,sizeof(double),(size_t) nnz,io);*/ /* actual data */
0097   fwrite (pr,sizeof(double),(size_t) nnz,io);
0098   fclose(io);
0099 
0100   return;
0101 }

Community support and wiki are available on Redmine. Last update: 18-Apr-2019.