Actual source code: ex2f.F90

petsc-main 2021-04-20
Report Typos and Errors
  1: ! Synchronized printing: Fortran Example


  4: program main
  5: #include <petsc/finclude/petscsys.h>
  6:       use petscmpi  ! or mpi or mpi_f08
  7:       use petscsys

  9:       implicit none
 10:       PetscErrorCode                    :: ierr
 11:       PetscMPIInt                       :: myRank,mySize
 12:       character(len=PETSC_MAX_PATH_LEN) :: outputString

 14:       ! Every PETSc program should begin with the PetscInitialize() routine.

 16:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 17:       if (ierr /= 0) then
 18:         write(6,*) 'Unable to initialize PETSc'
 19:         stop
 20:       endif

 22:       ! The following MPI calls return the number of processes
 23:       ! being used and the rank of this process in the group

 25:       call MPI_Comm_size(MPI_COMM_WORLD,mySize,ierr)
 26:       CHKERRA(ierr)
 27:       call MPI_Comm_rank(MPI_COMM_WORLD,myRank,ierr)
 28:       CHKERRA(ierr)

 30:       ! Here we would like to print only one message that represents
 31:       ! all the processes in the group
 32:       write(outputString,*) 'No of Processors = ', mysize, ', rank = ',myRank,'\n'
 33:       call PetscPrintf(PETSC_COMM_WORLD,outputString,ierr)
 34:       CHKERRA(ierr)

 36:       write(outputString,*) myRank,'Synchronized Hello World\n'
 37:       call PetscSynchronizedPrintf(PETSC_COMM_WORLD,outputString,ierr)
 38:       CHKERRA(ierr)
 39:       write(outputString,*) myRank,'Synchronized Hello World - Part II\n'
 40:       call PetscSynchronizedPrintf(PETSC_COMM_WORLD,outputString,ierr)
 41:       CHKERRA(ierr)
 42:       call PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT,ierr)
 43:       CHKERRA(ierr)

 45:       ! Here a barrier is used to separate the two program states.
 46:       call MPI_Barrier(PETSC_COMM_WORLD,ierr)
 47:       CHKERRA(ierr)

 49:       write(outputString,*) myRank,'Jumbled Hello World\n'
 50:       call PetscPrintf(PETSC_COMM_SELF,outputString,ierr)
 51:       CHKERRA(ierr)

 53:       call PetscFinalize(ierr)
 54:       CHKERRA(ierr)

 56: end program main

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