petsc-3.11.4 2019-09-28
Report Typos and Errors

MatLoad

Loads a matrix that has been stored in binary/HDF5 format with MatView(). The matrix format is determined from the options database. Generates a parallel MPI matrix if the communicator has more than one processor. The default matrix type is AIJ.

Synopsis

#include "petscmat.h" 
PetscErrorCode MatLoad(Mat newmat,PetscViewer viewer)
Collective on PetscViewer

Input Parameters

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

Options Database Keys

Used with block matrix formats (MATSEQBAIJ, ...) to specify block size
-matload_block_size <bs> -

Notes

If the Mat type has not yet been given then MATAIJ is used, call MatSetFromOptions() on the Mat before calling this routine if you wish to set it from the options database.

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.

Notes about the PETSc binary format

In case of PETSCVIEWERBINARY, a native PETSc binary format is used. Each of the blocks is read onto rank 0 and then shipped to its destination rank, one after another. Multiple objects, both matrices and vectors, can be stored within the same file. Their PetscObject name is ignored; they are loaded in the order of their storage.

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.

Notes about the HDF5 (MATLAB MAT-File Version 7.3) format

In case of PETSCVIEWERHDF5, a parallel HDF5 reader is used. Each processor's chunk is loaded independently by its owning rank. Multiple objects, both matrices and vectors, can be stored within the same file. They are looked up by their PetscObject name.

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.3
can 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

Current HDF5 (MAT-File) limitations

This reader currently supports only real MATSEQAIJ and MATMPIAIJ matrices.

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).

References

1. MATLAB(R) Documentation, manual page of save(), https://www.mathworks.com/help/matlab/ref/save.html#btox10b-1-version

Keywords

matrix, load, binary, input, HDF5

See Also

PetscViewerBinaryOpen(), PetscViewerSetType(), PetscViewerHDF5SetAIJNames(), MatView(), VecLoad()

Level

beginner

Location

src/mat/interface/matrix.c

Examples

src/vec/vec/examples/tutorials/ex6.c.html
src/mat/examples/tutorials/ex1.c.html
src/mat/examples/tutorials/ex9.c.html
src/mat/examples/tutorials/ex10.c.html
src/mat/examples/tutorials/ex12.c.html
src/mat/examples/tutorials/ex16.c.html
src/ksp/ksp/examples/tutorials/ex10.c.html
src/ksp/ksp/examples/tutorials/ex27.c.html
src/ksp/ksp/examples/tutorials/ex41.c.html
src/ksp/ksp/examples/tutorials/ex63.cxx.html
src/ksp/ksp/examples/tutorials/ex72.c.html

Implementations

MatLoad_MPI_DA in src/dm/impls/da/fdda.c
MatLoad_MPIAIJ in src/mat/impls/aij/mpi/mpiaij.c
MatLoad_MPIAIJ_Binary in src/mat/impls/aij/mpi/mpiaij.c
MatLoad_SeqAIJ in src/mat/impls/aij/seq/aij.c
MatLoad_SeqAIJ_Binary in src/mat/impls/aij/seq/aij.c
MatLoad_AIJ_HDF5 in src/mat/impls/aij/seq/aijhdf5.c
MatLoad_MPIBAIJ in src/mat/impls/baij/mpi/mpibaij.c
MatLoad_SeqBAIJ in src/mat/impls/baij/seq/baij.c
MatLoad_BlockMat in src/mat/impls/blockmat/seq/blockmat.c
MatLoad_MPIDense_DenseInFile in src/mat/impls/dense/mpi/mpidense.c
MatLoad_MPIDense in src/mat/impls/dense/mpi/mpidense.c
MatLoad_SeqDense in src/mat/impls/dense/seq/dense.c
MatLoad_Elemental in src/mat/impls/elemental/matelem.cxx
MatLoad_MPISBAIJ in src/mat/impls/sbaij/mpi/mpisbaij.c
MatLoad_SeqSBAIJ in src/mat/impls/sbaij/seq/sbaij.c

Index of all Mat routines
Table of Contents for all manual pages
Index of all manual pages