Actual source code: ex50.c

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

  3: #include <petscsys.h>

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

 14:   /*
 15:     Every PETSc routine should begin with the PetscInitialize() routine.
 16:     argc, argv - These command line arguments are taken to extract the options
 17:                  supplied to PETSc and options supplied to MPI.
 18:     help       - When PETSc executable is invoked with the option -help,
 19:                  it prints the various options that can be applied at
 20:                  runtime.  The user can use the "help" variable place
 21:                  additional help messages in this printout.
 22:   */
 23:   PetscFunctionBeginUser;
 24:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 25:   comm = PETSC_COMM_WORLD;
 26:   PetscCallMPI(MPI_Comm_size(comm, &size));
 27:   PetscCheck(size >= 4, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must run with at least 4 MPI ranks");
 28:   PetscCall(PetscOptionsCreateViewer(comm, NULL, NULL, "-viewer", &viewer, &format, &flg));
 29:   PetscCheck(viewer, PETSC_COMM_WORLD, PETSC_ERR_SUP, "Must use -viewer option");

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

 33:   PetscCall(PetscSubcommCreate(comm, &psubcomm));
 34:   PetscCall(PetscSubcommSetNumber(psubcomm, 2));
 35:   PetscCall(PetscSubcommSetType(psubcomm, PETSC_SUBCOMM_CONTIGUOUS));
 36:   /* enable runtime switch of psubcomm type, e.g., '-psubcomm_type interlaced */
 37:   PetscCall(PetscSubcommSetFromOptions(psubcomm));
 38:   subcomm = PetscSubcommChild(psubcomm);

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

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

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

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

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

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

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

 65: /*TEST

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

 71:    test:
 72:       suffix: 2
 73:       nsize: 4
 74:       args: -viewer -psubcomm_type interlaced

 76: TEST*/