Actual source code: swrite.c

petsc-3.3-p7 2013-05-11
  1: /*
  2:  
  3:     This is the equivalent of MATLAB's fwrite() only on sockets instead of
  4:    binary files.
  5: */

  7: #include <stdio.h>
  8: #include <petscsys.h>
  9: #include <../src/sys/viewer/impls/socket/socket.h>
 10: #include <mex.h>

 12: PetscErrorCode PetscBinaryWrite(int,void *p,int,PetscDataType,PetscBool );

 14: #define PETSC_MEX_ERROR(a) {fprintf(stdout,"sread: %s \n",a); return ;}
 15: /*-----------------------------------------------------------------*/
 16: /*                                                                 */
 17: /*-----------------------------------------------------------------*/
 20: void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
 21: {
 22:   int            i,fd,cnt,dt;

 25:   /* check output parameters */
 26:   if (nrhs != 3) PETSC_MEX_ERROR("Receive requires three input arguments.");
 27:   fd  = (int) mxGetScalar(prhs[0]);
 28:   cnt = mxGetNumberOfElements(prhs[1]);
 29:   dt  = (PetscDataType) mxGetScalar(prhs[2]);

 31:    if (dt == PETSC_DOUBLE) {
 32:     PetscBinaryWrite(fd,mxGetPr(prhs[1]),cnt,(PetscDataType)dt,PETSC_FALSE);if (ierr) PETSC_MEX_ERROR("Unable to send double items.");
 33:   } else if (dt == PETSC_INT) {
 34:     int *tmp = (int*) mxMalloc((cnt+5)*sizeof(int));
 35:     double *t = mxGetPr(prhs[1]);
 36:     for (i=0; i<cnt; i++) tmp[i] = (int)t[i];
 37:     PetscBinaryWrite(fd,tmp,cnt,(PetscDataType)dt,PETSC_FALSE);if (ierr) PETSC_MEX_ERROR("Unable to send int items.");
 38:     mxFree(tmp);
 39:   } else if (dt == PETSC_CHAR) {
 40:     char *tmp = (char*) mxMalloc((cnt+5)*sizeof(char));
 41:     mxGetNChars(prhs[1],tmp,cnt+1);
 42:     PetscBinaryWrite(fd,tmp,cnt,(PetscDataType)dt,PETSC_FALSE);if (ierr) PETSC_MEX_ERROR("Unable to send char items.");
 43:     mxFree(tmp);
 44:   } else {
 45:     PETSC_MEX_ERROR("Unknown datatype.");
 46:   }
 47:   return;
 48: }