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