Actual source code: ex50.c


  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>

 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;

 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:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 31:   comm = PETSC_COMM_WORLD;
 32:   MPI_Comm_size(comm,&size);
 33:   if (size < 4) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Must run with at least 4 MPI processes");
 34:   PetscOptionsGetViewer(comm,NULL,NULL,"-viewer",&viewer,&format,&flg);
 35:   if (!viewer) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Must use -viewer option");

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

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

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

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

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

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

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

 61:   PetscViewerRestoreSubViewer(subviewer,subsubcomm,&subsubviewer);
 62:   PetscViewerRestoreSubViewer(viewer,subcomm,&subviewer);

 64:   PetscSubcommDestroy(&psubsubcomm);
 65:   PetscSubcommDestroy(&psubcomm);
 66:   PetscViewerDestroy(&viewer);
 67:   PetscFinalize();
 68:   return ierr;
 69: }

 71: /*TEST

 73:    test:
 74:       nsize: 4
 75:       args: -viewer

 77: TEST*/