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