Actual source code: ex4f90.F90

  1: !
  2: !     This introductory example illustrates running PETSc on a subset
  3: !     of processes
  4: !
  5: ! -----------------------------------------------------------------------
  6: #include <petsc/finclude/petscsys.h>
  7: program main
  8:   use petscmpi  ! or mpi or mpi_f08
  9:   use petscsys
 10:   implicit none

 12:   PetscErrorCode ierr
 13:   PetscMPIInt rank, size, zero, two

 15: !     We must call MPI_Init() first, making us, not PETSc, responsible
 16: !     for MPI

 18:   PetscCallMPIA(MPI_Init(ierr))

 20: !     We can now change the communicator universe for PETSc

 22:   zero = 0
 23:   two = 2
 24:   PetscCallMPIA(MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr))
 25:   PetscCallMPIA(MPI_Comm_split(MPI_COMM_WORLD, mod(rank, two), zero, PETSC_COMM_WORLD, ierr))

 27: !     Every PETSc routine should begin with the PetscInitialize()
 28: !     routine.

 30:   PetscCallA(PetscInitialize(ierr))

 32: !     The following MPI calls return the number of processes being used
 33: !     and the rank of this process in the group.

 35:   PetscCallMPIA(MPI_Comm_size(PETSC_COMM_WORLD, size, ierr))
 36:   PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD, rank, ierr))

 38: !     Here we would like to print only one message that represents all
 39: !     the processes in the group.
 40:   if (rank == 0) write (6, 100) size, rank
 41: 100 format('No of Procs = ', i4, ' rank = ', i4)

 43: !     Always call PetscFinalize() before exiting a program.  This
 44: !     routine - finalizes the PETSc libraries as well as MPI - provides
 45: !     summary and diagnostic information if certain runtime options are
 46: !     chosen (e.g., -log_view).  See PetscFinalize() manpage for more
 47: !     information.

 49:   PetscCallA(PetscFinalize(ierr))
 50:   PetscCallMPIA(MPI_Comm_free(PETSC_COMM_WORLD, ierr))

 52: !     Since we initialized MPI, we must call MPI_Finalize()

 54:   PetscCallMPIA(MPI_Finalize(ierr))
 55: end

 57: !
 58: !/*TEST
 59: !
 60: !   test:
 61: !
 62: !TEST*/