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