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