#include "petscmat.h" PetscErrorCode MatLoad(Mat newmat,PetscViewer viewer)Collective on PetscViewer
newmat | - the newly loaded matrix, this needs to have been created with MatCreate() or some related function before a call to MatLoad() | |
viewer | - binary/HDF5 file viewer |
MatLoad() automatically loads into the options database any options given in the file filename.info where filename is the name of the file that was passed to the PetscViewerBinaryOpen(). The options in the info file will be ignored if you use the -viewer_binary_skip_info option.
If the type or size of newmat is not set before a call to MatLoad, PETSc sets the default matrix type AIJ and sets the local and global sizes. If type and/or size is already set, then the same are used.
In parallel, each processor can load a subset of rows (or the entire matrix). This routine is especially useful when a large matrix is stored on disk and only part of it is desired on each processor. For example, a parallel solver may access only some of the rows from each processor. The algorithm used here reads relatively small blocks of data rather than reading the entire matrix and then subsetting it.
Viewer's PetscViewerType must be either PETSCVIEWERBINARY or PETSCVIEWERHDF5. Such viewer can be created using PetscViewerBinaryOpen()/PetscViewerHDF5Open(), or the sequence like
PetscViewer v;
PetscViewerCreate(PETSC_COMM_WORLD,&v);
PetscViewerSetType(v,PETSCVIEWERBINARY);
PetscViewerSetFromOptions(v);
PetscViewerFileSetMode(v,FILE_MODE_READ);
PetscViewerFileSetName(v,"datafile");The optional PetscViewerSetFromOptions() call allows to override PetscViewerSetType() using option
-viewer_type {binary,hdf5}
See the example src/ksp/ksp/examples/tutorials/ex27.c with the first approach, and src/mat/examples/tutorials/ex10.c with the second approach.
Most users should not need to know the details of the binary storage format, since MatLoad() and MatView() completely hide these details. But for anyone who's interested, the standard binary matrix storage format is
int MAT_FILE_CLASSID
int number of rows
int number of columns
int total number of nonzeros
int *number nonzeros in each row
int *column indices of all nonzeros (starting index is zero)
PetscScalar *values of all nonzeros
PETSc automatically does the byte swapping for machines that store the bytes reversed, e.g. DEC alpha, freebsd, linux, Windows and the paragon; thus if you write your own binary read/write routines you have to swap the bytes; see PetscBinaryRead() and PetscBinaryWrite() to see how this may be done.
As the MATLAB MAT-File Version 7.3 format is also a HDF5 flavor, we decided to use by default the same structure and naming of the AIJ arrays and column count (see PetscViewerHDF5SetAIJNames()) within the HDF5 file. This means that a MAT file saved with -v7.3 flag, e.g.
save example.mat A b -v7.3can be directly read by this routine (see Reference 1 for details). Note that depending on your MATLAB version, this format might be a default, otherwise you can set it as default in Preferences.
Unless -nocompression flag is used to save the file in MATLAB, PETSc must be configured with ZLIB package.
See also examples src/mat/examples/tutorials/ex10.c and src/ksp/ksp/examples/tutorials/ex27.c
Corresponding MatView() is not yet implemented.
The loaded matrix is actually a transpose of the original one in MATLAB, unless you push PETSC_VIEWER_HDF5_MAT format (see examples above). With this format, matrix is automatically transposed by PETSc, unless the matrix is marked as SPD or symmetric (see MatSetOption(), MAT_SPD, MAT_SYMMETRIC).