Actual source code: ex2f.F90
petsc-3.12.4 2020-02-04
Report Typos and Errors

      1: ! Synchronized printing: Fortran Example
      2:
      3:
      4: program main
      5: #include <petsc/finclude/petscsys.h>
      6:       use petscsys
      7:
      8:       implicit none
      9:       PetscErrorCode    :: ierr
     10:       PetscMPIInt       :: myRank,mySize
     11:       character(len=80) :: outputString
     12:
     13:       ! Every PETSc program should begin with the PetscInitialize() routine.
     14:
     15:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     16:       if (ierr /= 0) then
     17:         write(6,*) 'Unable to initialize PETSc'
     18:         stop
     19:       endif
     20:
     21:       ! The following MPI calls return the number of processes
     22:       ! being used and the rank of this process in the group
     23:
     24:       call MPI_Comm_size(MPI_COMM_WORLD,mySize,ierr)
     25:       CHKERRA(ierr)
     26:       call MPI_Comm_rank(MPI_COMM_WORLD,myRank,ierr)
     27:       CHKERRA(ierr)
     28:
     29:       ! Here we would like to print only one message that represents
     30:       ! all the processes in the group
     31:       write(outputString,*) 'No of Processors = ', mysize, ', rank = ',myRank,'\n'
     32:       call PetscPrintf(PETSC_COMM_WORLD,outputString,ierr)
     33:       CHKERRA(ierr)
     34:
     35:       write(outputString,*) myRank,'Synchronized Hello World\n'
     36:       call PetscSynchronizedPrintf(PETSC_COMM_WORLD,outputString,ierr)
     37:       CHKERRA(ierr)
     38:       write(outputString,*) myRank,'Synchronized Hello World - Part II\n'
     39:       call PetscSynchronizedPrintf(PETSC_COMM_WORLD,outputString,ierr)
     40:       CHKERRA(ierr)
     41:       call PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT,ierr)
     42:       CHKERRA(ierr)
     43:
     44:       ! Here a barrier is used to separate the two program states.
     45:       call MPI_Barrier(PETSC_COMM_WORLD,ierr)
     46:       CHKERRA(ierr)
     47:
     48:       write(outputString,*) myRank,'Jumbled Hello World\n'
     49:       call PetscPrintf(PETSC_COMM_SELF,outputString,ierr)
     50:       CHKERRA(ierr)
     51:
     52:       call PetscFinalize(ierr)
     53:       CHKERRA(ierr)
     54:
     55: end program main
     56:
     57: !/*TEST
     58: !
     59: !   test:
     60: !
     61: !TEST*/