Date: 2022-03-31T04:46:17+00:00

Actual source code: ex2f.F90

      1: ! Synchronized printing: Fortran Example

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

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

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

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

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

     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)

     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)

     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)

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

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

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

     55: end program main

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