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