Date: 2022-03-31T04:46:17+00:00

Actual source code: ex4f.F

      1: !
      2: !     This introductory example illustrates running PETSc on a subset
      3: !     of processes
      4: !
      5: !/*T
      6: !   Concepts: introduction to PETSc;
      7: !   Concepts: process^subset set PETSC_COMM_WORLD
      8: !   Processors: 2
      9: !T*/
     10: ! -----------------------------------------------------------------------

     12:       program main
     13: #include <petsc/finclude/petscsys.h>
     14:       use petscmpi  ! or mpi or mpi_f08
     15:       use petscsys
     16:       implicit none
     17:       PetscErrorCode ierr
     18:       PetscMPIInt    rank, size,grank,zero,two
     19:       PetscReal globalrank

     21: !     We must call MPI_Init() first, making us, not PETSc, responsible
     22: !     for MPI

     24:       call MPI_Init(ierr)
     25: #if defined(PETSC_HAVE_ELEMENTAL)
     26:       call PetscElementalInitializePackage(ierr)
     27: #endif
     28: !     We can now change the communicator universe for PETSc

     30:       zero = 0
     31:       two = 2
     32:       call MPI_Comm_rank(MPI_COMM_WORLD,rank,ierr)
     33:       call MPI_Comm_split(MPI_COMM_WORLD,mod(rank,two),zero,            &
     34:      &     PETSC_COMM_WORLD,ierr)

     36: !     Every PETSc routine should begin with the PetscInitialize()
     37: !     routine.
     38:       call PetscInitializeNoArguments(ierr)
     39:       if (ierr .ne. 0) then
     40:          print*,'Unable to initialize PETSc'
     41:          stop
     42:       endif

     44: !     The following MPI calls return the number of processes being used
     45: !     and the rank of this process in the group.

     47:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
     48:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)

     50: !     Here we would like to print only one message that represents all
     51: !     the processes in the group. Sleep so that IO from different ranks
     52: !     don't get mixed up. Note this is not an ideal solution
     53:       call MPI_Comm_rank(MPI_COMM_WORLD,grank,ierr)
     54:       globalrank = grank
     55:       call PetscSleep(globalrank,ierr)
     56:       if (rank .eq. 0) write(6,100) size,rank
     57:  100  format('No of Procs = ',i4,' rank = ',i4)

     59: !     Always call PetscFinalize() before exiting a program.  This
     60: !     routine - finalizes the PETSc libraries as well as MPI - provides
     61: !     summary and diagnostic information if certain runtime options are
     62: !     chosen (e.g., -log_view).  See PetscFinalize() manpage for more
     63: !     information.

     65:       call PetscFinalize(ierr)
     66:       call MPI_Comm_free(PETSC_COMM_WORLD,ierr)
     67: #if defined(PETSC_HAVE_ELEMENTAL)
     68:       call PetscElementalFinalizePackage(ierr)
     69: #endif

     71: !     Since we initialized MPI, we must call MPI_Finalize()

     73:       call  MPI_Finalize(ierr)
     74:       end

     76: !/*TEST
     77: !
     78: !   test:
     79: !      nsize: 5
     80: !      filter: sort -b
     81: !      filter_output: sort -b
     82: !      requires: !cuda !saws
     83: !
     84: !TEST*/