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