xref: /petsc/src/sys/tutorials/ex1f.F90 (revision f97672e55eacc8688507b9471cd7ec2664d7f203)
1! Introductory example that illustrates printing: Fortran Example
2
3program main
4#include <petsc/finclude/petscsys.h>
5      use petscsys
6
7      implicit none
8      PetscErrorCode    :: ierr
9      PetscMPIInt       :: myRank,mySize
10      character(len=80) :: outputString
11
12      ! Every PETSc routine should begin with the PetscInitialize() routine.
13
14      call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
15      if (ierr/= 0) then
16        write(6,*) 'Unable to initialize PETSc'
17        stop
18      endif
19
20      ! We can now change the communicator universe for PETSc
21
22      call MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr); CHKERRA(ierr)
23      call MPI_Comm_rank(PETSC_COMM_WORLD,myRank,ierr); CHKERRA(ierr)
24
25      ! Here we would like to print only one message that represents all the processes in the group
26      ! We use PetscPrintf() with the
27      ! communicator PETSC_COMM_WORLD.  Thus, only one message is
28      ! printed representng PETSC_COMM_WORLD, i.e., all the processors.
29
30      write(outputString,*) 'No of Processors = ', mySize, ', rank = ',myRank,'\n'
31      call PetscPrintf(PETSC_COMM_WORLD,outputString,ierr); CHKERRA(ierr)
32
33      ! Here a barrier is used to separate the two program states.
34
35      call MPI_Barrier(PETSC_COMM_WORLD,ierr); CHKERRA(ierr)
36
37      ! Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF,
38      ! where each process is considered separately and prints independently
39      ! to the screen.  Thus, the output from different processes does not
40      ! appear in any particular order.
41
42      write(outputString,*) myRank,'Jumbled Hello World\n'
43      call PetscPrintf(PETSC_COMM_SELF,outputString,ierr); CHKERRA(ierr)
44
45      ! Always call PetscFinalize() before exiting a program.  This routine
46      ! - finalizes the PETSc libraries as well as MPI
47      ! - provides summary and diagnostic information if certain runtime
48      !   options are chosen (e.g., -log_view).  See PetscFinalize()
49      !  manpage for more information.
50
51      call PetscFinalize(ierr)
52
53end program main
54!/*TEST
55!
56!   test:
57!
58!TEST*/
59