Actual source code: receivesparse.c
1: /*$Id: receivesparse.c,v 1.16 2001/03/23 23:19:53 balay Exp $*/
2: /*
3: Part of the MatlabSockettool Package. Receive a sparse matrix
4: at a socket address, called by the receive.mex4 Matlab program.
6: Written by Barry Smith, bsmith@mcs.anl.gov 4/14/92
8: Since this is called from Matlab it cannot be compiled with C++.
9: */
10: #include <stdio.h>
11: #include petscsys.h
12: #include "mex.h"
14: #define ERROR(a) {fprintf(stdout,"RECEIVE: %s \n",a); return -1;}
17: int ReceiveSparseMatrix(mxArray *plhs[],int t)
18: {
19: int *tr,*tc,compx = 0;
20: int *r,*c;
21: int i,j,m,n,nnz,lnnz,jstart,jend,off = 0;
22: double *tv,*v,*diag,*vi;
24: /* get size of matrix */
25: if (PetscBinaryRead(t,&m,1,PETSC_INT)) ERROR("reading number columns");
26: if (PetscBinaryRead(t,&n,1,PETSC_INT)) ERROR("reading number rows");
27: /* get number of nonzeros */
28: if (PetscBinaryRead(t,&nnz,1,PETSC_INT)) ERROR("reading nnz");
29: if (PetscBinaryRead(t,&compx,1,PETSC_INT)) ERROR("reading row lengths");
30: /* Create a matrix for Matlab */
31: /* since Matlab stores by columns not rows we actually will
32: create transpose of desired matrix */
33: plhs[0] = mxCreateSparse(n,m,nnz,compx);
34: r = mxGetIr(plhs[0]);
35: c = mxGetJc(plhs[0]);
36: v = mxGetPr(plhs[0]);
37: /* Matlab sparse matrix pointers start at 0 not 1 */
38: if (!compx) {
39: if (PetscBinaryRead(t,v,nnz,PETSC_DOUBLE)) ERROR("reading values");
40: } else {
41: for (i=0; i<nnz; i++) {
42: vi = mxGetPi(plhs[0]);
43: if (PetscBinaryRead(t,v+i,1,PETSC_DOUBLE)) ERROR("reading values");
44: if (PetscBinaryRead(t,vi+i,1,PETSC_DOUBLE)) ERROR("reading values");
45: }
46: }
47: if (PetscBinaryRead(t,c,m+1,PETSC_INT)) ERROR("reading column pointers");
48: if (PetscBinaryRead(t,r,nnz,PETSC_INT)) ERROR("reading row pointers");
49: return 0;
50: }