Actual source code: ex4.c

petsc-3.12.5 2020-03-29
Report Typos and Errors
  1: static char help[] = "Introductory example that illustrates running PETSc on a subset of processes.\n\n";

  3: /*T
  4:    Concepts: introduction to PETSc;
  5:    Concepts: process^subset set PETSC_COMM_WORLD
  6:    Processors: 2

  8:    Note that this example is not checking the error codes from the MPI calls with CHKERRQ() before PETSc is initialized
  9:    and after PETSc is finalized. This is because the PETSc macro CHKERRQ() will not work in those circumstances. 
 10: T*/
 11:  #include <petscsys.h>

 13: int main(int argc, char *argv[])
 14: {
 16:   PetscMPIInt    rank, size;

 18:   /* We must call MPI_Init() first, making us, not PETSc, responsible for MPI */
 19:   MPI_Init(&argc, &argv);if (ierr) return ierr;

 21:   /* We can now change the communicator universe for PETSc */
 22:   MPI_Comm_rank(MPI_COMM_WORLD, &rank);if (ierr) return ierr;
 23:   MPI_Comm_split(MPI_COMM_WORLD, rank%2, 0, &PETSC_COMM_WORLD);if (ierr) return ierr;

 25:   /*
 26:     Every PETSc routine should begin with the PetscInitialize() routine.
 27:     argc, argv - These command line arguments are taken to extract the options
 28:                  supplied to PETSc and options supplied to MPI.
 29:     help       - When PETSc executable is invoked with the option -help,
 30:                  it prints the various options that can be applied at
 31:                  runtime.  The user can use the "help" variable place
 32:                  additional help messages in this printout.
 33:   */
 34:   PetscInitialize(&argc, &argv, (char*) 0, help);if (ierr) return ierr;

 36:   /*
 37:      The following MPI calls return the number of processes
 38:      being used and the rank of this process in the group.
 39:    */
 40:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 41:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 43:   /*
 44:      Here we would like to print only one message that represents
 45:      all the processes in the group.  We use PetscPrintf() with the
 46:      communicator PETSC_COMM_WORLD.  Thus, only one message is
 47:      printed representng PETSC_COMM_WORLD, i.e., all the processors.
 48:   */
 49:   PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n", size, rank);

 51:   /*
 52:      Always call PetscFinalize() before exiting a program.  This routine
 53:        - finalizes the PETSc libraries as well as MPI
 54:        - provides summary and diagnostic information if certain runtime
 55:          options are chosen (e.g., -log_view).  See PetscFinalize()
 56:      manpage for more information.
 57:   */
 58:   PetscFinalize();if (ierr) return ierr;

 60:   MPI_Comm_free(&PETSC_COMM_WORLD);if (ierr) return ierr;
 61:   /* Since we initialized MPI, we must call MPI_Finalize() */
 62:   MPI_Finalize();
 63:   return ierr;
 64: }


 67: /*TEST

 69:    test:
 70:       nsize: 5
 71:       args: -options_left no
 72:       filter: sort -b | grep -v saws_port_auto_selectcd
 73:       filter_output: sort -b

 75: TEST*/