xref: /petsc/src/mat/utils/matio.c (revision 77c4ece699e97450631aa6fc5b0ef04ff52df029)
1 #ifndef lint
2 static char vcid[] = "$Id: matio.c,v 1.23 1996/03/10 17:28:48 bsmith Exp bsmith $";
3 #endif
4 
5 /*
6    This file contains simple binary read/write routines for matrices.
7  */
8 
9 #include "petsc.h"
10 #include "vec/vecimpl.h"
11 #include "../matimpl.h"
12 #include "sys.h"
13 #include "pinclude/pviewer.h"
14 
15 extern int MatLoad_MPIRowbs(Viewer,MatType,Mat*);
16 extern int MatLoad_SeqAIJ(Viewer,MatType,Mat*);
17 extern int MatLoad_MPIAIJ(Viewer,MatType,Mat*);
18 extern int MatLoad_SeqBDiag(Viewer,MatType,Mat*);
19 extern int MatLoad_MPIBDiag(Viewer,MatType,Mat*);
20 extern int MatLoad_SeqDense(Viewer,MatType,Mat*);
21 extern int MatLoad_MPIDense(Viewer,MatType,Mat*);
22 extern int MatLoad_SeqBAIJ(Viewer,MatType,Mat*);
23 
24 
25 
26 /*@C
27    MatLoad - Loads a matrix that has been stored in binary format
28    with MatView().
29 
30    Input Parameters:
31 .  viewer - binary file viewer, created with ViewerFileOpenBinary()
32 .  outtype - type of matrix desired, for example MATSEQAIJ,
33    MATMPIROWBS, etc.  See types in petsc/include/mat.h.
34 
35    Output Parameters:
36 .  newmat - new matrix
37 
38    Notes:
39    In parallel, each processor can load a subset of rows (or the
40    entire matrix).  This routine is especially useful when a large
41    matrix is stored on disk and only part of it is desired on each
42    processor.  For example, a parallel solver may access only some of
43    the rows from each processor.  The algorithm used here reads
44    relatively small blocks of data rather than reading the entire
45    matrix and then subsetting it.
46 
47    Notes for advanced users:
48    Most users should not need to know the details of the binary storage
49    format, since MatLoad() and MatView() completely hide these details.
50    But for anyone who's interested, the standard binary matrix storage
51    format is
52 
53 $    int    MAT_COOKIE
54 $    int    number of rows
55 $    int    number of columns
56 $    int    total number of nonzeros
57 $    int    *number nonzeros in each row
58 $    int    *column indices of all nonzeros (starting index is zero)
59 $    Scalar *values of all nonzeros
60 
61 .keywords: matrix, load, binary, input
62 
63 .seealso: ViewerFileOpenBinary(), MatView(), VecLoad()
64  @*/
65 int MatLoad(Viewer viewer,MatType outtype,Mat *newmat)
66 {
67   int         ierr,set;
68   MatType     type;
69   ViewerType  vtype;
70   MPI_Comm    comm;
71   *newmat = 0;
72 
73   PetscValidHeaderSpecific(viewer,VIEWER_COOKIE);
74   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
75   if (vtype != BINARY_FILE_VIEWER)
76    SETERRQ(1,"MatLoad: Invalid viewer; open viewer with ViewerFileOpenBinary()");
77 
78   PetscObjectGetComm((PetscObject)viewer,&comm);
79   ierr = MatGetTypeFromOptions(comm,0,&type,&set); CHKERRQ(ierr);
80   if (!set) type = outtype;
81   PLogEventBegin(MAT_Load,viewer,0,0,0);
82 
83   if (type == MATSEQAIJ) {
84     ierr = MatLoad_SeqAIJ(viewer,type,newmat); CHKERRQ(ierr);
85   }
86   else if (type == MATMPIAIJ) {
87     ierr = MatLoad_MPIAIJ(viewer,type,newmat); CHKERRQ(ierr);
88   }
89   else if (type == MATSEQBDIAG) {
90     ierr = MatLoad_SeqBDiag(viewer,type,newmat); CHKERRQ(ierr);
91   }
92   else if (type == MATMPIBDIAG) {
93     ierr = MatLoad_MPIBDiag(viewer,type,newmat); CHKERRQ(ierr);
94   }
95   else if (type == MATSEQDENSE) {
96     ierr = MatLoad_SeqDense(viewer,type,newmat); CHKERRQ(ierr);
97   }
98   else if (type == MATMPIDENSE) {
99     ierr = MatLoad_MPIDense(viewer,type,newmat); CHKERRQ(ierr);
100   }
101 #if defined(HAVE_BLOCKSOLVE) && !defined(__cplusplus)
102   else if (type == MATMPIROWBS) {
103     ierr = MatLoad_MPIRowbs(viewer,type,newmat); CHKERRQ(ierr);
104   }
105 #endif
106   else if (type == MATSEQBAIJ) {
107     ierr = MatLoad_SeqBAIJ(viewer,type,newmat); CHKERRQ(ierr);
108   }
109   else {
110     SETERRQ(1,"MatLoad: cannot load with that matrix type yet");
111   }
112 
113   PLogEventEnd(MAT_Load,viewer,0,0,0);
114   return 0;
115 }
116