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