xref: /petsc/src/mat/utils/matio.c (revision 18be62a5feccf172f7bc80c15c4be8f6d6443e8b)
1 #define PETSCMAT_DLL
2 
3 /*
4    This file contains simple binary read/write routines for matrices.
5  */
6 
7 #include "src/mat/matimpl.h"             /*I  "petscmat.h"  I*/
8 #include "petscsys.h"
9 
10 #undef __FUNCT__
11 #define __FUNCT__ "MatLoadPrintHelp_Private"
12 static PetscErrorCode MatLoadPrintHelp_Private(Mat A)
13 {
14   static PetscTruth called = PETSC_FALSE;
15   MPI_Comm          comm = A->comm;
16   PetscErrorCode ierr;
17 
18   PetscFunctionBegin;
19   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
20   ierr = (*PetscHelpPrintf)(comm," Options for MatLoad:\n");CHKERRQ(ierr);
21   ierr = (*PetscHelpPrintf)(comm,"  -mat_type <type>\n");CHKERRQ(ierr);
22   ierr = (*PetscHelpPrintf)(comm,"  -matload_type <type>\n");CHKERRQ(ierr);
23   ierr = (*PetscHelpPrintf)(comm,"  -matload_block_size <block_size> :Used for MATBAIJ, MATBDIAG\n");CHKERRQ(ierr);
24   ierr = (*PetscHelpPrintf)(comm,"  -matload_bdiag_diags <s1,s2,s3,...> : Used for MATBDIAG\n");CHKERRQ(ierr);
25   PetscFunctionReturn(0);
26 }
27 
28 #undef __FUNCT__
29 #define __FUNCT__ "MatLoad"
30 /*@C
31    MatLoad - Loads a matrix that has been stored in binary format
32    with MatView().  The matrix format is determined from the options database.
33    Generates a parallel MPI matrix if the communicator has more than one
34    processor.  The default matrix type is AIJ.
35 
36    Collective on PetscViewer
37 
38    Input Parameters:
39 +  viewer - binary file viewer, created with PetscViewerBinaryOpen()
40 -  outtype - type of matrix desired, for example MATSEQAIJ,
41              MATMPIROWBS, etc.  See types in petsc/include/petscmat.h.
42 
43    Output Parameters:
44 .  newmat - new matrix
45 
46    Basic Options Database Keys:
47 +    -matload_type seqaij   - AIJ type
48 .    -matload_type mpiaij   - parallel AIJ type
49 .    -matload_type seqbaij  - block AIJ type
50 .    -matload_type mpibaij  - parallel block AIJ type
51 .    -matload_type seqsbaij - block symmetric AIJ type
52 .    -matload_type mpisbaij - parallel block symmetric AIJ type
53 .    -matload_type seqbdiag - block diagonal type
54 .    -matload_type mpibdiag - parallel block diagonal type
55 .    -matload_type mpirowbs - parallel rowbs type
56 .    -matload_type seqdense - dense type
57 .    -matload_type mpidense - parallel dense type
58 -    -matload_symmetric - matrix in file is symmetric
59 
60    More Options Database Keys:
61    Used with block matrix formats (MATSEQBAIJ, MATMPIBDIAG, ...) to specify
62    block size
63 .    -matload_block_size <bs>
64 
65    Used to specify block diagonal numbers for MATSEQBDIAG and MATMPIBDIAG formats
66 .    -matload_bdiag_diags <s1,s2,s3,...>
67 
68    Level: beginner
69 
70    Notes:
71    MatLoad() automatically loads into the options database any options
72    given in the file filename.info where filename is the name of the file
73    that was passed to the PetscViewerBinaryOpen(). The options in the info
74    file will be ignored if you use the -viewer_binary_skip_info option.
75 
76    In parallel, each processor can load a subset of rows (or the
77    entire matrix).  This routine is especially useful when a large
78    matrix is stored on disk and only part of it existsis desired on each
79    processor.  For example, a parallel solver may access only some of
80    the rows from each processor.  The algorithm used here reads
81    relatively small blocks of data rather than reading the entire
82    matrix and then subsetting it.
83 
84    Notes for advanced users:
85    Most users should not need to know the details of the binary storage
86    format, since MatLoad() and MatView() completely hide these details.
87    But for anyone who's interested, the standard binary matrix storage
88    format is
89 
90 $    int    MAT_FILE_COOKIE
91 $    int    number of rows
92 $    int    number of columns
93 $    int    total number of nonzeros
94 $    int    *number nonzeros in each row
95 $    int    *column indices of all nonzeros (starting index is zero)
96 $    PetscScalar *values of all nonzeros
97 
98    PETSc automatically does the byte swapping for
99 machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
100 linux, Windows and the paragon; thus if you write your own binary
101 read/write routines you have to swap the bytes; see PetscBinaryRead()
102 and PetscBinaryWrite() to see how this may be done.
103 
104 .keywords: matrix, load, binary, input
105 
106 .seealso: PetscViewerBinaryOpen(), MatView(), VecLoad()
107 
108  @*/
109 PetscErrorCode PETSCMAT_DLLEXPORT MatLoad(PetscViewer viewer, MatType outtype,Mat *newmat)
110 {
111   Mat            factory;
112   PetscErrorCode ierr;
113   PetscTruth     isbinary,flg;
114   MPI_Comm       comm;
115   PetscErrorCode (*r)(PetscViewer, MatType,Mat*);
116   char           mtype[256];
117   const char     *prefix;
118 
119   PetscFunctionBegin;
120   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,1);
121   PetscValidPointer(newmat,3);
122   *newmat = 0;
123 
124   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,(const char **)&prefix);CHKERRQ(ierr);
125   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
126   if (!isbinary) {
127     SETERRQ(PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
128   }
129 
130   ierr = PetscOptionsGetString(prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
131   if (flg) {
132     outtype = mtype;
133   }
134   ierr = PetscOptionsGetString(prefix,"-matload_type",mtype,256,&flg);CHKERRQ(ierr);
135   if (flg) {
136     outtype = mtype;
137   }
138   if (!outtype) outtype = MATAIJ;
139 
140   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
141   ierr = MatCreate(comm,&factory);CHKERRQ(ierr);
142   ierr = MatSetSizes(factory,0,0,0,0);CHKERRQ(ierr);
143   ierr = MatSetType(factory,outtype);CHKERRQ(ierr);
144   r = factory->ops->load;
145   ierr = MatDestroy(factory);
146   if (!r) SETERRQ1(PETSC_ERR_SUP,"MatLoad is not supported for type: %s",outtype);
147 
148   ierr = PetscLogEventBegin(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
149   ierr = (*r)(viewer,outtype,newmat);CHKERRQ(ierr);
150   ierr = PetscLogEventEnd(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
151 
152   ierr = PetscOptionsHasName(prefix,"-matload_symmetric",&flg);CHKERRQ(ierr);
153   if (flg) {
154     ierr = MatSetOption(*newmat,MAT_SYMMETRIC);CHKERRQ(ierr);
155     ierr = MatSetOption(*newmat,MAT_SYMMETRY_ETERNAL);CHKERRQ(ierr);
156   }
157   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
158   if (flg) {ierr = MatLoadPrintHelp_Private(*newmat);CHKERRQ(ierr); }
159   PetscFunctionReturn(0);
160 }
161 
162