writeBinaryIntArray_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   int  *pr; 
0024   int   m,i,j,k,p=1;
0025   int* n;
0026   FILE *io;
0027   size_t one = 1;
0028   int ARRAY_FILE_COOKIE=1;
0029   int CELLARRAY_FILE_COOKIE=0;
0030   (void) nlhs;     /* unused parameters */
0031   (void) plhs;
0032    int class,cm,*cn,index;
0033    mxArray* cell;
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 
0046   /* Get the length of the input string. */
0047   buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
0048 
0049   /* Allocate memory for input and output strings. */
0050   filename = mxCalloc(buflen, sizeof(char));
0051 
0052   /* Copy the string data from prhs[0] into a C string 
0053    * filename. */
0054   status = mxGetString(prhs[0], filename, buflen);
0055   if (status != 0) 
0056     mexWarnMsgTxt("Not enough space. String is truncated.");
0057     
0058   class=mxGetClassID(prhs[1]);  
0059 
0060   if (class==mxCELL_CLASS) {
0061     /*   mexPrintf("Cell Array \n");*/
0062       cm = mxGetNumberOfDimensions(prhs[1]);
0063     /*    mexPrintf("%i\t",cm);*/
0064       cn = mxGetDimensions(prhs[1]);
0065       for (j=0;j/*  mexPrintf("%i\t",cn[0]);*/
0067       /*  mexPrintf("%i\n",cn[1]);*/
0068       int ind[2];
0069       ind[1]=0;
0070       ind[0]=0;
0071  
0072       io = fopen(filename,"w");
0073       fwrite (&CELLARRAY_FILE_COOKIE,sizeof(int),one,io);
0074       fwrite (&cm,sizeof(int),one,io);
0075       fwrite (cn,sizeof(int),cm,io); 
0076       fwrite (&p,sizeof(int),one,io);
0077  
0078       for (i=0;ifor(k=0;k/*   mexPrintf("%i\t",m);*/
0087  
0088               for(j=0;j/*Writing to file*/ 
0092               fwrite (&m,sizeof(int),one,io);
0093               fwrite (n,sizeof(int),m,io); 
0094               fwrite (&p,sizeof(int),one,io);  
0095               fwrite(pr, sizeof(int),p,io); 
0096              /*   mexPrintf("pr=%i\t",*(pr+1));*/
0097           }
0098           ind[0]++;
0099       }
0100   } else{
0101        /*  mexPrintf("NA\n");*/
0102       pr = mxGetPr(prhs[1]);
0103       m = mxGetNumberOfDimensions(prhs[1]);
0104       n = mxGetDimensions(prhs[1]); 
0105       
0106       for(i=0;i/*    mexPrintf("%s=%d\n","p",p);*/
0109       }
0110  
0111       io = fopen(filename,"w");
0112         fwrite (&ARRAY_FILE_COOKIE,sizeof(int),one,io);   
0113        /*   mexPrintf("%s=%d\n","ARRAY_FILE_COOKIE",ARRAY_FILE_COOKIE); */
0114         fwrite (&m,sizeof(int),one,io);
0115        /*   mexPrintf("%s=%d\n","m",m); */
0116        /*   mexPrintf("%s=%d\n","n",n[0]); */
0117      
0118  
0119       fwrite (n,sizeof(int),m,io); 
0120       fwrite (&p,sizeof(int),one,io); 
0121       fwrite(pr, sizeof(int),p,io); 
0122   }
0123   fclose(io);
0124   return;
0125 }

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