Actual source code: matio.c

  1: /* 
  2:    This file contains simple binary read/write routines for matrices.
  3:  */

 5:  #include src/mat/matimpl.h
 6:  #include petscsys.h

 10: static PetscErrorCode MatLoadPrintHelp_Private(Mat A)
 11: {
 12:   static PetscTruth called = PETSC_FALSE;
 13:   MPI_Comm          comm = A->comm;
 15: 
 17:   if (called) {return(0);} else called = PETSC_TRUE;
 18:   (*PetscHelpPrintf)(comm," Options for MatLoad:\n");
 19:   (*PetscHelpPrintf)(comm,"  -mat_type <type>\n");
 20:   (*PetscHelpPrintf)(comm,"  -matload_type <type>\n");
 21:   (*PetscHelpPrintf)(comm,"  -matload_block_size <block_size> :Used for MATBAIJ, MATBDIAG\n");
 22:   (*PetscHelpPrintf)(comm,"  -matload_bdiag_diags <s1,s2,s3,...> : Used for MATBDIAG\n");
 23:   return(0);
 24: }

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

 34:    Collective on PetscViewer

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

 41:    Output Parameters:
 42: .  newmat - new matrix

 44:    Basic Options Database Keys:
 45: +    -matload_type seqaij   - AIJ type
 46: .    -matload_type mpiaij   - parallel AIJ type
 47: .    -matload_type seqbaij  - block AIJ type
 48: .    -matload_type mpibaij  - parallel block AIJ type
 49: .    -matload_type seqsbaij - block symmetric AIJ type
 50: .    -matload_type mpisbaij - parallel block symmetric AIJ type
 51: .    -matload_type seqbdiag - block diagonal type
 52: .    -matload_type mpibdiag - parallel block diagonal type
 53: .    -matload_type mpirowbs - parallel rowbs type
 54: .    -matload_type seqdense - dense type
 55: .    -matload_type mpidense - parallel dense type
 56: -    -matload_symmetric - matrix in file is symmetric

 58:    More Options Database Keys:
 59:    Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, ...) to specify
 60:    block size
 61: .    -matload_block_size <bs>

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

 66:    Level: beginner

 68:    Notes:
 69:    MatLoad() automatically loads into the options database any options
 70:    given in the file filename.info where filename is the name of the file
 71:    that was passed to the PetscViewerBinaryOpen(). The options in the info
 72:    file will be ignored if you use the -matload_ignore_info option.

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

 82:    Notes for advanced users:
 83:    Most users should not need to know the details of the binary storage
 84:    format, since MatLoad() and MatView() completely hide these details.
 85:    But for anyone who's interested, the standard binary matrix storage
 86:    format is

 88: $    int    MAT_FILE_COOKIE
 89: $    int    number of rows
 90: $    int    number of columns
 91: $    int    total number of nonzeros
 92: $    int    *number nonzeros in each row
 93: $    int    *column indices of all nonzeros (starting index is zero)
 94: $    PetscScalar *values of all nonzeros

 96:    PETSc automatically does the byte swapping for
 97: machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
 98: linux, Windows and the paragon; thus if you write your own binary
 99: read/write routines you have to swap the bytes; see PetscReadBinary()
100: and PetscWriteBinary() to see how this may be done.

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

104: .seealso: PetscViewerBinaryOpen(), MatView(), VecLoad()

106:  @*/
107: PetscErrorCode MatLoad(PetscViewer viewer,const MatType outtype,Mat *newmat)
108: {
109:   Mat            factory;
111:   PetscTruth     isbinary,flg;
112:   MPI_Comm       comm;
113:   PetscErrorCode (*r)(PetscViewer,const MatType,Mat*);
114:   char           mtype[256],*prefix;

119:   *newmat = 0;

121:   PetscObjectGetOptionsPrefix((PetscObject)viewer,&prefix);
122:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
123:   if (!isbinary) {
124:     SETERRQ(PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
125:   }

127:   PetscOptionsGetString(prefix,"-mat_type",mtype,256,&flg);
128:   if (flg) {
129:     outtype = mtype;
130:   }
131:   PetscOptionsGetString(prefix,"-matload_type",mtype,256,&flg);
132:   if (flg) {
133:     outtype = mtype;
134:   }
135:   if (!outtype) outtype = MATAIJ;

137:   PetscObjectGetComm((PetscObject)viewer,&comm);
138:   MatCreate(comm,0,0,0,0,&factory);
139:   MatSetType(factory,outtype);
140:   r = factory->ops->load;
141:   MatDestroy(factory);
142:   if (!r) SETERRQ1(PETSC_ERR_SUP,"MatLoad is not supported for type: %s",outtype);

144:   PetscLogEventBegin(MAT_Load,viewer,0,0,0);
145:   (*r)(viewer,outtype,newmat);
146:   PetscLogEventEnd(MAT_Load,viewer,0,0,0);

148:   PetscOptionsHasName(prefix,"-matload_symmetric",&flg);
149:   if (flg) {
150:     MatSetOption(*newmat,MAT_SYMMETRIC);
151:     MatSetOption(*newmat,MAT_SYMMETRY_ETERNAL);
152:   }
153:   PetscOptionsHasName(PETSC_NULL,"-help",&flg);
154:   if (flg) {MatLoadPrintHelp_Private(*newmat); }
155:   return(0);
156: }