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