Actual source code: matio.c

  1: /*$Id: matio.c,v 1.78 2001/04/10 19:35:59 bsmith Exp $*/

  3: /* 
  4:    This file contains simple binary read/write routines for matrices.
  5:  */

  7: #include "petsc.h"
  8: #include "src/mat/matimpl.h"             /*I  "petscmat.h"  I*/
  9: #include "petscsys.h"
 10: PetscTruth MatLoadRegisterAllCalled = PETSC_FALSE;
 11: PetscFList      MatLoadList              = 0;

 13: /*@C
 14:     MatLoadRegister - Allows one to register a routine that reads matrices
 15:         from a binary file for a particular matrix type.

 17:   Not Collective

 19:   Input Parameters:
 20: +   type - the type of matrix (defined in include/petscmat.h), for example, MATSEQAIJ.
 21: -   loader - the function that reads the matrix from the binary file.

 23:   Level: developer

 25: .seealso: MatLoadRegisterAll(), MatLoad()

 27: @*/
 28: int MatLoadRegister(char *sname,char *path,char *name,int (*function)(PetscViewer,MatType,Mat*))
 29: {
 30:   int  ierr;
 31:   char fullname[256];

 34:   PetscFListConcat(path,name,fullname);
 35:   PetscFListAdd(&MatLoadList,sname,fullname,(void (*)())function);
 36:   return(0);
 37: }

 39: static int MatLoadPrintHelp_Private(Mat A)
 40: {
 41:   static PetscTruth called = PETSC_FALSE;
 42:   MPI_Comm          comm = A->comm;
 43:   int               ierr;
 44: 
 46:   if (called) {return(0);} else called = PETSC_TRUE;
 47:   (*PetscHelpPrintf)(comm," Options for MatLoad:n");
 48:   (*PetscHelpPrintf)(comm,"  -mat_type <type>n");
 49:   (*PetscHelpPrintf)(comm,"  -matload_type <type>n");
 50:   (*PetscHelpPrintf)(comm,"  -matload_block_size <block_size> :Used for MATBAIJ, MATBDIAGn");
 51:   (*PetscHelpPrintf)(comm,"  -matload_bdiag_diags <s1,s2,s3,...> : Used for MATBDIAGn");
 52:   return(0);
 53: }

 55: /*@C
 56:    MatLoad - Loads a matrix that has been stored in binary format
 57:    with MatView().  The matrix format is determined from the options database.
 58:    Generates a parallel MPI matrix if the communicator has more than one
 59:    processor.  The default matrix type is AIJ.

 61:    Collective on PetscViewer

 63:    Input Parameters:
 64: +  viewer - binary file viewer, created with PetscViewerBinaryOpen()
 65: -  outtype - type of matrix desired, for example MATSEQAIJ,
 66:              MATMPIROWBS, etc.  See types in petsc/include/petscmat.h.

 68:    Output Parameters:
 69: .  newmat - new matrix

 71:    Basic Options Database Keys:
 72: +    -matload_type seqaij   - AIJ type
 73: .    -matload_type mpiaij   - parallel AIJ type
 74: .    -matload_type seqbaij  - block AIJ type
 75: .    -matload_type mpibaij  - parallel block AIJ type
 76: .    -matload_type seqbdiag - block diagonal type
 77: .    -matload_type mpibdiag - parallel block diagonal type
 78: .    -matload_type mpirowbs - parallel rowbs type
 79: .    -matload_type seqdense - dense type
 80: -    -matload_type mpidense - parallel dense type

 82:    More Options Database Keys:
 83:    Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, ...) to specify
 84:    block size
 85: .    -matload_block_size <bs>

 87:    Used to specify block diagonal numbers for MATSEQBDIAG and MATMPIBDIAG formats
 88: .    -matload_bdiag_diags <s1,s2,s3,...>

 90:    Level: beginner

 92:    Notes:
 93:    MatLoad() automatically loads into the options database any options
 94:    given in the file filename.info where filename is the name of the file
 95:    that was passed to the PetscViewerBinaryOpen(). The options in the info
 96:    file will be ignored if you use the -matload_ignore_info option.

 98:    In parallel, each processor can load a subset of rows (or the
 99:    entire matrix).  This routine is especially useful when a large
100:    matrix is stored on disk and only part of it existsis desired on each
101:    processor.  For example, a parallel solver may access only some of
102:    the rows from each processor.  The algorithm used here reads
103:    relatively small blocks of data rather than reading the entire
104:    matrix and then subsetting it.

106:    Notes for advanced users:
107:    Most users should not need to know the details of the binary storage
108:    format, since MatLoad() and MatView() completely hide these details.
109:    But for anyone who's interested, the standard binary matrix storage
110:    format is

112: $    int    MAT_COOKIE
113: $    int    number of rows
114: $    int    number of columns
115: $    int    total number of nonzeros
116: $    int    *number nonzeros in each row
117: $    int    *column indices of all nonzeros (starting index is zero)
118: $    Scalar *values of all nonzeros

120:    Note for Cray users, the int's stored in the binary file are 32 bit
121: integers; not 64 as they are represented in the memory, so if you
122: write your own routines to read/write these binary files from the Cray
123: you need to adjust the integer sizes that you read in, see
124: PetscReadBinary() and PetscWriteBinary() to see how this may be
125: done.

127:    In addition, PETSc automatically does the byte swapping for
128: machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
129: linux, nt and the paragon; thus if you write your own binary
130: read/write routines you have to swap the bytes; see PetscReadBinary()
131: and PetscWriteBinary() to see how this may be done.

133: .keywords: matrix, load, binary, input

135: .seealso: PetscViewerBinaryOpen(), MatView(), VecLoad(), MatLoadRegister(),
136:           MatLoadRegisterAll()

138:  @*/
139: int MatLoad(PetscViewer viewer,MatType outtype,Mat *newmat)
140: {
141:   int         ierr;
142:   PetscTruth  isbinary,flg;
143:   MPI_Comm    comm;
144:   int         (*r)(PetscViewer,MatType,Mat*);
145:   char        mtype[256];

149:   *newmat  = 0;

151:   if (!MatLoadRegisterAllCalled) {
152:     MatLoadRegisterAll(PETSC_NULL);
153:   }

155:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
156:   if (!isbinary) {
157:     SETERRQ(PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
158:   }

160:   PetscOptionsGetString(PETSC_NULL,"-mat_type",mtype,256,&flg);
161:   if (flg) {
162:     outtype = mtype;
163:   }
164:   PetscOptionsGetString(PETSC_NULL,"-matload_type",mtype,256,&flg);
165:   if (flg) {
166:     outtype = mtype;
167:   }
168:   PetscObjectGetComm((PetscObject)viewer,&comm);
169:   if (!outtype) outtype = MATMPIAIJ;
170:    PetscFListFind(comm,MatLoadList,outtype,(void(**)())&r);
171:   if (!r) SETERRQ1(1,"Unknown Mat type given: %s",outtype);

173:   PetscLogEventBegin(MAT_Load,viewer,0,0,0);
174:   (*r)(viewer,outtype,newmat);
175:   PetscLogEventEnd(MAT_Load,viewer,0,0,0);

177:   PetscOptionsHasName(PETSC_NULL,"-help",&flg);
178:   if (flg) {MatLoadPrintHelp_Private(*newmat); }
179:   return(0);
180: }