xref: /petsc/src/mat/utils/matio.c (revision 563e5c16c5fe2ffc90458153e21b6429a2478575)
1 #ifndef lint
2 static char vcid[] = "$Id: matio.c,v 1.27 1996/06/11 20:34:48 curfman Exp balay $";
3 #endif
4 
5 /*
6    This file contains simple binary read/write routines for matrices.
7  */
8 
9 #include "petsc.h"
10 #include "../matimpl.h"
11 #include "sys.h"
12 #include "pinclude/pviewer.h"
13 
14 extern int MatLoad_MPIRowbs(Viewer,MatType,Mat*);
15 extern int MatLoad_SeqAIJ(Viewer,MatType,Mat*);
16 extern int MatLoad_MPIAIJ(Viewer,MatType,Mat*);
17 extern int MatLoad_SeqBDiag(Viewer,MatType,Mat*);
18 extern int MatLoad_MPIBDiag(Viewer,MatType,Mat*);
19 extern int MatLoad_SeqDense(Viewer,MatType,Mat*);
20 extern int MatLoad_MPIDense(Viewer,MatType,Mat*);
21 extern int MatLoad_SeqBAIJ(Viewer,MatType,Mat*);
22 
23 extern int MatLoadGetInfo_Private(Viewer);
24 
25 /*@C
26    MatLoad - Loads a matrix that has been stored in binary format
27    with MatView().
28 
29    Input Parameters:
30 .  viewer - binary file viewer, created with ViewerFileOpenBinary()
31 .  outtype - type of matrix desired, for example MATSEQAIJ,
32    MATMPIROWBS, etc.  See types in petsc/include/mat.h.
33 
34    Output Parameters:
35 .  newmat - new matrix
36 
37    Options Database Key:
38    Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, etc.)
39 $    -matload_block_size <bs>
40 
41    Notes:
42    In parallel, each processor can load a subset of rows (or the
43    entire matrix).  This routine is especially useful when a large
44    matrix is stored on disk and only part of it is desired on each
45    processor.  For example, a parallel solver may access only some of
46    the rows from each processor.  The algorithm used here reads
47    relatively small blocks of data rather than reading the entire
48    matrix and then subsetting it.
49 
50    Notes for advanced users:
51    Most users should not need to know the details of the binary storage
52    format, since MatLoad() and MatView() completely hide these details.
53    But for anyone who's interested, the standard binary matrix storage
54    format is
55 
56 $    int    MAT_COOKIE
57 $    int    number of rows
58 $    int    number of columns
59 $    int    total number of nonzeros
60 $    int    *number nonzeros in each row
61 $    int    *column indices of all nonzeros (starting index is zero)
62 $    Scalar *values of all nonzeros
63 
64 .keywords: matrix, load, binary, input
65 
66 .seealso: ViewerFileOpenBinary(), MatView(), VecLoad()
67  @*/
68 int MatLoad(Viewer viewer,MatType outtype,Mat *newmat)
69 {
70   int         ierr,set;
71   MatType     type;
72   ViewerType  vtype;
73   MPI_Comm    comm;
74   *newmat = 0;
75 
76   PetscValidHeaderSpecific(viewer,VIEWER_COOKIE);
77   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
78   if (vtype != BINARY_FILE_VIEWER)
79    SETERRQ(1,"MatLoad: Invalid viewer; open viewer with ViewerFileOpenBinary()");
80 
81   PetscObjectGetComm((PetscObject)viewer,&comm);
82   ierr = MatGetTypeFromOptions(comm,0,&type,&set); CHKERRQ(ierr);
83   if (!set) type = outtype;
84 
85   ierr = MatLoadGetInfo_Private(viewer); CHKERRQ(ierr);
86 
87   PLogEventBegin(MAT_Load,viewer,0,0,0);
88 
89   if (type == MATSEQAIJ) {
90     ierr = MatLoad_SeqAIJ(viewer,type,newmat); CHKERRQ(ierr);
91   }
92   else if (type == MATMPIAIJ) {
93     ierr = MatLoad_MPIAIJ(viewer,type,newmat); CHKERRQ(ierr);
94   }
95   else if (type == MATSEQBDIAG) {
96     ierr = MatLoad_SeqBDiag(viewer,type,newmat); CHKERRQ(ierr);
97   }
98   else if (type == MATMPIBDIAG) {
99     ierr = MatLoad_MPIBDiag(viewer,type,newmat); CHKERRQ(ierr);
100   }
101   else if (type == MATSEQDENSE) {
102     ierr = MatLoad_SeqDense(viewer,type,newmat); CHKERRQ(ierr);
103   }
104   else if (type == MATMPIDENSE) {
105     ierr = MatLoad_MPIDense(viewer,type,newmat); CHKERRQ(ierr);
106   }
107   else if (type == MATMPIROWBS) {
108 #if defined(HAVE_BLOCKSOLVE) && !defined(__cplusplus)
109     ierr = MatLoad_MPIRowbs(viewer,type,newmat); CHKERRQ(ierr);
110 #else
111     SETERRQ(1,"MatLoad: MATMPIROWBS format does not support complex numbers.");
112 #endif
113   }
114   else if (type == MATSEQBAIJ) {
115     ierr = MatLoad_SeqBAIJ(viewer,type,newmat); CHKERRQ(ierr);
116   }
117   else if (type == MATMPIBAIJ) {
118     ierr = MatLoad_MPIBAIJ(viewer,type,newmat); CHKERRQ(ierr);
119   }
120   else {
121     SETERRQ(1,"MatLoad: cannot load with that matrix type yet");
122   }
123 
124   PLogEventEnd(MAT_Load,viewer,0,0,0);
125   return 0;
126 }
127 
128 /*
129     MatLoadGetInfo_Private - Loads the matrix options from the name.info file
130   if it exists.
131 
132 */
133 int MatLoadGetInfo_Private(Viewer viewer)
134 {
135   FILE *file;
136   char string[128],*first,*second,*final;
137   int  len,ierr,flg;
138 
139   ierr = OptionsHasName(PETSC_NULL,"-matload_ignore_info",&flg);CHKERRQ(ierr);
140   if (flg) return 0;
141 
142   ierr = ViewerBinaryGetInfoPointer(viewer,&file); CHKERRQ(ierr);
143   if (!file) return 0;
144 
145   /* read rows of the file adding them to options database */
146   while (fgets(string,128,file)) {
147     /* Comments are indicated by #, ! or % in the first column */
148     if (string[0] == '#') continue;
149     if (string[0] == '!') continue;
150     if (string[0] == '%') continue;
151     first = PetscStrtok(string," ");
152     second = PetscStrtok(0," ");
153     if (first && first[0] == '-') {
154       if (second) {final = second;} else {final = first;}
155       len = PetscStrlen(final);
156       while (len > 0 && (final[len-1] == ' ' || final[len-1] == '\n')) {
157         len--; final[len] = 0;
158       }
159       ierr = OptionsSetValue(first,second); CHKERRQ(ierr);
160     }
161   }
162   return 0;
163 
164 }
165