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