1
! Copyright (C) 2009 Imperial College London and others.
3
! Please see the AUTHORS file in the main source directory for a full list
4
! of copyright holders.
7
! Applied Modelling and Computation Group
8
! Department of Earth Science and Engineering
9
! Imperial College London
11
! g.gorman@imperial.ac.uk
13
! This library is free software; you can redistribute it and/or
14
! modify it under the terms of the GNU Lesser General Public
15
! License as published by the Free Software Foundation; either
16
! version 2.1 of the License.
18
! This library is distributed in the hope that it will be useful,
19
! but WITHOUT ANY WARRANTY; without even the implied warranty of
20
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
! Lesser General Public License for more details.
23
! You should have received a copy of the GNU Lesser General Public
24
! License along with this library; if not, write to the Free Software
25
! Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35
public::initialise, finalize, should_exit
39
logical::initialised=.false.
40
integer, dimension(:), allocatable::load, rrequest
41
integer::myrank, nprocs
42
real::imbalance_tol=0.5
46
subroutine initialise(count, tol)
47
integer, intent(in)::count
50
integer have_mpi_init, i, ierr
51
if(.not.initialised) then
52
call MPI_Initialized(have_mpi_init, ierr)
53
if(have_mpi_init.eq.0) then
56
call MPI_Comm_size(MPI_COMM_WORLD, nprocs, ierr)
60
call MPI_Comm_rank(MPI_COMM_WORLD, myrank, ierr)
61
allocate(load(0:nprocs-1))
62
call MPI_Allgather(count, 1, MPI_INTEGER, load, 1, &
63
MPI_INTEGER, MPI_COMM_WORLD, ierr)
65
allocate(rrequest(0:nprocs-1))
69
call MPI_Irecv(load(i), 1, MPI_INTEGER, i, 1, &
70
MPI_COMM_WORLD, rrequest(i), ierr)
72
rrequest(i) = MPI_REQUEST_NULL
82
end subroutine initialise
84
subroutine finalize(count)
85
integer, intent(in)::count
88
integer, allocatable, dimension(:)::request
89
integer, allocatable, dimension(:, :)::status
92
allocate(request(0:nprocs-1))
93
allocate(status(MPI_STATUS_SIZE, 0:nprocs-1))
97
call MPI_Isend(count, 1, MPI_INTEGER, i, 1, &
98
MPI_COMM_WORLD, request(i), ierr)
100
request(i) = MPI_REQUEST_NULL
104
call MPI_Waitall(nprocs, rrequest, status, ierr)
105
call MPI_Waitall(nprocs, request, status, ierr)
107
deallocate(request, rrequest, status, load)
109
initialised = .false.
111
end subroutine finalize
113
logical function should_exit(count)
114
integer, intent(in)::count
122
imbalance = 1.0 - real(sum(load))/(nprocs*maxval(load))
124
should_exit = (imbalance>imbalance_tol)
126
should_exit = .false.
129
should_exit = .false.
131
end function should_exit
133
end module AdaptProgress