xref: /petsc/src/mat/utils/matio.c (revision ddd1276785a76ea4a3b2575e2a65f0ba44dae580)
1 #ifndef lint
2 static char vcid[] = "$Id: matio.c,v 1.13 1995/10/06 22:25:04 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    Currently, the _entire_ matrix must be loaded.  This should
42    probably change.
43 
44 .seealso: MatView(), VecLoad()
45  @*/
46 int MatLoad(Viewer bview,MatType outtype,Mat *newmat)
47 {
48   PetscObject vobj = (PetscObject) bview;
49   int         ierr,set;
50   MatType     type;
51   *newmat = 0;
52 
53   PLogEventBegin(MAT_Load,bview,0,0,0);
54   ierr = MatGetFormatFromOptions(vobj->comm,&type,&set); CHKERRQ(ierr);
55   if (!set) type = outtype;
56 
57   PETSCVALIDHEADERSPECIFIC(vobj,VIEWER_COOKIE);
58   if (vobj->type != BINARY_FILE_VIEWER)
59    SETERRQ(1,"MatLoad: Invalid viewer; open viewer with ViewerFileOpenBinary()");
60 
61   if (type == MATSEQAIJ) {
62     ierr = MatLoad_SeqAIJ(bview,type,newmat); CHKERRQ(ierr);
63   }
64   else if (type == MATMPIAIJ || type == MATMPIROW) {
65     ierr = MatLoad_MPIAIJorMPIRow(bview,type,newmat); CHKERRQ(ierr);
66   }
67   else if (type == MATMPIBDIAG) {
68     ierr = MatLoad_MPIBDiag(bview,type,newmat); CHKERRQ(ierr);
69   }
70   else if (type == MATSEQROW) {
71     ierr = MatLoad_SeqRow(bview,type,newmat); CHKERRQ(ierr);
72   }
73 #if defined(HAVE_BLOCKSOLVE) && !defined(__cplusplus)
74   else if (type == MATMPIROWBS) {
75     ierr = MatLoad_MPIRowbs(bview,type,newmat); CHKERRQ(ierr);
76   }
77 #endif
78   else {
79     SETERRQ(1,"MatLoad: cannot load with that matrix type yet");
80   }
81 
82   PLogEventEnd(MAT_Load,bview,0,0,0);
83   return 0;
84 }
85