xref: /petsc/src/mat/utils/matio.c (revision 973046187a5e16e2b37c23287137a5e6ee1ae012)
1 /*$Id: matio.c,v 1.79 2001/08/06 21:16:10 bsmith Exp $*/
2 
3 /*
4    This file contains simple binary read/write routines for matrices.
5  */
6 
7 #include "src/mat/matimpl.h"             /*I  "petscmat.h"  I*/
8 #include "petscsys.h"
9 PetscTruth MatLoadRegisterAllCalled = PETSC_FALSE;
10 PetscFList      MatLoadList              = 0;
11 
12 #undef __FUNCT__
13 #define __FUNCT__ "MatLoadRegister"
14 /*@C
15     MatLoadRegister - Allows one to register a routine that reads matrices
16         from a binary file for a particular matrix type.
17 
18   Not Collective
19 
20   Input Parameters:
21 +   type - the type of matrix (defined in include/petscmat.h), for example, MATSEQAIJ.
22 -   loader - the function that reads the matrix from the binary file.
23 
24   Level: developer
25 
26 .seealso: MatLoadRegisterAll(), MatLoad()
27 
28 @*/
29 int MatLoadRegister(char *sname,char *path,char *name,int (*function)(PetscViewer,MatType,Mat*))
30 {
31   int  ierr;
32   char fullname[256];
33 
34   PetscFunctionBegin;
35   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
36   ierr = PetscFListAdd(&MatLoadList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
37   PetscFunctionReturn(0);
38 }
39 
40 #undef __FUNCT__
41 #define __FUNCT__ "MatLoadPrintHelp_Private"
42 static int MatLoadPrintHelp_Private(Mat A)
43 {
44   static PetscTruth called = PETSC_FALSE;
45   MPI_Comm          comm = A->comm;
46   int               ierr;
47 
48   PetscFunctionBegin;
49   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
50   ierr = (*PetscHelpPrintf)(comm," Options for MatLoad:\n");CHKERRQ(ierr);
51   ierr = (*PetscHelpPrintf)(comm,"  -mat_type <type>\n");CHKERRQ(ierr);
52   ierr = (*PetscHelpPrintf)(comm,"  -matload_type <type>\n");CHKERRQ(ierr);
53   ierr = (*PetscHelpPrintf)(comm,"  -matload_block_size <block_size> :Used for MATBAIJ, MATBDIAG\n");CHKERRQ(ierr);
54   ierr = (*PetscHelpPrintf)(comm,"  -matload_bdiag_diags <s1,s2,s3,...> : Used for MATBDIAG\n");CHKERRQ(ierr);
55   PetscFunctionReturn(0);
56 }
57 
58 #undef __FUNCT__
59 #define __FUNCT__ "MatLoad"
60 /*@C
61    MatLoad - Loads a matrix that has been stored in binary format
62    with MatView().  The matrix format is determined from the options database.
63    Generates a parallel MPI matrix if the communicator has more than one
64    processor.  The default matrix type is AIJ.
65 
66    Collective on PetscViewer
67 
68    Input Parameters:
69 +  viewer - binary file viewer, created with PetscViewerBinaryOpen()
70 -  outtype - type of matrix desired, for example MATSEQAIJ,
71              MATMPIROWBS, etc.  See types in petsc/include/petscmat.h.
72 
73    Output Parameters:
74 .  newmat - new matrix
75 
76    Basic Options Database Keys:
77 +    -matload_type seqaij   - AIJ type
78 .    -matload_type mpiaij   - parallel AIJ type
79 .    -matload_type seqbaij  - block AIJ type
80 .    -matload_type mpibaij  - parallel block AIJ type
81 .    -matload_type seqsbaij - block symmetric AIJ type
82 .    -matload_type mpisbaij - parallel block symmetric AIJ type
83 .    -matload_type seqbdiag - block diagonal type
84 .    -matload_type mpibdiag - parallel block diagonal type
85 .    -matload_type mpirowbs - parallel rowbs type
86 .    -matload_type seqdense - dense type
87 .    -matload_type mpidense - parallel dense type
88 -    -matload_symmetric - matrix in file is symmetric
89 
90    More Options Database Keys:
91    Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, ...) to specify
92    block size
93 .    -matload_block_size <bs>
94 
95    Used to specify block diagonal numbers for MATSEQBDIAG and MATMPIBDIAG formats
96 .    -matload_bdiag_diags <s1,s2,s3,...>
97 
98    Level: beginner
99 
100    Notes:
101    MatLoad() automatically loads into the options database any options
102    given in the file filename.info where filename is the name of the file
103    that was passed to the PetscViewerBinaryOpen(). The options in the info
104    file will be ignored if you use the -matload_ignore_info option.
105 
106    In parallel, each processor can load a subset of rows (or the
107    entire matrix).  This routine is especially useful when a large
108    matrix is stored on disk and only part of it existsis desired on each
109    processor.  For example, a parallel solver may access only some of
110    the rows from each processor.  The algorithm used here reads
111    relatively small blocks of data rather than reading the entire
112    matrix and then subsetting it.
113 
114    Notes for advanced users:
115    Most users should not need to know the details of the binary storage
116    format, since MatLoad() and MatView() completely hide these details.
117    But for anyone who's interested, the standard binary matrix storage
118    format is
119 
120 $    int    MAT_FILE_COOKIE
121 $    int    number of rows
122 $    int    number of columns
123 $    int    total number of nonzeros
124 $    int    *number nonzeros in each row
125 $    int    *column indices of all nonzeros (starting index is zero)
126 $    PetscScalar *values of all nonzeros
127 
128    Note for Cray users, the int's stored in the binary file are 32 bit
129 integers; not 64 as they are represented in the memory, so if you
130 write your own routines to read/write these binary files from the Cray
131 you need to adjust the integer sizes that you read in, see
132 PetscReadBinary() and PetscWriteBinary() to see how this may be
133 done.
134 
135    In addition, PETSc automatically does the byte swapping for
136 machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
137 linux, nt and the paragon; thus if you write your own binary
138 read/write routines you have to swap the bytes; see PetscReadBinary()
139 and PetscWriteBinary() to see how this may be done.
140 
141 .keywords: matrix, load, binary, input
142 
143 .seealso: PetscViewerBinaryOpen(), MatView(), VecLoad(), MatLoadRegister(),
144           MatLoadRegisterAll()
145 
146  @*/
147 int MatLoad(PetscViewer viewer,MatType outtype,Mat *newmat)
148 {
149   Mat         factory;
150   int         ierr;
151   PetscTruth  isbinary,flg;
152   MPI_Comm    comm;
153   int         (*r)(PetscViewer,MatType,Mat*);
154   char        mtype[256],*prefix;
155 
156   PetscFunctionBegin;
157   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
158 
159 #ifndef PETSC_USE_DYNAMIC_LIBRARIES
160   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
161 #endif
162 
163   *newmat = 0;
164 
165   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,&prefix);CHKERRQ(ierr);
166   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
167   if (!isbinary) {
168     SETERRQ(PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
169   }
170 
171   ierr = PetscOptionsGetString(prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
172   if (flg) {
173     outtype = mtype;
174   }
175   ierr = PetscOptionsGetString(prefix,"-matload_type",mtype,256,&flg);CHKERRQ(ierr);
176   if (flg) {
177     outtype = mtype;
178   }
179   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
180   if (!outtype) outtype = MATMPIAIJ;
181 
182   ierr = MatCreate(comm,0,0,0,0,&factory);CHKERRQ(ierr);
183   ierr = MatSetType(factory,outtype);CHKERRQ(ierr);
184   r = factory->ops->load;
185   ierr = MatDestroy(factory);
186   if (!r) {
187     ierr =  PetscFListFind(comm,MatLoadList,outtype,(void(**)(void))&r);CHKERRQ(ierr);
188     if (!r) SETERRQ1(1,"Unknown Mat type given: %s",outtype);
189   }
190 
191   ierr = PetscLogEventBegin(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
192   ierr = (*r)(viewer,outtype,newmat);CHKERRQ(ierr);
193   ierr = PetscLogEventEnd(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
194 
195   ierr = PetscOptionsHasName(prefix,"-matload_symmetric",&flg);CHKERRQ(ierr);
196   if (flg) {
197     ierr = MatSetOption(*newmat,MAT_SYMMETRIC);CHKERRQ(ierr);
198   }
199   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
200   if (flg) {ierr = MatLoadPrintHelp_Private(*newmat);CHKERRQ(ierr); }
201   PetscFunctionReturn(0);
202 }
203 
204