xref: /petsc/src/mat/utils/matio.c (revision 8754fef1c6c44b1bf197b8595720c8ac46b5e40d)
1 #include <petscviewer.h>
2 #include <petsc/private/matimpl.h>
3 
MatView_Binary_BlockSizes(Mat mat,PetscViewer viewer)4 PetscErrorCode MatView_Binary_BlockSizes(Mat mat, PetscViewer viewer)
5 {
6   FILE       *info;
7   PetscMPIInt rank;
8   PetscInt    rbs, cbs;
9   PetscBool   skip;
10 
11   PetscFunctionBegin;
12   PetscCall(PetscViewerBinaryGetSkipInfo(viewer, &skip));
13   if (skip) PetscFunctionReturn(PETSC_SUCCESS);
14   PetscCall(MatGetBlockSizes(mat, &rbs, &cbs));
15   PetscCall(PetscViewerBinaryGetInfoPointer(viewer, &info));
16   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
17   if (rank == 0 && info) {
18     if (rbs != cbs) PetscCall(PetscFPrintf(PETSC_COMM_SELF, info, "-matload_block_size %" PetscInt_FMT ",%" PetscInt_FMT "\n", rbs, cbs));
19     else PetscCall(PetscFPrintf(PETSC_COMM_SELF, info, "-matload_block_size %" PetscInt_FMT "\n", rbs));
20   }
21   PetscFunctionReturn(PETSC_SUCCESS);
22 }
23 
MatLoad_Binary_BlockSizes(Mat mat,PetscViewer viewer)24 PetscErrorCode MatLoad_Binary_BlockSizes(Mat mat, PetscViewer viewer)
25 {
26   PetscInt  rbs, cbs, bs[2], n = 2;
27   PetscBool set;
28 
29   PetscFunctionBegin;
30   /* get current block sizes */
31   PetscCall(MatGetBlockSizes(mat, &rbs, &cbs));
32   bs[0] = rbs;
33   bs[1] = cbs;
34   /* get block sizes from the options database */
35   PetscOptionsBegin(PetscObjectComm((PetscObject)viewer), NULL, "Options for loading matrix block size", "Mat");
36   PetscCall(PetscOptionsIntArray("-matload_block_size", "Set the block size used to store the matrix", "MatLoad", bs, &n, &set));
37   PetscOptionsEnd();
38   if (!set) PetscFunctionReturn(PETSC_SUCCESS);
39   if (n == 1) bs[1] = bs[0]; /* to support -matload_block_size <bs> */
40   /* set matrix block sizes */
41   if (bs[0] > 0) rbs = bs[0];
42   if (bs[1] > 0) cbs = bs[1];
43   PetscCall(MatSetBlockSizes(mat, rbs, cbs));
44   PetscFunctionReturn(PETSC_SUCCESS);
45 }
46