xref: /petsc/src/mat/utils/matio.c (revision c8a8475e04bcaa43590892a5c3e60c6f87bc31f7)
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 seqbdiag - block diagonal type
82 .    -matload_type mpibdiag - parallel block diagonal type
83 .    -matload_type mpirowbs - parallel rowbs type
84 .    -matload_type seqdense - dense type
85 .    -matload_type mpidense - parallel dense type
86 -    -matload_symmetric - matrix in file is symmetric
87 
88    More Options Database Keys:
89    Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, ...) to specify
90    block size
91 .    -matload_block_size <bs>
92 
93    Used to specify block diagonal numbers for MATSEQBDIAG and MATMPIBDIAG formats
94 .    -matload_bdiag_diags <s1,s2,s3,...>
95 
96    Level: beginner
97 
98    Notes:
99    MatLoad() automatically loads into the options database any options
100    given in the file filename.info where filename is the name of the file
101    that was passed to the PetscViewerBinaryOpen(). The options in the info
102    file will be ignored if you use the -matload_ignore_info option.
103 
104    In parallel, each processor can load a subset of rows (or the
105    entire matrix).  This routine is especially useful when a large
106    matrix is stored on disk and only part of it existsis desired on each
107    processor.  For example, a parallel solver may access only some of
108    the rows from each processor.  The algorithm used here reads
109    relatively small blocks of data rather than reading the entire
110    matrix and then subsetting it.
111 
112    Notes for advanced users:
113    Most users should not need to know the details of the binary storage
114    format, since MatLoad() and MatView() completely hide these details.
115    But for anyone who's interested, the standard binary matrix storage
116    format is
117 
118 $    int    MAT_FILE_COOKIE
119 $    int    number of rows
120 $    int    number of columns
121 $    int    total number of nonzeros
122 $    int    *number nonzeros in each row
123 $    int    *column indices of all nonzeros (starting index is zero)
124 $    PetscScalar *values of all nonzeros
125 
126    Note for Cray users, the int's stored in the binary file are 32 bit
127 integers; not 64 as they are represented in the memory, so if you
128 write your own routines to read/write these binary files from the Cray
129 you need to adjust the integer sizes that you read in, see
130 PetscReadBinary() and PetscWriteBinary() to see how this may be
131 done.
132 
133    In addition, PETSc automatically does the byte swapping for
134 machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
135 linux, nt and the paragon; thus if you write your own binary
136 read/write routines you have to swap the bytes; see PetscReadBinary()
137 and PetscWriteBinary() to see how this may be done.
138 
139 .keywords: matrix, load, binary, input
140 
141 .seealso: PetscViewerBinaryOpen(), MatView(), VecLoad(), MatLoadRegister(),
142           MatLoadRegisterAll()
143 
144  @*/
145 int MatLoad(PetscViewer viewer,MatType outtype,Mat *newmat)
146 {
147   int         ierr;
148   PetscTruth  isbinary,flg;
149   MPI_Comm    comm;
150   int         (*r)(PetscViewer,MatType,Mat*);
151   char        mtype[256];
152 
153   PetscFunctionBegin;
154   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
155   *newmat  = 0;
156 
157   if (!MatLoadRegisterAllCalled) {
158     ierr = MatLoadRegisterAll(PETSC_NULL);CHKERRQ(ierr);
159   }
160 
161   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
162   if (!isbinary) {
163     SETERRQ(PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
164   }
165 
166   ierr = PetscOptionsGetString(PETSC_NULL,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
167   if (flg) {
168     outtype = mtype;
169   }
170   ierr = PetscOptionsGetString(PETSC_NULL,"-matload_type",mtype,256,&flg);CHKERRQ(ierr);
171   if (flg) {
172     outtype = mtype;
173   }
174   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
175   if (!outtype) outtype = MATMPIAIJ;
176   ierr =  PetscFListFind(comm,MatLoadList,outtype,(void(**)(void))&r);CHKERRQ(ierr);
177   if (!r) SETERRQ1(1,"Unknown Mat type given: %s",outtype);
178 
179   ierr = PetscLogEventBegin(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
180   ierr = (*r)(viewer,outtype,newmat);CHKERRQ(ierr);
181   ierr = PetscLogEventEnd(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
182 
183   ierr = PetscOptionsHasName(PETSC_NULL,"-matload_symmetric",&flg);CHKERRQ(ierr);
184   if (flg) {
185     ierr = MatSetOption(*newmat,MAT_SYMMETRIC);CHKERRQ(ierr);
186   }
187   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
188   if (flg) {ierr = MatLoadPrintHelp_Private(*newmat);CHKERRQ(ierr); }
189   PetscFunctionReturn(0);
190 }
191 
192