xref: /petsc/src/sys/tutorials/ex4f90.F90 (revision a6f8f0eb0f15cce44058a6ad44e802f69a95fee6)
1!
2!     This introductory example illustrates running PETSc on a subset
3!     of processes
4!
5! -----------------------------------------------------------------------
6
7      program main
8#include <petsc/finclude/petscsys.h>
9      use petscmpi  ! or mpi or mpi_f08
10      use petscsys
11      implicit none
12
13      PetscErrorCode ierr
14      PetscMPIInt rank, size, zero, two
15
16!     We must call MPI_Init() first, making us, not PETSc, responsible
17!     for MPI
18
19      call MPI_Init(ierr)
20      if (ierr .ne. 0) then
21         print*,'Unable to initialize MPI'
22         stop
23      endif
24
25!     We can now change the communicator universe for PETSc
26
27      zero = 0
28      two = 2
29      call MPI_Comm_rank(MPI_COMM_WORLD,rank,ierr)
30      call MPI_Comm_split(MPI_COMM_WORLD,mod(rank,two),zero,PETSC_COMM_WORLD,ierr)
31
32!     Every PETSc routine should begin with the PetscInitialize()
33!     routine.
34
35      call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
36      if (ierr .ne. 0) then
37        print*,'Unable to initialize PETSc'
38        stop
39      endif
40
41!     The following MPI calls return the number of processes being used
42!     and the rank of this process in the group.
43
44      call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr);CHKERRA(ierr)
45      call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr);CHKERRA(ierr)
46
47!     Here we would like to print only one message that represents all
48!     the processes in the group.
49      if (rank .eq. 0) write(6,100) size,rank
50 100  format('No of Procs = ',i4,' rank = ',i4)
51
52!     Always call PetscFinalize() before exiting a program.  This
53!     routine - finalizes the PETSc libraries as well as MPI - provides
54!     summary and diagnostic information if certain runtime options are
55!     chosen (e.g., -log_view).  See PetscFinalize() manpage for more
56!     information.
57
58      call PetscFinalize(ierr)
59      call MPI_Comm_free(PETSC_COMM_WORLD,ierr)
60
61!     Since we initialized MPI, we must call MPI_Finalize()
62
63      call  MPI_Finalize(ierr)
64      end
65
66!
67!/*TEST
68!
69!   test:
70!
71!TEST*/
72