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