xref: /petsc/src/sys/tests/ex50.c (revision f97672e55eacc8688507b9471cd7ec2664d7f203)
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   PetscCall(PetscInitialize(&argc,&argv,(char*)0,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 processes");
29   PetscCall(PetscOptionsGetViewer(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