Actual source code: ex4f.F

petsc-master 2018-06-18
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:  #include <petsc/finclude/petscsys.h>
 14:       use petscsys
 15:       implicit none
 16:       PetscErrorCode ierr
 17:       PetscMPIInt    rank, size,grank
 18:       PetscReal globalrank

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

 23:       call MPI_Init(ierr)

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

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

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

 34:       call PetscInitializeNoArguments(ierr)
 35:       if (ierr .ne. 0) then
 36:          print*,'Unable to initialize PETSc'
 37:          stop
 38:       endif

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

 43:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
 44:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)


 47: !     Here we would like to print only one message that represents all
 48: !     the processes in the group. Sleep so that IO from different ranks
 49: !     don't get mixed up. Note this is not an ideal solution
 50:       call MPI_Comm_rank(MPI_COMM_WORLD,grank,ierr)
 51:       globalrank = grank
 52:       call PetscSleep(globalrank,ierr)
 53:       if (rank .eq. 0) write(6,100) size,rank
 54:  100  format('No of Procs = ',i4,' rank = ',i4)

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

 62:       call PetscFinalize(ierr)

 64:       call MPI_Comm_free(PETSC_COMM_WORLD,ierr)


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

 69:       call  MPI_Finalize(ierr)
 70:       end

 72: !/*TEST
 73: !
 74: !   test:
 75: !      nsize: 5
 76: !      filter: sort -b
 77: !      filter_output: sort -b
 78: !
 79: !TEST*/