writeBinaryTensor_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; 
0024   int   m,i,p=1;
0025   int* n;
0026   FILE *io;
0027   size_t one = 1;
0028   int MAT_FILE_COOKIE=1211216;
0029   (void) nlhs;     /* unused parameters */
0030   (void) plhs;
0031 
0032   char *filename;
0033   int   buflen,status;
0034     
0035   /* Check for proper number of arguments. */
0036   if (nrhs != 2) 
0037     mexErrMsgTxt("Two inputs required.");
0038 
0039   /* First argument must be a string. */
0040   if (mxIsChar(prhs[0]) != 1)
0041     mexErrMsgTxt("First argument must be a string.");
0042 
0043 
0044   /* Get the length of the input string. */
0045   buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
0046 
0047   /* Allocate memory for input and output strings. */
0048   filename = mxCalloc(buflen, sizeof(char));
0049 
0050   /* Copy the string data from prhs[0] into a C string 
0051    * filename. */
0052   status = mxGetString(prhs[0], filename, buflen);
0053   if (status != 0) 
0054     mexWarnMsgTxt("Not enough space. String is truncated.");
0055 
0056   /* Get the starting positions of all four data arrays. */ 
0057   pr = mxGetPr(prhs[1]);
0058   
0059   m = mxGetNumberOfDimensions(prhs[1]);
0060   n = mxGetDimensions(prhs[1]); 
0061 
0062 for(i=0;i"w");
0065   fwrite (&MAT_FILE_COOKIE,sizeof(int),one,io);   
0066   /* mexPrintf("%s=%d\n","MAT_FILE_COOKIE",MAT_FILE_COOKIE); */
0067   fwrite (&m,sizeof(int),one,io);
0068   /*mexPrintf("%s=%d\n","m",m);*/
0069 
0070   fwrite (n,sizeof(int),m,io); 
0071   fwrite (&p,sizeof(int),one,io);
0072  
0073  /* for(i=0;i<m;i++){
0074   mexPrintf("%i\n",*(n+i));}  
0075    mexPrintf("%s=%d\n","p",p);*/
0076    
0077    
0078    
0079    
0080    
0081   fwrite(pr, sizeof(double),p,io);
0082 /*   mexPrintf("%s=%d\n","sizeofdouble",sizeof(double));
0083    mexPrintf("%s=%d\n","sizeofint",sizeof(int));*/
0084   
0085 /*  for(i=0;i<p;i++){mexPrintf("%g\t",*(pr+i));}*/
0086   
0087   fclose(io);
0088 
0089   return;
0090 }

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