/*================================================================= * Mex function to write sparse matrix in PETSc binary format. * USAGE: writePetscBinaryMat(filename,A'), where * filename: string containing name of file to write to * A: sparse matrix to write out. * IMPORTANT: To write out matrix A you MUST pass A' (A transpose) * as the 2d argument. This is because matlab stores * sparse matrices internally in column major format. * This is exactly the opposite of what is needed for * writing out a PETSc binary file. * this program tests to see if the host system is little-endian and if so * swaps the byte ordering of the data that is written into the binary file * to ensure that the data file is always written in big-endian format *=================================================================*/ #include #include #include "matrix.h" #include "mex.h" #define LITTLE_ENDIAN_SYS 0 #define BIG_ENDIAN_SYS 1 #define DoByteSwap(x) ByteSwap((unsigned char *) &x, sizeof(x)) int machineEndianness(){ long int i = 1; const char *p = (const char *) &i; /* check if lowest address contains the least significant byte */ if (p[0] == 1) return LITTLE_ENDIAN_SYS; else return BIG_ENDIAN_SYS; } void ByteSwap(unsigned char * b, int n){ register int i = 0; register int j = n-1; unsigned char temp; while (i