Actual source code: ex4f90.F90

petsc-3.12.5 2020-03-29
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

 17:       integer ierr
 18:       integer rank, size

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

 23:       call MPI_Init(ierr)
 24:       if (ierr .ne. 0) then
 25:          print*,'Unable to initialize MPI'
 26:          stop
 27:       endif

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

 31:       call MPI_Comm_rank(MPI_COMM_WORLD,rank,ierr)
 32:       call MPI_Comm_split(MPI_COMM_WORLD,mod(rank,2),0,PETSC_COMM_WORLD,ierr)

 34: !     Every PETSc routine should begin with the PetscInitialize()
 35: !     routine.

 37:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 38:       if (ierr .ne. 0) then
 39:         print*,'Unable to initialize PETSc'
 40:         stop
 41:       endif

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

 46:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr);CHKERRA(ierr)
 47:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr);CHKERRA(ierr)


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

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

 61:       call PetscFinalize(ierr)
 62:       call MPI_Comm_free(PETSC_COMM_WORLD,ierr)

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

 66:       call  MPI_Finalize(ierr)
 67:       end

 69: !
 70: !/*TEST
 71: !
 72: !   test:
 73: !
 74: !TEST*/