xref: /petsc/src/sys/tutorials/ex2f.F90 (revision 74df5e01f481fb3fe90b32c3b4345ef0122eb3ce)
1! Synchronized printing: Fortran Example
2
3program main
4#include <petsc/finclude/petscsys.h>
5  use petscmpi  ! or mpi or mpi_f08
6  use petscsys
7
8  implicit none
9  PetscErrorCode                    :: ierr
10  PetscMPIInt                       :: rank, size
11  character(len=PETSC_MAX_PATH_LEN) :: outputString
12
13  ! Every PETSc program should begin with the PetscInitialize() routine.
14
15  PetscCallA(PetscInitialize(ierr))
16
17  ! The following MPI calls return the number of processes
18  ! being used and the rank of this process in the group
19
20  PetscCallMPIA(MPI_Comm_size(MPI_COMM_WORLD, size, ierr))
21  PetscCallMPIA(MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr))
22
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))
27
28  write (outputString, *) rank, 'Synchronized Hello World\n'
29  PetscCallA(PetscSynchronizedPrintf(PETSC_COMM_WORLD, outputString, ierr))
30
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))
34
35  ! Here a barrier is used to separate the two program states.
36  PetscCallMPIA(MPI_Barrier(PETSC_COMM_WORLD, ierr))
37
38  write (outputString, *) rank, 'Jumbled Hello World\n'
39  PetscCallA(PetscPrintf(PETSC_COMM_SELF, outputString, ierr))
40
41  PetscCallA(PetscFinalize(ierr))
42end program main
43
44!/*TEST
45!
46!   test:
47!
48!TEST*/
49