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