| 1 |
! This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
! License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
! file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
module mbd_blacs |
| 5 |
|
| 6 |
use mbd_constants |
| 7 |
|
| 8 |
implicit none |
| 9 |
|
| 10 |
private |
| 11 |
public :: blacs_all_reduce |
| 12 |
|
| 13 |
type, public :: blacs_grid_t |
| 14 |
integer :: ctx = -1 |
| 15 |
integer :: sys_ctx = -1 |
| 16 |
integer :: nprows |
| 17 |
integer :: npcols |
| 18 |
integer :: my_prow |
| 19 |
integer :: my_pcol |
| 20 |
contains |
| 21 |
procedure :: init => blacs_grid_init |
| 22 |
procedure :: destroy => blacs_grid_destroy |
| 23 |
end type |
| 24 |
|
| 25 |
type, public :: blacs_desc_t |
| 26 |
integer, allocatable :: i_atom(:) |
| 27 |
integer, allocatable :: j_atom(:) |
| 28 |
integer :: n_atoms |
| 29 |
integer :: desc(9) |
| 30 |
integer :: ctx |
| 31 |
integer :: blocksize |
| 32 |
integer :: comm = -1 |
| 33 |
contains |
| 34 |
procedure :: init => blacs_desc_init |
| 35 |
end type |
| 36 |
|
| 37 |
interface blacs_all_reduce |
| 38 |
module procedure all_reduce_real_scalar |
| 39 |
module procedure all_reduce_complex_scalar |
| 40 |
module procedure all_reduce_real_1d |
| 41 |
module procedure all_reduce_real_2d |
| 42 |
module procedure all_reduce_complex_1d |
| 43 |
module procedure all_reduce_complex_2d |
| 44 |
end interface |
| 45 |
|
| 46 |
interface |
| 47 |
! The following interfaces were written by hand based on |
| 48 |
! https://www.netlib.org/blacs/BLACS/QRef.html and https://www.ibm.com/docs/ |
| 49 |
subroutine BLACS_PINFO(MYPNUM, NPROCS) |
| 50 |
integer :: MYPNUM, NPROCS |
| 51 |
end |
| 52 |
subroutine BLACS_GRIDINIT(ICONTXT, ORDER, NPROW, NPCOL) |
| 53 |
integer :: ICONTXT, NPROW, NPCOL |
| 54 |
character :: ORDER |
| 55 |
end |
| 56 |
subroutine BLACS_GRIDINFO(ICONTXT, NPROW, NPCOL, MYPROW, MYPCOL) |
| 57 |
integer :: ICONTXT, NPROW, NPCOL, MYPROW, MYPCOL |
| 58 |
end |
| 59 |
subroutine BLACS_GRIDEXIT(ICONTXT) |
| 60 |
integer :: ICONTXT |
| 61 |
end |
| 62 |
subroutine BLACS_GET(ICONTXT, WHAT, VAL) |
| 63 |
integer :: ICONTXT, WHAT, VAL |
| 64 |
end subroutine |
| 65 |
integer function SYS2BLACS_HANDLE(COMM) |
| 66 |
integer :: COMM |
| 67 |
end |
| 68 |
subroutine FREE_BLACS_SYSTEM_HANDLE(IHANDLE) |
| 69 |
integer :: IHANDLE |
| 70 |
end |
| 71 |
integer function NUMROC(n, nb, iproc, isrcproc, nprocs) |
| 72 |
integer :: n, nb, iproc, isrcproc, nprocs |
| 73 |
end |
| 74 |
subroutine DGSUM2D(ICONTXT, SCOPE, TOP, M, N, A, LDA, RDEST, CDEST) |
| 75 |
integer :: ICONTXT, RDEST, CDEST, M, N, LDA |
| 76 |
character :: SCOPE, TOP |
| 77 |
double precision :: A(*) |
| 78 |
end |
| 79 |
subroutine ZGSUM2D(ICONTXT, SCOPE, TOP, M, N, A, LDA, RDEST, CDEST) |
| 80 |
import :: dp |
| 81 |
integer :: ICONTXT, RDEST, CDEST, M, N, LDA |
| 82 |
character :: SCOPE, TOP |
| 83 |
complex(dp) :: A(*) |
| 84 |
end |
| 85 |
|
| 86 |
! The following interfaces were taken straight from the ScaLAPACK codebase |
| 87 |
SUBROUTINE DESCINIT(DESC, M, N, MB, NB, IRSRC, ICSRC, ICTXT, LLD, INFO) |
| 88 |
INTEGER ICSRC, ICTXT, INFO, IRSRC, LLD, M, MB, N, NB |
| 89 |
INTEGER DESC(*) |
| 90 |
END |
| 91 |
end interface |
| 92 |
|
| 93 |
contains |
| 94 |
|
| 95 |
subroutine blacs_grid_init(this, n_tasks, comm) |
| 96 |
class(blacs_grid_t), intent(inout) :: this |
| 97 |
integer, intent(in), optional :: n_tasks |
| 98 |
integer, intent(in), optional :: comm |
| 99 |
|
| 100 |
integer :: my_task, n_tasks_, nprows, icontxt |
| 101 |
|
| 102 |
if (present(n_tasks)) then |
| 103 |
n_tasks_ = n_tasks |
| 104 |
else |
| 105 |
call BLACS_PINFO(my_task, n_tasks_) |
| 106 |
end if |
| 107 |
do nprows = int(sqrt(dble(n_tasks_))), 1, -1 |
| 108 |
if (mod(n_tasks_, nprows) == 0) exit |
| 109 |
end do |
| 110 |
this%nprows = nprows |
| 111 |
this%npcols = n_tasks_ / this%nprows |
| 112 |
! icontxt starts as the system context and is replaced in place by |
| 113 |
! BLACS_GRIDINIT with the grid context; sys_ctx retains the system handle |
| 114 |
! (when we own one) so it can be freed in blacs_grid_destroy. |
| 115 |
if (present(comm)) then |
| 116 |
this%sys_ctx = sys2blacs_handle(comm) |
| 117 |
icontxt = this%sys_ctx |
| 118 |
else |
| 119 |
call BLACS_GET(0, 0, icontxt) |
| 120 |
this%sys_ctx = -1 |
| 121 |
end if |
| 122 |
call BLACS_GRIDINIT(icontxt, 'R', this%nprows, this%npcols) |
| 123 |
this%ctx = icontxt |
| 124 |
call BLACS_GRIDINFO( & |
| 125 |
this%ctx, this%nprows, this%npcols, this%my_prow, this%my_pcol & |
| 126 |
) |
| 127 |
end subroutine |
| 128 |
|
| 129 |
! TODO this should be made a destructor once support for gfortran 4.9 is dropped |
| 130 |
subroutine blacs_grid_destroy(this) |
| 131 |
class(blacs_grid_t), intent(inout) :: this |
| 132 |
|
| 133 |
call BLACS_GRIDEXIT(this%ctx) |
| 134 |
if (this%sys_ctx /= -1) then |
| 135 |
call FREE_BLACS_SYSTEM_HANDLE(this%sys_ctx) |
| 136 |
this%sys_ctx = -1 |
| 137 |
end if |
| 138 |
end subroutine |
| 139 |
|
| 140 |
subroutine blacs_desc_init(this, n_atoms, grid, max_atoms_per_block) |
| 141 |
class(blacs_desc_t), intent(out) :: this |
| 142 |
type(blacs_grid_t), intent(in) :: grid |
| 143 |
integer, intent(in) :: n_atoms |
| 144 |
integer, intent(in) :: max_atoms_per_block |
| 145 |
|
| 146 |
integer :: my_nratoms, my_ncatoms, ierr, atoms_per_block, n_proc |
| 147 |
|
| 148 |
this%ctx = grid%ctx |
| 149 |
this%n_atoms = n_atoms |
| 150 |
n_proc = max(grid%nprows, grid%npcols) |
| 151 |
if (n_proc == 1) return |
| 152 |
atoms_per_block = (n_atoms - 1) / (n_proc - 1) |
| 153 |
if (atoms_per_block == 0) return |
| 154 |
atoms_per_block = min(atoms_per_block, max_atoms_per_block) |
| 155 |
my_nratoms = NUMROC(n_atoms, atoms_per_block, grid%my_prow, 0, grid%nprows) |
| 156 |
my_ncatoms = NUMROC(n_atoms, atoms_per_block, grid%my_pcol, 0, grid%npcols) |
| 157 |
this%blocksize = 3 * atoms_per_block |
| 158 |
call DESCINIT( & |
| 159 |
this%desc, 3 * n_atoms, 3 * n_atoms, this%blocksize, this%blocksize, 0, 0, & |
| 160 |
grid%ctx, 3 * my_nratoms, ierr & |
| 161 |
) |
| 162 |
this%i_atom = idx_map( & |
| 163 |
grid%my_prow, grid%nprows, n_atoms, atoms_per_block, my_nratoms & |
| 164 |
) |
| 165 |
this%j_atom = idx_map( & |
| 166 |
grid%my_pcol, grid%npcols, n_atoms, atoms_per_block, my_ncatoms & |
| 167 |
) |
| 168 |
end subroutine |
| 169 |
|
| 170 |
function idx_map(my_task, n_tasks, n, blocksize, nidx) |
| 171 |
integer, intent(in) :: my_task, n_tasks, n, blocksize, nidx |
| 172 |
integer :: idx_map(nidx) |
| 173 |
|
| 174 |
integer :: i, i_block, n_in_block, my_i |
| 175 |
|
| 176 |
i_block = 0 |
| 177 |
n_in_block = 0 |
| 178 |
my_i = 1 |
| 179 |
do i = 1, n |
| 180 |
if (mod(i_block, n_tasks) == my_task) then |
| 181 |
idx_map(my_i) = i |
| 182 |
my_i = my_i + 1 |
| 183 |
end if |
| 184 |
n_in_block = n_in_block + 1 |
| 185 |
if (n_in_block == blocksize) then |
| 186 |
n_in_block = 0 |
| 187 |
i_block = i_block + 1 |
| 188 |
end if |
| 189 |
end do |
| 190 |
end function |
| 191 |
|
| 192 |
subroutine all_reduce_real_scalar(x, blacs) |
| 193 |
real(dp), intent(inout) :: x |
| 194 |
type(blacs_desc_t), intent(in) :: blacs |
| 195 |
|
| 196 |
real(dp), pointer :: x_arr(:, :) |
| 197 |
|
| 198 |
x_arr(1, 1) = x |
| 199 |
call DGSUM2D(blacs%ctx, 'A', ' ', 1, 1, x_arr, 1, -1, -1) |
| 200 |
x = x_arr(1, 1) |
| 201 |
end subroutine |
| 202 |
|
| 203 |
subroutine all_reduce_complex_scalar(x, blacs) |
| 204 |
complex(dp), intent(inout) :: x |
| 205 |
type(blacs_desc_t), intent(in) :: blacs |
| 206 |
|
| 207 |
complex(dp) :: x_arr(1, 1) |
| 208 |
|
| 209 |
x_arr(1, 1) = x |
| 210 |
call ZGSUM2D(blacs%ctx, 'A', ' ', 1, 1, x_arr, 1, -1, -1) |
| 211 |
x = x_arr(1, 1) |
| 212 |
end subroutine |
| 213 |
|
| 214 |
subroutine all_reduce_real_1d(A, blacs) |
| 215 |
real(dp), target, intent(inout) :: A(:) |
| 216 |
type(blacs_desc_t), intent(in) :: blacs |
| 217 |
|
| 218 |
real(dp), pointer :: A_p(:, :) |
| 219 |
|
| 220 |
A_p(1:size(A), 1:1) => A |
| 221 |
call DGSUM2D(blacs%ctx, 'A', ' ', size(A), 1, A_p, size(A), -1, -1) |
| 222 |
end subroutine |
| 223 |
|
| 224 |
subroutine all_reduce_real_2d(A, blacs) |
| 225 |
real(dp), intent(inout) :: A(:, :) |
| 226 |
type(blacs_desc_t), intent(in) :: blacs |
| 227 |
|
| 228 |
call DGSUM2D( & |
| 229 |
blacs%ctx, 'A', ' ', size(A, 1), size(A, 2), A, size(A, 1), -1, -1 & |
| 230 |
) |
| 231 |
end subroutine |
| 232 |
|
| 233 |
subroutine all_reduce_complex_1d(A, blacs) |
| 234 |
complex(dp), target, intent(inout) :: A(:) |
| 235 |
type(blacs_desc_t), intent(in) :: blacs |
| 236 |
|
| 237 |
complex(dp), pointer :: A_p(:, :) |
| 238 |
|
| 239 |
A_p(1:size(A), 1:1) => A |
| 240 |
call ZGSUM2D(blacs%ctx, 'A', ' ', size(A), 1, A_p, size(A), -1, -1) |
| 241 |
end subroutine |
| 242 |
|
| 243 |
subroutine all_reduce_complex_2d(A, blacs) |
| 244 |
complex(dp), intent(inout) :: A(:, :) |
| 245 |
type(blacs_desc_t), intent(in) :: blacs |
| 246 |
|
| 247 |
call ZGSUM2D( & |
| 248 |
blacs%ctx, 'A', ' ', size(A, 1), size(A, 2), A, size(A, 1), -1, -1 & |
| 249 |
) |
| 250 |
end subroutine |
| 251 |
|
| 252 |
end module |