Actual source code: receivesparse.c

      1: /*
      2:     Part of the MatlabSockettool Package. Receive a sparse matrix
      3:   at a socket address, called by the receive.mex4 Matlab program.

      5:         Written by Barry Smith, bsmith@mcs.anl.gov 4/14/92

      7:    Since this is called from Matlab it cannot be compiled with C++.
      8: */
      9: #include <stdio.h>
     10:  #include petscsys.h
     11: #include "mex.h"

     13: #define PETSC_MEX_ERROR(a) {fprintf(stdout,"RECEIVE: %s \n",a); return -1;}
     16: PetscErrorCode ReceiveSparseMatrix(mxArray *plhs[],int t)
     17: {
     18:   int    *tr,*tc,compx = 0;
     19:   int    *r,*c;
     20:   int    i,j,m,n,nnz,lnnz,jstart,jend,off = 0;
     21:   double *tv,*v,*diag,*vi;

     23:   /* get size of matrix */
     24:   if (PetscBinaryRead(t,&m,1,PETSC_INT))   PETSC_MEX_ERROR("reading number columns");
     25:   if (PetscBinaryRead(t,&n,1,PETSC_INT))   PETSC_MEX_ERROR("reading number rows");
     26:   /* get number of nonzeros */
     27:   if (PetscBinaryRead(t,&nnz,1,PETSC_INT))   PETSC_MEX_ERROR("reading nnz");
     28:   if (PetscBinaryRead(t,&compx,1,PETSC_INT))   PETSC_MEX_ERROR("reading row lengths");
     29:   /* Create a matrix for Matlab */
     30:   /* since Matlab stores by columns not rows we actually will
     31:      create transpose of desired matrix */
     32:   plhs[0] = mxCreateSparse(n,m,nnz,compx);
     33:   r = mxGetIr(plhs[0]);
     34:   c = mxGetJc(plhs[0]);
     35:   v = mxGetPr(plhs[0]);
     36:   /* Matlab sparse matrix pointers start at 0 not 1 */
     37:   if (!compx) {
     38:     if (PetscBinaryRead(t,v,nnz,PETSC_DOUBLE)) PETSC_MEX_ERROR("reading values");
     39:   } else {
     40:     for (i=0; i<nnz; i++) {
     41:       vi = mxGetPi(plhs[0]);
     42:       if (PetscBinaryRead(t,v+i,1,PETSC_DOUBLE)) PETSC_MEX_ERROR("reading values");
     43:       if (PetscBinaryRead(t,vi+i,1,PETSC_DOUBLE)) PETSC_MEX_ERROR("reading values");
     44:     }
     45:   }
     46:   if (PetscBinaryRead(t,c,m+1,PETSC_INT)) PETSC_MEX_ERROR("reading column pointers");
     47:   if (PetscBinaryRead(t,r,nnz,PETSC_INT)) PETSC_MEX_ERROR("reading row pointers");
     48:   return 0;
     49: }