Actual source code: ex4f90.F90

petsc-3.7.7 2017-09-25
Report Typos and Errors
  1: !
  2: !     This introductory example illustrates running PETSc on a subset
  3: !     of processes
  4: !
  5: !/*T
  6: !   Concepts: introduction to PETSc;
  7: !   Concepts: process^subset set PETSC_COMM_WORLD
  8: !   Processors: 2
  9: !T*/
 10: ! -----------------------------------------------------------------------

 12:       program main
 13:       use petscsys
 14:       implicit none

 16:       integer ierr
 17:       integer rank, size

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

 22:       call MPI_Init(ierr)

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

 26:       call MPI_Comm_rank(MPI_COMM_WORLD,rank,ierr)
 27:       call MPI_Comm_split(MPI_COMM_WORLD,mod(rank,2),0,                 &
 28:      &     PETSC_COMM_WORLD,ierr)

 30: !     Every PETSc routine should begin with the PetscInitialize()
 31: !     routine.

 33:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)

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

 38:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
 39:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)


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

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

 53:       call PetscFinalize(ierr)
 54:       call MPI_Comm_free(PETSC_COMM_WORLD,ierr)

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

 58:       call  MPI_Finalize(ierr)
 59:       end