comm_handling_m.f90 Source File


Source Code

module comm_handling_m
    !! Contains information about MPI processes and functionality to change
    !! the communicator.
    use MPI

    implicit none
    private

    integer :: communicator = MPI_COMM_WORLD
    !! Communicator used in the library
    integer :: master = 0
    !! Rank of the master process

    public :: get_communicator
    public :: setup_comm
    public :: is_master

contains

    integer function get_communicator()
        !! Returns the MPI communicator
        get_communicator = communicator
    end function

    subroutine setup_comm(comm, rank_master)
        !! Sets the communication to the given communicator. Allows to
        !! change the rank of the master proc.
        integer, intent(in) :: comm
        !! Communicator
        integer, optional, intent(in) :: rank_master
        !! Rank of the master process

        communicator = comm
        if(present(rank_master)) master = rank_master

    end subroutine

    logical function is_master()
        !! Returns if the current proc is the MPI master or not
        integer :: rank, ierr
        logical, save :: res = .false.
        logical, save :: visited = .false.

        if(.not. visited) then
            call MPI_COMM_RANK(communicator, rank, ierr)
            if (rank == master) then
                res = .true.
            else
                res = .false.
            endif
            visited = .true.
        end if
        is_master = res
    end function

end module