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