xref: /petsc/src/sys/tests/ex50.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
1 
2 static char help[] = "Tests using PetscViewerGetSubViewer() recursively\n\n";
3 
4 #include <petscsys.h>
5 #include <petscviewer.h>
6 
7 int main(int argc, char **argv)
8 {
9   PetscViewer       viewer, subviewer, subsubviewer;
10   PetscViewerFormat format;
11   PetscBool         flg;
12   PetscSubcomm      psubcomm, psubsubcomm;
13   MPI_Comm          comm, subcomm, subsubcomm;
14   PetscMPIInt       size;
15 
16   /*
17     Every PETSc routine should begin with the PetscInitialize() routine.
18     argc, argv - These command line arguments are taken to extract the options
19                  supplied to PETSc and options supplied to MPI.
20     help       - When PETSc executable is invoked with the option -help,
21                  it prints the various options that can be applied at
22                  runtime.  The user can use the "help" variable place
23                  additional help messages in this printout.
24   */
25   PetscFunctionBeginUser;
26   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
27   comm = PETSC_COMM_WORLD;
28   PetscCallMPI(MPI_Comm_size(comm, &size));
29   PetscCheck(size >= 4, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must run with at least 4 MPI ranks");
30   PetscCall(PetscOptionsGetViewer(comm, NULL, NULL, "-viewer", &viewer, &format, &flg));
31   PetscCheck(viewer, PETSC_COMM_WORLD, PETSC_ERR_SUP, "Must use -viewer option");
32 
33   PetscCall(PetscViewerASCIIPrintf(viewer, "Print called on original full viewer %d\n", PetscGlobalRank));
34 
35   PetscCall(PetscSubcommCreate(comm, &psubcomm));
36   PetscCall(PetscSubcommSetNumber(psubcomm, 2));
37   PetscCall(PetscSubcommSetType(psubcomm, PETSC_SUBCOMM_CONTIGUOUS));
38   /* enable runtime switch of psubcomm type, e.g., '-psubcomm_type interlaced */
39   PetscCall(PetscSubcommSetFromOptions(psubcomm));
40   subcomm = PetscSubcommChild(psubcomm);
41 
42   PetscCall(PetscViewerGetSubViewer(viewer, subcomm, &subviewer));
43 
44   PetscCall(PetscViewerASCIIPrintf(subviewer, "  Print called on sub viewers %d\n", PetscGlobalRank));
45 
46   PetscCall(PetscSubcommCreate(subcomm, &psubsubcomm));
47   PetscCall(PetscSubcommSetNumber(psubsubcomm, 2));
48   PetscCall(PetscSubcommSetType(psubsubcomm, PETSC_SUBCOMM_CONTIGUOUS));
49   /* enable runtime switch of psubcomm type, e.g., '-psubcomm_type interlaced */
50   PetscCall(PetscSubcommSetFromOptions(psubsubcomm));
51   subsubcomm = PetscSubcommChild(psubsubcomm);
52 
53   PetscCall(PetscViewerGetSubViewer(subviewer, subsubcomm, &subsubviewer));
54 
55   PetscCall(PetscViewerASCIIPrintf(subsubviewer, "  Print called on sub sub viewers %d\n", PetscGlobalRank));
56 
57   PetscCall(PetscViewerRestoreSubViewer(subviewer, subsubcomm, &subsubviewer));
58   PetscCall(PetscViewerRestoreSubViewer(viewer, subcomm, &subviewer));
59 
60   PetscCall(PetscSubcommDestroy(&psubsubcomm));
61   PetscCall(PetscSubcommDestroy(&psubcomm));
62   PetscCall(PetscViewerDestroy(&viewer));
63   PetscCall(PetscFinalize());
64   return 0;
65 }
66 
67 /*TEST
68 
69    test:
70       nsize: 4
71       args: -viewer
72 
73 TEST*/
74