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