57 use,
intrinsic :: iso_fortran_env
62#if defined(HAVE_OPENMP)
77 multicomm_create_all_pairs, &
88 integer,
public,
parameter :: &
92 integer,
public,
parameter :: &
96 integer,
parameter :: n_par_types = 4
97 character(len=11),
parameter :: par_types(0:n_par_types) = &
106 integer,
parameter :: MAX_INDEX = 5
117 integer,
public :: par_strategy
119 integer,
allocatable :: group_sizes(:)
120 integer,
allocatable,
public :: who_am_i(:)
121 type(MPI_Comm),
allocatable,
public :: group_comm(:)
125 type(mpi_grp_t),
public :: intranode_grp
126 type(mpi_grp_t),
public :: internode_grp
128 type(mpi_grp_t),
public :: master_grp
129 type(mpi_grp_t),
public :: base_grp
133 logical :: have_slaves
136 integer :: full_comm_rank
138 integer :: master_comm_rank
141 logical :: reorder_ranks
147 type(mpi_grp_t) :: grp
149 integer,
allocatable,
public :: schedule(:, :)
156 type(multicomm_all_pairs_t),
intent(inout) :: apout
157 type(multicomm_all_pairs_t),
intent(in) :: apin
162 apout%rounds = apin%rounds
163 if (
allocated(apin%schedule))
then
164 safe_allocate(apout%schedule(1:
size(apin%schedule, 1), 1:
size(apin%schedule, 2)))
165 apout%schedule = apin%schedule
167 if (
allocated(apout%schedule))
then
168 safe_deallocate_a(apout%schedule)
177 subroutine multicomm_init(mc, namespace, base_grp, mode_para, n_node, index_range, min_range)
178 type(multicomm_t),
intent(out) :: mc
179 type(namespace_t),
intent(in) :: namespace
180 type(mpi_grp_t),
intent(in) :: base_grp
181 type(calc_mode_par_t),
intent(in) :: mode_para
182 integer,
intent(in) :: n_node
183 integer(int64),
intent(inout) :: index_range(:)
184 integer,
intent(in) :: min_range(:)
186 integer :: ii, num_slaves, slave_level, ipar
188 integer :: parallel_mask, default_mask
192 mc%base_grp = base_grp
195 parallel_mask = mode_para%parallel_mask()
196 default_mask = mode_para%default_parallel_mask()
217 if (
bitand(default_mask, ibset(0, ipar - 1)) /= 0)
then
218 default(ipar) = par_auto
319 mc%have_slaves = .false.
328 mc%group_sizes(ipar) =
parse(ipar)
329 else if (
parse(ipar) /= 1)
then
330 call messages_write(
'Ignoring specification for ' // par_types(ipar))
332 call messages_write(
'This parallelization strategy is not available.')
349 call parse_variable(namespace,
'ParallelizationNumberSlaves', 0, num_slaves)
353 mc%have_slaves = (num_slaves > 0)
355 if (mc%have_slaves)
then
361 if (mc%group_sizes(ii) == 1) mc%par_strategy = ibclr(mc%par_strategy, ii - 1)
382 if (base_grp%size > 1)
then
387 if (
parse(ipar) == par_auto .or.
parse(ipar) > 1)
then
388 mc%par_strategy = ibset(mc%par_strategy, ipar - 1)
392 if (mc%par_strategy /=
bitand(mc%par_strategy, parallel_mask))
then
393 call messages_write(
'Parallelization strategies unavailable for this run mode are being discarded.')
397 mc%par_strategy =
bitand(mc%par_strategy, parallel_mask)
400 message(1) =
"More than one node is available, but this run mode cannot run with the requested parallelization."
401 message(2) =
"Please select a parallelization strategy compatible with"
403 do ii = 1, n_par_types
404 if (
bitand(parallel_mask, 2**(ii - 1)) /= 0)
then
406 write(
message(jj),
'(2a)')
" -> ", par_types(ii)
410 write(
message(jj),
'(a,i6)')
"mc%par_strategy is : ",mc%par_strategy
418#if defined(HAVE_OPENMP)
421 mc%nthreads = omp_get_num_threads()
427 message(1) =
"Info: Octopus will run in *serial*"
430 write(
message(1),
'(a)')
'Info: Octopus will run in *parallel*'
432 write(
message(3),
'(a, i8)')
' Number of processes :', base_grp%size
433 write(
message(4),
'(a, i8)')
' Number of threads per process :', mc%nthreads
444 integer :: ii, nn, kk, n_divisors, divisors(1:50)
467 if (mc%group_sizes(ipar) == par_auto) cycle
469 if (mc%group_sizes(ipar) > n_group_max(ipar))
then
470 call messages_write(
'The number of processors specified for '//par_types(ipar)//
'(')
473 call messages_write(
'is larger than the degrees of freedom for that level (')
479 if (mod(nn, mc%group_sizes(ipar)) /= 0)
then
480 call messages_write(
'The number of processors specified for '//par_types(ipar)//
'(')
483 call messages_write(
'is not a divisor of the number of processors (')
489 nn = nn/mc%group_sizes(ipar)
496 if (mc%group_sizes(ipar) /= par_auto) cycle
498 n_divisors = ubound(divisors, dim = 1)
501 mc%group_sizes(ipar) = nn
502 do ii = 2, n_divisors
503 if (divisors(ii) > n_group_max(ipar))
then
504 mc%group_sizes(ipar) = divisors(ii - 1)
509 nn = nn/mc%group_sizes(ipar)
522 integer(int64) :: jj, n_max
523 integer :: real_group_sizes(1:MAX_INDEX)
527 if (num_slaves > 0)
then
529 if (mc%group_sizes(slave_level) < num_slaves + 1)
then
530 message(1) =
'Too many nodes assigned to task parallelization.'
534 write(
message(1),
'(a,i6)')
'Info: Number of slaves nodes :', &
535 num_slaves*product(mc%group_sizes(1:slave_level - 1))
543 real_group_sizes(kk) = mc%group_sizes(kk)
546 if (kk == slave_level) real_group_sizes(kk) = real_group_sizes(kk) - num_slaves
547 write(
message(ii),
'(3a,i6,a,i12,a)')
'Info: Number of nodes in ', &
548 par_types(kk),
' group:', real_group_sizes(kk),
' (', index_range(kk),
')'
553 if (product(mc%group_sizes(1:
p_strategy_max)) /= base_grp%size)
then
554 write(
message(1),
'(a)')
'Inconsistent number of processors:'
555 write(
message(2),
'(a,i6)')
' MPI processes = ', base_grp%size
558 message(5) =
'You probably have a problem in the ParDomains, ParStates, ParKPoints or ParOther.'
563 message(1) =
"Could not distribute nodes in parallel job. Most likely you are trying to"
564 message(2) =
"use too many nodes for the job."
570 message(1) =
"I have fewer elements in a parallel group than recommended."
571 message(2) =
"Maybe you should reduce the number of nodes."
578 n_max = ceiling(real(index_range(ii), real64) / real(real_group_sizes(ii)), real64)
579 jj = n_max*real_group_sizes(ii)
580 frac = frac*(
m_one - real(jj - index_range(ii), real64) / real(jj, real64) )
583 write(
message(1),
'(a,f5.2,a)')
"Info: Octopus will waste at least ", &
584 (
m_one - frac)*100.0_real64,
"% of computer time."
585 if (frac < 0.8_real64)
then
586 message(2) =
"Usually a number of processors which is a multiple of small primes is best."
598 logical :: dim_mask(MAX_INDEX)
599 integer :: i_strategy, irank
600 logical :: reorder, periodic_mask(MAX_INDEX)
601 integer :: coords(MAX_INDEX)
602 type(mpi_comm) :: new_comm
603 integer :: new_comm_size
604 character(len=6) :: node_type
606 type(mpi_group) :: base_group, reorder_group
607 integer :: ranks(base_grp%size)
608 integer :: ii, jj, kk, ll, nn
609 type(mpi_comm) :: reorder_comm
614 mc%node_type = p_master
623 mc%full_comm = mpi_comm_null
624 mc%slave_intercomm = mpi_comm_null
626 if (mc%reorder_ranks)
then
630 call mpi_comm_group(base_grp%comm, base_group)
633 do ii = 1, mc%group_sizes(1)
634 do jj = 1, mc%group_sizes(2)
635 do kk = 1, mc%group_sizes(3)
636 do ll = 1, mc%group_sizes(4)
637 ranks(nn) = (ll-1)*mc%group_sizes(3)*mc%group_sizes(2)*mc%group_sizes(1) &
638 + (kk-1)*mc%group_sizes(2)*mc%group_sizes(1) &
639 + (jj-1)*mc%group_sizes(1) + ii - 1
645 call mpi_group_incl(base_group, base_grp%size, ranks, reorder_group)
647 call mpi_comm_create(base_grp%comm, reorder_group, reorder_comm)
658 periodic_mask = .false.
667 call mpi_cart_create(reorder_grp%comm,
p_strategy_max, mc%group_sizes, periodic_mask, reorder, mc%full_comm)
669 call mpi_comm_rank(mc%full_comm, mc%full_comm_rank)
672 call mpi_cart_coords(mc%full_comm, mc%full_comm_rank,
p_strategy_max, coords)
675 if (coords(slave_level) >= mc%group_sizes(slave_level) - num_slaves)
then
679 if (mc%node_type == p_master)
then
680 mc%group_sizes(slave_level) = mc%group_sizes(slave_level) - num_slaves
682 mc%group_sizes(slave_level) = num_slaves
685 call mpi_comm_split(mc%full_comm, mc%node_type, mc%full_comm_rank, new_comm)
686 assert(new_comm /= mpi_comm_null)
687 call mpi_comm_size(new_comm, new_comm_size)
690 if (product(mc%group_sizes(:)) /= new_comm_size)
then
691 write(stderr,*)
'node ', base_grp%rank,
': mc%group_sizes = ', mc%group_sizes,
' new_comm_size = ', new_comm_size
692 call base_grp%barrier()
693 assert(product(mc%group_sizes(:)) == new_comm_size)
695 call mpi_cart_create(new_comm,
p_strategy_max, mc%group_sizes, periodic_mask, reorder, mc%master_comm)
696 assert(mc%master_comm /= mpi_comm_null)
698 call mpi_comm_free(new_comm)
700 call mpi_comm_rank(mc%master_comm, mc%master_comm_rank)
708 dim_mask(i_strategy) = .
true.
709 call mpi_cart_sub(mc%master_comm, dim_mask, mc%group_comm(i_strategy))
710 call mpi_comm_rank(mc%group_comm(i_strategy), mc%who_am_i(i_strategy))
717 call mpi_cart_sub(mc%master_comm, dim_mask, mc%dom_st_comm)
723 call mpi_cart_sub(mc%master_comm, dim_mask, mc%st_kpt_comm)
730 call mpi_cart_sub(mc%master_comm, dim_mask, mc%dom_st_kpt_comm)
732 if (num_slaves > 0)
call create_slave_intercommunicators()
734 call create_intranode_communicator(base_grp, mc%intranode_grp, mc%internode_grp)
737 mc%group_comm = base_grp%comm
739 mc%master_comm = base_grp%comm
740 mc%dom_st_comm = base_grp%comm
741 mc%st_kpt_comm = base_grp%comm
742 mc%dom_st_kpt_comm = base_grp%comm
750 write(
message(1),
'(a)')
'Debug: MPI Task Assignment to MPI Groups'
751 write(
message(2),
'(5a10)')
'World',
'Domains',
'States',
'K-Points',
'Other'
754 if (mc%node_type ==
p_slave)
then
759 do irank = 0, mc%base_grp%size - 1
760 if (mc%base_grp%rank == irank)
then
765 call mc%base_grp%barrier()
775 subroutine create_slave_intercommunicators()
776 integer :: remote_leader
778 integer :: coords(max_index)
785 call mpi_cart_coords(mc%full_comm, mc%full_comm_rank,
p_strategy_max, coords)
788 if (mc%node_type ==
p_slave)
then
789 coords(slave_level) = 0
791 coords(slave_level) = mc%group_sizes(slave_level)
793 call mpi_cart_rank(mc%full_comm, coords, remote_leader)
797 call mpi_intercomm_create(mc%group_comm(slave_level), 0, base_grp%comm, remote_leader, tag, mc%slave_intercomm)
800 end subroutine create_slave_intercommunicators
820 call mpi_comm_free(mc%group_comm(ii))
822 call mpi_comm_free(mc%dom_st_comm)
823 call mpi_comm_free(mc%st_kpt_comm)
824 call mpi_comm_free(mc%dom_st_kpt_comm)
825 call mpi_comm_free(mc%full_comm)
826 call mpi_comm_free(mc%master_comm)
833 safe_deallocate_a(mc%group_sizes)
834 safe_deallocate_a(mc%group_comm)
835 safe_deallocate_a(mc%who_am_i)
844 integer,
intent(in) :: level
846 rr =
bitand(mc%par_strategy, 2**(level - 1)) /= 0
860 subroutine multicomm_create_all_pairs(mpi_grp, ap)
861 type(mpi_grp_t),
intent(in) :: mpi_grp
864 integer :: grp_size, rounds, ir, in
866 push_sub(create_all_pairs)
869 grp_size = mpi_grp%size
872 if (mod(grp_size, 2) == 0)
then
873 rounds = grp_size - 1
880 safe_allocate(ap%schedule(0:grp_size - 1, 1:rounds))
882 do in = 0, grp_size - 1
883 ap%schedule(in, ir) = get_partner(in + 1, ir) - 1
887 pop_sub(create_all_pairs)
893 integer pure function get_partner(in, ir)
894 integer,
intent(in) :: in, ir
898 if (mod(grp_size, 2) == 0)
then
899 get_partner = get_partner_even(grp_size, in - 1, ir - 1) + 1
901 get_partner = get_partner_odd(grp_size, in - 1, ir - 1) + 1
904 end function get_partner
907 integer pure function get_partner_even(grp_size, ii, rr) result(pp)
908 integer,
intent(in) :: grp_size, ii, rr
918 elseif (ii == rr + 1)
then
923 pp = modulo(2 * rr - ii + 1, 2 * mm - 1) + 1
926 end function get_partner_even
929 integer pure function get_partner_odd(grp_size, ii, rr) result(pp)
930 integer,
intent(in) :: grp_size, ii, rr
936 mm = (grp_size + 1) / 2
938 pp = get_partner_even(grp_size + 1, ii, rr)
940 if (pp == 2 * mm - 1)
then
944 end function get_partner_odd
946 end subroutine multicomm_create_all_pairs
953 integer,
intent(in) :: nobjs
954 integer,
intent(in) :: nprocs
955 integer,
intent(out) :: istart(:)
956 integer,
intent(out) :: ifinal(:)
957 integer,
optional,
intent(out) :: lsize(:)
958 logical,
optional,
intent(in) :: scalapack_compat
960 integer :: ii, jj, rank
961 logical :: scalapack_compat_
964 scalapack_compat_ = optional_default(scalapack_compat, .false.)
965#ifndef HAVE_SCALAPACK
966 scalapack_compat_ = .false.
969 if (scalapack_compat_)
then
971 if (mod(nobjs, nprocs) /= 0) nbl = nbl + 1
976 size = numroc(nobjs, nbl, rank - 1, 0, nprocs)
979 if (rank > 1) istart(rank) = ifinal(rank - 1) + 1
980 ifinal(rank) = istart(rank) +
size - 1
988 if (nprocs <= nobjs)
then
991 do rank = 0, nprocs - 1
993 ii = nobjs - jj*nprocs
994 if (ii > 0 .and. rank < ii)
then
996 istart(rank + 1) = rank*jj + 1
997 ifinal(rank + 1) = istart(rank + 1) + jj - 1
999 ifinal(rank + 1) = nobjs - (nprocs - rank - 1)*jj
1000 istart(rank + 1) = ifinal(rank + 1) - jj + 1
1006 if (ii <= nobjs)
then
1017 if (
present(lsize))
then
1018 lsize(1:nprocs) = ifinal(1:nprocs) - istart(1:nprocs) + 1
1019 assert(sum(lsize(1:nprocs)) == nobjs)
1029 integer,
intent(in) :: nobjs
1030 integer,
intent(out) :: ini
1031 integer,
intent(out) :: nobjs_loc
1035 integer,
allocatable :: istart(:), ifinal(:), lsize(:)
1042 nthreads = omp_get_num_threads()
1043 allocate(istart(1:nthreads))
1044 allocate(ifinal(1:nthreads))
1045 allocate(lsize(1:nthreads))
1047 rank = 1 + omp_get_thread_num()
1049 nobjs_loc = lsize(rank)
1065 slave = this%node_type ==
p_slave
1073 have_slaves = this%have_slaves
subroutine group_comm_create()
subroutine sanity_check()
check if a balanced distribution of nodes will be used
subroutine assign_nodes()
This module contains interfaces for BLACS routines Interfaces are from http:
This module handles the calculation mode.
integer, parameter, public p_strategy_max
integer, parameter, public p_strategy_kpoints
parallelization in k-points
integer, parameter, public p_strategy_other
something else like e-h pairs
integer, parameter, public p_strategy_domains
parallelization in domains
integer, parameter, public p_strategy_serial
single domain, all states, k-points on a single processor
integer, parameter, public p_strategy_states
parallelization in states
type(debug_t), save, public debug
real(real64), parameter, public m_one
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
character(len=512), private msg
subroutine, public messages_warning(no_lines, all_nodes, namespace)
subroutine, public messages_obsolete_variable(namespace, name, rep)
subroutine, public messages_new_line()
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
subroutine, public messages_experimental(name, namespace)
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
This module contains some common usage patterns of MPI routines.
subroutine mpi_grp_copy(mpi_grp_out, mpi_grp_in)
MPI_THREAD_FUNNELED allows for calls to MPI from an OMP region if the thread is the team master.
type(mpi_comm), parameter, public mpi_comm_undefined
used to indicate a communicator has not been initialized
subroutine mpi_grp_init(grp, comm)
Initialize MPI group instance.
This module handles the communicators for the various parallelization strategies.
logical pure function, public multicomm_strategy_is_parallel(mc, level)
integer, parameter, public p_slave
subroutine, public multicomm_divide_range(nobjs, nprocs, istart, ifinal, lsize, scalapack_compat)
This routine uses the one-factorization (or near-one-factorization of a complete graph to construct a...
subroutine, public multicomm_all_pairs_copy(apout, apin)
subroutine, public multicomm_end(mc)
integer, parameter, public par_no
logical pure function, public multicomm_have_slaves(this)
logical pure function, public multicomm_is_slave(this)
subroutine, public multicomm_init(mc, namespace, base_grp, mode_para, n_node, index_range, min_range)
create index and domain communicators
subroutine, public multicomm_divide_range_omp(nobjs, ini, nobjs_loc)
Function to divide the range of numbers from 1 to nobjs between all available threads with OpenMP.
This module is intended to contain simple general-purpose utility functions and procedures.
subroutine, public get_divisors(nn, n_divisors, divisors)
This is defined even when running serial.
An all-pairs communication schedule for a given group.
Stores all communicators and groups.