xref: /petsc/src/mat/utils/matio.c (revision 788b2dce13f2cc3ea9754c3db760959df2322823)
1 #ifndef lint
2 static char vcid[] = "$Id: matio.c,v 1.9 1995/09/21 20:11:31 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 <unistd.h>
11 #include "vec/vecimpl.h"
12 #include "sysio.h"
13 #include "pinclude/pviewer.h"
14 #include "matimpl.h"
15 #include "row.h"
16 
17 extern int MatLoad_MPIRowbs(Viewer,MatType,Mat *);
18 extern int MatLoad_SeqAIJ(Viewer,MatType,Mat *);
19 
20 
21 /* -------------------------------------------------------------------- */
22 
23 
24 /* @
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, MATMPIROWBS,..
31 
32    Output Parameters:
33 .  newmat - new matrix
34 
35    Notes:
36    In parallel, each processor can load a subset of rows (or the
37    entire matrix).  This routine is especially useful when a large
38    matrix is stored on disk and only part of it is desired on each
39    processor.  For example, a parallel solver may access only some of
40    the rows from each processor.  The algorithm used here reads
41    relatively small blocks of data rather than reading the entire
42    matrix and then subsetting it.
43 
44    Currently, the _entire_ matrix must be loaded.  This should
45    probably change.
46 
47 .seealso: MatView(), VecLoad()
48 */
49 int MatLoad(Viewer bview,MatType outtype,Mat *newmat)
50 {
51   PetscObject vobj = (PetscObject) bview;
52   int         ierr,set;
53   MatType     type;
54   *newmat = 0;
55 
56   PLogEventBegin(MAT_Load,bview,0,0,0);
57   ierr = MatGetFormatFromOptions(vobj->comm,&type,&set); CHKERRQ(ierr);
58   if (!set) type = outtype;
59 
60   PETSCVALIDHEADERSPECIFIC(vobj,VIEWER_COOKIE);
61   if (vobj->type != BINARY_FILE_VIEWER)
62    SETERRQ(1,"MatLoad: Invalid viewer; open viewer with ViewerFileOpenBinary()");
63 
64   if (type == MATSEQAIJ) {
65     ierr = MatLoad_SeqAIJ(bview,type,newmat); CHKERRQ(ierr);
66   }
67 #if defined(HAVE_BLOCKSOLVE) && !defined(__cplusplus)
68   else if (type == MATMPIROWBS) {
69     ierr = MatLoad_MPIRowbs(bview,type,newmat); CHKERRQ(ierr);
70   }
71 #endif
72   else {
73     SETERRQ(1,"MatLoad: cannot load with that matrix type yet");
74   }
75 
76   PLogEventEnd(MAT_Load,bview,0,0,0);
77   return 0;
78 }
79