Actual source code: ex4.c

petsc-3.3-p7 2013-05-11
  1: /* Program usage:  mpiexec -n 2 ex1 [-help] [all PETSc options] */

  3: static char help[] = "Introductory example that illustrates running PETSc on a subset of processes.\n\n";

  5: /*T
  6:    Concepts: introduction to PETSc;
  7:    Concepts: process^subset set PETSC_COMM_WORLD
  8:    Processors: 2
  9: T*/
 10:  #include <petscsys.h>

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

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

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

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

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

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

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

 59:   /* Since we initialized MPI, we must call MPI_Finalize() */
 60:   MPI_Finalize();
 61:   return 0;
 62: }