Actual source code: ex15.c

petsc-3.7.5 2017-01-01
Report Typos and Errors
  2: static char help[] = "Tests using PetscViewerGetSubViewer() recursively\n\n";

  4: /*T
  5:    Concepts: viewers
  6:    Processors: n
  7: T*/
  8: #include <petscsys.h>
  9: #include <petscviewer.h>


 14: int main(int argc,char **argv)
 15: {
 16:   PetscErrorCode    ierr;
 17:   PetscViewer       viewer,subviewer,subsubviewer;
 18:   PetscViewerFormat format;
 19:   PetscBool         flg;
 20:   PetscSubcomm      psubcomm,psubsubcomm;
 21:   MPI_Comm          comm,subcomm,subsubcomm;
 22:   PetscMPIInt       size;

 24:   /*
 25:     Every PETSc routine should begin with the PetscInitialize() routine.
 26:     argc, argv - These command line arguments are taken to extract the options
 27:                  supplied to PETSc and options supplied to MPI.
 28:     help       - When PETSc executable is invoked with the option -help,
 29:                  it prints the various options that can be applied at
 30:                  runtime.  The user can use the "help" variable place
 31:                  additional help messages in this printout.
 32:   */
 33:   PetscInitialize(&argc,&argv,(char*)0,help);
 34:   comm = PETSC_COMM_WORLD;
 35:   MPI_Comm_size(comm,&size);
 36:   if (size < 4) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Must run with at least 4 MPI processes");
 37:   PetscOptionsGetViewer(comm,NULL,"-viewer",&viewer,&format,&flg);
 38:   if (!viewer) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Must use -viewer option");

 40:   PetscViewerASCIIPrintf(viewer,"Print called on original full viewer %d\n",PetscGlobalRank);

 42:   PetscSubcommCreate(comm,&psubcomm);
 43:   PetscSubcommSetNumber(psubcomm,2);
 44:   PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);
 45:   /* enable runtime switch of psubcomm type, e.g., '-psubcomm_type interlaced */
 46:   PetscSubcommSetFromOptions(psubcomm);
 47:   subcomm = PetscSubcommChild(psubcomm);

 49:   PetscViewerGetSubViewer(viewer,subcomm,&subviewer);

 51:   PetscViewerASCIIPrintf(subviewer,"  Print called on sub viewers %d\n",PetscGlobalRank);

 53:   PetscSubcommCreate(subcomm,&psubsubcomm);
 54:   PetscSubcommSetNumber(psubsubcomm,2);
 55:   PetscSubcommSetType(psubsubcomm,PETSC_SUBCOMM_CONTIGUOUS);
 56:   /* enable runtime switch of psubcomm type, e.g., '-psubcomm_type interlaced */
 57:   PetscSubcommSetFromOptions(psubsubcomm);
 58:   subsubcomm = PetscSubcommChild(psubsubcomm);

 60:   PetscViewerGetSubViewer(subviewer,subsubcomm,&subsubviewer);

 62:   PetscViewerASCIIPrintf(subsubviewer,"  Print called on sub sub viewers %d\n",PetscGlobalRank);

 64:   PetscViewerRestoreSubViewer(subviewer,subsubcomm,&subsubviewer);
 65:   PetscViewerRestoreSubViewer(viewer,subcomm,&subviewer);

 67:   PetscSubcommDestroy(&psubsubcomm);
 68:   PetscSubcommDestroy(&psubcomm);
 69:   PetscViewerDestroy(&viewer);
 70:   PetscFinalize();
 71:   return 0;
 72: }