Actual source code: ex50.c

  1: static char help[] = "Tests using PetscViewerGetSubViewer() recursively\n\n";

  3: #include <petscsys.h>
  4: #include <petscviewer.h>

  6: int main(int argc, char **argv)
  7: {
  8:   PetscViewer       viewer, subviewer, subsubviewer;
  9:   PetscViewerFormat format;
 10:   PetscBool         flg;
 11:   PetscSubcomm      psubcomm, psubsubcomm;
 12:   MPI_Comm          comm, subcomm, subsubcomm;
 13:   PetscMPIInt       size;

 15:   /*
 16:     Every PETSc routine should begin with the PetscInitialize() routine.
 17:     argc, argv - These command line arguments are taken to extract the options
 18:                  supplied to PETSc and options supplied to MPI.
 19:     help       - When PETSc executable is invoked with the option -help,
 20:                  it prints the various options that can be applied at
 21:                  runtime.  The user can use the "help" variable place
 22:                  additional help messages in this printout.
 23:   */
 24:   PetscFunctionBeginUser;
 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 ranks");
 29:   PetscCall(PetscOptionsGetViewer(comm, NULL, NULL, "-viewer", &viewer, &format, &flg));
 30:   PetscCheck(viewer, PETSC_COMM_WORLD, PETSC_ERR_SUP, "Must use -viewer option");

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

 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);

 41:   PetscCall(PetscViewerGetSubViewer(viewer, subcomm, &subviewer));

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

 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);

 52:   PetscCall(PetscViewerGetSubViewer(subviewer, subsubcomm, &subsubviewer));

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

 56:   PetscCall(PetscViewerRestoreSubViewer(subviewer, subsubcomm, &subsubviewer));
 57:   PetscCall(PetscViewerRestoreSubViewer(viewer, subcomm, &subviewer));

 59:   PetscCall(PetscSubcommDestroy(&psubsubcomm));
 60:   PetscCall(PetscSubcommDestroy(&psubcomm));
 61:   PetscCall(PetscViewerDestroy(&viewer));
 62:   PetscCall(PetscFinalize());
 63:   return 0;
 64: }

 66: /*TEST

 68:    test:
 69:       nsize: 4
 70:       args: -viewer

 72: TEST*/