xref: /petsc/src/mat/utils/matio.c (revision dba47a550923b04c7c4ebbb735eb62a1b3e4e9ae)
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 -matload_ignore_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 PetscReadBinary()
102 and PetscWriteBinary() 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,const MatType outtype,Mat *newmat)
110 {
111   Mat            factory;
112   PetscErrorCode ierr;
113   PetscTruth     isbinary,flg;
114   MPI_Comm       comm;
115   PetscErrorCode (*r)(PetscViewer,const MatType,Mat*);
116   char           mtype[256],*prefix;
117 
118   PetscFunctionBegin;
119   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,1);
120   PetscValidPointer(newmat,3);
121   *newmat = 0;
122 
123   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,&prefix);CHKERRQ(ierr);
124   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
125   if (!isbinary) {
126     SETERRQ(PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
127   }
128 
129   ierr = PetscOptionsGetString(prefix,"-mat_type",mtype,256,&flg);CHKERRQ(ierr);
130   if (flg) {
131     outtype = mtype;
132   }
133   ierr = PetscOptionsGetString(prefix,"-matload_type",mtype,256,&flg);CHKERRQ(ierr);
134   if (flg) {
135     outtype = mtype;
136   }
137   if (!outtype) outtype = MATAIJ;
138 
139   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
140   ierr = MatCreate(comm,0,0,0,0,&factory);CHKERRQ(ierr);
141   ierr = MatSetType(factory,outtype);CHKERRQ(ierr);
142   r = factory->ops->load;
143   ierr = MatDestroy(factory);
144   if (!r) SETERRQ1(PETSC_ERR_SUP,"MatLoad is not supported for type: %s",outtype);
145 
146   ierr = PetscLogEventBegin(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
147   ierr = (*r)(viewer,outtype,newmat);CHKERRQ(ierr);
148   ierr = PetscLogEventEnd(MAT_Load,viewer,0,0,0);CHKERRQ(ierr);
149 
150   ierr = PetscOptionsHasName(prefix,"-matload_symmetric",&flg);CHKERRQ(ierr);
151   if (flg) {
152     ierr = MatSetOption(*newmat,MAT_SYMMETRIC);CHKERRQ(ierr);
153     ierr = MatSetOption(*newmat,MAT_SYMMETRY_ETERNAL);CHKERRQ(ierr);
154   }
155   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
156   if (flg) {ierr = MatLoadPrintHelp_Private(*newmat);CHKERRQ(ierr); }
157   PetscFunctionReturn(0);
158 }
159 
160