xref: /petsc/src/mat/utils/matio.c (revision 4bf112e79d3f87ddad6dbf7b300a6f889c1e7d34)
1 /*
2    This file contains simple binary read/write routines for matrices.
3  */
4 
5 #include "src/mat/matimpl.h"             /*I  "petscmat.h"  I*/
6 #include "petscsys.h"
7 
8 #undef __FUNCT__
9 #define __FUNCT__ "MatLoadPrintHelp_Private"
10 static PetscErrorCode MatLoadPrintHelp_Private(Mat A)
11 {
12   static PetscTruth called = PETSC_FALSE;
13   MPI_Comm          comm = A->comm;
14   PetscErrorCode ierr;
15 
16   PetscFunctionBegin;
17   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
18   ierr = (*PetscHelpPrintf)(comm," Options for MatLoad:\n");CHKERRQ(ierr);
19   ierr = (*PetscHelpPrintf)(comm,"  -mat_type <type>\n");CHKERRQ(ierr);
20   ierr = (*PetscHelpPrintf)(comm,"  -matload_type <type>\n");CHKERRQ(ierr);
21   ierr = (*PetscHelpPrintf)(comm,"  -matload_block_size <block_size> :Used for MATBAIJ, MATBDIAG\n");CHKERRQ(ierr);
22   ierr = (*PetscHelpPrintf)(comm,"  -matload_bdiag_diags <s1,s2,s3,...> : Used for MATBDIAG\n");CHKERRQ(ierr);
23   PetscFunctionReturn(0);
24 }
25 
26 #undef __FUNCT__
27 #define __FUNCT__ "MatLoad"
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.
33 
34    Collective on PetscViewer
35 
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.
40 
41    Output Parameters:
42 .  newmat - new matrix
43 
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
57 
58    More Options Database Keys:
59    Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, ...) to specify
60    block size
61 .    -matload_block_size <bs>
62 
63    Used to specify block diagonal numbers for MATSEQBDIAG and MATMPIBDIAG formats
64 .    -matload_bdiag_diags <s1,s2,s3,...>
65 
66    Level: beginner
67 
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.
73 
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.
81 
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
87 
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
95 
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.
101 
102 .keywords: matrix, load, binary, input
103 
104 .seealso: PetscViewerBinaryOpen(), MatView(), VecLoad()
105 
106  @*/
107 PetscErrorCode MatLoad(PetscViewer viewer,const MatType outtype,Mat *newmat)
108 {
109   Mat            factory;
110   PetscErrorCode ierr;
111   PetscTruth     isbinary,flg;
112   MPI_Comm       comm;
113   PetscErrorCode (*r)(PetscViewer,const MatType,Mat*);
114   char           mtype[256],*prefix;
115 
116   PetscFunctionBegin;
117   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,1);
118   PetscValidPointer(newmat,3);
119   *newmat = 0;
120 
121   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,&prefix);CHKERRQ(ierr);
122   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
123   if (!isbinary) {
124     SETERRQ(PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
125   }
126 
127   ierr = PetscOptionsGetString(prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
128   if (flg) {
129     outtype = mtype;
130   }
131   ierr = PetscOptionsGetString(prefix,"-matload_type",mtype,256,&flg);CHKERRQ(ierr);
132   if (flg) {
133     outtype = mtype;
134   }
135   if (!outtype) outtype = MATAIJ;
136 
137   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
138   ierr = MatCreate(comm,0,0,0,0,&factory);CHKERRQ(ierr);
139   ierr = MatSetType(factory,outtype);CHKERRQ(ierr);
140   r = factory->ops->load;
141   ierr = MatDestroy(factory);
142   if (!r) SETERRQ1(PETSC_ERR_SUP,"MatLoad is not supported for type: %s",outtype);
143 
144   ierr = PetscLogEventBegin(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
145   ierr = (*r)(viewer,outtype,newmat);CHKERRQ(ierr);
146   ierr = PetscLogEventEnd(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
147 
148   ierr = PetscOptionsHasName(prefix,"-matload_symmetric",&flg);CHKERRQ(ierr);
149   if (flg) {
150     ierr = MatSetOption(*newmat,MAT_SYMMETRIC);CHKERRQ(ierr);
151     ierr = MatSetOption(*newmat,MAT_SYMMETRY_ETERNAL);CHKERRQ(ierr);
152   }
153   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
154   if (flg) {ierr = MatLoadPrintHelp_Private(*newmat);CHKERRQ(ierr); }
155   PetscFunctionReturn(0);
156 }
157 
158