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