xref: /petsc/src/sys/tutorials/ex2f.F90 (revision f84028053d197aff3fdf060e86d88b3f1b0f0110)
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(PETSC_NULL_CHARACTER,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
36      ! Here a barrier is used to separate the two program states.
37      PetscCallMPIA(MPI_Barrier(PETSC_COMM_WORLD,ierr))
38
39      write(outputString,*) rank,'Jumbled Hello World\n'
40      PetscCallA(PetscPrintf(PETSC_COMM_SELF,outputString,ierr))
41
42      PetscCallA(PetscFinalize(ierr))
43end program main
44
45!/*TEST
46!
47!   test:
48!
49!TEST*/
50