Date: 2026-03-01T05:25:14+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                       :: rank, size
     11:   character(len=PETSC_MAX_PATH_LEN) :: outputString

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

     15:   PetscCallA(PetscInitialize(ierr))

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

     20:   PetscCallMPIA(MPI_Comm_size(MPI_COMM_WORLD, size, ierr))
     21:   PetscCallMPIA(MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr))

     23:   ! Here we would like to print only one message that represents
     24:   ! all the processes in the group
     25:   write (outputString, *) 'No of Processors = ', size, ', rank = ', rank, '\n'
     26:   PetscCallA(PetscPrintf(PETSC_COMM_WORLD, outputString, ierr))

     28:   write (outputString, *) rank, 'Synchronized Hello World\n'
     29:   PetscCallA(PetscSynchronizedPrintf(PETSC_COMM_WORLD, outputString, ierr))

     31:   write (outputString, *) rank, 'Synchronized Hello World - Part II\n'
     32:   PetscCallA(PetscSynchronizedPrintf(PETSC_COMM_WORLD, outputString, ierr))
     33:   PetscCallA(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT, ierr))

     35:   ! Here a barrier is used to separate the two program states.
     36:   PetscCallMPIA(MPI_Barrier(PETSC_COMM_WORLD, ierr))

     38:   write (outputString, *) rank, 'Jumbled Hello World\n'
     39:   PetscCallA(PetscPrintf(PETSC_COMM_SELF, outputString, ierr))

     41:   PetscCallA(PetscFinalize(ierr))
     42: end program main

     44: !/*TEST
     45: !
     46: !   test:
     47: !
     48: !TEST*/