Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief generate the tasks lists used by collocate and integrate routines
10 : !> \par History
11 : !> 01.2008 [Joost VandeVondele] refactered out of qs_collocate / qs_integrate
12 : !> \author Joost VandeVondele
13 : ! **************************************************************************************************
14 : MODULE task_list_methods
15 : USE offload_api, ONLY: offload_create_buffer, offload_buffer_type
16 : USE grid_api, ONLY: grid_create_basis_set, grid_create_task_list
17 : USE ao_util, ONLY: exp_radius_very_extended
18 : USE basis_set_types, ONLY: get_gto_basis_set, &
19 : gto_basis_set_p_type, &
20 : gto_basis_set_type
21 : USE cell_types, ONLY: cell_type, &
22 : pbc
23 : USE cp_control_types, ONLY: dft_control_type
24 : USE cp_log_handling, ONLY: cp_logger_get_default_unit_nr
25 : USE cube_utils, ONLY: compute_cube_center, &
26 : cube_info_type, &
27 : return_cube, &
28 : return_cube_nonortho
29 : USE cp_dbcsr_api, ONLY: dbcsr_convert_sizes_to_offsets, &
30 : dbcsr_finalize, &
31 : dbcsr_get_block_p, &
32 : dbcsr_get_info, &
33 : dbcsr_p_type, &
34 : dbcsr_put_block, &
35 : dbcsr_type, &
36 : dbcsr_work_create
37 : USE gaussian_gridlevels, ONLY: gaussian_gridlevel, &
38 : gridlevel_info_type
39 : USE kinds, ONLY: default_string_length, &
40 : dp, &
41 : int_8
42 : USE kpoint_types, ONLY: get_kpoint_info, &
43 : kpoint_type
44 : USE memory_utilities, ONLY: reallocate
45 : USE message_passing, ONLY: &
46 : mp_comm_type
47 : USE particle_types, ONLY: particle_type
48 : USE particle_methods, ONLY: get_particle_set
49 : USE pw_env_types, ONLY: pw_env_get, &
50 : pw_env_type
51 : USE qs_kind_types, ONLY: get_qs_kind, &
52 : qs_kind_type
53 : USE qs_ks_types, ONLY: get_ks_env, &
54 : qs_ks_env_type
55 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
56 : USE realspace_grid_types, ONLY: realspace_grid_desc_p_type, &
57 : realspace_grid_desc_type, &
58 : rs_grid_create, &
59 : rs_grid_locate_rank, &
60 : rs_grid_release, &
61 : rs_grid_reorder_ranks, realspace_grid_type
62 : USE task_list_types, ONLY: deserialize_task, &
63 : reallocate_tasks, &
64 : serialize_task, &
65 : task_list_type, &
66 : atom_pair_type, &
67 : task_size_in_int8, &
68 : task_type
69 : USE util, ONLY: sort
70 :
71 : !$ USE OMP_LIB, ONLY: omp_destroy_lock, omp_get_num_threads, omp_init_lock, &
72 : !$ omp_lock_kind, omp_set_lock, omp_unset_lock
73 : #include "./base/base_uses.f90"
74 :
75 : #:include './common/array_sort.fypp'
76 :
77 : IMPLICIT NONE
78 :
79 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .FALSE.
80 :
81 : PRIVATE
82 :
83 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'task_list_methods'
84 :
85 : PUBLIC :: generate_qs_task_list, &
86 : task_list_inner_loop
87 : PUBLIC :: distribute_tasks, &
88 : rs_distribute_matrix, &
89 : rs_scatter_matrices, &
90 : rs_gather_matrices, &
91 : rs_copy_to_buffer, &
92 : rs_copy_to_matrices
93 :
94 : CONTAINS
95 :
96 : ! **************************************************************************************************
97 : !> \brief ...
98 : !> \param ks_env ...
99 : !> \param task_list ...
100 : !> \param basis_type ...
101 : !> \param reorder_rs_grid_ranks Flag that indicates if this routine should
102 : !> or should not overwrite the rs descriptor (see comment below)
103 : !> \param skip_load_balance_distributed ...
104 : !> \param pw_env_external ...
105 : !> \param sab_orb_external ...
106 : !> \param ext_kpoints ...
107 : !> \par History
108 : !> 01.2008 factored out of calculate_rho_elec [Joost VandeVondele]
109 : !> 04.2010 divides tasks into grid levels and atom pairs for integrate/collocate [Iain Bethune]
110 : !> (c) The Numerical Algorithms Group (NAG) Ltd, 2010 on behalf of the HECToR project
111 : !> 06.2015 adjusted to be used with multiple images (k-points) [JGH]
112 : !> \note If this routine is called several times with different task lists,
113 : !> the default behaviour is to re-optimize the grid ranks and overwrite
114 : !> the rs descriptor and grids. reorder_rs_grid_ranks = .FALSE. prevents the code
115 : !> of performing a new optimization by leaving the rank order in
116 : !> its current state.
117 : ! **************************************************************************************************
118 17890 : SUBROUTINE generate_qs_task_list(ks_env, task_list, basis_type, &
119 : reorder_rs_grid_ranks, skip_load_balance_distributed, &
120 : pw_env_external, sab_orb_external, ext_kpoints)
121 :
122 : TYPE(qs_ks_env_type), POINTER :: ks_env
123 : TYPE(task_list_type), POINTER :: task_list
124 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
125 : LOGICAL, INTENT(IN) :: reorder_rs_grid_ranks, &
126 : skip_load_balance_distributed
127 : TYPE(pw_env_type), OPTIONAL, POINTER :: pw_env_external
128 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
129 : OPTIONAL, POINTER :: sab_orb_external
130 : TYPE(kpoint_type), OPTIONAL, POINTER :: ext_kpoints
131 :
132 : CHARACTER(LEN=*), PARAMETER :: routineN = 'generate_qs_task_list'
133 : INTEGER, PARAMETER :: max_tasks = 2000
134 :
135 : INTEGER :: cindex, curr_tasks, handle, i, iatom, iatom_old, igrid_level, igrid_level_old, &
136 : ikind, ilevel, img, img_old, ipair, ipgf, iset, itask, jatom, jatom_old, jkind, jpgf, &
137 : jset, maxpgf, maxset, natoms, nimages, nkind, nseta, nsetb, output_unit, slot
138 17890 : INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: blocks
139 : INTEGER, DIMENSION(3) :: cellind
140 17890 : INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, npgfa, &
141 17890 : npgfb, nsgf
142 17890 : INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
143 : LOGICAL :: dokp
144 : REAL(KIND=dp) :: kind_radius_a, kind_radius_b
145 : REAL(KIND=dp), DIMENSION(3) :: ra, rab
146 17890 : REAL(KIND=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
147 17890 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: rpgfa, rpgfb, zeta, zetb
148 : TYPE(cell_type), POINTER :: cell
149 17890 : TYPE(cube_info_type), DIMENSION(:), POINTER :: cube_info
150 : TYPE(dft_control_type), POINTER :: dft_control
151 : TYPE(gridlevel_info_type), POINTER :: gridlevel_info
152 17890 : TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
153 : TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b, orb_basis_set
154 : TYPE(kpoint_type), POINTER :: kpoints
155 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
156 17890 : POINTER :: sab_orb
157 17890 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
158 : TYPE(pw_env_type), POINTER :: pw_env
159 17890 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
160 : TYPE(qs_kind_type), POINTER :: qs_kind
161 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
162 17890 : POINTER :: rs_descs
163 17890 : TYPE(realspace_grid_type), DIMENSION(:), POINTER :: rs_grids
164 17890 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
165 :
166 17890 : CALL timeset(routineN, handle)
167 :
168 : CALL get_ks_env(ks_env, &
169 : qs_kind_set=qs_kind_set, &
170 : cell=cell, &
171 : particle_set=particle_set, &
172 17890 : dft_control=dft_control)
173 :
174 17890 : CALL get_ks_env(ks_env, sab_orb=sab_orb)
175 17890 : IF (PRESENT(sab_orb_external)) sab_orb => sab_orb_external
176 :
177 17890 : CALL get_ks_env(ks_env, pw_env=pw_env)
178 17890 : IF (PRESENT(pw_env_external)) pw_env => pw_env_external
179 17890 : CALL pw_env_get(pw_env, rs_descs=rs_descs, rs_grids=rs_grids)
180 :
181 : ! *** assign from pw_env
182 17890 : gridlevel_info => pw_env%gridlevel_info
183 17890 : cube_info => pw_env%cube_info
184 :
185 : ! find maximum numbers
186 17890 : nkind = SIZE(qs_kind_set)
187 17890 : natoms = SIZE(particle_set)
188 17890 : maxset = 0
189 17890 : maxpgf = 0
190 49169 : DO ikind = 1, nkind
191 31279 : qs_kind => qs_kind_set(ikind)
192 : CALL get_qs_kind(qs_kind=qs_kind, &
193 31279 : basis_set=orb_basis_set, basis_type=basis_type)
194 :
195 31279 : IF (.NOT. ASSOCIATED(orb_basis_set)) CYCLE
196 31279 : CALL get_gto_basis_set(gto_basis_set=orb_basis_set, npgf=npgfa, nset=nseta)
197 :
198 31279 : maxset = MAX(nseta, maxset)
199 113422 : maxpgf = MAX(MAXVAL(npgfa), maxpgf)
200 : END DO
201 :
202 : ! kpoint related
203 17890 : nimages = dft_control%nimages
204 17890 : dokp = .FALSE.
205 17890 : NULLIFY (cell_to_index)
206 17890 : NULLIFY (kpoints)
207 17890 : IF (PRESENT(ext_kpoints)) THEN
208 864 : IF (ASSOCIATED(ext_kpoints)) THEN
209 22 : dokp = .TRUE.
210 22 : CALL get_kpoint_info(kpoint=ext_kpoints, cell_to_index=cell_to_index)
211 22 : nimages = SIZE(ext_kpoints%index_to_cell, 2)
212 : END IF
213 : END IF
214 17890 : IF (.NOT. dokp .AND. nimages > 1) THEN
215 1252 : dokp = .TRUE.
216 1252 : CALL get_ks_env(ks_env=ks_env, kpoints=kpoints)
217 1252 : CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index)
218 : END IF
219 :
220 : ! free the atom_pair lists if allocated
221 17890 : IF (ASSOCIATED(task_list%atom_pair_send)) DEALLOCATE (task_list%atom_pair_send)
222 17890 : IF (ASSOCIATED(task_list%atom_pair_recv)) DEALLOCATE (task_list%atom_pair_recv)
223 :
224 : ! construct a list of all tasks
225 17890 : IF (.NOT. ASSOCIATED(task_list%tasks)) THEN
226 10622 : CALL reallocate_tasks(task_list%tasks, max_tasks)
227 : END IF
228 17890 : task_list%ntasks = 0
229 17890 : curr_tasks = SIZE(task_list%tasks)
230 :
231 84949 : ALLOCATE (basis_set_list(nkind))
232 49169 : DO ikind = 1, nkind
233 31279 : qs_kind => qs_kind_set(ikind)
234 : CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a, &
235 31279 : basis_type=basis_type)
236 49169 : IF (ASSOCIATED(basis_set_a)) THEN
237 31279 : basis_set_list(ikind)%gto_basis_set => basis_set_a
238 : ELSE
239 0 : NULLIFY (basis_set_list(ikind)%gto_basis_set)
240 : END IF
241 : END DO
242 : !!$OMP PARALLEL DEFAULT(NONE) &
243 : !!$OMP SHARED (sab_orb, dokp, basis_set_list, task_list, rs_descs, dft_control, cube_info, gridlevel_info, &
244 : !!$OMP curr_tasks, maxpgf, maxset, natoms, nimages, particle_set, cell_to_index, cell) &
245 : !!$OMP PRIVATE (ikind, jkind, iatom, jatom, rab, cellind, basis_set_a, basis_set_b, ra, &
246 : !!$OMP la_max, la_min, npgfa, nseta, rpgfa, set_radius_a, kind_radius_a, zeta, &
247 : !!$OMP lb_max, lb_min, npgfb, nsetb, rpgfb, set_radius_b, kind_radius_b, zetb, &
248 : !!$OMP cindex, slot)
249 : ! Loop over neighbor list
250 : !!$OMP DO SCHEDULE(GUIDED)
251 1953817 : DO slot = 1, sab_orb(1)%nl_size
252 1935927 : ikind = sab_orb(1)%nlist_task(slot)%ikind
253 1935927 : jkind = sab_orb(1)%nlist_task(slot)%jkind
254 1935927 : iatom = sab_orb(1)%nlist_task(slot)%iatom
255 1935927 : jatom = sab_orb(1)%nlist_task(slot)%jatom
256 7743708 : rab(1:3) = sab_orb(1)%nlist_task(slot)%r(1:3)
257 7743708 : cellind(1:3) = sab_orb(1)%nlist_task(slot)%cell(1:3)
258 :
259 1935927 : basis_set_a => basis_set_list(ikind)%gto_basis_set
260 1935927 : IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
261 1935927 : basis_set_b => basis_set_list(jkind)%gto_basis_set
262 1935927 : IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
263 1935927 : ra(:) = pbc(particle_set(iatom)%r, cell)
264 : ! basis ikind
265 1935927 : la_max => basis_set_a%lmax
266 1935927 : la_min => basis_set_a%lmin
267 1935927 : npgfa => basis_set_a%npgf
268 1935927 : nseta = basis_set_a%nset
269 1935927 : rpgfa => basis_set_a%pgf_radius
270 1935927 : set_radius_a => basis_set_a%set_radius
271 1935927 : kind_radius_a = basis_set_a%kind_radius
272 1935927 : zeta => basis_set_a%zet
273 : ! basis jkind
274 1935927 : lb_max => basis_set_b%lmax
275 1935927 : lb_min => basis_set_b%lmin
276 1935927 : npgfb => basis_set_b%npgf
277 1935927 : nsetb = basis_set_b%nset
278 1935927 : rpgfb => basis_set_b%pgf_radius
279 1935927 : set_radius_b => basis_set_b%set_radius
280 1935927 : kind_radius_b = basis_set_b%kind_radius
281 1935927 : zetb => basis_set_b%zet
282 :
283 1935927 : IF (dokp) THEN
284 585219 : cindex = cell_to_index(cellind(1), cellind(2), cellind(3))
285 : ELSE
286 1350708 : cindex = 1
287 : END IF
288 :
289 : CALL task_list_inner_loop(task_list%tasks, task_list%ntasks, curr_tasks, &
290 : rs_descs, dft_control, cube_info, gridlevel_info, cindex, &
291 : iatom, jatom, rpgfa, rpgfb, zeta, zetb, kind_radius_b, &
292 : set_radius_a, set_radius_b, ra, rab, &
293 1953817 : la_max, la_min, lb_max, lb_min, npgfa, npgfb, nseta, nsetb)
294 :
295 : END DO
296 : !!$OMP END PARALLEL
297 :
298 : ! redistribute the task list so that all tasks map on the local rs grids
299 : CALL distribute_tasks( &
300 : rs_descs=rs_descs, ntasks=task_list%ntasks, natoms=natoms, &
301 : tasks=task_list%tasks, atom_pair_send=task_list%atom_pair_send, &
302 : atom_pair_recv=task_list%atom_pair_recv, symmetric=.TRUE., &
303 : reorder_rs_grid_ranks=reorder_rs_grid_ranks, &
304 17890 : skip_load_balance_distributed=skip_load_balance_distributed)
305 :
306 : ! compute offsets for rs_scatter_matrix / rs_copy_matrix
307 53670 : ALLOCATE (nsgf(natoms))
308 17890 : CALL get_particle_set(particle_set, qs_kind_set, basis=basis_set_list, nsgf=nsgf)
309 17890 : IF (ASSOCIATED(task_list%atom_pair_send)) THEN
310 : ! Needed whenever tasks use matrix blocks from other ranks.
311 : CALL rs_calc_offsets(pairs=task_list%atom_pair_send, &
312 : nsgf=nsgf, &
313 : group_size=rs_descs(1)%rs_desc%group_size, &
314 : pair_offsets=task_list%pair_offsets_send, &
315 : rank_offsets=task_list%rank_offsets_send, &
316 : rank_sizes=task_list%rank_sizes_send, &
317 15860 : buffer_size=task_list%buffer_size_send)
318 : ELSE
319 : ! A rebuilt task list can return to owner-local execution.
320 2030 : IF (ASSOCIATED(task_list%pair_offsets_send)) DEALLOCATE (task_list%pair_offsets_send)
321 2030 : IF (ASSOCIATED(task_list%rank_offsets_send)) DEALLOCATE (task_list%rank_offsets_send)
322 2030 : IF (ASSOCIATED(task_list%rank_sizes_send)) DEALLOCATE (task_list%rank_sizes_send)
323 2030 : task_list%buffer_size_send = 0
324 : END IF
325 : CALL rs_calc_offsets(pairs=task_list%atom_pair_recv, &
326 : nsgf=nsgf, &
327 : group_size=rs_descs(1)%rs_desc%group_size, &
328 : pair_offsets=task_list%pair_offsets_recv, &
329 : rank_offsets=task_list%rank_offsets_recv, &
330 : rank_sizes=task_list%rank_sizes_recv, &
331 17890 : buffer_size=task_list%buffer_size_recv)
332 17890 : IF (ASSOCIATED(task_list%atom_pair_send)) THEN
333 : CALL rs_group_atom_pairs(task_list%atom_pair_send, &
334 15860 : task_list%unpack_block_starts, task_list%unpack_pair_indices)
335 : ELSE
336 : CALL rs_group_atom_pairs(task_list%atom_pair_recv, &
337 2030 : task_list%unpack_block_starts, task_list%unpack_pair_indices)
338 : END IF
339 17890 : DEALLOCATE (basis_set_list, nsgf)
340 :
341 : ! If the rank order has changed, reallocate any of the distributed rs_grids
342 17890 : IF (reorder_rs_grid_ranks) THEN
343 76228 : DO i = 1, gridlevel_info%ngrid_levels
344 76228 : IF (rs_descs(i)%rs_desc%distributed) THEN
345 56 : CALL rs_grid_release(rs_grids(i))
346 56 : CALL rs_grid_create(rs_grids(i), rs_descs(i)%rs_desc)
347 : END IF
348 : END DO
349 : END IF
350 :
351 : CALL create_grid_task_list(task_list=task_list, &
352 : qs_kind_set=qs_kind_set, &
353 : particle_set=particle_set, &
354 : cell=cell, &
355 : basis_type=basis_type, &
356 17890 : rs_grids=rs_grids)
357 :
358 : ! Now we have the final list of tasks, setup the task_list with the
359 : ! data needed for the loops in integrate_v/calculate_rho
360 :
361 17890 : IF (ASSOCIATED(task_list%taskstart)) THEN
362 7268 : DEALLOCATE (task_list%taskstart)
363 : END IF
364 17890 : IF (ASSOCIATED(task_list%taskstop)) THEN
365 7268 : DEALLOCATE (task_list%taskstop)
366 : END IF
367 17890 : IF (ASSOCIATED(task_list%npairs)) THEN
368 7268 : DEALLOCATE (task_list%npairs)
369 : END IF
370 :
371 : ! First, count the number of unique atom pairs per grid level
372 :
373 53670 : ALLOCATE (task_list%npairs(SIZE(rs_descs)))
374 :
375 17890 : iatom_old = -1; jatom_old = -1; igrid_level_old = -1; img_old = -1
376 17890 : ipair = 0
377 88728 : task_list%npairs = 0
378 :
379 10780892 : DO i = 1, task_list%ntasks
380 10763002 : igrid_level = task_list%tasks(i)%grid_level
381 10763002 : img = task_list%tasks(i)%image
382 10763002 : iatom = task_list%tasks(i)%iatom
383 10763002 : jatom = task_list%tasks(i)%jatom
384 10763002 : iset = task_list%tasks(i)%iset
385 10763002 : jset = task_list%tasks(i)%jset
386 10763002 : ipgf = task_list%tasks(i)%ipgf
387 10763002 : jpgf = task_list%tasks(i)%jpgf
388 10780892 : IF (igrid_level /= igrid_level_old) THEN
389 49858 : IF (igrid_level_old /= -1) THEN
390 31982 : task_list%npairs(igrid_level_old) = ipair
391 : END IF
392 : ipair = 1
393 : igrid_level_old = igrid_level
394 : iatom_old = iatom
395 : jatom_old = jatom
396 : img_old = img
397 10713144 : ELSE IF (iatom /= iatom_old .OR. jatom /= jatom_old .OR. img /= img_old) THEN
398 1299312 : ipair = ipair + 1
399 1299312 : iatom_old = iatom
400 1299312 : jatom_old = jatom
401 1299312 : img_old = img
402 : END IF
403 : END DO
404 : ! Take care of the last iteration
405 17890 : IF (task_list%ntasks /= 0) THEN
406 17876 : task_list%npairs(igrid_level) = ipair
407 : END IF
408 :
409 : ! Second, for each atom pair, find the indices in the task list
410 : ! of the first and last task
411 :
412 : ! Array sized for worst case
413 142384 : ALLOCATE (task_list%taskstart(MAXVAL(task_list%npairs), SIZE(rs_descs)))
414 142384 : ALLOCATE (task_list%taskstop(MAXVAL(task_list%npairs), SIZE(rs_descs)))
415 :
416 17890 : iatom_old = -1; jatom_old = -1; igrid_level_old = -1; img_old = -1
417 17890 : ipair = 0
418 2980680 : task_list%taskstart = 0
419 2980680 : task_list%taskstop = 0
420 :
421 10780892 : DO i = 1, task_list%ntasks
422 10763002 : igrid_level = task_list%tasks(i)%grid_level
423 10763002 : img = task_list%tasks(i)%image
424 10763002 : iatom = task_list%tasks(i)%iatom
425 10763002 : jatom = task_list%tasks(i)%jatom
426 10763002 : iset = task_list%tasks(i)%iset
427 10763002 : jset = task_list%tasks(i)%jset
428 10763002 : ipgf = task_list%tasks(i)%ipgf
429 10763002 : jpgf = task_list%tasks(i)%jpgf
430 10780892 : IF (igrid_level /= igrid_level_old) THEN
431 49858 : IF (igrid_level_old /= -1) THEN
432 31982 : task_list%taskstop(ipair, igrid_level_old) = i - 1
433 : END IF
434 49858 : ipair = 1
435 49858 : task_list%taskstart(ipair, igrid_level) = i
436 49858 : igrid_level_old = igrid_level
437 49858 : iatom_old = iatom
438 49858 : jatom_old = jatom
439 49858 : img_old = img
440 10713144 : ELSE IF (iatom /= iatom_old .OR. jatom /= jatom_old .OR. img /= img_old) THEN
441 1299312 : ipair = ipair + 1
442 1299312 : task_list%taskstart(ipair, igrid_level) = i
443 1299312 : task_list%taskstop(ipair - 1, igrid_level) = i - 1
444 1299312 : iatom_old = iatom
445 1299312 : jatom_old = jatom
446 1299312 : img_old = img
447 : END IF
448 : END DO
449 : ! Take care of the last iteration
450 17890 : IF (task_list%ntasks /= 0) THEN
451 17876 : task_list%taskstop(ipair, igrid_level) = task_list%ntasks
452 : END IF
453 :
454 : ! Debug task destribution
455 : IF (debug_this_module) THEN
456 : output_unit = cp_logger_get_default_unit_nr(local=.TRUE.)
457 : tasks => task_list%tasks
458 : IF (output_unit > 0) WRITE (output_unit, *)
459 : IF (output_unit > 0) WRITE (output_unit, *) "Total number of tasks ", task_list%ntasks
460 : DO igrid_level = 1, gridlevel_info%ngrid_levels
461 : IF (output_unit > 0) WRITE (output_unit, *) "Total number of pairs(grid_level) ", &
462 : igrid_level, task_list%npairs(igrid_level)
463 : END DO
464 : IF (output_unit > 0) WRITE (output_unit, *)
465 :
466 : DO igrid_level = 1, gridlevel_info%ngrid_levels
467 :
468 : ALLOCATE (blocks(natoms, natoms, nimages))
469 : blocks = -1
470 : DO ipair = 1, task_list%npairs(igrid_level)
471 : itask = task_list%taskstart(ipair, igrid_level)
472 : ilevel = task_list%tasks(itask)%grid_level
473 : img = task_list%tasks(itask)%image
474 : iatom = task_list%tasks(itask)%iatom
475 : jatom = task_list%tasks(itask)%jatom
476 : iset = task_list%tasks(itask)%iset
477 : jset = task_list%tasks(itask)%jset
478 : ipgf = task_list%tasks(itask)%ipgf
479 : jpgf = task_list%tasks(itask)%jpgf
480 : IF (blocks(iatom, jatom, img) == -1 .AND. blocks(jatom, iatom, img) == -1) THEN
481 : blocks(iatom, jatom, img) = 1
482 : blocks(jatom, iatom, img) = 1
483 : ELSE
484 : IF (output_unit > 0) WRITE (output_unit, *) "TASK LIST CONFLICT IN PAIR ", ipair
485 : IF (output_unit > 0) WRITE (output_unit, *) "Reuse of iatom, jatom, image ", iatom, jatom, img
486 : END IF
487 :
488 : iatom_old = iatom
489 : jatom_old = jatom
490 : img_old = img
491 : DO itask = task_list%taskstart(ipair, igrid_level), task_list%taskstop(ipair, igrid_level)
492 : ilevel = task_list%tasks(itask)%grid_level
493 : img = task_list%tasks(itask)%image
494 : iatom = task_list%tasks(itask)%iatom
495 : jatom = task_list%tasks(itask)%jatom
496 : iset = task_list%tasks(itask)%iset
497 : jset = task_list%tasks(itask)%jset
498 : ipgf = task_list%tasks(itask)%ipgf
499 : jpgf = task_list%tasks(itask)%jpgf
500 : IF (iatom /= iatom_old .OR. jatom /= jatom_old .OR. img /= img_old) THEN
501 : IF (output_unit > 0) WRITE (output_unit, *) "TASK LIST CONFLICT IN TASK ", itask
502 : IF (output_unit > 0) WRITE (output_unit, *) "Inconsistent iatom, jatom, image ", iatom, jatom, img
503 : IF (output_unit > 0) WRITE (output_unit, *) "Should be iatom, jatom, image ", iatom_old, jatom_old, img_old
504 : END IF
505 :
506 : END DO
507 : END DO
508 : DEALLOCATE (blocks)
509 :
510 : END DO
511 :
512 : END IF
513 :
514 17890 : CALL timestop(handle)
515 :
516 17890 : END SUBROUTINE generate_qs_task_list
517 :
518 : ! **************************************************************************************************
519 : !> \brief Sends the task list data to the grid API.
520 : !> \author Ole Schuett
521 : ! **************************************************************************************************
522 17890 : SUBROUTINE create_grid_task_list(task_list, qs_kind_set, particle_set, cell, basis_type, rs_grids)
523 : TYPE(task_list_type), POINTER :: task_list
524 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
525 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
526 : TYPE(cell_type), POINTER :: cell
527 : CHARACTER(LEN=*) :: basis_type
528 : TYPE(realspace_grid_type), DIMENSION(:), POINTER :: rs_grids
529 :
530 : TYPE(gto_basis_set_type), POINTER :: orb_basis_set
531 : INTEGER :: nset, natoms, nkinds, ntasks, &
532 : ikind, iatom, itask, nsgf
533 17890 : INTEGER, DIMENSION(:), ALLOCATABLE :: atom_kinds, level_list, iatom_list, jatom_list, &
534 17890 : iset_list, jset_list, ipgf_list, jpgf_list, &
535 17890 : border_mask_list, block_num_list
536 17890 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: radius_list
537 17890 : REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: rab_list, atom_positions
538 17890 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
539 17890 : INTEGER, DIMENSION(:, :), POINTER :: first_sgf
540 17890 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: sphi, zet
541 17890 : INTEGER, DIMENSION(:), POINTER :: lmax, lmin, npgf, nsgf_set
542 :
543 17890 : nkinds = SIZE(qs_kind_set)
544 17890 : natoms = SIZE(particle_set)
545 17890 : ntasks = task_list%ntasks
546 17890 : tasks => task_list%tasks
547 :
548 17890 : IF (.NOT. ASSOCIATED(task_list%grid_basis_sets)) THEN
549 : ! Basis sets do not change during simulation - only need to create them once.
550 50259 : ALLOCATE (task_list%grid_basis_sets(nkinds))
551 29015 : DO ikind = 1, nkinds
552 18393 : CALL get_qs_kind(qs_kind_set(ikind), basis_type=basis_type, basis_set=orb_basis_set)
553 : CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
554 : nset=nset, &
555 : nsgf=nsgf, &
556 : nsgf_set=nsgf_set, &
557 : npgf=npgf, &
558 : first_sgf=first_sgf, &
559 : lmax=lmax, &
560 : lmin=lmin, &
561 : sphi=sphi, &
562 18393 : zet=zet)
563 : CALL grid_create_basis_set(nset=nset, &
564 : nsgf=nsgf, &
565 : maxco=SIZE(sphi, 1), &
566 : maxpgf=SIZE(zet, 1), &
567 : lmin=lmin, &
568 : lmax=lmax, &
569 : npgf=npgf, &
570 : nsgf_set=nsgf_set, &
571 : first_sgf=first_sgf, &
572 : sphi=sphi, &
573 : zet=zet, &
574 29015 : basis_set=task_list%grid_basis_sets(ikind))
575 : END DO
576 : END IF
577 :
578 : ! Pack task list infos
579 89450 : ALLOCATE (atom_kinds(natoms), atom_positions(3, natoms))
580 81951 : DO iatom = 1, natoms
581 64061 : atom_kinds(iatom) = particle_set(iatom)%atomic_kind%kind_number
582 81951 : atom_positions(:, iatom) = pbc(particle_set(iatom)%r, cell)
583 : END DO
584 :
585 89408 : ALLOCATE (level_list(ntasks), iatom_list(ntasks), jatom_list(ntasks))
586 89394 : ALLOCATE (iset_list(ntasks), jset_list(ntasks), ipgf_list(ntasks), jpgf_list(ntasks))
587 53642 : ALLOCATE (border_mask_list(ntasks), block_num_list(ntasks))
588 89422 : ALLOCATE (radius_list(ntasks), rab_list(3, ntasks))
589 :
590 10780892 : DO itask = 1, ntasks
591 10763002 : level_list(itask) = tasks(itask)%grid_level
592 10763002 : iatom_list(itask) = tasks(itask)%iatom
593 10763002 : jatom_list(itask) = tasks(itask)%jatom
594 10763002 : iset_list(itask) = tasks(itask)%iset
595 10763002 : jset_list(itask) = tasks(itask)%jset
596 10763002 : ipgf_list(itask) = tasks(itask)%ipgf
597 10763002 : jpgf_list(itask) = tasks(itask)%jpgf
598 10763002 : IF (tasks(itask)%dist_type == 2) THEN
599 0 : border_mask_list(itask) = IAND(63, NOT(tasks(itask)%subpatch_pattern)) ! invert last 6 bits
600 : ELSE
601 10763002 : border_mask_list(itask) = 0 ! no masking
602 : END IF
603 10763002 : block_num_list(itask) = tasks(itask)%pair_index ! change of nomenclature pair_index -> block_num
604 10763002 : radius_list(itask) = tasks(itask)%radius
605 43069898 : rab_list(:, itask) = tasks(itask)%rab(:)
606 : END DO
607 :
608 : CALL grid_create_task_list(ntasks=ntasks, &
609 : natoms=natoms, &
610 : nkinds=nkinds, &
611 : nblocks=SIZE(task_list%pair_offsets_recv), &
612 : block_offsets=task_list%pair_offsets_recv, &
613 : atom_positions=atom_positions, &
614 : atom_kinds=atom_kinds, &
615 : basis_sets=task_list%grid_basis_sets, &
616 : level_list=level_list, &
617 : iatom_list=iatom_list, &
618 : jatom_list=jatom_list, &
619 : iset_list=iset_list, &
620 : jset_list=jset_list, &
621 : ipgf_list=ipgf_list, &
622 : jpgf_list=jpgf_list, &
623 : border_mask_list=border_mask_list, &
624 : block_num_list=block_num_list, &
625 : radius_list=radius_list, &
626 : rab_list=rab_list, &
627 : rs_grids=rs_grids, &
628 17890 : task_list=task_list%grid_task_list)
629 :
630 17890 : CALL offload_create_buffer(task_list%buffer_size_recv, task_list%pab_buffer)
631 17890 : CALL offload_create_buffer(task_list%buffer_size_recv, task_list%hab_buffer)
632 :
633 35780 : END SUBROUTINE create_grid_task_list
634 :
635 : ! **************************************************************************************************
636 : !> \brief ...
637 : !> \param tasks ...
638 : !> \param ntasks ...
639 : !> \param curr_tasks ...
640 : !> \param rs_descs ...
641 : !> \param dft_control ...
642 : !> \param cube_info ...
643 : !> \param gridlevel_info ...
644 : !> \param cindex ...
645 : !> \param iatom ...
646 : !> \param jatom ...
647 : !> \param rpgfa ...
648 : !> \param rpgfb ...
649 : !> \param zeta ...
650 : !> \param zetb ...
651 : !> \param kind_radius_b ...
652 : !> \param set_radius_a ...
653 : !> \param set_radius_b ...
654 : !> \param ra ...
655 : !> \param rab ...
656 : !> \param la_max ...
657 : !> \param la_min ...
658 : !> \param lb_max ...
659 : !> \param lb_min ...
660 : !> \param npgfa ...
661 : !> \param npgfb ...
662 : !> \param nseta ...
663 : !> \param nsetb ...
664 : !> \par History
665 : !> Joost VandeVondele: 10.2008 refactored
666 : ! **************************************************************************************************
667 1946988 : SUBROUTINE task_list_inner_loop(tasks, ntasks, curr_tasks, rs_descs, dft_control, &
668 : cube_info, gridlevel_info, cindex, &
669 : iatom, jatom, rpgfa, rpgfb, zeta, zetb, kind_radius_b, set_radius_a, set_radius_b, ra, rab, &
670 : la_max, la_min, lb_max, lb_min, npgfa, npgfb, nseta, nsetb)
671 :
672 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
673 : INTEGER :: ntasks, curr_tasks
674 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
675 : POINTER :: rs_descs
676 : TYPE(dft_control_type), POINTER :: dft_control
677 : TYPE(cube_info_type), DIMENSION(:), POINTER :: cube_info
678 : TYPE(gridlevel_info_type), POINTER :: gridlevel_info
679 : INTEGER :: cindex, iatom, jatom
680 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: rpgfa, rpgfb, zeta, zetb
681 : REAL(KIND=dp) :: kind_radius_b
682 : REAL(KIND=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
683 : REAL(KIND=dp), DIMENSION(3) :: ra, rab
684 : INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, npgfa, &
685 : npgfb
686 : INTEGER :: nseta, nsetb
687 :
688 : INTEGER :: cube_center(3), igrid_level, ipgf, iset, &
689 : jpgf, jset, lb_cube(3), ub_cube(3)
690 : REAL(KIND=dp) :: dab, rab2, radius, zetp
691 :
692 1946988 : rab2 = rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3)
693 1946988 : dab = SQRT(rab2)
694 :
695 5744915 : loop_iset: DO iset = 1, nseta
696 :
697 3797927 : IF (set_radius_a(iset) + kind_radius_b < dab) CYCLE loop_iset
698 :
699 11443272 : loop_jset: DO jset = 1, nsetb
700 :
701 6723645 : IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE loop_jset
702 :
703 19478699 : loop_ipgf: DO ipgf = 1, npgfa(iset)
704 :
705 11357773 : IF (rpgfa(ipgf, iset) + set_radius_b(jset) < dab) CYCLE loop_ipgf
706 :
707 34025980 : loop_jpgf: DO jpgf = 1, npgfb(jset)
708 :
709 20593642 : IF (rpgfa(ipgf, iset) + rpgfb(jpgf, jset) < dab) CYCLE loop_jpgf
710 :
711 10917064 : zetp = zeta(ipgf, iset) + zetb(jpgf, jset)
712 10917064 : igrid_level = gaussian_gridlevel(gridlevel_info, zetp)
713 :
714 : CALL compute_pgf_properties(cube_center, lb_cube, ub_cube, radius, &
715 : rs_descs(igrid_level)%rs_desc, cube_info(igrid_level), &
716 : la_max(iset), zeta(ipgf, iset), la_min(iset), &
717 : lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
718 10917064 : ra, rab, rab2, dft_control%qs_control%eps_rho_rspace)
719 :
720 : CALL pgf_to_tasks(tasks, ntasks, curr_tasks, &
721 : rab, cindex, iatom, jatom, iset, jset, ipgf, jpgf, &
722 : la_max(iset), lb_max(jset), rs_descs(igrid_level)%rs_desc, &
723 : igrid_level, gridlevel_info%ngrid_levels, cube_center, &
724 31951415 : lb_cube, ub_cube, radius)
725 :
726 : END DO loop_jpgf
727 :
728 : END DO loop_ipgf
729 :
730 : END DO loop_jset
731 :
732 : END DO loop_iset
733 :
734 1946988 : END SUBROUTINE task_list_inner_loop
735 :
736 : ! **************************************************************************************************
737 : !> \brief combines the calculation of several basic properties of a given pgf:
738 : !> its center, the bounding cube, the radius, the cost,
739 : !> tries to predict the time needed for processing this task
740 : !> in this way an improved load balance might be obtained
741 : !> \param cube_center ...
742 : !> \param lb_cube ...
743 : !> \param ub_cube ...
744 : !> \param radius ...
745 : !> \param rs_desc ...
746 : !> \param cube_info ...
747 : !> \param la_max ...
748 : !> \param zeta ...
749 : !> \param la_min ...
750 : !> \param lb_max ...
751 : !> \param zetb ...
752 : !> \param lb_min ...
753 : !> \param ra ...
754 : !> \param rab ...
755 : !> \param rab2 ...
756 : !> \param eps ...
757 : !> \par History
758 : !> 10.2008 refactored [Joost VandeVondele]
759 : !> \note
760 : !> -) this requires the radius to be computed in the same way as
761 : !> collocate_pgf_product, we should factor that part into a subroutine
762 : !> -) we're assuming that integrate_pgf and collocate_pgf are the same cost for load balancing
763 : !> this is more or less true for map_consistent
764 : !> -) in principle, the computed radius could be recycled in integrate_pgf/collocate_pgf if it is certainly
765 : !> the same, this could lead to a small speedup
766 : !> -) the cost function is a fit through the median cost of mapping a pgf with a given l and a given radius (in grid points)
767 : !> fitting the measured data on an opteron/g95 using the expression
768 : !> a*(l+b)(r+c)**3+d which is based on the innerloop of the collocating routines
769 : ! **************************************************************************************************
770 10917064 : SUBROUTINE compute_pgf_properties(cube_center, lb_cube, ub_cube, radius, &
771 : rs_desc, cube_info, la_max, zeta, la_min, lb_max, zetb, lb_min, ra, rab, rab2, eps)
772 :
773 : INTEGER, DIMENSION(3), INTENT(OUT) :: cube_center, lb_cube, ub_cube
774 : REAL(KIND=dp), INTENT(OUT) :: radius
775 : TYPE(realspace_grid_desc_type), POINTER :: rs_desc
776 : TYPE(cube_info_type), INTENT(IN) :: cube_info
777 : INTEGER, INTENT(IN) :: la_max
778 : REAL(KIND=dp), INTENT(IN) :: zeta
779 : INTEGER, INTENT(IN) :: la_min, lb_max
780 : REAL(KIND=dp), INTENT(IN) :: zetb
781 : INTEGER, INTENT(IN) :: lb_min
782 : REAL(KIND=dp), INTENT(IN) :: ra(3), rab(3), rab2, eps
783 :
784 : INTEGER :: extent(3)
785 10917064 : INTEGER, DIMENSION(:), POINTER :: sphere_bounds
786 : REAL(KIND=dp) :: cutoff, f, prefactor, rb(3), zetp
787 : REAL(KIND=dp), DIMENSION(3) :: rp
788 :
789 : ! the radius for this task
790 :
791 10917064 : zetp = zeta + zetb
792 43668256 : rp(:) = ra(:) + zetb/zetp*rab(:)
793 43668256 : rb(:) = ra(:) + rab(:)
794 10917064 : cutoff = 1.0_dp
795 10917064 : f = zetb/zetp
796 10917064 : prefactor = EXP(-zeta*f*rab2)
797 : radius = exp_radius_very_extended(la_min, la_max, lb_min, lb_max, ra=ra, rb=rb, rp=rp, &
798 10917064 : zetp=zetp, eps=eps, prefactor=prefactor, cutoff=cutoff)
799 :
800 10917064 : CALL compute_cube_center(cube_center, rs_desc, zeta, zetb, ra, rab)
801 : ! compute cube_center, the center of the gaussian product to map (folded to within the unit cell)
802 43668256 : cube_center(:) = MODULO(cube_center(:), rs_desc%npts(:))
803 43668256 : cube_center(:) = cube_center(:) + rs_desc%lb(:)
804 :
805 10917064 : IF (rs_desc%orthorhombic) THEN
806 10171331 : CALL return_cube(cube_info, radius, lb_cube, ub_cube, sphere_bounds)
807 : ELSE
808 745733 : CALL return_cube_nonortho(cube_info, radius, lb_cube, ub_cube, rp)
809 : !? unclear if extent is computed correctly.
810 2982932 : extent(:) = ub_cube(:) - lb_cube(:)
811 2982932 : lb_cube(:) = -extent(:)/2 - 1
812 2982932 : ub_cube(:) = extent(:)/2
813 : END IF
814 :
815 10917064 : END SUBROUTINE compute_pgf_properties
816 : ! **************************************************************************************************
817 : !> \brief predicts the cost of a task in kcycles for a given task
818 : !> the model is based on a fit of actual data, and might need updating
819 : !> as collocate_pgf_product changes (or CPUs/compilers change)
820 : !> maybe some dynamic approach, improving the cost model on the fly could
821 : !> work as well
822 : !> the cost model does not yet take into account the fraction of space
823 : !> that is mapped locally for a given cube and rs_grid (generalised tasks)
824 : !> \param lb_cube ...
825 : !> \param ub_cube ...
826 : !> \param fraction ...
827 : !> \param lmax ...
828 : !> \param is_ortho ...
829 : !> \return ...
830 : ! **************************************************************************************************
831 10917064 : INTEGER FUNCTION cost_model(lb_cube, ub_cube, fraction, lmax, is_ortho)
832 : INTEGER, DIMENSION(3), INTENT(IN) :: lb_cube, ub_cube
833 : REAL(KIND=dp), INTENT(IN) :: fraction
834 : INTEGER :: lmax
835 : LOGICAL :: is_ortho
836 :
837 : INTEGER :: cmax
838 : REAL(KIND=dp) :: v1, v2, v3, v4, v5
839 :
840 43668256 : cmax = MAXVAL(((ub_cube - lb_cube) + 1)/2)
841 :
842 10917064 : IF (is_ortho) THEN
843 : v1 = 1.504760E+00_dp
844 : v2 = 3.126770E+00_dp
845 : v3 = 5.074106E+00_dp
846 : v4 = 1.091568E+00_dp
847 : v5 = 1.070187E+00_dp
848 : ELSE
849 745733 : v1 = 7.831105E+00_dp
850 745733 : v2 = 2.675174E+00_dp
851 745733 : v3 = 7.546553E+00_dp
852 745733 : v4 = 6.122446E-01_dp
853 745733 : v5 = 3.886382E+00_dp
854 : END IF
855 10917064 : cost_model = CEILING(((lmax + v1)*(cmax + v2)**3*v3*fraction + v4 + v5*lmax**7)/1000.0_dp)
856 :
857 10917064 : END FUNCTION cost_model
858 : ! **************************************************************************************************
859 : !> \brief pgf_to_tasks converts a given pgf to one or more tasks, in particular
860 : !> this determines by which CPUs a given pgf gets collocated
861 : !> the format of the task array is as follows
862 : !> tasks(1,i) := destination
863 : !> tasks(2,i) := source
864 : !> tasks(3,i) := compressed type (iatom, jatom, ....)
865 : !> tasks(4,i) := type (0: replicated, 1: distributed local, 2: distributed generalised)
866 : !> tasks(5,i) := cost
867 : !> tasks(6,i) := alternate destination code (0 if none available)
868 : !>
869 : !> \param tasks ...
870 : !> \param ntasks ...
871 : !> \param curr_tasks ...
872 : !> \param rab ...
873 : !> \param cindex ...
874 : !> \param iatom ...
875 : !> \param jatom ...
876 : !> \param iset ...
877 : !> \param jset ...
878 : !> \param ipgf ...
879 : !> \param jpgf ...
880 : !> \param la_max ...
881 : !> \param lb_max ...
882 : !> \param rs_desc ...
883 : !> \param igrid_level ...
884 : !> \param n_levels ...
885 : !> \param cube_center ...
886 : !> \param lb_cube ...
887 : !> \param ub_cube ...
888 : !> \par History
889 : !> 10.2008 Refactored based on earlier routines by MattW [Joost VandeVondele]
890 : ! **************************************************************************************************
891 10917064 : SUBROUTINE pgf_to_tasks(tasks, ntasks, curr_tasks, &
892 : rab, cindex, iatom, jatom, iset, jset, ipgf, jpgf, &
893 : la_max, lb_max, rs_desc, igrid_level, n_levels, &
894 : cube_center, lb_cube, ub_cube, radius)
895 :
896 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
897 : INTEGER, INTENT(INOUT) :: ntasks, curr_tasks
898 : REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rab
899 : INTEGER, INTENT(IN) :: cindex, iatom, jatom, iset, jset, ipgf, &
900 : jpgf, la_max, lb_max
901 : TYPE(realspace_grid_desc_type), POINTER :: rs_desc
902 : INTEGER, INTENT(IN) :: igrid_level, n_levels
903 : INTEGER, DIMENSION(3), INTENT(IN) :: cube_center, lb_cube, ub_cube
904 : REAL(KIND=dp), INTENT(IN) :: radius
905 :
906 : INTEGER, PARAMETER :: add_tasks = 1000
907 : REAL(kind=dp), PARAMETER :: mult_tasks = 2.0_dp
908 :
909 : INTEGER :: added_tasks, cost, j, lmax
910 : LOGICAL :: is_ortho
911 : REAL(KIND=dp) :: tfraction
912 :
913 : !$OMP SINGLE
914 10917064 : ntasks = ntasks + 1
915 10917064 : IF (ntasks > curr_tasks) THEN
916 4735 : curr_tasks = INT((curr_tasks + add_tasks)*mult_tasks)
917 4735 : CALL reallocate_tasks(tasks, curr_tasks)
918 : END IF
919 : !$OMP END SINGLE
920 :
921 10917064 : IF (rs_desc%distributed) THEN
922 :
923 : ! finds the node(s) that need to process this task
924 : ! on exit tasks(:)%dist_type is 1 for distributed tasks and 2 for generalised tasks
925 : CALL rs_find_node(rs_desc, igrid_level, n_levels, cube_center, &
926 1476 : ntasks=ntasks, tasks=tasks, lb_cube=lb_cube, ub_cube=ub_cube, added_tasks=added_tasks)
927 :
928 : ELSE
929 10915588 : tasks(ntasks)%destination = encode_rank(rs_desc%my_pos, igrid_level, n_levels)
930 10915588 : tasks(ntasks)%dist_type = 0
931 10915588 : tasks(ntasks)%subpatch_pattern = 0
932 10915588 : added_tasks = 1
933 : END IF
934 :
935 10917064 : lmax = la_max + lb_max
936 10917064 : is_ortho = (tasks(ntasks)%dist_type == 0 .OR. tasks(ntasks)%dist_type == 1) .AND. rs_desc%orthorhombic
937 : ! we assume the load is shared equally between processes dealing with a generalised Gaussian.
938 : ! this could be refined in the future
939 10917064 : tfraction = 1.0_dp/added_tasks
940 :
941 10917064 : cost = cost_model(lb_cube, ub_cube, tfraction, lmax, is_ortho)
942 :
943 21834128 : DO j = 1, added_tasks
944 10917064 : tasks(ntasks - added_tasks + j)%source = encode_rank(rs_desc%my_pos, igrid_level, n_levels)
945 10917064 : tasks(ntasks - added_tasks + j)%cost = cost
946 10917064 : tasks(ntasks - added_tasks + j)%grid_level = igrid_level
947 10917064 : tasks(ntasks - added_tasks + j)%image = cindex
948 10917064 : tasks(ntasks - added_tasks + j)%iatom = iatom
949 10917064 : tasks(ntasks - added_tasks + j)%jatom = jatom
950 10917064 : tasks(ntasks - added_tasks + j)%iset = iset
951 10917064 : tasks(ntasks - added_tasks + j)%jset = jset
952 10917064 : tasks(ntasks - added_tasks + j)%ipgf = ipgf
953 10917064 : tasks(ntasks - added_tasks + j)%jpgf = jpgf
954 43668256 : tasks(ntasks - added_tasks + j)%rab = rab
955 21834128 : tasks(ntasks - added_tasks + j)%radius = radius
956 : END DO
957 :
958 10917064 : END SUBROUTINE pgf_to_tasks
959 :
960 : ! **************************************************************************************************
961 : !> \brief performs load balancing of the tasks on the distributed grids
962 : !> \param tasks ...
963 : !> \param ntasks ...
964 : !> \param rs_descs ...
965 : !> \param grid_level ...
966 : !> \param natoms ...
967 : !> \par History
968 : !> created 2008-10-03 [Joost VandeVondele]
969 : ! **************************************************************************************************
970 56 : SUBROUTINE load_balance_distributed(tasks, ntasks, rs_descs, grid_level, natoms)
971 :
972 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
973 : INTEGER :: ntasks
974 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
975 : POINTER :: rs_descs
976 : INTEGER :: grid_level, natoms
977 :
978 : CHARACTER(LEN=*), PARAMETER :: routineN = 'load_balance_distributed'
979 :
980 : INTEGER :: handle
981 56 : INTEGER, DIMENSION(:, :, :), POINTER :: list
982 :
983 56 : CALL timeset(routineN, handle)
984 :
985 56 : NULLIFY (list)
986 : ! here we create for each cpu (0:ncpu-1) a list of possible destinations.
987 : ! if a destination would not be in this list, it is a bug
988 56 : CALL create_destination_list(list, rs_descs, grid_level)
989 :
990 : ! now, walk over the tasks, filling in the loads of each destination
991 56 : CALL compute_load_list(list, rs_descs, grid_level, tasks, ntasks, natoms, create_list=.TRUE.)
992 :
993 : ! optimize loads & fluxes
994 56 : CALL optimize_load_list(list, rs_descs(1)%rs_desc%group, rs_descs(1)%rs_desc%my_pos)
995 :
996 : ! now, walk over the tasks, using the list to set the destinations
997 56 : CALL compute_load_list(list, rs_descs, grid_level, tasks, ntasks, natoms, create_list=.FALSE.)
998 :
999 56 : DEALLOCATE (list)
1000 :
1001 56 : CALL timestop(handle)
1002 :
1003 56 : END SUBROUTINE load_balance_distributed
1004 :
1005 : ! **************************************************************************************************
1006 : !> \brief this serial routine adjusts the fluxes in the global list
1007 : !>
1008 : !> \param list_global ...
1009 : !> \par History
1010 : !> created 2008-10-06 [Joost VandeVondele]
1011 : ! **************************************************************************************************
1012 28 : SUBROUTINE balance_global_list(list_global)
1013 : INTEGER, DIMENSION(:, :, 0:) :: list_global
1014 :
1015 : CHARACTER(LEN=*), PARAMETER :: routineN = 'balance_global_list'
1016 : INTEGER, PARAMETER :: Max_Iter = 100
1017 : REAL(KIND=dp), PARAMETER :: Tolerance_factor = 0.005_dp
1018 :
1019 : INTEGER :: dest, handle, icpu, idest, iflux, &
1020 : ilocal, k, maxdest, Ncpu, Nflux
1021 28 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: flux_connections
1022 : LOGICAL :: solution_optimal
1023 : REAL(KIND=dp) :: average, load_shift, max_load_shift, &
1024 : tolerance
1025 28 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: load, optimized_flux, optimized_load
1026 28 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: flux_limits
1027 :
1028 28 : CALL timeset(routineN, handle)
1029 :
1030 28 : Ncpu = SIZE(list_global, 3)
1031 28 : maxdest = SIZE(list_global, 2)
1032 84 : ALLOCATE (load(0:Ncpu - 1))
1033 28 : load = 0.0_dp
1034 56 : ALLOCATE (optimized_load(0:Ncpu - 1))
1035 :
1036 : ! figure out the number of fluxes
1037 : ! we assume that the global_list is symmetric
1038 84 : Nflux = 0
1039 84 : DO icpu = 0, ncpu - 1
1040 196 : DO idest = 1, maxdest
1041 112 : dest = list_global(1, idest, icpu)
1042 168 : IF (dest < ncpu .AND. dest > icpu) Nflux = Nflux + 1
1043 : END DO
1044 : END DO
1045 84 : ALLOCATE (optimized_flux(Nflux))
1046 84 : ALLOCATE (flux_limits(2, Nflux))
1047 84 : ALLOCATE (flux_connections(2, Nflux))
1048 :
1049 : ! reorder data
1050 28 : flux_limits = 0
1051 28 : Nflux = 0
1052 84 : DO icpu = 0, ncpu - 1
1053 168 : load(icpu) = SUM(list_global(2, :, icpu))
1054 196 : DO idest = 1, maxdest
1055 112 : dest = list_global(1, idest, icpu)
1056 168 : IF (dest < ncpu) THEN
1057 112 : IF (dest /= icpu) THEN
1058 56 : IF (dest > icpu) THEN
1059 28 : Nflux = Nflux + 1
1060 28 : flux_limits(2, Nflux) = list_global(2, idest, icpu)
1061 28 : flux_connections(1, Nflux) = icpu
1062 28 : flux_connections(2, Nflux) = dest
1063 : ELSE
1064 28 : DO iflux = 1, Nflux
1065 28 : IF (flux_connections(1, iflux) == dest .AND. flux_connections(2, iflux) == icpu) THEN
1066 28 : flux_limits(1, iflux) = -list_global(2, idest, icpu)
1067 28 : EXIT
1068 : END IF
1069 : END DO
1070 : END IF
1071 : END IF
1072 : END IF
1073 : END DO
1074 : END DO
1075 :
1076 28 : solution_optimal = .FALSE.
1077 28 : optimized_flux = 0.0_dp
1078 :
1079 : ! an iterative solver, if iterated till convergence the maximum load is minimal
1080 : ! we terminate before things are fully converged, since this does show up in the timings
1081 : ! once the largest shift becomes less than a small fraction of the average load, we're done
1082 : ! we're perfectly happy if the load balance is within 1 percent or so
1083 : ! the maximum load normally converges even faster
1084 84 : average = SUM(load)/SIZE(load)
1085 28 : tolerance = Tolerance_factor*average
1086 :
1087 84 : optimized_load(:) = load
1088 333 : DO k = 1, Max_iter
1089 : max_load_shift = 0.0_dp
1090 660 : DO iflux = 1, Nflux
1091 330 : load_shift = (optimized_load(flux_connections(1, iflux)) - optimized_load(flux_connections(2, iflux)))/2
1092 330 : load_shift = MAX(flux_limits(1, iflux) - optimized_flux(iflux), load_shift)
1093 330 : load_shift = MIN(flux_limits(2, iflux) - optimized_flux(iflux), load_shift)
1094 330 : max_load_shift = MAX(ABS(load_shift), max_load_shift)
1095 330 : optimized_load(flux_connections(1, iflux)) = optimized_load(flux_connections(1, iflux)) - load_shift
1096 330 : optimized_load(flux_connections(2, iflux)) = optimized_load(flux_connections(2, iflux)) + load_shift
1097 660 : optimized_flux(iflux) = optimized_flux(iflux) + load_shift
1098 : END DO
1099 333 : IF (max_load_shift < tolerance) THEN
1100 84 : solution_optimal = .TRUE.
1101 : EXIT
1102 : END IF
1103 : END DO
1104 :
1105 : ! now adjust the load list to reflect the optimized fluxes
1106 : ! reorder data
1107 : Nflux = 0
1108 84 : DO icpu = 0, ncpu - 1
1109 168 : DO idest = 1, maxdest
1110 168 : IF (list_global(1, idest, icpu) == icpu) ilocal = idest
1111 : END DO
1112 196 : DO idest = 1, maxdest
1113 112 : dest = list_global(1, idest, icpu)
1114 168 : IF (dest < ncpu) THEN
1115 112 : IF (dest /= icpu) THEN
1116 56 : IF (dest > icpu) THEN
1117 28 : Nflux = Nflux + 1
1118 28 : IF (optimized_flux(Nflux) > 0) THEN
1119 : list_global(2, ilocal, icpu) = list_global(2, ilocal, icpu) + &
1120 0 : list_global(2, idest, icpu) - NINT(optimized_flux(Nflux))
1121 0 : list_global(2, idest, icpu) = NINT(optimized_flux(Nflux))
1122 : ELSE
1123 : list_global(2, ilocal, icpu) = list_global(2, ilocal, icpu) + &
1124 28 : list_global(2, idest, icpu)
1125 28 : list_global(2, idest, icpu) = 0
1126 : END IF
1127 : ELSE
1128 28 : DO iflux = 1, Nflux
1129 28 : IF (flux_connections(1, iflux) == dest .AND. flux_connections(2, iflux) == icpu) THEN
1130 28 : IF (optimized_flux(iflux) > 0) THEN
1131 : list_global(2, ilocal, icpu) = list_global(2, ilocal, icpu) + &
1132 0 : list_global(2, idest, icpu)
1133 0 : list_global(2, idest, icpu) = 0
1134 : ELSE
1135 : list_global(2, ilocal, icpu) = list_global(2, ilocal, icpu) + &
1136 28 : list_global(2, idest, icpu) + NINT(optimized_flux(iflux))
1137 28 : list_global(2, idest, icpu) = -NINT(optimized_flux(iflux))
1138 : END IF
1139 : EXIT
1140 : END IF
1141 : END DO
1142 : END IF
1143 : END IF
1144 : END IF
1145 : END DO
1146 : END DO
1147 :
1148 28 : CALL timestop(handle)
1149 :
1150 56 : END SUBROUTINE balance_global_list
1151 :
1152 : ! **************************************************************************************************
1153 : !> \brief this routine gets back optimized loads for all destinations
1154 : !>
1155 : !> \param list ...
1156 : !> \param group ...
1157 : !> \param my_pos ...
1158 : !> \par History
1159 : !> created 2008-10-06 [Joost VandeVondele]
1160 : !> Modified 2016-01 [EPCC] Reduce memory requirements on P processes
1161 : !> from O(P^2) to O(P)
1162 : ! **************************************************************************************************
1163 56 : SUBROUTINE optimize_load_list(list, group, my_pos)
1164 : INTEGER, DIMENSION(:, :, 0:) :: list
1165 : TYPE(mp_comm_type), INTENT(IN) :: group
1166 : INTEGER, INTENT(IN) :: my_pos
1167 :
1168 : CHARACTER(LEN=*), PARAMETER :: routineN = 'optimize_load_list'
1169 : INTEGER, PARAMETER :: rank_of_root = 0
1170 :
1171 : INTEGER :: handle, icpu, idest, maxdest, ncpu
1172 : INTEGER, ALLOCATABLE, DIMENSION(:) :: load_all
1173 56 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: load_partial
1174 : INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: list_global
1175 :
1176 56 : CALL timeset(routineN, handle)
1177 :
1178 56 : ncpu = SIZE(list, 3)
1179 56 : maxdest = SIZE(list, 2)
1180 :
1181 : !find total workload ...
1182 168 : ALLOCATE (load_all(maxdest*ncpu))
1183 112 : load_all(:) = RESHAPE(list(2, :, :), [maxdest*ncpu])
1184 56 : CALL group%sum(load_all(:), rank_of_root)
1185 :
1186 : ! ... and optimise the work per process
1187 224 : ALLOCATE (list_global(2, maxdest, ncpu))
1188 56 : IF (rank_of_root == my_pos) THEN
1189 196 : list_global(1, :, :) = list(1, :, :)
1190 252 : list_global(2, :, :) = RESHAPE(load_all, [maxdest, ncpu])
1191 28 : CALL balance_global_list(list_global)
1192 : END IF
1193 56 : CALL group%bcast(list_global, rank_of_root)
1194 :
1195 : !figure out how much can be sent to other processes
1196 224 : ALLOCATE (load_partial(maxdest, ncpu))
1197 : ! send 'load_all', which is a copy of 'list' (but without leading dimension/stride)
1198 168 : CALL group%sum_partial(RESHAPE(load_all, [maxdest, ncpu]), load_partial(:, :))
1199 :
1200 168 : DO icpu = 1, ncpu
1201 392 : DO idest = 1, maxdest
1202 :
1203 : !need to deduct 1 because `list' was passed in to this routine as being indexed from zero
1204 336 : IF (load_partial(idest, icpu) > list_global(2, idest, icpu)) THEN
1205 38 : IF (load_partial(idest, icpu) - list(2, idest, icpu - 1) < list_global(2, idest, icpu)) THEN
1206 : list(2, idest, icpu - 1) = list_global(2, idest, icpu) &
1207 10 : - (load_partial(idest, icpu) - list(2, idest, icpu - 1))
1208 : ELSE
1209 28 : list(2, idest, icpu - 1) = 0
1210 : END IF
1211 : END IF
1212 :
1213 : END DO
1214 : END DO
1215 :
1216 : !clean up before leaving
1217 56 : DEALLOCATE (load_all)
1218 56 : DEALLOCATE (list_global)
1219 56 : DEALLOCATE (load_partial)
1220 :
1221 56 : CALL timestop(handle)
1222 56 : END SUBROUTINE optimize_load_list
1223 :
1224 : ! **************************************************************************************************
1225 : !> \brief fill the load list with values derived from the tasks array
1226 : !> from the alternate locations, we select the alternate location that
1227 : !> can be used without increasing the number of matrix blocks needed to
1228 : !> distribute.
1229 : !> Replicated tasks are not yet considered
1230 : !>
1231 : !> \param list ...
1232 : !> \param rs_descs ...
1233 : !> \param grid_level ...
1234 : !> \param tasks ...
1235 : !> \param ntasks ...
1236 : !> \param natoms ...
1237 : !> \param create_list ...
1238 : !> \par History
1239 : !> created 2008-10-06 [Joost VandeVondele]
1240 : ! **************************************************************************************************
1241 112 : SUBROUTINE compute_load_list(list, rs_descs, grid_level, tasks, ntasks, natoms, create_list)
1242 : INTEGER, DIMENSION(:, :, 0:) :: list
1243 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
1244 : POINTER :: rs_descs
1245 : INTEGER :: grid_level
1246 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
1247 : INTEGER :: ntasks, natoms
1248 : LOGICAL :: create_list
1249 :
1250 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_load_list'
1251 :
1252 : INTEGER :: cost, dest, handle, i, iatom, ilevel, img, img_old, iopt, ipgf, iset, itask, &
1253 : itask_start, itask_stop, jatom, jpgf, jset, li, maxdest, ncpu, ndest_pair, nopt, nshort, &
1254 : rank
1255 : INTEGER(KIND=int_8) :: bit_pattern, ipair, ipair_old, natom8
1256 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:) :: loads
1257 112 : INTEGER, ALLOCATABLE, DIMENSION(:) :: all_dests, index
1258 : INTEGER, DIMENSION(6) :: options
1259 :
1260 112 : CALL timeset(routineN, handle)
1261 :
1262 336 : ALLOCATE (loads(0:rs_descs(grid_level)%rs_desc%group_size - 1))
1263 112 : CALL get_current_loads(loads, rs_descs, grid_level, ntasks, tasks, use_reordered_ranks=.FALSE.)
1264 :
1265 112 : maxdest = SIZE(list, 2)
1266 112 : ncpu = SIZE(list, 3)
1267 112 : natom8 = natoms
1268 :
1269 : ! first find the tasks that deal with the same atom pair
1270 112 : itask_stop = 0
1271 112 : ipair_old = HUGE(ipair_old)
1272 112 : img_old = -1
1273 112 : ALLOCATE (all_dests(0))
1274 112 : ALLOCATE (INDEX(0))
1275 :
1276 : DO
1277 :
1278 : ! first find the range of tasks that deal with the same atom pair
1279 306 : itask_start = itask_stop + 1
1280 306 : itask_stop = itask_start
1281 306 : IF (itask_stop > ntasks) EXIT
1282 194 : ilevel = tasks(itask_stop)%grid_level
1283 194 : img_old = tasks(itask_stop)%image
1284 194 : iatom = tasks(itask_stop)%iatom
1285 194 : jatom = tasks(itask_stop)%jatom
1286 194 : iset = tasks(itask_stop)%iset
1287 194 : jset = tasks(itask_stop)%jset
1288 194 : ipgf = tasks(itask_stop)%ipgf
1289 194 : jpgf = tasks(itask_stop)%jpgf
1290 :
1291 194 : ipair_old = (iatom - 1)*natom8 + (jatom - 1)
1292 : DO
1293 7842 : IF (itask_stop + 1 > ntasks) EXIT
1294 7750 : ilevel = tasks(itask_stop + 1)%grid_level
1295 7750 : img = tasks(itask_stop + 1)%image
1296 7750 : iatom = tasks(itask_stop + 1)%iatom
1297 7750 : jatom = tasks(itask_stop + 1)%jatom
1298 7750 : iset = tasks(itask_stop + 1)%iset
1299 7750 : jset = tasks(itask_stop + 1)%jset
1300 7750 : ipgf = tasks(itask_stop + 1)%ipgf
1301 7750 : jpgf = tasks(itask_stop + 1)%jpgf
1302 :
1303 7750 : ipair = (iatom - 1)*natom8 + (jatom - 1)
1304 7842 : IF (ipair == ipair_old .AND. img == img_old) THEN
1305 : itask_stop = itask_stop + 1
1306 : ELSE
1307 : EXIT
1308 : END IF
1309 : END DO
1310 194 : ipair = ipair_old
1311 194 : nshort = itask_stop - itask_start + 1
1312 :
1313 : ! find the unique list of destinations on this grid level only
1314 194 : DEALLOCATE (all_dests)
1315 582 : ALLOCATE (all_dests(nshort))
1316 194 : DEALLOCATE (index)
1317 388 : ALLOCATE (INDEX(nshort))
1318 8036 : DO i = 1, nshort
1319 7842 : ilevel = tasks(itask_start + i - 1)%grid_level
1320 7842 : img = tasks(itask_start + i - 1)%image
1321 7842 : iatom = tasks(itask_start + i - 1)%iatom
1322 7842 : jatom = tasks(itask_start + i - 1)%jatom
1323 7842 : iset = tasks(itask_start + i - 1)%iset
1324 7842 : jset = tasks(itask_start + i - 1)%jset
1325 7842 : ipgf = tasks(itask_start + i - 1)%ipgf
1326 7842 : jpgf = tasks(itask_start + i - 1)%jpgf
1327 :
1328 8036 : IF (ilevel == grid_level) THEN
1329 2952 : all_dests(i) = decode_rank(tasks(itask_start + i - 1)%destination, SIZE(rs_descs))
1330 : ELSE
1331 4890 : all_dests(i) = HUGE(all_dests(i))
1332 : END IF
1333 : END DO
1334 194 : CALL sort(all_dests, nshort, index)
1335 194 : ndest_pair = 1
1336 7842 : DO i = 2, nshort
1337 7842 : IF ((all_dests(ndest_pair) /= all_dests(i)) .AND. (all_dests(i) /= HUGE(all_dests(i)))) THEN
1338 10 : ndest_pair = ndest_pair + 1
1339 10 : all_dests(ndest_pair) = all_dests(i)
1340 : END IF
1341 : END DO
1342 :
1343 8148 : DO itask = itask_start, itask_stop
1344 :
1345 7842 : dest = decode_rank(tasks(itask)%destination, SIZE(rs_descs)) ! notice that dest can be changed
1346 7842 : ilevel = tasks(itask)%grid_level
1347 7842 : img = tasks(itask)%image
1348 7842 : iatom = tasks(itask)%iatom
1349 7842 : jatom = tasks(itask)%jatom
1350 7842 : iset = tasks(itask)%iset
1351 7842 : jset = tasks(itask)%jset
1352 7842 : ipgf = tasks(itask)%ipgf
1353 7842 : jpgf = tasks(itask)%jpgf
1354 :
1355 : ! Only proceed with tasks which are on this grid level
1356 7842 : IF (ilevel /= grid_level) CYCLE
1357 2952 : ipair = (iatom - 1)*natom8 + (jatom - 1)
1358 2952 : cost = INT(tasks(itask)%cost)
1359 :
1360 194 : SELECT CASE (tasks(itask)%dist_type)
1361 : CASE (1)
1362 2952 : bit_pattern = tasks(itask)%subpatch_pattern
1363 2952 : nopt = 0
1364 2952 : IF (BTEST(bit_pattern, 0)) THEN
1365 0 : rank = rs_grid_locate_rank(rs_descs(ilevel)%rs_desc, dest, [-1, 0, 0])
1366 0 : IF (ANY(all_dests(1:ndest_pair) == rank)) THEN
1367 0 : nopt = nopt + 1
1368 0 : options(nopt) = rank
1369 : END IF
1370 : END IF
1371 2952 : IF (BTEST(bit_pattern, 1)) THEN
1372 0 : rank = rs_grid_locate_rank(rs_descs(ilevel)%rs_desc, dest, [+1, 0, 0])
1373 0 : IF (ANY(all_dests(1:ndest_pair) == rank)) THEN
1374 0 : nopt = nopt + 1
1375 0 : options(nopt) = rank
1376 : END IF
1377 : END IF
1378 2952 : IF (BTEST(bit_pattern, 2)) THEN
1379 48 : rank = rs_grid_locate_rank(rs_descs(ilevel)%rs_desc, dest, [0, -1, 0])
1380 96 : IF (ANY(all_dests(1:ndest_pair) == rank)) THEN
1381 0 : nopt = nopt + 1
1382 0 : options(nopt) = rank
1383 : END IF
1384 : END IF
1385 2952 : IF (BTEST(bit_pattern, 3)) THEN
1386 0 : rank = rs_grid_locate_rank(rs_descs(ilevel)%rs_desc, dest, [0, +1, 0])
1387 0 : IF (ANY(all_dests(1:ndest_pair) == rank)) THEN
1388 0 : nopt = nopt + 1
1389 0 : options(nopt) = rank
1390 : END IF
1391 : END IF
1392 2952 : IF (BTEST(bit_pattern, 4)) THEN
1393 1342 : rank = rs_grid_locate_rank(rs_descs(ilevel)%rs_desc, dest, [0, 0, -1])
1394 2584 : IF (ANY(all_dests(1:ndest_pair) == rank)) THEN
1395 100 : nopt = nopt + 1
1396 100 : options(nopt) = rank
1397 : END IF
1398 : END IF
1399 2952 : IF (BTEST(bit_pattern, 5)) THEN
1400 500 : rank = rs_grid_locate_rank(rs_descs(ilevel)%rs_desc, dest, [0, 0, +1])
1401 1000 : IF (ANY(all_dests(1:ndest_pair) == rank)) THEN
1402 80 : nopt = nopt + 1
1403 80 : options(nopt) = rank
1404 : END IF
1405 : END IF
1406 2952 : IF (nopt > 0) THEN
1407 : ! set it to the rank with the lowest load
1408 180 : rank = options(1)
1409 180 : DO iopt = 2, nopt
1410 180 : IF (loads(rank) > loads(options(iopt))) rank = options(iopt)
1411 : END DO
1412 : ELSE
1413 2772 : rank = dest
1414 : END IF
1415 2952 : li = list_index(list, rank, dest)
1416 2952 : IF (create_list) THEN
1417 1476 : list(2, li, dest) = list(2, li, dest) + cost
1418 : ELSE
1419 1476 : IF (list(1, li, dest) == dest) THEN
1420 1386 : tasks(itask)%destination = encode_rank(dest, ilevel, SIZE(rs_descs))
1421 : ELSE
1422 90 : IF (list(2, li, dest) >= cost) THEN
1423 10 : list(2, li, dest) = list(2, li, dest) - cost
1424 10 : tasks(itask)%destination = encode_rank(list(1, li, dest), ilevel, SIZE(rs_descs))
1425 : ELSE
1426 80 : tasks(itask)%destination = encode_rank(dest, ilevel, SIZE(rs_descs))
1427 : END IF
1428 : END IF
1429 : END IF
1430 : CASE (2) ! generalised
1431 0 : li = list_index(list, dest, dest)
1432 0 : IF (create_list) THEN
1433 0 : list(2, li, dest) = list(2, li, dest) + cost
1434 : ELSE
1435 0 : IF (list(1, li, dest) == dest) THEN
1436 0 : tasks(itask)%destination = encode_rank(dest, ilevel, SIZE(rs_descs))
1437 : ELSE
1438 0 : IF (list(2, li, dest) >= cost) THEN
1439 0 : list(2, li, dest) = list(2, li, dest) - cost
1440 0 : tasks(itask)%destination = encode_rank(list(1, li, dest), ilevel, SIZE(rs_descs))
1441 : ELSE
1442 0 : tasks(itask)%destination = encode_rank(dest, ilevel, SIZE(rs_descs))
1443 : END IF
1444 : END IF
1445 : END IF
1446 : CASE DEFAULT
1447 7842 : CPABORT("Unknown task list distribution type")
1448 : END SELECT
1449 :
1450 : END DO
1451 :
1452 : END DO
1453 :
1454 112 : CALL timestop(handle)
1455 :
1456 224 : END SUBROUTINE compute_load_list
1457 : ! **************************************************************************************************
1458 : !> \brief small helper function to return the proper index in the list array
1459 : !>
1460 : !> \param list ...
1461 : !> \param rank ...
1462 : !> \param dest ...
1463 : !> \return ...
1464 : !> \par History
1465 : !> created 2008-10-06 [Joost VandeVondele]
1466 : ! **************************************************************************************************
1467 2952 : INTEGER FUNCTION list_index(list, rank, dest)
1468 : INTEGER, DIMENSION(:, :, 0:), INTENT(IN) :: list
1469 : INTEGER, INTENT(IN) :: rank, dest
1470 :
1471 2952 : list_index = 1
1472 1424 : DO
1473 4376 : IF (list(1, list_index, dest) == rank) EXIT
1474 1424 : list_index = list_index + 1
1475 : END DO
1476 2952 : END FUNCTION list_index
1477 : ! **************************************************************************************************
1478 : !> \brief create a list with possible destinations (i.e. the central cpu and neighbors) for each cpu
1479 : !> note that we allocate it with an additional field to store the load of this destination
1480 : !>
1481 : !> \param list ...
1482 : !> \param rs_descs ...
1483 : !> \param grid_level ...
1484 : !> \par History
1485 : !> created 2008-10-06 [Joost VandeVondele]
1486 : ! **************************************************************************************************
1487 56 : SUBROUTINE create_destination_list(list, rs_descs, grid_level)
1488 : INTEGER, DIMENSION(:, :, :), POINTER :: list
1489 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
1490 : POINTER :: rs_descs
1491 : INTEGER, INTENT(IN) :: grid_level
1492 :
1493 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_destination_list'
1494 :
1495 : INTEGER :: handle, i, icpu, j, maxcount, ncpu, &
1496 : ultimate_max
1497 56 : INTEGER, ALLOCATABLE, DIMENSION(:) :: index, sublist
1498 :
1499 56 : CALL timeset(routineN, handle)
1500 :
1501 56 : CPASSERT(.NOT. ASSOCIATED(list))
1502 56 : ncpu = rs_descs(grid_level)%rs_desc%group_size
1503 56 : ultimate_max = 7
1504 :
1505 168 : ALLOCATE (list(2, ultimate_max, 0:ncpu - 1))
1506 :
1507 56 : ALLOCATE (INDEX(ultimate_max))
1508 56 : ALLOCATE (sublist(ultimate_max))
1509 448 : sublist = HUGE(sublist)
1510 :
1511 56 : maxcount = 1
1512 168 : DO icpu = 0, ncpu - 1
1513 112 : sublist(1) = icpu
1514 112 : sublist(2) = rs_grid_locate_rank(rs_descs(grid_level)%rs_desc, icpu, [-1, 0, 0])
1515 112 : sublist(3) = rs_grid_locate_rank(rs_descs(grid_level)%rs_desc, icpu, [+1, 0, 0])
1516 112 : sublist(4) = rs_grid_locate_rank(rs_descs(grid_level)%rs_desc, icpu, [0, -1, 0])
1517 112 : sublist(5) = rs_grid_locate_rank(rs_descs(grid_level)%rs_desc, icpu, [0, +1, 0])
1518 112 : sublist(6) = rs_grid_locate_rank(rs_descs(grid_level)%rs_desc, icpu, [0, 0, -1])
1519 112 : sublist(7) = rs_grid_locate_rank(rs_descs(grid_level)%rs_desc, icpu, [0, 0, +1])
1520 : ! only retain unique values of the destination
1521 112 : CALL sort(sublist, ultimate_max, index)
1522 112 : j = 1
1523 784 : DO i = 2, 7
1524 784 : IF (sublist(i) /= sublist(j)) THEN
1525 112 : j = j + 1
1526 112 : sublist(j) = sublist(i)
1527 : END IF
1528 : END DO
1529 112 : maxcount = MAX(maxcount, j)
1530 672 : sublist(j + 1:ultimate_max) = HUGE(sublist)
1531 896 : list(1, :, icpu) = sublist
1532 952 : list(2, :, icpu) = 0
1533 : END DO
1534 :
1535 56 : CALL reallocate(list, 1, 2, 1, maxcount, 0, ncpu - 1)
1536 :
1537 56 : CALL timestop(handle)
1538 :
1539 112 : END SUBROUTINE create_destination_list
1540 :
1541 : ! **************************************************************************************************
1542 : !> \brief given a task list, compute the load of each process everywhere
1543 : !> giving this function the ability to loop over a (sub)set of rs_grids,
1544 : !> and do all the communication in one shot, would speed it up
1545 : !> \param loads ...
1546 : !> \param rs_descs ...
1547 : !> \param grid_level ...
1548 : !> \param ntasks ...
1549 : !> \param tasks ...
1550 : !> \param use_reordered_ranks ...
1551 : !> \par History
1552 : !> none
1553 : !> \author MattW 21/11/2007
1554 : ! **************************************************************************************************
1555 500 : SUBROUTINE get_current_loads(loads, rs_descs, grid_level, ntasks, tasks, use_reordered_ranks)
1556 : INTEGER(KIND=int_8), DIMENSION(:) :: loads
1557 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
1558 : POINTER :: rs_descs
1559 : INTEGER :: grid_level, ntasks
1560 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
1561 : LOGICAL, INTENT(IN) :: use_reordered_ranks
1562 :
1563 : CHARACTER(LEN=*), PARAMETER :: routineN = 'get_current_loads'
1564 :
1565 : INTEGER :: handle, i, iatom, ilevel, img, ipgf, &
1566 : iset, jatom, jpgf, jset
1567 : INTEGER(KIND=int_8) :: total_cost_local
1568 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:) :: recv_buf_i, send_buf_i
1569 : TYPE(realspace_grid_desc_type), POINTER :: desc
1570 :
1571 500 : CALL timeset(routineN, handle)
1572 :
1573 500 : desc => rs_descs(grid_level)%rs_desc
1574 :
1575 : ! allocate local arrays
1576 1500 : ALLOCATE (send_buf_i(desc%group_size))
1577 1000 : ALLOCATE (recv_buf_i(desc%group_size))
1578 :
1579 : ! communication step 1 : compute the total local cost of the tasks
1580 : ! each proc needs to know the amount of work he will receive
1581 :
1582 : ! send buffer now contains for each target the cost of the tasks it will receive
1583 500 : send_buf_i = 0
1584 39092 : DO i = 1, ntasks
1585 38592 : ilevel = tasks(i)%grid_level
1586 38592 : img = tasks(i)%image
1587 38592 : iatom = tasks(i)%iatom
1588 38592 : jatom = tasks(i)%jatom
1589 38592 : iset = tasks(i)%iset
1590 38592 : jset = tasks(i)%jset
1591 38592 : ipgf = tasks(i)%ipgf
1592 38592 : jpgf = tasks(i)%jpgf
1593 38592 : IF (ilevel /= grid_level) CYCLE
1594 11144 : IF (use_reordered_ranks) THEN
1595 : send_buf_i(rs_descs(ilevel)%rs_desc%virtual2real(decode_rank(tasks(i)%destination, SIZE(rs_descs))) + 1) = &
1596 : send_buf_i(rs_descs(ilevel)%rs_desc%virtual2real(decode_rank(tasks(i)%destination, SIZE(rs_descs))) + 1) &
1597 3846 : + tasks(i)%cost
1598 : ELSE
1599 : send_buf_i(decode_rank(tasks(i)%destination, SIZE(rs_descs)) + 1) = &
1600 : send_buf_i(decode_rank(tasks(i)%destination, SIZE(rs_descs)) + 1) &
1601 6798 : + tasks(i)%cost
1602 : END IF
1603 : END DO
1604 500 : CALL desc%group%alltoall(send_buf_i, recv_buf_i, 1)
1605 :
1606 : ! communication step 2 : compute the global cost of the tasks
1607 1500 : total_cost_local = SUM(recv_buf_i)
1608 :
1609 : ! after this step, the recv buffer contains the local cost for each CPU
1610 1500 : CALL desc%group%allgather(total_cost_local, loads)
1611 :
1612 500 : CALL timestop(handle)
1613 :
1614 1000 : END SUBROUTINE get_current_loads
1615 : ! **************************************************************************************************
1616 : !> \brief performs load balancing shifting tasks on the replicated grids
1617 : !> this modifies the destination of some of the tasks on replicated
1618 : !> grids, and in this way balances the load
1619 : !> \param rs_descs ...
1620 : !> \param ntasks ...
1621 : !> \param tasks ...
1622 : !> \param redistributed true if any task changes rank; identical on all ranks
1623 : !> \par History
1624 : !> none
1625 : !> \author MattW 21/11/2007
1626 : ! **************************************************************************************************
1627 17890 : SUBROUTINE load_balance_replicated(rs_descs, ntasks, tasks, redistributed)
1628 :
1629 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
1630 : POINTER :: rs_descs
1631 : INTEGER :: ntasks
1632 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
1633 : LOGICAL, INTENT(OUT), OPTIONAL :: redistributed
1634 :
1635 : CHARACTER(LEN=*), PARAMETER :: routineN = 'load_balance_replicated'
1636 :
1637 : INTEGER :: handle, i, iatom, ilevel, img, ipgf, &
1638 : iset, j, jatom, jpgf, jset, &
1639 : no_overloaded, no_underloaded, &
1640 : proc_receiving, moved
1641 : INTEGER(KIND=int_8) :: average_cost, cost_task_rep, count, &
1642 : max_cost, offset, total_cost_global
1643 : LOGICAL :: fully_replicated
1644 17890 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:) :: load_imbalance, loads, recv_buf_i
1645 17890 : INTEGER, ALLOCATABLE, DIMENSION(:) :: index
1646 : TYPE(realspace_grid_desc_type), POINTER :: desc
1647 :
1648 17890 : CALL timeset(routineN, handle)
1649 :
1650 17890 : desc => rs_descs(1)%rs_desc
1651 :
1652 17890 : IF (PRESENT(redistributed)) redistributed = .FALSE.
1653 17890 : IF (desc%group_size == 1) THEN
1654 1226 : CALL timestop(handle)
1655 1226 : RETURN
1656 : END IF
1657 :
1658 : ! allocate local arrays
1659 49992 : ALLOCATE (recv_buf_i(desc%group_size))
1660 33328 : ALLOCATE (loads(desc%group_size))
1661 :
1662 16664 : fully_replicated = .TRUE.
1663 82562 : DO i = 1, SIZE(rs_descs)
1664 82756 : fully_replicated = fully_replicated .AND. .NOT. rs_descs(i)%rs_desc%distributed
1665 : END DO
1666 :
1667 16664 : IF (fully_replicated) THEN
1668 : ! These tasks still reside on their matrix owners. One gather suffices for all levels.
1669 16614 : cost_task_rep = 0
1670 9508922 : DO i = 1, ntasks
1671 9508922 : cost_task_rep = cost_task_rep + tasks(i)%cost
1672 : END DO
1673 16614 : CALL desc%group%allgather(cost_task_rep, recv_buf_i)
1674 : ELSE
1675 50 : recv_buf_i = 0
1676 244 : DO i = 1, SIZE(rs_descs)
1677 194 : CALL get_current_loads(loads, rs_descs, i, ntasks, tasks, use_reordered_ranks=.TRUE.)
1678 632 : recv_buf_i(:) = recv_buf_i + loads
1679 : END DO
1680 : END IF
1681 :
1682 49992 : total_cost_global = SUM(recv_buf_i)
1683 16664 : average_cost = total_cost_global/desc%group_size
1684 49992 : max_cost = MAXVAL(recv_buf_i)
1685 :
1686 16664 : IF (fully_replicated) THEN
1687 : ! No assignment can improve on the rounded-up mean load.
1688 16614 : IF (max_cost <= (total_cost_global + desc%group_size - 1)/desc%group_size) THEN
1689 522 : CALL timestop(handle)
1690 522 : RETURN
1691 : END IF
1692 : END IF
1693 :
1694 : !
1695 : ! compute how to redistribute the replicated tasks so that the average cost is reached
1696 : !
1697 :
1698 : ! load imbalance measures the load of a given CPU relative
1699 : ! to the optimal load distribution (load=average)
1700 48426 : ALLOCATE (load_imbalance(desc%group_size))
1701 48426 : ALLOCATE (INDEX(desc%group_size))
1702 :
1703 48426 : load_imbalance(:) = recv_buf_i - average_cost
1704 16142 : no_overloaded = 0
1705 16142 : no_underloaded = 0
1706 :
1707 48426 : DO i = 1, desc%group_size
1708 32284 : IF (load_imbalance(i) > 0) no_overloaded = no_overloaded + 1
1709 48426 : IF (load_imbalance(i) < 0) no_underloaded = no_underloaded + 1
1710 : END DO
1711 :
1712 : ! sort the recv_buffer on number of tasks, gives us index which provides a
1713 : ! mapping between processor ranks and how overloaded the processor
1714 16142 : CALL sort(recv_buf_i, SIZE(recv_buf_i), index)
1715 :
1716 : ! find out the number of replicated tasks each proc has
1717 : ! but only those tasks which have not yet been assigned
1718 16142 : cost_task_rep = 0
1719 6816116 : DO i = 1, ntasks
1720 : IF (tasks(i)%dist_type == 0 &
1721 6816116 : .AND. decode_rank(tasks(i)%destination, SIZE(rs_descs)) == decode_rank(tasks(i)%source, SIZE(rs_descs))) THEN
1722 6798498 : cost_task_rep = cost_task_rep + tasks(i)%cost
1723 : END IF
1724 : END DO
1725 :
1726 : ! now, correct the load imbalance for the overloaded CPUs
1727 : ! they will send away not more than the total load of replicated tasks
1728 16142 : CALL desc%group%allgather(cost_task_rep, recv_buf_i)
1729 :
1730 48426 : DO i = 1, desc%group_size
1731 : ! At the moment we can only offload replicated tasks
1732 48426 : IF (load_imbalance(i) > 0) load_imbalance(i) = MIN(load_imbalance(i), recv_buf_i(i))
1733 : END DO
1734 :
1735 : ! simplest algorithm I can think of of is that the processor with the most
1736 : ! excess tasks fills up the process needing most, then moves on to next most.
1737 : ! At the moment if we've got less replicated tasks than we're overloaded then
1738 : ! task balancing will be incomplete
1739 :
1740 : ! only need to do anything if I've excess tasks
1741 16142 : IF (load_imbalance(desc%my_pos + 1) > 0) THEN
1742 :
1743 8069 : count = 0 ! weighted amount of tasks offloaded
1744 8069 : offset = 0 ! no of underloaded processes already filled by other more overloaded procs
1745 :
1746 : ! calculate offset
1747 8069 : DO i = desc%group_size, desc%group_size - no_overloaded + 1, -1
1748 8069 : IF (INDEX(i) == desc%my_pos + 1) THEN
1749 : EXIT
1750 : ELSE
1751 0 : offset = offset + load_imbalance(INDEX(i))
1752 : END IF
1753 : END DO
1754 :
1755 : ! find my starting processor to send to
1756 8069 : proc_receiving = HUGE(proc_receiving)
1757 8069 : DO i = 1, no_underloaded
1758 8069 : offset = offset + load_imbalance(INDEX(i))
1759 8069 : IF (offset < 0 .OR. (.NOT. fully_replicated .AND. offset == 0)) THEN
1760 : proc_receiving = i
1761 : EXIT
1762 : END IF
1763 : END DO
1764 :
1765 : ! offset now contains minus the number of tasks proc_receiving requires
1766 : ! we fill this up by adjusting the destination of tasks on the replicated grid,
1767 : ! then move to next most underloaded proc
1768 578061 : DO j = 1, ntasks
1769 : IF (tasks(j)%dist_type == 0 &
1770 578061 : .AND. decode_rank(tasks(j)%destination, SIZE(rs_descs)) == decode_rank(tasks(j)%source, SIZE(rs_descs))) THEN
1771 : ! just avoid sending to non existing procs due to integer truncation
1772 : ! in the computation of the average
1773 577472 : IF (proc_receiving > no_underloaded) EXIT
1774 : ! set new destination
1775 577472 : ilevel = tasks(j)%grid_level
1776 577472 : img = tasks(j)%image
1777 577472 : iatom = tasks(j)%iatom
1778 577472 : jatom = tasks(j)%jatom
1779 577472 : iset = tasks(j)%iset
1780 577472 : jset = tasks(j)%jset
1781 577472 : ipgf = tasks(j)%ipgf
1782 577472 : jpgf = tasks(j)%jpgf
1783 577472 : tasks(j)%destination = encode_rank(INDEX(proc_receiving) - 1, ilevel, SIZE(rs_descs))
1784 577472 : offset = offset + tasks(j)%cost
1785 577472 : count = count + tasks(j)%cost
1786 577472 : IF (count >= load_imbalance(desc%my_pos + 1)) EXIT
1787 569446 : IF (offset > 0 .OR. (fully_replicated .AND. offset == 0)) THEN
1788 43 : proc_receiving = proc_receiving + 1
1789 : ! just avoid sending to non existing procs due to integer truncation
1790 : ! in the computation of the average
1791 43 : IF (proc_receiving > no_underloaded) EXIT
1792 0 : offset = load_imbalance(INDEX(proc_receiving))
1793 : END IF
1794 : END IF
1795 : END DO
1796 : END IF
1797 :
1798 16142 : DEALLOCATE (index)
1799 16142 : DEALLOCATE (load_imbalance)
1800 :
1801 16142 : IF (fully_replicated) THEN
1802 : ! Indivisible expensive tasks can defeat the greedy assignment. Keep only an improvement.
1803 16092 : loads = 0
1804 6812220 : DO i = 1, ntasks
1805 6796128 : j = decode_rank(tasks(i)%destination, SIZE(rs_descs)) + 1
1806 6812220 : loads(j) = loads(j) + tasks(i)%cost
1807 : END DO
1808 16092 : CALL desc%group%sum(loads)
1809 48276 : IF (MAXVAL(loads) >= max_cost) THEN
1810 141104 : DO i = 1, ntasks
1811 141104 : tasks(i)%destination = tasks(i)%source
1812 : END DO
1813 : END IF
1814 : END IF
1815 :
1816 16142 : IF (PRESENT(redistributed)) THEN
1817 16092 : moved = 0
1818 6812220 : DO i = 1, ntasks
1819 6812220 : IF (tasks(i)%destination /= tasks(i)%source) moved = 1
1820 : END DO
1821 16092 : CALL desc%group%max(moved)
1822 16092 : redistributed = moved > 0
1823 : END IF
1824 :
1825 16142 : CALL timestop(handle)
1826 :
1827 35780 : END SUBROUTINE load_balance_replicated
1828 :
1829 : ! **************************************************************************************************
1830 : !> \brief given an input task list, redistribute so that all tasks can be processed locally,
1831 : !> i.e. dest equals rank
1832 : !> \param rs_descs ...
1833 : !> \param ntasks ...
1834 : !> \param tasks ...
1835 : !> \param ntasks_recv ...
1836 : !> \param tasks_recv ...
1837 : !> \par History
1838 : !> none
1839 : !> \author MattW 21/11/2007
1840 : ! **************************************************************************************************
1841 15860 : SUBROUTINE create_local_tasks(rs_descs, ntasks, tasks, ntasks_recv, tasks_recv)
1842 :
1843 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
1844 : POINTER :: rs_descs
1845 : INTEGER :: ntasks
1846 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
1847 : INTEGER :: ntasks_recv
1848 : TYPE(task_type), DIMENSION(:), POINTER :: tasks_recv
1849 :
1850 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_local_tasks'
1851 :
1852 : INTEGER :: handle, i, j, k, l, rank
1853 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:) :: recv_buf, send_buf
1854 : INTEGER, ALLOCATABLE, DIMENSION(:) :: recv_disps, recv_sizes, send_disps, &
1855 : send_sizes
1856 : TYPE(realspace_grid_desc_type), POINTER :: desc
1857 :
1858 15860 : CALL timeset(routineN, handle)
1859 :
1860 15860 : desc => rs_descs(1)%rs_desc
1861 :
1862 : ! allocate local arrays
1863 47580 : ALLOCATE (send_sizes(desc%group_size))
1864 31720 : ALLOCATE (recv_sizes(desc%group_size))
1865 31720 : ALLOCATE (send_disps(desc%group_size))
1866 31720 : ALLOCATE (recv_disps(desc%group_size))
1867 47580 : ALLOCATE (send_buf(desc%group_size))
1868 31720 : ALLOCATE (recv_buf(desc%group_size))
1869 :
1870 : ! fill send buffer, now counting how many tasks will be send (stored in an int8 array for convenience only).
1871 15860 : send_buf = 0
1872 6675012 : DO i = 1, ntasks
1873 : rank = rs_descs(decode_level(tasks(i)%destination, SIZE(rs_descs))) &
1874 6659152 : %rs_desc%virtual2real(decode_rank(tasks(i)%destination, SIZE(rs_descs)))
1875 6675012 : send_buf(rank + 1) = send_buf(rank + 1) + 1
1876 : END DO
1877 :
1878 15860 : CALL desc%group%alltoall(send_buf, recv_buf, 1)
1879 :
1880 : ! pack the tasks, and send them around
1881 :
1882 15860 : send_sizes = 0
1883 15860 : send_disps = 0
1884 15860 : recv_sizes = 0
1885 15860 : recv_disps = 0
1886 :
1887 15860 : send_sizes(1) = INT(send_buf(1)*task_size_in_int8)
1888 15860 : recv_sizes(1) = INT(recv_buf(1)*task_size_in_int8)
1889 31720 : DO i = 2, desc%group_size
1890 15860 : send_sizes(i) = INT(send_buf(i)*task_size_in_int8)
1891 15860 : recv_sizes(i) = INT(recv_buf(i)*task_size_in_int8)
1892 15860 : send_disps(i) = send_disps(i - 1) + send_sizes(i - 1)
1893 31720 : recv_disps(i) = recv_disps(i - 1) + recv_sizes(i - 1)
1894 : END DO
1895 :
1896 : ! deallocate old send/recv buffers
1897 15860 : DEALLOCATE (send_buf)
1898 15860 : DEALLOCATE (recv_buf)
1899 :
1900 : ! allocate them with new sizes
1901 78962 : ALLOCATE (send_buf(SUM(send_sizes)))
1902 79298 : ALLOCATE (recv_buf(SUM(recv_sizes)))
1903 :
1904 : ! do packing
1905 15860 : send_buf = 0
1906 15860 : send_sizes = 0
1907 6675012 : DO j = 1, ntasks
1908 : i = rs_descs(decode_level(tasks(j)%destination, SIZE(rs_descs))) &
1909 6659152 : %rs_desc%virtual2real(decode_rank(tasks(j)%destination, SIZE(rs_descs))) + 1
1910 6659152 : l = send_disps(i) + send_sizes(i)
1911 6659152 : CALL serialize_task(tasks(j), send_buf(l + 1:l + task_size_in_int8))
1912 6675012 : send_sizes(i) = send_sizes(i) + task_size_in_int8
1913 : END DO
1914 :
1915 : ! do communication
1916 15860 : CALL desc%group%alltoall(send_buf, send_sizes, send_disps, recv_buf, recv_sizes, recv_disps)
1917 :
1918 15860 : DEALLOCATE (send_buf)
1919 :
1920 47580 : ntasks_recv = SUM(recv_sizes)/task_size_in_int8
1921 6754310 : ALLOCATE (tasks_recv(ntasks_recv))
1922 :
1923 : ! do unpacking
1924 15860 : l = 0
1925 47580 : DO i = 1, desc%group_size
1926 6706732 : DO j = 0, recv_sizes(i)/task_size_in_int8 - 1
1927 6659152 : l = l + 1
1928 6659152 : k = recv_disps(i) + j*task_size_in_int8
1929 6690872 : CALL deserialize_task(tasks_recv(l), recv_buf(k + 1:k + task_size_in_int8))
1930 : END DO
1931 : END DO
1932 :
1933 15860 : DEALLOCATE (recv_buf)
1934 15860 : DEALLOCATE (send_sizes)
1935 15860 : DEALLOCATE (recv_sizes)
1936 15860 : DEALLOCATE (send_disps)
1937 15860 : DEALLOCATE (recv_disps)
1938 :
1939 15860 : CALL timestop(handle)
1940 :
1941 15860 : END SUBROUTINE create_local_tasks
1942 :
1943 : ! **************************************************************************************************
1944 : !> \brief Assembles tasks to be performed on local grid
1945 : !> \param rs_descs the grids
1946 : !> \param ntasks Number of tasks for local processing
1947 : !> \param natoms ...
1948 : !> \param nimages ...
1949 : !> \param tasks the task set generated on this processor
1950 : !> \param rval ...
1951 : !> \param atom_pair_send ...
1952 : !> \param atom_pair_recv ...
1953 : !> \param symmetric ...
1954 : !> \param reorder_rs_grid_ranks ...
1955 : !> \param skip_load_balance_distributed ...
1956 : !> \param balance_replicated allow balancing when all grid levels are replicated (default true)
1957 : !> \par History
1958 : !> none
1959 : !> \author MattW 21/11/2007
1960 : ! **************************************************************************************************
1961 20176 : SUBROUTINE distribute_tasks(rs_descs, ntasks, natoms, &
1962 : tasks, atom_pair_send, atom_pair_recv, &
1963 : symmetric, reorder_rs_grid_ranks, skip_load_balance_distributed, &
1964 : balance_replicated)
1965 :
1966 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
1967 : POINTER :: rs_descs
1968 : INTEGER :: ntasks, natoms
1969 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
1970 : TYPE(atom_pair_type), DIMENSION(:), POINTER :: atom_pair_send, atom_pair_recv
1971 : LOGICAL, INTENT(IN) :: symmetric, reorder_rs_grid_ranks, &
1972 : skip_load_balance_distributed
1973 : LOGICAL, INTENT(IN), OPTIONAL :: balance_replicated
1974 :
1975 : CHARACTER(LEN=*), PARAMETER :: routineN = 'distribute_tasks'
1976 :
1977 : INTEGER :: handle, igrid_level, irank, ntasks_recv
1978 : INTEGER(KIND=int_8) :: load_gap, max_load, replicated_load
1979 20176 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:) :: total_loads, total_loads_tmp, trial_loads
1980 20176 : INTEGER(KIND=int_8), DIMENSION(:, :), POINTER :: loads
1981 20176 : INTEGER, ALLOCATABLE, DIMENSION(:) :: indices, real2virtual, total_index
1982 : LOGICAL :: distributed_grids, fixed_first_grid, &
1983 : redistribute_matrices, allow_balance
1984 : TYPE(realspace_grid_desc_type), POINTER :: desc
1985 20176 : TYPE(task_type), DIMENSION(:), POINTER :: tasks_recv
1986 :
1987 20176 : CALL timeset(routineN, handle)
1988 :
1989 20176 : CPASSERT(ASSOCIATED(tasks))
1990 :
1991 : ! *** figure out if we have distributed grids
1992 20176 : distributed_grids = .FALSE.
1993 100122 : DO igrid_level = 1, SIZE(rs_descs)
1994 100122 : IF (rs_descs(igrid_level)%rs_desc%distributed) THEN
1995 56 : distributed_grids = .TRUE.
1996 : END IF
1997 : END DO
1998 20176 : desc => rs_descs(1)%rs_desc
1999 :
2000 20176 : IF (distributed_grids) THEN
2001 :
2002 200 : ALLOCATE (loads(0:desc%group_size - 1, SIZE(rs_descs)))
2003 150 : ALLOCATE (total_loads(0:desc%group_size - 1))
2004 :
2005 50 : total_loads = 0
2006 :
2007 : ! First round of balancing on the distributed grids
2008 : ! we just balance each of the distributed grids independently
2009 244 : DO igrid_level = 1, SIZE(rs_descs)
2010 244 : IF (rs_descs(igrid_level)%rs_desc%distributed) THEN
2011 :
2012 56 : IF (.NOT. skip_load_balance_distributed) THEN
2013 56 : CALL load_balance_distributed(tasks, ntasks, rs_descs, igrid_level, natoms)
2014 : END IF
2015 :
2016 : CALL get_current_loads(loads(:, igrid_level), rs_descs, igrid_level, ntasks, &
2017 56 : tasks, use_reordered_ranks=.FALSE.)
2018 :
2019 168 : total_loads(:) = total_loads + loads(:, igrid_level)
2020 :
2021 : END IF
2022 : END DO
2023 :
2024 : ! calculate the total load of replicated tasks, so we can decide if it is worth
2025 : ! reordering the distributed grid levels
2026 :
2027 50 : replicated_load = 0
2028 244 : DO igrid_level = 1, SIZE(rs_descs)
2029 244 : IF (.NOT. rs_descs(igrid_level)%rs_desc%distributed) THEN
2030 : CALL get_current_loads(loads(:, igrid_level), rs_descs, igrid_level, ntasks, &
2031 138 : tasks, use_reordered_ranks=.FALSE.)
2032 414 : replicated_load = replicated_load + SUM(loads(:, igrid_level))
2033 : END IF
2034 : END DO
2035 :
2036 : ! Now we adjust the rank ordering based on the current loads
2037 : ! we leave the first distributed level and all the replicated levels in the default order
2038 50 : IF (reorder_rs_grid_ranks) THEN
2039 50 : fixed_first_grid = .FALSE.
2040 244 : DO igrid_level = 1, SIZE(rs_descs)
2041 244 : IF (rs_descs(igrid_level)%rs_desc%distributed) THEN
2042 56 : IF (fixed_first_grid .EQV. .FALSE.) THEN
2043 150 : total_loads(:) = loads(:, igrid_level)
2044 : fixed_first_grid = .TRUE.
2045 : ELSE
2046 18 : ALLOCATE (trial_loads(0:desc%group_size - 1))
2047 :
2048 18 : trial_loads(:) = total_loads + loads(:, igrid_level)
2049 18 : max_load = MAXVAL(trial_loads)
2050 : load_gap = 0
2051 18 : DO irank = 0, desc%group_size - 1
2052 18 : load_gap = load_gap + max_load - trial_loads(irank)
2053 : END DO
2054 :
2055 : ! If there is not enough replicated load to load balance well enough
2056 : ! then we will reorder this grid level
2057 6 : IF (load_gap > replicated_load*1.05_dp) THEN
2058 :
2059 18 : ALLOCATE (indices(0:desc%group_size - 1))
2060 12 : ALLOCATE (total_index(0:desc%group_size - 1))
2061 12 : ALLOCATE (total_loads_tmp(0:desc%group_size - 1))
2062 12 : ALLOCATE (real2virtual(0:desc%group_size - 1))
2063 :
2064 18 : total_loads_tmp(:) = total_loads
2065 6 : CALL sort(total_loads_tmp, desc%group_size, total_index)
2066 6 : CALL sort(loads(:, igrid_level), desc%group_size, indices)
2067 :
2068 : ! Reorder so that the rank with smallest load on this grid level is paired with
2069 : ! the highest load in total
2070 18 : DO irank = 0, desc%group_size - 1
2071 : total_loads(total_index(irank) - 1) = total_loads(total_index(irank) - 1) + &
2072 12 : loads(desc%group_size - irank - 1, igrid_level)
2073 18 : real2virtual(total_index(irank) - 1) = indices(desc%group_size - irank - 1) - 1
2074 : END DO
2075 :
2076 6 : CALL rs_grid_reorder_ranks(rs_descs(igrid_level)%rs_desc, real2virtual)
2077 :
2078 6 : DEALLOCATE (indices)
2079 6 : DEALLOCATE (total_index)
2080 6 : DEALLOCATE (total_loads_tmp)
2081 6 : DEALLOCATE (real2virtual)
2082 : ELSE
2083 0 : total_loads(:) = trial_loads
2084 : END IF
2085 :
2086 6 : DEALLOCATE (trial_loads)
2087 :
2088 : END IF
2089 : END IF
2090 : END DO
2091 : END IF
2092 :
2093 : ! Now we use the replicated tasks to balance out the rest of the load
2094 50 : CALL load_balance_replicated(rs_descs, ntasks, tasks)
2095 :
2096 50 : DEALLOCATE (loads)
2097 50 : DEALLOCATE (total_loads)
2098 : END IF
2099 :
2100 20176 : redistribute_matrices = distributed_grids
2101 20176 : allow_balance = .TRUE.
2102 20176 : IF (PRESENT(balance_replicated)) allow_balance = balance_replicated
2103 20176 : IF (.NOT. distributed_grids .AND. allow_balance) THEN
2104 17840 : CALL load_balance_replicated(rs_descs, ntasks, tasks, redistributed=redistribute_matrices)
2105 : END IF
2106 :
2107 20176 : IF (redistribute_matrices) THEN
2108 :
2109 : ! given a list of tasks, this will do the needed reshuffle so that all tasks will be local
2110 15860 : CALL create_local_tasks(rs_descs, ntasks, tasks, ntasks_recv, tasks_recv)
2111 :
2112 : !
2113 : ! tasks list are complete, we can compute the list of atomic blocks (atom pairs)
2114 : ! we will be sending. These lists are needed for redistribute_matrix.
2115 : !
2116 15860 : CALL get_atom_pair(atom_pair_send, tasks, ntasks=ntasks, send=.TRUE., symmetric=symmetric, rs_descs=rs_descs)
2117 :
2118 15860 : CALL get_atom_pair(atom_pair_recv, tasks_recv, ntasks=ntasks_recv, send=.FALSE., symmetric=symmetric, rs_descs=rs_descs)
2119 :
2120 : ! cleanup, at this point we don't need the original tasks anymore
2121 15860 : DEALLOCATE (tasks)
2122 :
2123 : ELSE
2124 4316 : tasks_recv => tasks
2125 4316 : ntasks_recv = ntasks
2126 4316 : CALL get_atom_pair(atom_pair_recv, tasks_recv, ntasks=ntasks_recv, send=.FALSE., symmetric=symmetric, rs_descs=rs_descs)
2127 : ! Tasks remain on matrix owners, so no matrix communication plan is needed.
2128 : END IF
2129 :
2130 : ! here we sort the task list we will process locally.
2131 60463 : ALLOCATE (indices(ntasks_recv))
2132 20176 : CALL tasks_sort(tasks_recv, ntasks_recv, indices)
2133 20176 : DEALLOCATE (indices)
2134 :
2135 : !
2136 : ! final lists are ready
2137 : !
2138 :
2139 20176 : tasks => tasks_recv
2140 20176 : ntasks = ntasks_recv
2141 :
2142 20176 : CALL timestop(handle)
2143 :
2144 40352 : END SUBROUTINE distribute_tasks
2145 :
2146 : ! **************************************************************************************************
2147 : !> \brief ...
2148 : !> \param atom_pair ...
2149 : !> \param my_tasks ...
2150 : !> \param send ...
2151 : !> \param symmetric ...
2152 : !> \param natoms ...
2153 : !> \param nimages ...
2154 : !> \param rs_descs ...
2155 : ! **************************************************************************************************
2156 36036 : SUBROUTINE get_atom_pair(atom_pair, tasks, ntasks, send, symmetric, rs_descs)
2157 :
2158 : TYPE(atom_pair_type), DIMENSION(:), POINTER :: atom_pair
2159 : TYPE(task_type), DIMENSION(:), INTENT(INOUT) :: tasks
2160 : INTEGER, INTENT(IN) :: ntasks
2161 : LOGICAL, INTENT(IN) :: send, symmetric
2162 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), INTENT(IN) :: rs_descs
2163 :
2164 : INTEGER :: i, ilevel, iatom, jatom, npairs, virt_rank
2165 36036 : INTEGER, DIMENSION(:), ALLOCATABLE :: indices
2166 36036 : TYPE(atom_pair_type), DIMENSION(:), ALLOCATABLE :: atom_pair_tmp
2167 :
2168 36036 : CPASSERT(.NOT. ASSOCIATED(atom_pair))
2169 36036 : IF (ntasks == 0) THEN
2170 403 : ALLOCATE (atom_pair(0))
2171 : RETURN
2172 : END IF
2173 :
2174 : ! calculate list of atom pairs
2175 : ! fill pair list taking into account symmetry
2176 17683115 : ALLOCATE (atom_pair_tmp(ntasks))
2177 17611849 : DO i = 1, ntasks
2178 17576216 : atom_pair_tmp(i)%image = tasks(i)%image
2179 17576216 : iatom = tasks(i)%iatom
2180 17576216 : jatom = tasks(i)%jatom
2181 17576216 : IF (symmetric .AND. iatom > jatom) THEN
2182 : ! iatom / jatom swapped
2183 6656034 : atom_pair_tmp(i)%row = jatom
2184 6656034 : atom_pair_tmp(i)%col = iatom
2185 : ELSE
2186 10920182 : atom_pair_tmp(i)%row = iatom
2187 10920182 : atom_pair_tmp(i)%col = jatom
2188 : END IF
2189 :
2190 17611849 : IF (send) THEN
2191 : ! If sending, we need to use the 'real rank' as the pair has to be sent to the process which
2192 : ! actually has the correct part of the rs_grid to do the mapping
2193 6659152 : ilevel = tasks(i)%grid_level
2194 6659152 : virt_rank = decode_rank(tasks(i)%destination, SIZE(rs_descs))
2195 6659152 : atom_pair_tmp(i)%rank = rs_descs(ilevel)%rs_desc%virtual2real(virt_rank)
2196 : ELSE
2197 : ! If we are receiving, then no conversion is needed as the rank is that of the process with the
2198 : ! required matrix block, and the ordering of the rs grid is irrelevant
2199 10917064 : atom_pair_tmp(i)%rank = decode_rank(tasks(i)%source, SIZE(rs_descs))
2200 : END IF
2201 : END DO
2202 :
2203 : ! find unique atom pairs that I'm sending/receiving
2204 106899 : ALLOCATE (indices(ntasks))
2205 35633 : CALL atom_pair_sort(atom_pair_tmp, ntasks, indices)
2206 35633 : npairs = 1
2207 35633 : tasks(indices(1))%pair_index = 1
2208 17576216 : DO i = 2, ntasks
2209 17540583 : IF (atom_pair_less_than(atom_pair_tmp(i - 1), atom_pair_tmp(i))) THEN
2210 1124716 : npairs = npairs + 1
2211 1124716 : atom_pair_tmp(npairs) = atom_pair_tmp(i)
2212 : END IF
2213 17576216 : tasks(indices(i))%pair_index = npairs
2214 : END DO
2215 35633 : DEALLOCATE (indices)
2216 :
2217 : ! Copy unique pairs to final location.
2218 1267248 : ALLOCATE (atom_pair(npairs))
2219 1195982 : atom_pair(:) = atom_pair_tmp(:npairs)
2220 35633 : DEALLOCATE (atom_pair_tmp)
2221 :
2222 : END SUBROUTINE get_atom_pair
2223 :
2224 : ! **************************************************************************************************
2225 : !> \brief redistributes the matrix so that it can be used in realspace operations
2226 : !> i.e. according to the task lists for collocate and integrate.
2227 : !> This routine can become a bottleneck in large calculations.
2228 : !> \param rs_descs ...
2229 : !> \param pmats ...
2230 : !> \param atom_pair_send ...
2231 : !> \param atom_pair_recv ...
2232 : !> \param natoms ...
2233 : !> \param nimages ...
2234 : !> \param scatter ...
2235 : !> \param hmats ...
2236 : ! **************************************************************************************************
2237 336 : SUBROUTINE rs_distribute_matrix(rs_descs, pmats, atom_pair_send, atom_pair_recv, &
2238 : nimages, scatter, hmats)
2239 :
2240 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
2241 : POINTER :: rs_descs
2242 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: pmats
2243 : TYPE(atom_pair_type), DIMENSION(:), POINTER :: atom_pair_send, atom_pair_recv
2244 : INTEGER :: nimages
2245 : LOGICAL :: scatter
2246 : TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
2247 : POINTER :: hmats
2248 :
2249 : CHARACTER(LEN=*), PARAMETER :: routineN = 'rs_distribute_matrix'
2250 :
2251 : INTEGER :: acol, arow, handle, i, img, j, k, l, me, &
2252 : nblkcols_total, nblkrows_total, ncol, &
2253 : nrow, nthread, nthread_left
2254 336 : INTEGER, ALLOCATABLE, DIMENSION(:) :: first_col, first_row, last_col, last_row, recv_disps, &
2255 336 : recv_pair_count, recv_pair_disps, recv_sizes, send_disps, send_pair_count, &
2256 336 : send_pair_disps, send_sizes
2257 336 : INTEGER, DIMENSION(:), POINTER :: col_blk_size, row_blk_size
2258 : LOGICAL :: found
2259 336 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), TARGET :: recv_buf_r, send_buf_r
2260 336 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: h_block, p_block
2261 : TYPE(dbcsr_type), POINTER :: hmat, pmat
2262 : TYPE(realspace_grid_desc_type), POINTER :: desc
2263 336 : REAL(kind=dp), DIMENSION(:), POINTER :: vector
2264 :
2265 336 : !$ INTEGER(kind=omp_lock_kind), ALLOCATABLE, DIMENSION(:) :: locks
2266 :
2267 336 : CALL timeset(routineN, handle)
2268 :
2269 336 : IF (.NOT. scatter) THEN
2270 0 : CPASSERT(PRESENT(hmats))
2271 : END IF
2272 :
2273 336 : desc => rs_descs(1)%rs_desc
2274 336 : me = desc%my_pos + 1
2275 :
2276 : ! allocate local arrays
2277 1008 : ALLOCATE (send_sizes(desc%group_size))
2278 672 : ALLOCATE (recv_sizes(desc%group_size))
2279 672 : ALLOCATE (send_disps(desc%group_size))
2280 672 : ALLOCATE (recv_disps(desc%group_size))
2281 672 : ALLOCATE (send_pair_count(desc%group_size))
2282 672 : ALLOCATE (recv_pair_count(desc%group_size))
2283 672 : ALLOCATE (send_pair_disps(desc%group_size))
2284 672 : ALLOCATE (recv_pair_disps(desc%group_size))
2285 :
2286 336 : pmat => pmats(1)%matrix
2287 : CALL dbcsr_get_info(pmat, &
2288 : row_blk_size=row_blk_size, &
2289 : col_blk_size=col_blk_size, &
2290 : nblkrows_total=nblkrows_total, &
2291 336 : nblkcols_total=nblkcols_total)
2292 0 : ALLOCATE (first_row(nblkrows_total), last_row(nblkrows_total), &
2293 2352 : first_col(nblkcols_total), last_col(nblkcols_total))
2294 336 : CALL dbcsr_convert_sizes_to_offsets(row_blk_size, first_row, last_row)
2295 336 : CALL dbcsr_convert_sizes_to_offsets(col_blk_size, first_col, last_col)
2296 :
2297 : ! set up send buffer sizes
2298 336 : send_sizes = 0
2299 336 : send_pair_count = 0
2300 1512 : DO i = 1, SIZE(atom_pair_send)
2301 1176 : k = atom_pair_send(i)%rank + 1 ! proc we're sending this block to
2302 1176 : arow = atom_pair_send(i)%row
2303 1176 : acol = atom_pair_send(i)%col
2304 1176 : nrow = last_row(arow) - first_row(arow) + 1
2305 1176 : ncol = last_col(acol) - first_col(acol) + 1
2306 1176 : send_sizes(k) = send_sizes(k) + nrow*ncol
2307 1512 : send_pair_count(k) = send_pair_count(k) + 1
2308 : END DO
2309 :
2310 336 : send_disps = 0
2311 336 : send_pair_disps = 0
2312 672 : DO i = 2, desc%group_size
2313 336 : send_disps(i) = send_disps(i - 1) + send_sizes(i - 1)
2314 672 : send_pair_disps(i) = send_pair_disps(i - 1) + send_pair_count(i - 1)
2315 : END DO
2316 :
2317 1680 : ALLOCATE (send_buf_r(SUM(send_sizes)))
2318 :
2319 : ! set up recv buffer
2320 :
2321 336 : recv_sizes = 0
2322 336 : recv_pair_count = 0
2323 1512 : DO i = 1, SIZE(atom_pair_recv)
2324 1176 : k = atom_pair_recv(i)%rank + 1 ! proc we're receiving this data from
2325 1176 : arow = atom_pair_recv(i)%row
2326 1176 : acol = atom_pair_recv(i)%col
2327 1176 : nrow = last_row(arow) - first_row(arow) + 1
2328 1176 : ncol = last_col(acol) - first_col(acol) + 1
2329 1176 : recv_sizes(k) = recv_sizes(k) + nrow*ncol
2330 1512 : recv_pair_count(k) = recv_pair_count(k) + 1
2331 : END DO
2332 :
2333 336 : recv_disps = 0
2334 336 : recv_pair_disps = 0
2335 672 : DO i = 2, desc%group_size
2336 336 : recv_disps(i) = recv_disps(i - 1) + recv_sizes(i - 1)
2337 672 : recv_pair_disps(i) = recv_pair_disps(i - 1) + recv_pair_count(i - 1)
2338 : END DO
2339 1680 : ALLOCATE (recv_buf_r(SUM(recv_sizes)))
2340 :
2341 : !$OMP PARALLEL DEFAULT(NONE) &
2342 : !$OMP SHARED(desc,send_pair_count,send_pair_disps,nimages),&
2343 : !$OMP SHARED(last_row,first_row,last_col,first_col),&
2344 : !$OMP SHARED(pmats,send_buf_r,send_disps,send_sizes),&
2345 : !$OMP SHARED(atom_pair_send,me,hmats,nblkrows_total),&
2346 : !$OMP SHARED(atom_pair_recv,recv_buf_r,scatter,recv_pair_disps), &
2347 : !$OMP SHARED(recv_sizes,recv_disps,recv_pair_count,locks), &
2348 : !$OMP PRIVATE(i,img,arow,acol,nrow,ncol,p_block,found,j,k,l),&
2349 336 : !$OMP PRIVATE(nthread,h_block,nthread_left,hmat,pmat,vector)
2350 :
2351 : nthread = 1
2352 : !$ nthread = omp_get_num_threads()
2353 : nthread_left = 1
2354 : !$ nthread_left = MAX(1, nthread - 1)
2355 :
2356 : ! do packing
2357 : !$OMP DO schedule(guided)
2358 : DO l = 1, desc%group_size
2359 : IF (l == me) CYCLE
2360 : send_sizes(l) = 0
2361 : DO i = 1, send_pair_count(l)
2362 : arow = atom_pair_send(send_pair_disps(l) + i)%row
2363 : acol = atom_pair_send(send_pair_disps(l) + i)%col
2364 : img = atom_pair_send(send_pair_disps(l) + i)%image
2365 : nrow = last_row(arow) - first_row(arow) + 1
2366 : ncol = last_col(acol) - first_col(acol) + 1
2367 : pmat => pmats(img)%matrix
2368 : CALL dbcsr_get_block_p(matrix=pmat, row=arow, col=acol, block=p_block, found=found)
2369 : CPASSERT(found)
2370 :
2371 : DO k = 1, ncol
2372 : DO j = 1, nrow
2373 : send_buf_r(send_disps(l) + send_sizes(l) + j + (k - 1)*nrow) = p_block(j, k)
2374 : END DO
2375 : END DO
2376 : send_sizes(l) = send_sizes(l) + nrow*ncol
2377 : END DO
2378 : END DO
2379 : !$OMP END DO
2380 :
2381 : IF (.NOT. scatter) THEN
2382 : ! We need locks to protect concurrent summation into H
2383 : !$OMP SINGLE
2384 : !$ ALLOCATE (locks(nthread*10))
2385 : !$OMP END SINGLE
2386 :
2387 : !$OMP DO
2388 : !$ do i = 1, nthread*10
2389 : !$ call omp_init_lock(locks(i))
2390 : !$ end do
2391 : !$OMP END DO
2392 : END IF
2393 :
2394 : !$OMP MASTER
2395 : ! do communication
2396 : CALL desc%group%alltoall(send_buf_r, send_sizes, send_disps, &
2397 : recv_buf_r, recv_sizes, recv_disps)
2398 : !$OMP END MASTER
2399 :
2400 : ! If this is a scatter, then no need to copy local blocks,
2401 : ! If not, we sum them directly into H (bypassing the alltoall)
2402 : IF (.NOT. scatter) THEN
2403 :
2404 : ! Distribute work over remaining threads assuming one is still in the alltoall
2405 : !$OMP DO schedule(dynamic,MAX(1,send_pair_count(me)/nthread_left))
2406 : DO i = 1, send_pair_count(me)
2407 : arow = atom_pair_send(send_pair_disps(me) + i)%row
2408 : acol = atom_pair_send(send_pair_disps(me) + i)%col
2409 : img = atom_pair_send(send_pair_disps(me) + i)%image
2410 : nrow = last_row(arow) - first_row(arow) + 1
2411 : ncol = last_col(acol) - first_col(acol) + 1
2412 : hmat => hmats(img)%matrix
2413 : pmat => pmats(img)%matrix
2414 : CALL dbcsr_get_block_p(matrix=hmat, row=arow, col=acol, BLOCK=h_block, found=found)
2415 : CPASSERT(found)
2416 : CALL dbcsr_get_block_p(matrix=pmat, row=arow, col=acol, BLOCK=p_block, found=found)
2417 : CPASSERT(found)
2418 :
2419 : !$ call omp_set_lock(locks((arow - 1)*nthread*10/nblkrows_total + 1))
2420 : DO k = 1, ncol
2421 : DO j = 1, nrow
2422 : h_block(j, k) = h_block(j, k) + p_block(j, k)
2423 : END DO
2424 : END DO
2425 : !$ call omp_unset_lock(locks((arow - 1)*nthread*10/nblkrows_total + 1))
2426 : END DO
2427 : !$OMP END DO
2428 : ELSE
2429 : ! We will insert new blocks into P, so create mutable work matrices
2430 : DO img = 1, nimages
2431 : pmat => pmats(img)%matrix
2432 : CALL dbcsr_work_create(pmat, work_mutable=.TRUE., &
2433 : nblks_guess=SIZE(atom_pair_recv)/nthread, sizedata_guess=SIZE(recv_buf_r)/nthread, &
2434 : n=nthread)
2435 : END DO
2436 : END IF
2437 :
2438 : ! wait for comm and setup to finish
2439 : !$OMP BARRIER
2440 :
2441 : !do unpacking
2442 : !$OMP DO schedule(guided)
2443 : DO l = 1, desc%group_size
2444 : IF (l == me) CYCLE
2445 : recv_sizes(l) = 0
2446 : DO i = 1, recv_pair_count(l)
2447 : arow = atom_pair_recv(recv_pair_disps(l) + i)%row
2448 : acol = atom_pair_recv(recv_pair_disps(l) + i)%col
2449 : img = atom_pair_recv(recv_pair_disps(l) + i)%image
2450 : nrow = last_row(arow) - first_row(arow) + 1
2451 : ncol = last_col(acol) - first_col(acol) + 1
2452 : pmat => pmats(img)%matrix
2453 : NULLIFY (p_block)
2454 : CALL dbcsr_get_block_p(matrix=pmat, row=arow, col=acol, BLOCK=p_block, found=found)
2455 :
2456 : IF (PRESENT(hmats)) THEN
2457 : hmat => hmats(img)%matrix
2458 : CALL dbcsr_get_block_p(matrix=hmat, row=arow, col=acol, BLOCK=h_block, found=found)
2459 : CPASSERT(found)
2460 : END IF
2461 :
2462 : IF (scatter .AND. .NOT. ASSOCIATED(p_block)) THEN
2463 : vector => recv_buf_r(recv_disps(l) + recv_sizes(l) + 1:recv_disps(l) + recv_sizes(l) + nrow*ncol)
2464 : CALL dbcsr_put_block(pmat, arow, acol, block=RESHAPE(vector, [nrow, ncol]))
2465 : END IF
2466 : IF (.NOT. scatter) THEN
2467 : !$ call omp_set_lock(locks((arow - 1)*nthread*10/nblkrows_total + 1))
2468 : DO k = 1, ncol
2469 : DO j = 1, nrow
2470 : h_block(j, k) = h_block(j, k) + recv_buf_r(recv_disps(l) + recv_sizes(l) + j + (k - 1)*nrow)
2471 : END DO
2472 : END DO
2473 : !$ call omp_unset_lock(locks((arow - 1)*nthread*10/nblkrows_total + 1))
2474 : END IF
2475 : recv_sizes(l) = recv_sizes(l) + nrow*ncol
2476 : END DO
2477 : END DO
2478 : !$OMP END DO
2479 :
2480 : !$ IF (.not. scatter) THEN
2481 : !$OMP DO
2482 : !$ do i = 1, nthread*10
2483 : !$ call omp_destroy_lock(locks(i))
2484 : !$ end do
2485 : !$OMP END DO
2486 : !$ END IF
2487 :
2488 : !$OMP SINGLE
2489 : !$ IF (.not. scatter) THEN
2490 : !$ DEALLOCATE (locks)
2491 : !$ END IF
2492 : !$OMP END SINGLE NOWAIT
2493 :
2494 : IF (scatter) THEN
2495 : ! Blocks were added to P
2496 : DO img = 1, nimages
2497 : pmat => pmats(img)%matrix
2498 : CALL dbcsr_finalize(pmat)
2499 : END DO
2500 : END IF
2501 : !$OMP END PARALLEL
2502 :
2503 336 : DEALLOCATE (send_buf_r)
2504 336 : DEALLOCATE (recv_buf_r)
2505 :
2506 336 : DEALLOCATE (send_sizes)
2507 336 : DEALLOCATE (recv_sizes)
2508 336 : DEALLOCATE (send_disps)
2509 336 : DEALLOCATE (recv_disps)
2510 336 : DEALLOCATE (send_pair_count)
2511 336 : DEALLOCATE (recv_pair_count)
2512 336 : DEALLOCATE (send_pair_disps)
2513 336 : DEALLOCATE (recv_pair_disps)
2514 :
2515 336 : DEALLOCATE (first_row, last_row, first_col, last_col)
2516 :
2517 336 : CALL timestop(handle)
2518 :
2519 1008 : END SUBROUTINE rs_distribute_matrix
2520 :
2521 : ! **************************************************************************************************
2522 : !> \brief Calculates offsets and sizes for rs_scatter_matrix and rs_copy_matrix.
2523 : !> \author Ole Schuett
2524 : ! **************************************************************************************************
2525 33750 : SUBROUTINE rs_calc_offsets(pairs, nsgf, group_size, &
2526 : pair_offsets, rank_offsets, rank_sizes, buffer_size)
2527 : TYPE(atom_pair_type), DIMENSION(:), INTENT(IN) :: pairs
2528 : INTEGER, DIMENSION(:), INTENT(IN) :: nsgf
2529 : INTEGER, INTENT(IN) :: group_size
2530 : INTEGER, DIMENSION(:), POINTER :: pair_offsets, rank_offsets, rank_sizes
2531 : INTEGER, INTENT(INOUT) :: buffer_size
2532 :
2533 : INTEGER :: acol, arow, i, block_size, total_size, k, prev_k
2534 :
2535 33750 : IF (ASSOCIATED(pair_offsets)) DEALLOCATE (pair_offsets)
2536 33750 : IF (ASSOCIATED(rank_offsets)) DEALLOCATE (rank_offsets)
2537 33750 : IF (ASSOCIATED(rank_sizes)) DEALLOCATE (rank_sizes)
2538 :
2539 : ! calculate buffer_size and pair_offsets
2540 100898 : ALLOCATE (pair_offsets(SIZE(pairs)))
2541 33750 : total_size = 0
2542 1183077 : DO i = 1, SIZE(pairs)
2543 1149327 : pair_offsets(i) = total_size
2544 1149327 : arow = pairs(i)%row
2545 1149327 : acol = pairs(i)%col
2546 1149327 : block_size = nsgf(arow)*nsgf(acol)
2547 1183077 : total_size = total_size + block_size
2548 : END DO
2549 33750 : buffer_size = total_size
2550 :
2551 : ! calculate rank_offsets and rank_sizes
2552 101250 : ALLOCATE (rank_offsets(group_size))
2553 67500 : ALLOCATE (rank_sizes(group_size))
2554 100024 : rank_offsets = 0
2555 100024 : rank_sizes = 0
2556 33750 : IF (SIZE(pairs) > 0) THEN
2557 33398 : prev_k = pairs(1)%rank + 1
2558 1182725 : DO i = 1, SIZE(pairs)
2559 1149327 : k = pairs(i)%rank + 1
2560 1149327 : CPASSERT(k >= prev_k) ! expecting the pairs to be ordered by rank
2561 1182725 : IF (k > prev_k) THEN
2562 15544 : rank_offsets(k) = pair_offsets(i)
2563 15544 : rank_sizes(prev_k) = rank_offsets(k) - rank_offsets(prev_k)
2564 15544 : prev_k = k
2565 : END IF
2566 : END DO
2567 33398 : rank_sizes(k) = buffer_size - rank_offsets(k) ! complete last rank
2568 : END IF
2569 :
2570 33750 : END SUBROUTINE rs_calc_offsets
2571 :
2572 : ! **************************************************************************************************
2573 : !> \brief Group contributions to each matrix block without changing the communication order.
2574 : !> Rebuilt with the task list; contains indices only, independent of matrices and threads.
2575 : !> \param pairs atom pairs in communication order
2576 : !> \param block_starts first position of each block in pair_indices, plus an end sentinel
2577 : !> \param pair_indices original pair indices grouped by destination block
2578 : ! **************************************************************************************************
2579 17890 : SUBROUTINE rs_group_atom_pairs(pairs, block_starts, pair_indices)
2580 : TYPE(atom_pair_type), DIMENSION(:), INTENT(IN) :: pairs
2581 : INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: block_starts, pair_indices
2582 :
2583 : INTEGER :: i, nblocks, npairs
2584 17890 : INTEGER, ALLOCATABLE, DIMENSION(:) :: starts
2585 17890 : TYPE(atom_pair_type), ALLOCATABLE, DIMENSION(:) :: sorted_pairs
2586 :
2587 17890 : npairs = SIZE(pairs)
2588 849720 : ALLOCATE (sorted_pairs(npairs), pair_indices(npairs), starts(npairs + 1))
2589 743080 : sorted_pairs(:) = pairs(:)
2590 : ! Ignore source rank for grouping. The stable sort preserves contribution order within each block.
2591 743080 : sorted_pairs(:)%rank = 0
2592 17890 : CALL atom_pair_sort(sorted_pairs, npairs, pair_indices)
2593 :
2594 17890 : nblocks = 0
2595 743080 : DO i = 1, npairs
2596 725190 : IF (i > 1) THEN
2597 707650 : IF (.NOT. atom_pair_less_than(sorted_pairs(i - 1), sorted_pairs(i))) CYCLE
2598 : END IF
2599 717374 : nblocks = nblocks + 1
2600 743080 : starts(nblocks) = i
2601 : END DO
2602 17890 : starts(nblocks + 1) = npairs + 1
2603 71560 : ALLOCATE (block_starts(nblocks + 1))
2604 753154 : block_starts(:) = starts(:nblocks + 1)
2605 :
2606 17890 : END SUBROUTINE rs_group_atom_pairs
2607 :
2608 : ! **************************************************************************************************
2609 : !> \brief Scatters dbcsr matrix blocks and receives them into a buffer as needed before collocation.
2610 : !> \author Ole Schuett
2611 : ! **************************************************************************************************
2612 296039 : SUBROUTINE rs_scatter_matrices(src_matrices, dest_buffer, task_list, group)
2613 : TYPE(dbcsr_p_type), DIMENSION(:), INTENT(IN) :: src_matrices
2614 : TYPE(offload_buffer_type), INTENT(INOUT) :: dest_buffer
2615 : TYPE(task_list_type), INTENT(IN) :: task_list
2616 : TYPE(mp_comm_type), INTENT(IN) :: group
2617 :
2618 : CHARACTER(LEN=*), PARAMETER :: routineN = 'rs_scatter_matrices'
2619 :
2620 : INTEGER :: handle
2621 296039 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: buffer_send
2622 :
2623 296039 : CALL timeset(routineN, handle)
2624 :
2625 296039 : IF (.NOT. ASSOCIATED(task_list%atom_pair_send)) THEN
2626 29735 : CALL rs_copy_to_buffer(src_matrices, dest_buffer, task_list)
2627 29735 : CALL timestop(handle)
2628 : RETURN
2629 : END IF
2630 794255 : ALLOCATE (buffer_send(task_list%buffer_size_send))
2631 :
2632 : ! pack dbcsr blocks into send buffer
2633 : CPASSERT(ASSOCIATED(task_list%atom_pair_send))
2634 : CALL rs_pack_buffer(src_matrices=src_matrices, &
2635 : dest_buffer=buffer_send, &
2636 : atom_pair=task_list%atom_pair_send, &
2637 266304 : pair_offsets=task_list%pair_offsets_send)
2638 :
2639 : ! mpi all-to-all communication, receiving directly into blocks_recv%buffer.
2640 : CALL group%alltoall(buffer_send, task_list%rank_sizes_send, task_list%rank_offsets_send, &
2641 : dest_buffer%host_buffer, &
2642 2396736 : task_list%rank_sizes_recv, task_list%rank_offsets_recv)
2643 :
2644 266304 : DEALLOCATE (buffer_send)
2645 266304 : CALL timestop(handle)
2646 :
2647 296039 : END SUBROUTINE rs_scatter_matrices
2648 :
2649 : ! **************************************************************************************************
2650 : !> \brief Gather the dbcsr matrix blocks and receives them into a buffer as needed after integration.
2651 : !> \author Ole Schuett
2652 : ! **************************************************************************************************
2653 248162 : SUBROUTINE rs_gather_matrices(src_buffer, dest_matrices, task_list, group)
2654 : TYPE(offload_buffer_type), INTENT(IN) :: src_buffer
2655 : TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT) :: dest_matrices
2656 : TYPE(task_list_type), INTENT(IN) :: task_list
2657 : TYPE(mp_comm_type), INTENT(IN) :: group
2658 :
2659 : CHARACTER(LEN=*), PARAMETER :: routineN = 'rs_gather_matrices'
2660 :
2661 : INTEGER :: handle
2662 248162 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: buffer_send
2663 :
2664 248162 : CALL timeset(routineN, handle)
2665 :
2666 248162 : IF (.NOT. ASSOCIATED(task_list%atom_pair_send)) THEN
2667 31902 : CALL rs_copy_to_matrices(src_buffer, dest_matrices, task_list)
2668 31902 : CALL timestop(handle)
2669 : RETURN
2670 : END IF
2671 :
2672 : ! Caution: The meaning of send and recv are reversed in this routine.
2673 645246 : ALLOCATE (buffer_send(task_list%buffer_size_send)) ! e.g. this is actually used for receiving
2674 :
2675 : ! mpi all-to-all communication
2676 : CALL group%alltoall(src_buffer%host_buffer, task_list%rank_sizes_recv, task_list%rank_offsets_recv, &
2677 1946340 : buffer_send, task_list%rank_sizes_send, task_list%rank_offsets_send)
2678 :
2679 : ! unpack dbcsr blocks from send buffer
2680 216260 : CPASSERT(ASSOCIATED(task_list%atom_pair_send))
2681 : CALL rs_unpack_buffer(src_buffer=buffer_send, &
2682 : dest_matrices=dest_matrices, &
2683 : atom_pair=task_list%atom_pair_send, &
2684 : pair_offsets=task_list%pair_offsets_send, &
2685 : block_starts=task_list%unpack_block_starts, &
2686 216260 : pair_indices=task_list%unpack_pair_indices)
2687 :
2688 216260 : DEALLOCATE (buffer_send)
2689 216260 : CALL timestop(handle)
2690 :
2691 248162 : END SUBROUTINE rs_gather_matrices
2692 :
2693 : ! **************************************************************************************************
2694 : !> \brief Copies the DBCSR blocks into buffer, replaces rs_scatter_matrix for non-distributed grids.
2695 : !> \author Ole Schuett
2696 : ! **************************************************************************************************
2697 29735 : SUBROUTINE rs_copy_to_buffer(src_matrices, dest_buffer, task_list)
2698 : TYPE(dbcsr_p_type), DIMENSION(:), INTENT(IN) :: src_matrices
2699 : TYPE(offload_buffer_type), INTENT(INOUT) :: dest_buffer
2700 : TYPE(task_list_type), INTENT(IN) :: task_list
2701 :
2702 : CALL rs_pack_buffer(src_matrices=src_matrices, &
2703 : dest_buffer=dest_buffer%host_buffer, &
2704 : atom_pair=task_list%atom_pair_recv, &
2705 29735 : pair_offsets=task_list%pair_offsets_recv)
2706 :
2707 29735 : END SUBROUTINE rs_copy_to_buffer
2708 :
2709 : ! **************************************************************************************************
2710 : !> \brief Copies from buffer into DBCSR matrics, replaces rs_gather_matrix for non-distributed grids.
2711 : !> \author Ole Schuett
2712 : ! **************************************************************************************************
2713 31902 : SUBROUTINE rs_copy_to_matrices(src_buffer, dest_matrices, task_list)
2714 : TYPE(offload_buffer_type), INTENT(IN) :: src_buffer
2715 : TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT) :: dest_matrices
2716 : TYPE(task_list_type), INTENT(IN) :: task_list
2717 :
2718 : CALL rs_unpack_buffer(src_buffer=src_buffer%host_buffer, &
2719 : dest_matrices=dest_matrices, &
2720 : atom_pair=task_list%atom_pair_recv, &
2721 : pair_offsets=task_list%pair_offsets_recv, &
2722 : block_starts=task_list%unpack_block_starts, &
2723 31902 : pair_indices=task_list%unpack_pair_indices)
2724 :
2725 31902 : END SUBROUTINE rs_copy_to_matrices
2726 :
2727 : ! **************************************************************************************************
2728 : !> \brief Helper routine for rs_scatter_matrix and rs_copy_to_buffer.
2729 : !> \author Ole Schuett
2730 : ! **************************************************************************************************
2731 296039 : SUBROUTINE rs_pack_buffer(src_matrices, dest_buffer, atom_pair, pair_offsets)
2732 : TYPE(dbcsr_p_type), DIMENSION(:), INTENT(IN) :: src_matrices
2733 : REAL(KIND=dp), DIMENSION(:), INTENT(INOUT) :: dest_buffer
2734 : TYPE(atom_pair_type), DIMENSION(:), INTENT(IN) :: atom_pair
2735 : INTEGER, DIMENSION(:), INTENT(IN) :: pair_offsets
2736 :
2737 : INTEGER :: acol, arow, img, i, offset, block_size
2738 : LOGICAL :: found
2739 296039 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: block
2740 :
2741 : !$OMP PARALLEL DEFAULT(NONE), &
2742 : !$OMP SHARED(src_matrices,atom_pair,pair_offsets,dest_buffer), &
2743 296039 : !$OMP PRIVATE(acol,arow,img,i,offset,block_size,found,block)
2744 : !$OMP DO schedule(guided)
2745 : DO i = 1, SIZE(atom_pair)
2746 : arow = atom_pair(i)%row
2747 : acol = atom_pair(i)%col
2748 : img = atom_pair(i)%image
2749 : CALL dbcsr_get_block_p(matrix=src_matrices(img)%matrix, row=arow, col=acol, &
2750 : block=block, found=found)
2751 : CPASSERT(found)
2752 : block_size = SIZE(block)
2753 : offset = pair_offsets(i)
2754 : dest_buffer(offset + 1:offset + block_size) = RESHAPE(block, shape=[block_size])
2755 : END DO
2756 : !$OMP END DO
2757 : !$OMP END PARALLEL
2758 :
2759 296039 : END SUBROUTINE rs_pack_buffer
2760 :
2761 : ! **************************************************************************************************
2762 : !> \brief Helper routine for rs_gather_matrix and rs_copy_to_matrices.
2763 : !> \author Ole Schuett
2764 : ! **************************************************************************************************
2765 248162 : SUBROUTINE rs_unpack_buffer(src_buffer, dest_matrices, atom_pair, pair_offsets, block_starts, pair_indices)
2766 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: src_buffer
2767 : TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT) :: dest_matrices
2768 : TYPE(atom_pair_type), DIMENSION(:), INTENT(IN) :: atom_pair
2769 : INTEGER, DIMENSION(:), INTENT(IN) :: pair_offsets
2770 : INTEGER, DIMENSION(:), INTENT(IN) :: block_starts, pair_indices
2771 :
2772 : INTEGER :: acol, arow, i, iblock, img, ipair, &
2773 : j, ncols, nrows, offset, pair
2774 : LOGICAL :: found
2775 248162 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: block
2776 :
2777 : ! One thread owns each destination block and accumulates all ranks' contributions.
2778 : !$OMP PARALLEL DEFAULT(NONE), &
2779 : !$OMP SHARED(src_buffer,atom_pair,pair_offsets,dest_matrices,block_starts,pair_indices), &
2780 248162 : !$OMP PRIVATE(acol,arow,i,iblock,img,ipair,j,ncols,nrows,offset,pair,found,block)
2781 : !$OMP DO schedule(guided)
2782 : DO iblock = 1, SIZE(block_starts) - 1
2783 : pair = pair_indices(block_starts(iblock))
2784 : arow = atom_pair(pair)%row
2785 : acol = atom_pair(pair)%col
2786 : img = atom_pair(pair)%image
2787 : CALL dbcsr_get_block_p(matrix=dest_matrices(img)%matrix, row=arow, col=acol, &
2788 : block=block, found=found)
2789 : CPASSERT(found)
2790 : nrows = SIZE(block, 1)
2791 : ncols = SIZE(block, 2)
2792 : DO ipair = block_starts(iblock), block_starts(iblock + 1) - 1
2793 : offset = pair_offsets(pair_indices(ipair))
2794 : DO j = 1, ncols
2795 : DO i = 1, nrows
2796 : block(i, j) = block(i, j) + src_buffer(offset + i + (j - 1)*nrows)
2797 : END DO
2798 : END DO
2799 : END DO
2800 : END DO
2801 : !$OMP END DO
2802 : !$OMP END PARALLEL
2803 :
2804 248162 : END SUBROUTINE rs_unpack_buffer
2805 :
2806 : ! **************************************************************************************************
2807 : !> \brief determines the rank of the processor who's real rs grid contains point
2808 : !> \param rs_desc ...
2809 : !> \param igrid_level ...
2810 : !> \param n_levels ...
2811 : !> \param cube_center ...
2812 : !> \param ntasks ...
2813 : !> \param tasks ...
2814 : !> \param lb_cube ...
2815 : !> \param ub_cube ...
2816 : !> \param added_tasks ...
2817 : !> \par History
2818 : !> 11.2007 created [MattW]
2819 : !> 10.2008 rewritten [Joost VandeVondele]
2820 : !> \author MattW
2821 : ! **************************************************************************************************
2822 1476 : SUBROUTINE rs_find_node(rs_desc, igrid_level, n_levels, cube_center, ntasks, tasks, &
2823 : lb_cube, ub_cube, added_tasks)
2824 :
2825 : TYPE(realspace_grid_desc_type), POINTER :: rs_desc
2826 : INTEGER, INTENT(IN) :: igrid_level, n_levels
2827 : INTEGER, DIMENSION(3), INTENT(IN) :: cube_center
2828 : INTEGER, INTENT(INOUT) :: ntasks
2829 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
2830 : INTEGER, DIMENSION(3), INTENT(IN) :: lb_cube, ub_cube
2831 : INTEGER, INTENT(OUT) :: added_tasks
2832 :
2833 : INTEGER, PARAMETER :: add_tasks = 1000
2834 : REAL(kind=dp), PARAMETER :: mult_tasks = 2.0_dp
2835 :
2836 : INTEGER :: bit_index, coord(3), curr_tasks, dest, i, icoord(3), idest, itask, ix, iy, iz, &
2837 : lb_coord(3), lb_domain(3), lbc(3), ub_coord(3), ub_domain(3), ubc(3)
2838 : INTEGER :: bit_pattern
2839 : LOGICAL :: dir_periodic(3)
2840 :
2841 1476 : coord(1) = rs_desc%x2coord(cube_center(1))
2842 1476 : coord(2) = rs_desc%y2coord(cube_center(2))
2843 1476 : coord(3) = rs_desc%z2coord(cube_center(3))
2844 1476 : dest = rs_desc%coord2rank(coord(1), coord(2), coord(3))
2845 :
2846 : ! the real cube coordinates
2847 5904 : lbc = lb_cube + cube_center
2848 5904 : ubc = ub_cube + cube_center
2849 :
2850 10332 : IF (ALL((rs_desc%lb_global(:, dest) - rs_desc%border) <= lbc) .AND. &
2851 : ALL((rs_desc%ub_global(:, dest) + rs_desc%border) >= ubc)) THEN
2852 : !standard distributed collocation/integration
2853 1476 : tasks(ntasks)%destination = encode_rank(dest, igrid_level, n_levels)
2854 1476 : tasks(ntasks)%dist_type = 1
2855 1476 : tasks(ntasks)%subpatch_pattern = 0
2856 1476 : added_tasks = 1
2857 :
2858 : ! here we figure out if there is an alternate location for this task
2859 : ! i.e. even though the cube_center is not in the real local domain,
2860 : ! it might fully fit in the halo of the neighbor
2861 : ! if its radius is smaller than the maximum radius
2862 : ! the list of possible neighbors is stored here as a bit pattern
2863 : ! to reconstruct what the rank of the neigbor is,
2864 : ! we can use (note this requires the correct rs_grid)
2865 : ! IF (BTEST(bit_pattern,0)) rank=rs_grid_locate_rank(rs_desc,tasks(ntasks)%destination,[-1,0,0])
2866 : ! IF (BTEST(bit_pattern,1)) rank=rs_grid_locate_rank(rs_desc,tasks(ntasks)%destination,[+1,0,0])
2867 : ! IF (BTEST(bit_pattern,2)) rank=rs_grid_locate_rank(rs_desc,tasks(ntasks)%destination,[0,-1,0])
2868 : ! IF (BTEST(bit_pattern,3)) rank=rs_grid_locate_rank(rs_desc,tasks(ntasks)%destination,[0,+1,0])
2869 : ! IF (BTEST(bit_pattern,4)) rank=rs_grid_locate_rank(rs_desc,tasks(ntasks)%destination,[0,0,-1])
2870 : ! IF (BTEST(bit_pattern,5)) rank=rs_grid_locate_rank(rs_desc,tasks(ntasks)%destination,[0,0,+1])
2871 1476 : bit_index = 0
2872 1476 : bit_pattern = 0
2873 5904 : DO i = 1, 3
2874 5904 : IF (rs_desc%perd(i) == 1) THEN
2875 2952 : bit_pattern = IBCLR(bit_pattern, bit_index)
2876 2952 : bit_index = bit_index + 1
2877 2952 : bit_pattern = IBCLR(bit_pattern, bit_index)
2878 2952 : bit_index = bit_index + 1
2879 : ELSE
2880 : ! fits the left neighbor ?
2881 1476 : IF (ubc(i) <= rs_desc%lb_global(i, dest) - 1 + rs_desc%border) THEN
2882 695 : bit_pattern = IBSET(bit_pattern, bit_index)
2883 695 : bit_index = bit_index + 1
2884 : ELSE
2885 781 : bit_pattern = IBCLR(bit_pattern, bit_index)
2886 781 : bit_index = bit_index + 1
2887 : END IF
2888 : ! fits the right neighbor ?
2889 1476 : IF (lbc(i) >= rs_desc%ub_global(i, dest) + 1 - rs_desc%border) THEN
2890 250 : bit_pattern = IBSET(bit_pattern, bit_index)
2891 250 : bit_index = bit_index + 1
2892 : ELSE
2893 1226 : bit_pattern = IBCLR(bit_pattern, bit_index)
2894 1226 : bit_index = bit_index + 1
2895 : END IF
2896 : END IF
2897 : END DO
2898 1476 : tasks(ntasks)%subpatch_pattern = bit_pattern
2899 :
2900 : ELSE
2901 : ! generalised collocation/integration needed
2902 : ! first we figure out how many neighbors we have to add to include the lbc/ubc
2903 : ! in the available domains (inclusive of halo points)
2904 : ! first we 'ignore' periodic boundary conditions
2905 : ! i.e. ub_coord-lb_coord+1 might be larger than group_dim
2906 0 : lb_coord = coord
2907 0 : ub_coord = coord
2908 0 : lb_domain = rs_desc%lb_global(:, dest) - rs_desc%border
2909 0 : ub_domain = rs_desc%ub_global(:, dest) + rs_desc%border
2910 0 : DO i = 1, 3
2911 : ! only if the grid is not periodic in this direction we need to take care of adding neighbors
2912 0 : IF (rs_desc%perd(i) == 0) THEN
2913 : ! if the domain lower bound is greater than the lbc we need to add the size of the neighbor domain
2914 0 : DO
2915 0 : IF (lb_domain(i) > lbc(i)) THEN
2916 0 : lb_coord(i) = lb_coord(i) - 1
2917 0 : icoord = MODULO(lb_coord, rs_desc%group_dim)
2918 0 : idest = rs_desc%coord2rank(icoord(1), icoord(2), icoord(3))
2919 0 : lb_domain(i) = lb_domain(i) - (rs_desc%ub_global(i, idest) - rs_desc%lb_global(i, idest) + 1)
2920 : ELSE
2921 : EXIT
2922 : END IF
2923 : END DO
2924 : ! same for the upper bound
2925 0 : DO
2926 0 : IF (ub_domain(i) < ubc(i)) THEN
2927 0 : ub_coord(i) = ub_coord(i) + 1
2928 0 : icoord = MODULO(ub_coord, rs_desc%group_dim)
2929 0 : idest = rs_desc%coord2rank(icoord(1), icoord(2), icoord(3))
2930 0 : ub_domain(i) = ub_domain(i) + (rs_desc%ub_global(i, idest) - rs_desc%lb_global(i, idest) + 1)
2931 : ELSE
2932 : EXIT
2933 : END IF
2934 : END DO
2935 : END IF
2936 : END DO
2937 :
2938 : ! some care is needed for the periodic boundaries
2939 0 : DO i = 1, 3
2940 0 : IF (ub_domain(i) - lb_domain(i) + 1 >= rs_desc%npts(i)) THEN
2941 0 : dir_periodic(i) = .TRUE.
2942 0 : lb_coord(i) = 0
2943 0 : ub_coord(i) = rs_desc%group_dim(i) - 1
2944 : ELSE
2945 0 : dir_periodic(i) = .FALSE.
2946 : END IF
2947 : END DO
2948 :
2949 0 : added_tasks = PRODUCT(ub_coord - lb_coord + 1)
2950 0 : itask = ntasks
2951 0 : ntasks = ntasks + added_tasks - 1
2952 0 : IF (ntasks > SIZE(tasks)) THEN
2953 0 : curr_tasks = INT((SIZE(tasks) + add_tasks)*mult_tasks)
2954 0 : CALL reallocate_tasks(tasks, curr_tasks)
2955 : END IF
2956 0 : DO iz = lb_coord(3), ub_coord(3)
2957 0 : DO iy = lb_coord(2), ub_coord(2)
2958 0 : DO ix = lb_coord(1), ub_coord(1)
2959 0 : icoord = MODULO([ix, iy, iz], rs_desc%group_dim)
2960 0 : idest = rs_desc%coord2rank(icoord(1), icoord(2), icoord(3))
2961 0 : tasks(itask)%destination = encode_rank(idest, igrid_level, n_levels)
2962 0 : tasks(itask)%dist_type = 2
2963 0 : tasks(itask)%subpatch_pattern = 0
2964 : ! encode the domain size for this task
2965 : ! if the bit is set, we need to add the border in that direction
2966 0 : IF (ix == lb_coord(1) .AND. .NOT. dir_periodic(1)) THEN
2967 0 : tasks(itask)%subpatch_pattern = IBSET(tasks(itask)%subpatch_pattern, 0)
2968 : END IF
2969 0 : IF (ix == ub_coord(1) .AND. .NOT. dir_periodic(1)) THEN
2970 0 : tasks(itask)%subpatch_pattern = IBSET(tasks(itask)%subpatch_pattern, 1)
2971 : END IF
2972 0 : IF (iy == lb_coord(2) .AND. .NOT. dir_periodic(2)) THEN
2973 0 : tasks(itask)%subpatch_pattern = IBSET(tasks(itask)%subpatch_pattern, 2)
2974 : END IF
2975 0 : IF (iy == ub_coord(2) .AND. .NOT. dir_periodic(2)) THEN
2976 0 : tasks(itask)%subpatch_pattern = IBSET(tasks(itask)%subpatch_pattern, 3)
2977 : END IF
2978 0 : IF (iz == lb_coord(3) .AND. .NOT. dir_periodic(3)) THEN
2979 0 : tasks(itask)%subpatch_pattern = IBSET(tasks(itask)%subpatch_pattern, 4)
2980 : END IF
2981 0 : IF (iz == ub_coord(3) .AND. .NOT. dir_periodic(3)) THEN
2982 0 : tasks(itask)%subpatch_pattern = IBSET(tasks(itask)%subpatch_pattern, 5)
2983 : END IF
2984 0 : itask = itask + 1
2985 : END DO
2986 : END DO
2987 : END DO
2988 : END IF
2989 :
2990 1476 : END SUBROUTINE rs_find_node
2991 :
2992 : ! **************************************************************************************************
2993 : !> \brief utility functions for encoding the grid level with a rank, allowing
2994 : !> different grid levels to maintain a different rank ordering without
2995 : !> losing information. These encoded_ints are stored in the tasks(1:2,:) array
2996 : !> \param rank ...
2997 : !> \param grid_level ...
2998 : !> \param n_levels ...
2999 : !> \return ...
3000 : !> \par History
3001 : !> 4.2009 created [Iain Bethune]
3002 : !> (c) The Numerical Algorithms Group (NAG) Ltd, 2009 on behalf of the HECToR project
3003 : ! **************************************************************************************************
3004 22413076 : FUNCTION encode_rank(rank, grid_level, n_levels) RESULT(encoded_int)
3005 :
3006 : INTEGER, INTENT(IN) :: rank, grid_level, n_levels
3007 : INTEGER :: encoded_int
3008 :
3009 : ! ordered so can still sort by rank
3010 :
3011 22413076 : encoded_int = rank*n_levels + grid_level - 1
3012 :
3013 22413076 : END FUNCTION encode_rank
3014 :
3015 : ! **************************************************************************************************
3016 : !> \brief ...
3017 : !> \param encoded_int ...
3018 : !> \param n_levels ...
3019 : !> \return ...
3020 : ! **************************************************************************************************
3021 52474670 : FUNCTION decode_rank(encoded_int, n_levels) RESULT(rank)
3022 :
3023 : INTEGER, INTENT(IN) :: encoded_int
3024 : INTEGER, INTENT(IN) :: n_levels
3025 : INTEGER :: rank
3026 :
3027 52474670 : rank = INT(encoded_int/n_levels)
3028 :
3029 52474670 : END FUNCTION decode_rank
3030 :
3031 : ! **************************************************************************************************
3032 : !> \brief ...
3033 : !> \param encoded_int ...
3034 : !> \param n_levels ...
3035 : !> \return ...
3036 : ! **************************************************************************************************
3037 13318304 : FUNCTION decode_level(encoded_int, n_levels) RESULT(grid_level)
3038 :
3039 : INTEGER, INTENT(IN) :: encoded_int
3040 : INTEGER, INTENT(IN) :: n_levels
3041 : INTEGER :: grid_level
3042 :
3043 13318304 : grid_level = INT(MODULO(encoded_int, n_levels)) + 1
3044 :
3045 13318304 : END FUNCTION decode_level
3046 :
3047 : ! **************************************************************************************************
3048 : !> \brief Sort pgf index pair (ipgf,iset,iatom),(jpgf,jset,jatom) for which all atom pairs are
3049 : !> grouped, and for each atom pair all set pairs are grouped and for each set pair,
3050 : !> all pgfs are grouped.
3051 : !> This yields the right order of the tasks for collocation after the sort
3052 : !> in distribute_tasks. E.g. for a atom pair, all sets and pgfs are computed in one go.
3053 : !> The exception is the gridlevel. Tasks are first ordered wrt to grid_level. This implies
3054 : !> that a given density matrix block will be decontracted several times,
3055 : !> but cache effects on the grid make up for this.
3056 : !> \param a ...
3057 : !> \param b ...
3058 : !> \return ...
3059 : !> \author Ole Schuett
3060 : ! **************************************************************************************************
3061 104601444 : PURE FUNCTION tasks_less_than(a, b) RESULT(res)
3062 : TYPE(task_type), INTENT(IN) :: a, b
3063 : LOGICAL :: res
3064 :
3065 104601444 : IF (a%grid_level /= b%grid_level) THEN
3066 25401277 : res = a%grid_level < b%grid_level
3067 :
3068 79200167 : ELSE IF (a%image /= b%image) THEN
3069 17842793 : res = a%image < b%image
3070 :
3071 61357374 : ELSE IF (a%iatom /= b%iatom) THEN
3072 9024181 : res = a%iatom < b%iatom
3073 :
3074 52333193 : ELSE IF (a%jatom /= b%jatom) THEN
3075 9115750 : res = a%jatom < b%jatom
3076 :
3077 43217443 : ELSE IF (a%iset /= b%iset) THEN
3078 6497986 : res = a%iset < b%iset
3079 :
3080 36719457 : ELSE IF (a%jset /= b%jset) THEN
3081 6816005 : res = a%jset < b%jset
3082 :
3083 29903452 : ELSE IF (a%ipgf /= b%ipgf) THEN
3084 8578445 : res = a%ipgf < b%ipgf
3085 :
3086 : ELSE
3087 21325007 : res = a%jpgf < b%jpgf
3088 :
3089 : END IF
3090 104601444 : END FUNCTION tasks_less_than
3091 :
3092 249505411 : #:call array_sort(prefix='tasks', type='TYPE(task_type)')
3093 : #:endcall
3094 :
3095 : ! **************************************************************************************************
3096 : !> \brief Order atom pairs to find duplicates.
3097 : !> \param a ...
3098 : !> \param b ...
3099 : !> \return ...
3100 : !> \author Ole Schuett
3101 : ! **************************************************************************************************
3102 86747516 : PURE FUNCTION atom_pair_less_than(a, b) RESULT(res)
3103 : TYPE(atom_pair_type), INTENT(IN) :: a, b
3104 : LOGICAL :: res
3105 :
3106 86747516 : IF (a%rank /= b%rank) THEN
3107 1878606 : res = a%rank < b%rank
3108 :
3109 84868910 : ELSE IF (a%row /= b%row) THEN
3110 12195783 : res = a%row < b%row
3111 :
3112 72673127 : ELSE IF (a%col /= b%col) THEN
3113 11698827 : res = a%col < b%col
3114 :
3115 : ELSE
3116 60974300 : res = a%image < b%image
3117 :
3118 : END IF
3119 86747516 : END FUNCTION atom_pair_less_than
3120 :
3121 185969382 : #:call array_sort(prefix='atom_pair', type='TYPE(atom_pair_type)')
3122 : #:endcall
3123 :
3124 : END MODULE task_list_methods
|