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