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