test_comm_handler_m.pf Source File


Contents


Source Code

module test_comm_handler_m
    use pfunit
    use MPI
    implicit none
contains

    @test(npes = [4])
    subroutine test_comm_handler(this)
        !! test of communication handler
        use comm_handler_m, only : comm_handler_t
        implicit none

        class (MpiTestMethod), intent(inout) :: this
        integer :: comm_world
        type(comm_handler_t), allocatable :: comm_handler
        
        integer :: rank, ierr, iret
        integer :: nplanes, nspecies, ncart, species, plane, ival, isum, ref

        comm_world = this%getMpiCommunicator()
        call MPI_comm_rank(comm_world, rank, ierr)
                
        if (rank == 0) then     
            write(*,*)''
            write(*,*)''
            write(*,'(A80)')'test_comm_handler ---------------------------------------------------------'
        endif
 
        ! Test comm_handler only accross planes
        allocate(comm_handler)
        nplanes = 4
        nspecies = 1
        ncart = nplanes * nspecies
        call comm_handler%init(comm_world, nplanes, nspecies)

        iret = comm_handler%get_ncart()
        @assertEqual(iret, ncart)
        iret = comm_handler%get_nplanes()
        @assertEqual(iret, nplanes)
        iret = comm_handler%get_nspecies()
        @assertEqual(iret, nspecies)
        @assertEqual(comm_handler%get_rank(), rank)
        species = comm_handler%get_species()
        @assertEqual(species, 0)
        plane = comm_handler%get_plane()
        @assertEqual(plane, rank)
        
        ival = hash(plane, species)
        
        call MPI_allreduce(ival, isum, 1, MPI_INTEGER, MPI_SUM, &
                           comm_handler%get_comm_cart(), ierr)
        ref = 92
        @assertEqual(isum, ref)
        
        call MPI_allreduce(ival, isum, 1, MPI_INTEGER, MPI_SUM, &
                           comm_handler%get_comm_planes(), ierr)
        ref = 92
        @assertEqual(isum, ref)
        
        call MPI_allreduce(ival, isum, 1, MPI_INTEGER, MPI_SUM, &
                           comm_handler%get_comm_species(), ierr)
        ref = hash(plane, 0)
        @assertEqual(isum, ref)        
        deallocate(comm_handler)
        
        ! Test comm_handler only accross species
        allocate(comm_handler)
        nplanes = 1
        nspecies = 4
        ncart = nplanes * nspecies
        call comm_handler%init(comm_world, nplanes, nspecies)

        iret = comm_handler%get_ncart()
        @assertEqual(iret, ncart)
        iret = comm_handler%get_nplanes()
        @assertEqual(iret, nplanes)
        iret = comm_handler%get_nspecies()
        @assertEqual(iret, nspecies)
        @assertEqual(comm_handler%get_rank(), rank)
        species = comm_handler%get_species()
        @assertEqual(species, rank)
        plane = comm_handler%get_plane()
        @assertEqual(plane, 0)
        
        ival = hash(plane, species)
        
        call MPI_allreduce(ival, isum, 1, MPI_INTEGER, MPI_SUM, &
                           comm_handler%get_comm_cart(), ierr)
        ref = 10
        @assertEqual(isum, ref)
        
        call MPI_allreduce(ival, isum, 1, MPI_INTEGER, MPI_SUM, &
                           comm_handler%get_comm_planes(), ierr)
        ref = hash(0, species)
        @assertEqual(isum, ref)
        
        call MPI_allreduce(ival, isum, 1, MPI_INTEGER, MPI_SUM, &
                           comm_handler%get_comm_species(), ierr)
        ref = 10
        @assertEqual(isum, ref)        
        deallocate(comm_handler)
        
        ! Test comm_handler accross planes and species
        allocate(comm_handler)
        nplanes = 2
        nspecies = 2
        ncart = nplanes * nspecies
        call comm_handler%init(comm_world, nplanes, nspecies)

        iret = comm_handler%get_ncart()
        @assertEqual(iret, ncart)
        iret = comm_handler%get_nplanes()
        @assertEqual(iret, nplanes)
        iret = comm_handler%get_nspecies()
        @assertEqual(iret, nspecies)
        @assertEqual(comm_handler%get_rank(), rank)
        species = comm_handler%get_species()
        plane = comm_handler%get_plane()
        
        ival = hash(plane, species)
        
        call MPI_allreduce(ival, isum, 1, MPI_INTEGER, MPI_SUM, &
                           comm_handler%get_comm_cart(), ierr)
        ref = 34
        @assertEqual(isum, ref)
        
        call MPI_allreduce(ival, isum, 1, MPI_INTEGER, MPI_SUM, &
                           comm_handler%get_comm_planes(), ierr)
        if (species == 0) then
            ref = 18
        elseif (species == 1) then
            ref = 16
        else
            @assertTrue(.false.)
        endif
        @assertEqual(isum, ref)
        
        call MPI_allreduce(ival, isum, 1, MPI_INTEGER, MPI_SUM, &
                           comm_handler%get_comm_species(), ierr)
        if (plane == 0) then
            ref = 7
        elseif (plane == 1) then
            ref = 27
        else
            @assertTrue(.false.)
        endif
        @assertEqual(isum, ref)        
        deallocate(comm_handler)
                       
        if (rank == 0) then
            write(*,'(A80)')'test_comm_handler complete ------------------------------------------------'
            write(*,*)''
            write(*,*)''
        endif

    contains
        
        integer function hash(plane, species)
            !! Creates some hash values with plane and species
            integer, intent(in) :: plane
            !! Plane
            integer, intent(in) :: species
            !! Species
            hash = 2 * (plane + 3) * (plane+1) - (species + 2)
        end 

    end subroutine

end module