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