ReadBinaryIntArray_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 #define MAT_OUT   plhs[0]
0018  
0019 /* mexFunction is the gateway routine for the MEX-file. */ 
0020 void mexFunction(int nlhs, mxArray *plhs[],
0021              int nrhs, const mxArray *prhs[] )
0022          {
0023  char *file;
0024  int length,status;
0025  int *cookie,*ndims,*dim,*size,*vals;
0026  int ii,result;
0027  int *matout;
0028  
0029  if (mxIsChar(prhs[0]) != 1)
0030     {mexErrMsgTxt("Input must be a string.");}
0031  else{
0032     length=(mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
0033     file=mxCalloc(length,sizeof(char));
0034     status = mxGetString(prhs[0], file, length);
0035     if (status != 0) 
0036        {mexWarnMsgTxt("Not enough space. String is truncated.");}
0037  }
0038  
0039  FILE *TENSOR = fopen(file,"r");/* Open file */
0040  
0041  cookie = mxCalloc(1,sizeof(int));
0042  if (cookie == NULL) {mexErrMsgTxt("Memory allocation error in ReadBinaryIntArray_mex !");}
0043  result = fread(cookie,sizeof(int),1,TENSOR);/* copy the file into the buffer:*/
0044  if (result != 1) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0045  /*mexPrintf("Cookie=%i\n",cookie[0]);*//* Display COOKIE */
0046  
0047  ndims = mxCalloc(1,sizeof(int));
0048  if (ndims == NULL) {mexErrMsgTxt("Memory allocation error in ReadBinaryIntArray_mex !");}
0049  result = fread(ndims,sizeof(int),1,TENSOR);/*copy the file into the buffer:*/
0050  if (result != 1) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0051  /*mexPrintf("Ndims=%i\n",ndims[0]);*//* Display Ndims */
0052 
0053  dim = mxCalloc(ndims[0],sizeof(int));
0054  if (dim == NULL) {mexErrMsgTxt("Memory allocation error in ReadBinaryIntArray_mex !");}
0055  result = fread(dim,sizeof(int),ndims[0],TENSOR);/* copy the file into the buffer:*/
0056  if (result != ndims[0]) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0057  /*for (ii = 0;ii < ndims[0];ii++) {
0058      mexPrintf("dim[%i]=%i\n",ii,dim[ii]);
0059  }*/
0060 
0061  size = mxCalloc(1,sizeof(int));
0062  if (size == NULL) {mexErrMsgTxt("Memory allocation error in ReadBinaryIntArray_mex !");}
0063  result = fread(size,sizeof(int),1,TENSOR);/* copy the file into the buffer:*/
0064  if (result != 1) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0065  /*mexPrintf("Size=%i\n",size[0]);*//* Display Size */
0066  
0067  vals = mxCalloc(size[0],sizeof(int));
0068  if (vals == NULL) {mexErrMsgTxt("Memory allocation error in ReadBinaryIntArray_mex !");}
0069  result = fread(vals,sizeof(int),size[0],TENSOR);/* copy the file into the buffer:*/
0070  if (result != size[0]) {mexErrMsgTxt("Reading file problem in ReadBinaryIntArray_mex !");}
0071  /* for (ii = 0;ii < size[0];ii++) {
0072      mexPrintf("vals[%i]=%i\n",ii,vals[ii]);
0073  }*/
0074  
0075  fclose(TENSOR);
0076  
0077 
0078  MAT_OUT = mxCreateNumericArray(ndims[0],dim,mxINT32_CLASS,mxREAL);
0079  matout = mxGetData(MAT_OUT);
0080  for (ii = 0;ii < size[0];ii++) {
0081      matout[ii] = vals[ii];/*Write output*/
0082  }
0083 
0084  
0085  /* Clear allocated memory */
0086  mxFree (cookie);
0087  mxFree (dim);
0088  mxFree (ndims);
0089  mxFree (size);
0090  mxFree (vals);
0091  
0092  return;
0093 }

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