(git:b6ef100)
Loading...
Searching...
No Matches
realspace_grid_types.F
Go to the documentation of this file.
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!> \note
10!> Basic type for real space grid methods
11!> \par History
12!> JGH (22-May-2002) : New routine rs_grid_zero
13!> JGH (12-Jun-2002) : Bug fix for mpi groups
14!> JGH (19-Jun-2003) : Added routine for task distribution
15!> JGH (23-Nov-2003) : Added routine for task loop separation
16!> \author JGH (18-Mar-2001)
17! **************************************************************************************************
21 USE kahan_sum, ONLY: accurate_sum
22 USE kinds, ONLY: dp,&
23 int_8
24 USE machine, ONLY: m_memory
25 USE mathlib, ONLY: det_3x3
26 USE message_passing, ONLY: mp_comm_null,&
35 USE pw_grid_types, ONLY: pw_mode_local,&
37 USE pw_grids, ONLY: pw_grid_release,&
40 USE pw_types, ONLY: pw_r3d_rs_type
41 USE util, ONLY: get_limit
42
43!$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
44
45#include "../base/base_uses.f90"
46
47 IMPLICIT NONE
48
49 PRIVATE
50 PUBLIC :: realspace_grid_type, &
55
56 PUBLIC :: transfer_rs2pw, &
71
72 INTEGER, PARAMETER, PUBLIC :: rsgrid_distributed = 0, &
75
76 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .false.
77 CHARACTER(len=*), PARAMETER, PRIVATE :: modulen = 'realspace_grid_types'
78
79! **************************************************************************************************
81 INTEGER :: distribution_type = rsgrid_replicated
82 INTEGER :: distribution_layout(3) = -1
83 REAL(kind=dp) :: memory_factor = 0.0_dp
84 LOGICAL :: lock_distribution = .false.
85 INTEGER :: nsmax = -1
86 REAL(kind=dp) :: halo_reduction_factor = 1.0_dp
88
89! **************************************************************************************************
91 TYPE(pw_grid_type), POINTER :: pw => null() ! the pw grid
92
93 INTEGER :: ref_count = 0 ! reference count
94
95 INTEGER(int_8) :: ngpts = 0_int_8 ! # grid points
96 INTEGER, DIMENSION(3) :: npts = 0 ! # grid points per dimension
97 INTEGER, DIMENSION(3) :: lb = 0 ! lower bounds
98 INTEGER, DIMENSION(3) :: ub = 0 ! upper bounds
99
100 INTEGER :: border = 0 ! border points
101
102 INTEGER, DIMENSION(3) :: perd = -1 ! periodicity enforced
103 REAL(kind=dp), DIMENSION(3, 3) :: dh = 0.0_dp ! incremental grid matrix
104 REAL(kind=dp), DIMENSION(3, 3) :: dh_inv = 0.0_dp ! inverse incremental grid matrix
105 LOGICAL :: orthorhombic = .true. ! grid symmetry
106
107 LOGICAL :: parallel = .true. ! whether the corresponding pw grid is distributed
108 LOGICAL :: distributed = .true. ! whether the rs grid is distributed
109 ! these MPI related quantities are only meaningful depending on how the grid has been laid out
110 ! they are most useful for fully distributed grids, where they reflect the topology of the grid
111 TYPE(mp_comm_type) :: group = mp_comm_null
112 INTEGER :: my_pos = -1
113 INTEGER :: group_size = 0
114 INTEGER, DIMENSION(3) :: group_dim = -1
115 INTEGER, DIMENSION(3) :: group_coor = -1
116 INTEGER, DIMENSION(3) :: neighbours = -1
117 ! only meaningful on distributed grids
118 ! a list of bounds for each CPU
119 INTEGER, DIMENSION(:, :), ALLOCATABLE :: lb_global
120 INTEGER, DIMENSION(:, :), ALLOCATABLE :: ub_global
121 ! a mapping from linear rank to 3d coord
122 INTEGER, DIMENSION(:, :), ALLOCATABLE :: rank2coord
123 INTEGER, DIMENSION(:, :, :), ALLOCATABLE :: coord2rank
124 ! a mapping from index to rank (which allows to figure out easily on which rank a given point of the grid is)
125 INTEGER, DIMENSION(:), ALLOCATABLE :: x2coord
126 INTEGER, DIMENSION(:), ALLOCATABLE :: y2coord
127 INTEGER, DIMENSION(:), ALLOCATABLE :: z2coord
128
129 INTEGER :: my_virtual_pos = -1
130 INTEGER, DIMENSION(3) :: virtual_group_coor = -1
131
132 INTEGER, DIMENSION(:), ALLOCATABLE :: virtual2real, real2virtual
133
135
137
138 TYPE(realspace_grid_desc_type), POINTER :: desc => null()
139
140 INTEGER :: ngpts_local = -1 ! local dimensions
141 INTEGER, DIMENSION(3) :: npts_local = -1
142 INTEGER, DIMENSION(3) :: lb_local = -1
143 INTEGER, DIMENSION(3) :: ub_local = -1
144 INTEGER, DIMENSION(3) :: lb_real = -1 ! lower bounds of the real local data
145 INTEGER, DIMENSION(3) :: ub_real = -1 ! upper bounds of the real local data
146
147 INTEGER, DIMENSION(:), ALLOCATABLE :: px, py, pz ! index translators
148 TYPE(offload_buffer_type) :: buffer = offload_buffer_type() ! owner of the grid's memory
149 REAL(kind=dp), DIMENSION(:, :, :), CONTIGUOUS, POINTER :: r => null() ! the grid (pointer to buffer%host_buffer)
150
151 END TYPE realspace_grid_type
152
153! **************************************************************************************************
155 TYPE(realspace_grid_type), POINTER :: rs_grid => null()
156 END TYPE realspace_grid_p_type
157
159 TYPE(realspace_grid_desc_type), POINTER :: rs_desc => null()
161
162CONTAINS
163
164! **************************************************************************************************
165!> \brief returns the 1D rank of the task which is a cartesian shift away from 1D rank rank_in
166!> only possible if rs_grid is a distributed grid
167!> \param rs_desc ...
168!> \param rank_in ...
169!> \param shift ...
170!> \return ...
171! **************************************************************************************************
172 PURE FUNCTION rs_grid_locate_rank(rs_desc, rank_in, shift) RESULT(rank_out)
173 TYPE(realspace_grid_desc_type), INTENT(IN) :: rs_desc
174 INTEGER, INTENT(IN) :: rank_in
175 INTEGER, DIMENSION(3), INTENT(IN) :: shift
176 INTEGER :: rank_out
177
178 INTEGER :: coord(3)
179
180 coord = modulo(rs_desc%rank2coord(:, rank_in) + shift, rs_desc%group_dim)
181 rank_out = rs_desc%coord2rank(coord(1), coord(2), coord(3))
182 END FUNCTION rs_grid_locate_rank
183
184! **************************************************************************************************
185!> \brief Determine the setup of real space grids - this is divided up into the
186!> creation of a descriptor and the actual grid itself (see rs_grid_create)
187!> \param desc ...
188!> \param pw_grid ...
189!> \param input_settings ...
190!> \param border_points ...
191!> \par History
192!> JGH (08-Jun-2003) : nsmax <= 0 indicates fully replicated grid
193!> Iain Bethune (05-Sep-2008) : modified cut heuristic
194!> (c) The Numerical Algorithms Group (NAG) Ltd, 2008 on behalf of the HECToR project
195!> - Create a descriptor for realspace grids with a number of border
196!> points as exactly given by the optional argument border_points.
197!> These grids are always distributed.
198!> (27.11.2013, Matthias Krack)
199!> \author JGH (18-Mar-2001)
200! **************************************************************************************************
201 SUBROUTINE rs_grid_create_descriptor(desc, pw_grid, input_settings, border_points)
202 TYPE(realspace_grid_desc_type), POINTER :: desc
203 TYPE(pw_grid_type), INTENT(INOUT), TARGET :: pw_grid
204 TYPE(realspace_grid_input_type), INTENT(IN) :: input_settings
205 INTEGER, INTENT(IN), OPTIONAL :: border_points
206
207 CHARACTER(LEN=*), PARAMETER :: routinen = 'rs_grid_create_descriptor'
208
209 INTEGER :: border_size, dir, handle, i, j, k, l, &
210 lb(2), min_npts_real, n_slices(3), &
211 n_slices_tmp(3), nmin
212 LOGICAL :: overlap
213 REAL(kind=dp) :: ratio, ratio_best, volume, volume_dist
214
215 CALL timeset(routinen, handle)
216
217 IF (PRESENT(border_points)) THEN
218 border_size = border_points
219 ELSE
220 border_size = 0
221 END IF
222
223 ALLOCATE (desc)
224
225 CALL pw_grid%para%group%sync()
226
227 desc%pw => pw_grid
228 CALL pw_grid_retain(desc%pw)
229
230 desc%dh = pw_grid%dh
231 desc%dh_inv = pw_grid%dh_inv
232 desc%orthorhombic = pw_grid%orthorhombic
233 desc%ref_count = 1
234
235 IF (pw_grid%para%mode == pw_mode_local) THEN
236 ! The corresponding group has dimension 1
237 ! All operations will be done locally
238 desc%npts = pw_grid%npts
239 desc%ngpts = product(int(desc%npts, kind=int_8))
240 desc%lb = pw_grid%bounds(1, :)
241 desc%ub = pw_grid%bounds(2, :)
242 desc%border = border_size
243 IF (border_size == 0) THEN
244 desc%perd = 1
245 ELSE
246 desc%perd = 0
247 END IF
248 desc%parallel = .false.
249 desc%distributed = .false.
250 desc%group = mp_comm_null
251 desc%group_size = 1
252 desc%group_dim = 1
253 desc%group_coor = 0
254 desc%my_pos = 0
255 ELSE
256 ! group size of desc grid
257 ! global grid dimensions are still the same
258 desc%group_size = pw_grid%para%group%num_pe
259 desc%npts = pw_grid%npts
260 desc%ngpts = product(int(desc%npts, kind=int_8))
261 desc%lb = pw_grid%bounds(1, :)
262 desc%ub = pw_grid%bounds(2, :)
263
264 ! this is the eventual border size
265 IF (border_size == 0) THEN
266 nmin = (input_settings%nsmax + 1)/2
267 nmin = max(0, nint(nmin*input_settings%halo_reduction_factor))
268 ELSE
269 ! Set explicitly the requested border size
270 nmin = border_size
271 END IF
272
273 IF (input_settings%distribution_type == rsgrid_replicated) THEN
274
275 n_slices = 1
276 IF (border_size > 0) THEN
277 CALL cp_abort(__location__, &
278 "An explicit border size > 0 is not yet working for "// &
279 "replicated realspace grids. Request DISTRIBUTION_TYPE "// &
280 "distributed for RS_GRID explicitly.")
281 END IF
282
283 ELSE
284
285 n_slices = 1
286 ratio_best = -huge(ratio_best)
287
288 ! don't allow distributions with more processors than real grid points
289 DO k = 1, min(desc%npts(3), desc%group_size)
290 DO j = 1, min(desc%npts(2), desc%group_size)
291 i = min(desc%npts(1), desc%group_size/(j*k))
292 n_slices_tmp = [i, j, k]
293
294 ! we don't match the actual number of CPUs
295 IF (product(n_slices_tmp) /= desc%group_size) cycle
296
297 ! we see if there has been a input constraint
298 ! i.e. if the layout is not -1 we need to fullfil it
299 IF (.NOT. all(pack(n_slices_tmp == input_settings%distribution_layout, &
300 [-1, -1, -1] /= input_settings%distribution_layout) &
301 )) cycle
302
303 ! We can not work with a grid that has more local than global grid points.
304 ! This can happen when a halo region wraps around and overlaps with the other halo.
305 overlap = .false.
306 DO dir = 1, 3
307 IF (n_slices_tmp(dir) > 1) THEN
308 DO l = 0, n_slices_tmp(dir) - 1
309 lb = get_limit(desc%npts(dir), n_slices_tmp(dir), l)
310 IF (lb(2) - lb(1) + 1 + 2*nmin > desc%npts(dir)) overlap = .true.
311 END DO
312 END IF
313 END DO
314 IF (overlap) cycle
315
316 ! a heuristic optimisation to reduce the memory usage
317 ! we go for the smallest local to real volume
318 ! volume of the box without the wings / volume of the box with the wings
319 ! with prefactodesc to promote less cuts in Z dimension
320 ratio = product(real(desc%npts, kind=dp)/n_slices_tmp)/ &
321 product(real(desc%npts, kind=dp)/n_slices_tmp + &
322 merge([0.0, 0.0, 0.0], 2*[1.06*nmin, 1.05*nmin, 1.03*nmin], n_slices_tmp == [1, 1, 1]))
323 IF (ratio > ratio_best) THEN
324 ratio_best = ratio
325 n_slices = n_slices_tmp
326 END IF
327
328 END DO
329 END DO
330
331 ! if automatic we can still decide this is a replicated grid
332 ! if the memory gain (or the gain is messages) is too small.
333 IF (input_settings%distribution_type == rsgrid_automatic) THEN
334 volume = product(real(desc%npts, kind=dp))
335 volume_dist = product(real(desc%npts, kind=dp)/n_slices + &
336 merge([0, 0, 0], 2*[nmin, nmin, nmin], n_slices == [1, 1, 1]))
337 IF (volume < volume_dist*input_settings%memory_factor) THEN
338 n_slices = 1
339 END IF
340 END IF
341
342 END IF
343
344 desc%group_dim(:) = n_slices(:)
345 CALL desc%group%from_dup(pw_grid%para%group)
346 desc%group_size = desc%group%num_pe
347 desc%my_pos = desc%group%mepos
348
349 IF (all(n_slices == 1)) THEN
350 ! CASE 1 : only one slice: we do not need overlapping regions and special
351 ! recombination of the total density
352 desc%border = border_size
353 IF (border_size == 0) THEN
354 desc%perd = 1
355 ELSE
356 desc%perd = 0
357 END IF
358 desc%distributed = .false.
359 desc%parallel = .true.
360 desc%group_coor(:) = 0
361 desc%my_virtual_pos = 0
362
363 ALLOCATE (desc%virtual2real(0:desc%group_size - 1))
364 ALLOCATE (desc%real2virtual(0:desc%group_size - 1))
365 ! Start with no reordering
366 DO i = 0, desc%group_size - 1
367 desc%virtual2real(i) = i
368 desc%real2virtual(i) = i
369 END DO
370 ELSE
371 ! CASE 2 : general case
372 ! periodicity is no longer enforced arbritary directions
373 IF (border_size == 0) THEN
374 desc%perd = 1
375 DO dir = 1, 3
376 IF (n_slices(dir) > 1) desc%perd(dir) = 0
377 END DO
378 ELSE
379 desc%perd(:) = 0
380 END IF
381 ! we keep a border of nmin points
382 desc%border = nmin
383 ! we are going parallel on the real space grid
384 desc%parallel = .true.
385 desc%distributed = .true.
386
387 ! set up global info about the distribution
388 ALLOCATE (desc%rank2coord(3, 0:desc%group_size - 1))
389 ALLOCATE (desc%coord2rank(0:desc%group_dim(1) - 1, 0:desc%group_dim(2) - 1, 0:desc%group_dim(3) - 1))
390 ALLOCATE (desc%lb_global(3, 0:desc%group_size - 1))
391 ALLOCATE (desc%ub_global(3, 0:desc%group_size - 1))
392 ALLOCATE (desc%x2coord(desc%lb(1):desc%ub(1)))
393 ALLOCATE (desc%y2coord(desc%lb(2):desc%ub(2)))
394 ALLOCATE (desc%z2coord(desc%lb(3):desc%ub(3)))
395
396 DO i = 0, desc%group_size - 1
397 ! Calculate coordinates in a row-major order (to be SMP-friendly)
398 desc%rank2coord(1, i) = i/(desc%group_dim(2)*desc%group_dim(3))
399 desc%rank2coord(2, i) = modulo(i, desc%group_dim(2)*desc%group_dim(3)) &
400 /desc%group_dim(3)
401 desc%rank2coord(3, i) = modulo(i, desc%group_dim(3))
402
403 IF (i == desc%my_pos) THEN
404 desc%group_coor = desc%rank2coord(:, i)
405 END IF
406
407 desc%coord2rank(desc%rank2coord(1, i), desc%rank2coord(2, i), desc%rank2coord(3, i)) = i
408 ! the lb_global and ub_global correspond to lb_real and ub_real of each task
409 desc%lb_global(:, i) = desc%lb
410 desc%ub_global(:, i) = desc%ub
411 DO dir = 1, 3
412 IF (desc%group_dim(dir) > 1) THEN
413 lb = get_limit(desc%npts(dir), desc%group_dim(dir), desc%rank2coord(dir, i))
414 desc%lb_global(dir, i) = lb(1) + desc%lb(dir) - 1
415 desc%ub_global(dir, i) = lb(2) + desc%lb(dir) - 1
416 END IF
417 END DO
418 END DO
419
420 ! map a grid point to a CPU coord
421 DO dir = 1, 3
422 DO l = 0, desc%group_dim(dir) - 1
423 IF (desc%group_dim(dir) > 1) THEN
424 lb = get_limit(desc%npts(dir), desc%group_dim(dir), l)
425 lb = lb + desc%lb(dir) - 1
426 ELSE
427 lb(1) = desc%lb(dir)
428 lb(2) = desc%ub(dir)
429 END IF
430 SELECT CASE (dir)
431 CASE (1)
432 desc%x2coord(lb(1):lb(2)) = l
433 CASE (2)
434 desc%y2coord(lb(1):lb(2)) = l
435 CASE (3)
436 desc%z2coord(lb(1):lb(2)) = l
437 END SELECT
438 END DO
439 END DO
440
441 ! an upper bound for the number of neighbours the border is overlapping with
442 DO dir = 1, 3
443 desc%neighbours(dir) = 0
444 IF ((n_slices(dir) > 1) .OR. (border_size > 0)) THEN
445 min_npts_real = huge(0)
446 DO l = 0, n_slices(dir) - 1
447 lb = get_limit(desc%npts(dir), n_slices(dir), l)
448 min_npts_real = min(lb(2) - lb(1) + 1, min_npts_real)
449 END DO
450 desc%neighbours(dir) = (desc%border + min_npts_real - 1)/min_npts_real
451 END IF
452 END DO
453
454 ALLOCATE (desc%virtual2real(0:desc%group_size - 1))
455 ALLOCATE (desc%real2virtual(0:desc%group_size - 1))
456 ! Start with no reordering
457 DO i = 0, desc%group_size - 1
458 desc%virtual2real(i) = i
459 desc%real2virtual(i) = i
460 END DO
461
462 desc%my_virtual_pos = desc%real2virtual(desc%my_pos)
463 desc%virtual_group_coor(:) = desc%rank2coord(:, desc%my_virtual_pos)
464
465 END IF
466 END IF
467
468 CALL timestop(handle)
469
470 END SUBROUTINE rs_grid_create_descriptor
471
472! **************************************************************************************************
473!> \brief ...
474!> \param rs ...
475!> \param desc ...
476! **************************************************************************************************
477 SUBROUTINE rs_grid_create(rs, desc)
478 TYPE(realspace_grid_type), INTENT(OUT) :: rs
479 TYPE(realspace_grid_desc_type), INTENT(INOUT), &
480 TARGET :: desc
481
482 CHARACTER(LEN=*), PARAMETER :: routinen = 'rs_grid_create'
483
484 INTEGER :: handle
485
486 CALL timeset(routinen, handle)
487
488 rs%desc => desc
489 CALL rs_grid_retain_descriptor(rs%desc)
490
491 IF (desc%pw%para%mode == pw_mode_local) THEN
492 ! The corresponding group has dimension 1
493 ! All operations will be done locally
494 rs%lb_real = desc%lb
495 rs%ub_real = desc%ub
496 rs%lb_local = rs%lb_real - desc%border*(1 - desc%perd)
497 rs%ub_local = rs%ub_real + desc%border*(1 - desc%perd)
498 rs%npts_local = rs%ub_local - rs%lb_local + 1
499 rs%ngpts_local = product(rs%npts_local)
500 END IF
501
502 IF (all(rs%desc%group_dim == 1)) THEN
503 ! CASE 1 : only one slice: we do not need overlapping regions and special
504 ! recombination of the total density
505 rs%lb_real = desc%lb
506 rs%ub_real = desc%ub
507 rs%lb_local = rs%lb_real - desc%border*(1 - desc%perd)
508 rs%ub_local = rs%ub_real + desc%border*(1 - desc%perd)
509 rs%npts_local = rs%ub_local - rs%lb_local + 1
510 rs%ngpts_local = product(rs%npts_local)
511 ELSE
512 ! CASE 2 : general case
513 ! extract some more derived quantities about the local grid
514 rs%lb_real = desc%lb_global(:, desc%my_virtual_pos)
515 rs%ub_real = desc%ub_global(:, desc%my_virtual_pos)
516 rs%lb_local = rs%lb_real - desc%border*(1 - desc%perd)
517 rs%ub_local = rs%ub_real + desc%border*(1 - desc%perd)
518 rs%npts_local = rs%ub_local - rs%lb_local + 1
519 rs%ngpts_local = product(rs%npts_local)
520 END IF
521
522 CALL offload_create_buffer(rs%ngpts_local, rs%buffer)
523 rs%r(rs%lb_local(1):rs%ub_local(1), &
524 rs%lb_local(2):rs%ub_local(2), &
525 rs%lb_local(3):rs%ub_local(3)) => rs%buffer%host_buffer
526
527 ALLOCATE (rs%px(desc%npts(1)))
528 ALLOCATE (rs%py(desc%npts(2)))
529 ALLOCATE (rs%pz(desc%npts(3)))
530
531 CALL timestop(handle)
532
533 END SUBROUTINE rs_grid_create
534
535! **************************************************************************************************
536!> \brief Defines a new ordering of ranks on this realspace grid, recalculating
537!> the data bounds and reallocating the grid. As a result, each MPI process
538!> now has a real rank (i.e., its rank in the MPI communicator from the pw grid)
539!> and a virtual rank (the rank of the process where the data now owned by this
540!> process would reside in an ordinary cartesian distribution).
541!> NB. Since the grid size required may change, the caller should be sure to release
542!> and recreate the corresponding rs_grids
543!> The desc%real2virtual and desc%virtual2real arrays can be used to map
544!> a physical rank to the 'rank' of data owned by that process and vice versa
545!> \param desc ...
546!> \param real2virtual ...
547!> \par History
548!> 04-2009 created [Iain Bethune]
549!> (c) The Numerical Algorithms Group (NAG) Ltd, 2009 on behalf of the HECToR project
550! **************************************************************************************************
551 PURE SUBROUTINE rs_grid_reorder_ranks(desc, real2virtual)
552
553 TYPE(realspace_grid_desc_type), INTENT(INOUT) :: desc
554 INTEGER, DIMENSION(:), INTENT(IN) :: real2virtual
555
556 INTEGER :: i
557
558 desc%real2virtual(:) = real2virtual
559
560 DO i = 0, desc%group_size - 1
561 desc%virtual2real(desc%real2virtual(i)) = i
562 END DO
563
564 desc%my_virtual_pos = desc%real2virtual(desc%my_pos)
565
566 IF (.NOT. all(desc%group_dim == 1)) THEN
567 desc%virtual_group_coor(:) = desc%rank2coord(:, desc%my_virtual_pos)
568 END IF
569
570 END SUBROUTINE rs_grid_reorder_ranks
571
572! **************************************************************************************************
573!> \brief Print information on grids to output
574!> \param rs ...
575!> \param iounit ...
576!> \author JGH (17-May-2007)
577! **************************************************************************************************
578 SUBROUTINE rs_grid_print(rs, iounit)
579 TYPE(realspace_grid_type), INTENT(IN) :: rs
580 INTEGER, INTENT(in) :: iounit
581
582 INTEGER :: dir, i, nn
583 REAL(kind=dp) :: pp(3)
584
585 IF (rs%desc%parallel) THEN
586 IF (iounit > 0) THEN
587 WRITE (iounit, '(/,A,T71,I10)') &
588 " RS_GRID| Information for grid number ", rs%desc%pw%id_nr
589 DO i = 1, 3
590 WRITE (iounit, '(A,I3,T30,2I8,T62,A,T71,I10)') " RS_GRID| Bounds ", &
591 i, rs%desc%lb(i), rs%desc%ub(i), "Points:", rs%desc%npts(i)
592 END DO
593 IF (.NOT. rs%desc%distributed) THEN
594 WRITE (iounit, '(A)') " RS_GRID| Real space fully replicated"
595 WRITE (iounit, '(A,T71,I10)') &
596 " RS_GRID| Group size ", rs%desc%group_dim(2)
597 ELSE
598 DO dir = 1, 3
599 IF (rs%desc%perd(dir) /= 1) THEN
600 WRITE (iounit, '(A,T71,I3,A)') &
601 " RS_GRID| Real space distribution over ", rs%desc%group_dim(dir), " groups"
602 WRITE (iounit, '(A,T71,I10)') &
603 " RS_GRID| Real space distribution along direction ", dir
604 WRITE (iounit, '(A,T71,I10)') &
605 " RS_GRID| Border size ", rs%desc%border
606 END IF
607 END DO
608 END IF
609 END IF
610 IF (rs%desc%distributed) THEN
611 DO dir = 1, 3
612 IF (rs%desc%perd(dir) /= 1) THEN
613 nn = rs%npts_local(dir)
614 CALL rs%desc%group%sum(nn)
615 pp(1) = real(nn, kind=dp)/real(product(rs%desc%group_dim), kind=dp)
616 nn = rs%npts_local(dir)
617 CALL rs%desc%group%max(nn)
618 pp(2) = real(nn, kind=dp)
619 nn = rs%npts_local(dir)
620 CALL rs%desc%group%min(nn)
621 pp(3) = real(nn, kind=dp)
622 IF (iounit > 0) THEN
623 WRITE (iounit, '(A,T48,A)') " RS_GRID| Distribution", &
624 " Average Max Min"
625 WRITE (iounit, '(A,T45,F12.1,2I12)') " RS_GRID| Planes ", &
626 pp(1), nint(pp(2)), nint(pp(3))
627 END IF
628 END IF
629 END DO
630! WRITE ( iounit, '(/)' )
631 END IF
632 ELSE
633 IF (iounit > 0) THEN
634 WRITE (iounit, '(/,A,T71,I10)') &
635 " RS_GRID| Information for grid number ", rs%desc%pw%id_nr
636 DO i = 1, 3
637 WRITE (iounit, '(A,I3,T30,2I8,T62,A,T71,I10)') " RS_GRID| Bounds ", &
638 i, rs%desc%lb(i), rs%desc%ub(i), "Points:", rs%desc%npts(i)
639 END DO
640! WRITE ( iounit, '(/)' )
641 END IF
642 END IF
643
644 END SUBROUTINE rs_grid_print
645
646! **************************************************************************************************
647!> \brief ...
648!> \param rs ...
649!> \param pw ...
650! **************************************************************************************************
651 SUBROUTINE transfer_rs2pw(rs, pw)
652 TYPE(realspace_grid_type), INTENT(IN) :: rs
653 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw
654
655 CHARACTER(len=*), PARAMETER :: routinen = 'transfer_rs2pw'
656
657 INTEGER :: handle, handle2, i
658
659 CALL timeset(routinen, handle2)
660 CALL timeset(routinen//"_"//trim(adjustl(cp_to_string(ceiling(pw%pw_grid%cutoff/10)*10))), handle)
661
662 IF (.NOT. ASSOCIATED(rs%desc%pw, pw%pw_grid)) THEN
663 cpabort("Different rs and pw indentifiers")
664 END IF
665
666 IF (rs%desc%distributed) THEN
667 CALL transfer_rs2pw_distributed(rs, pw)
668 ELSE IF (rs%desc%parallel) THEN
669 CALL transfer_rs2pw_replicated(rs, pw)
670 ELSE ! treat simple serial case locally
671 IF (rs%desc%border == 0) THEN
672 CALL dcopy(SIZE(rs%r), rs%r, 1, pw%array, 1)
673 ELSE
674 cpassert(lbound(pw%array, 3) == rs%lb_real(3))
675!$OMP PARALLEL DO DEFAULT(NONE) SHARED(pw,rs)
676 DO i = rs%lb_real(3), rs%ub_real(3)
677 pw%array(:, :, i) = rs%r(rs%lb_real(1):rs%ub_real(1), &
678 rs%lb_real(2):rs%ub_real(2), i)
679 END DO
680!$OMP END PARALLEL DO
681 END IF
682 END IF
683
684 CALL timestop(handle)
685 CALL timestop(handle2)
686
687 END SUBROUTINE transfer_rs2pw
688
689! **************************************************************************************************
690!> \brief ...
691!> \param rs ...
692!> \param pw ...
693! **************************************************************************************************
694 SUBROUTINE transfer_pw2rs(rs, pw)
695
696 TYPE(realspace_grid_type), INTENT(IN) :: rs
697 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw
698
699 CHARACTER(len=*), PARAMETER :: routinen = 'transfer_pw2rs'
700
701 INTEGER :: handle, handle2, i, im, j, jm, k, km
702
703 CALL timeset(routinen, handle2)
704 CALL timeset(routinen//"_"//trim(adjustl(cp_to_string(ceiling(pw%pw_grid%cutoff/10)*10))), handle)
705
706 IF (.NOT. ASSOCIATED(rs%desc%pw, pw%pw_grid)) THEN
707 cpabort("Different rs and pw indentifiers")
708 END IF
709
710 IF (rs%desc%distributed) THEN
711 CALL transfer_pw2rs_distributed(rs, pw)
712 ELSE IF (rs%desc%parallel) THEN
713 CALL transfer_pw2rs_replicated(rs, pw)
714 ELSE ! treat simple serial case locally
715 IF (rs%desc%border == 0) THEN
716 CALL dcopy(SIZE(rs%r), pw%array, 1, rs%r, 1)
717 ELSE
718!$OMP PARALLEL DO DEFAULT(NONE) &
719!$OMP PRIVATE(i,im,j,jm,k,km) &
720!$OMP SHARED(pw,rs)
721 DO k = rs%lb_local(3), rs%ub_local(3)
722 IF (k < rs%lb_real(3)) THEN
723 km = k + rs%desc%npts(3)
724 ELSE IF (k > rs%ub_real(3)) THEN
725 km = k - rs%desc%npts(3)
726 ELSE
727 km = k
728 END IF
729 DO j = rs%lb_local(2), rs%ub_local(2)
730 IF (j < rs%lb_real(2)) THEN
731 jm = j + rs%desc%npts(2)
732 ELSE IF (j > rs%ub_real(2)) THEN
733 jm = j - rs%desc%npts(2)
734 ELSE
735 jm = j
736 END IF
737 DO i = rs%lb_local(1), rs%ub_local(1)
738 IF (i < rs%lb_real(1)) THEN
739 im = i + rs%desc%npts(1)
740 ELSE IF (i > rs%ub_real(1)) THEN
741 im = i - rs%desc%npts(1)
742 ELSE
743 im = i
744 END IF
745 rs%r(i, j, k) = pw%array(im, jm, km)
746 END DO
747 END DO
748 END DO
749!$OMP END PARALLEL DO
750 END IF
751 END IF
752
753 CALL timestop(handle)
754 CALL timestop(handle2)
755
756 END SUBROUTINE transfer_pw2rs
757
758! **************************************************************************************************
759!> \brief transfer from a realspace grid to a planewave grid
760!> \param rs ...
761!> \param pw ...
762! **************************************************************************************************
763 SUBROUTINE transfer_rs2pw_replicated(rs, pw)
764 TYPE(realspace_grid_type), INTENT(IN) :: rs
765 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw
766
767 INTEGER :: dest, ii, ip, ix, iy, iz, nma, nn, s(3), &
768 source
769 INTEGER, ALLOCATABLE, DIMENSION(:) :: rcount
770 INTEGER, DIMENSION(3) :: lb, ub
771 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: recvbuf, sendbuf, swaparray
772
773 associate(np => pw%pw_grid%para%group%num_pe, bo => pw%pw_grid%para%bo(1:2, 1:3, 0:pw%pw_grid%para%group%num_pe - 1, 1), &
774 pbo => pw%pw_grid%bounds, group => pw%pw_grid%para%group, mepos => pw%pw_grid%para%group%mepos, &
775 grid => rs%r)
776 ALLOCATE (rcount(0:np - 1))
777 DO ip = 1, np
778 rcount(ip - 1) = product(bo(2, :, ip) - bo(1, :, ip) + 1)
779 END DO
780 nma = maxval(rcount(0:np - 1))
781 ALLOCATE (sendbuf(nma), recvbuf(nma))
782 sendbuf = 1.0e99_dp; recvbuf = 1.0e99_dp ! init mpi'ed buffers to silence warnings under valgrind
783
784 !sample peak memory
785 CALL m_memory()
786
787 dest = modulo(mepos + 1, np)
788 source = modulo(mepos - 1, np)
789 sendbuf = 0.0_dp
790
791 DO ip = 1, np
792
793 lb = pbo(1, :) + bo(1, :, modulo(mepos - ip, np) + 1) - 1
794 ub = pbo(1, :) + bo(2, :, modulo(mepos - ip, np) + 1) - 1
795 ! this loop takes about the same time as the message passing call
796 ! notice that the range of ix is only a small fraction of the first index of grid
797 ! therefore it seems faster to have the second index as the innermost loop
798 ! if this runs on many cpus
799 ! tested on itanium, pentium4, opteron, ultrasparc...
800 s = ub - lb + 1
801 DO iz = lb(3), ub(3)
802 DO ix = lb(1), ub(1)
803 ii = (iz - lb(3))*s(1)*s(2) + (ix - lb(1)) + 1
804 DO iy = lb(2), ub(2)
805 sendbuf(ii) = sendbuf(ii) + grid(ix, iy, iz)
806 ii = ii + s(1)
807 END DO
808 END DO
809 END DO
810 IF (ip == np) EXIT
811 CALL group%sendrecv(sendbuf, dest, recvbuf, source, 13)
812 CALL move_alloc(sendbuf, swaparray)
813 CALL move_alloc(recvbuf, sendbuf)
814 CALL move_alloc(swaparray, recvbuf)
815 END DO
816 nn = rcount(mepos)
817 END associate
818
819 CALL dcopy(nn, sendbuf, 1, pw%array, 1)
820
821 DEALLOCATE (rcount)
822 DEALLOCATE (sendbuf)
823 DEALLOCATE (recvbuf)
824
825 END SUBROUTINE transfer_rs2pw_replicated
826
827! **************************************************************************************************
828!> \brief transfer from a planewave grid to a realspace grid
829!> \param rs ...
830!> \param pw ...
831! **************************************************************************************************
832 SUBROUTINE transfer_pw2rs_replicated(rs, pw)
833 TYPE(realspace_grid_type), INTENT(IN) :: rs
834 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw
835
836 INTEGER :: dest, i, ii, im, ip, ix, iy, iz, j, jm, &
837 k, km, nma, nn, source
838 INTEGER, ALLOCATABLE, DIMENSION(:) :: rcount
839 INTEGER, DIMENSION(3) :: lb, ub
840 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: recvbuf, sendbuf, swaparray
841 TYPE(mp_request_type), DIMENSION(2) :: req
842
843 associate(np => pw%pw_grid%para%group%num_pe, bo => pw%pw_grid%para%bo(1:2, 1:3, 0:pw%pw_grid%para%group%num_pe - 1, 1), &
844 pbo => pw%pw_grid%bounds, group => pw%pw_grid%para%group, mepos => pw%pw_grid%para%group%mepos, &
845 grid => rs%r)
846 ALLOCATE (rcount(0:np - 1))
847 DO ip = 1, np
848 rcount(ip - 1) = product(bo(2, :, ip) - bo(1, :, ip) + 1)
849 END DO
850 nma = maxval(rcount(0:np - 1))
851 ALLOCATE (sendbuf(nma), recvbuf(nma))
852 sendbuf = 1.0e99_dp; recvbuf = 1.0e99_dp ! init mpi'ed buffers to silence warnings under valgrind
853
854 !sample peak memory
855 CALL m_memory()
856
857 nn = rcount(mepos)
858 CALL dcopy(nn, pw%array, 1, sendbuf, 1)
859
860 dest = modulo(mepos + 1, np)
861 source = modulo(mepos - 1, np)
862
863 DO ip = 0, np - 1
864 ! we must shift the buffer only np-1 times around
865 IF (ip /= np - 1) THEN
866 CALL group%isendrecv(sendbuf, dest, recvbuf, source, &
867 req(1), req(2), 13)
868 END IF
869 lb = pbo(1, :) + bo(1, :, modulo(mepos - ip, np) + 1) - 1
870 ub = pbo(1, :) + bo(2, :, modulo(mepos - ip, np) + 1) - 1
871 ii = 0
872 ! this loop takes about the same time as the message passing call
873 ! If I read the code correctly then:
874 DO iz = lb(3), ub(3)
875 DO iy = lb(2), ub(2)
876 DO ix = lb(1), ub(1)
877 ii = ii + 1
878 grid(ix, iy, iz) = sendbuf(ii)
879 END DO
880 END DO
881 END DO
882 IF (ip /= np - 1) THEN
883 CALL mp_waitall(req)
884 END IF
885 CALL move_alloc(sendbuf, swaparray)
886 CALL move_alloc(recvbuf, sendbuf)
887 CALL move_alloc(swaparray, recvbuf)
888 END DO
889 IF (rs%desc%border > 0) THEN
890!$OMP PARALLEL DO DEFAULT(NONE) &
891!$OMP PRIVATE(i,im,j,jm,k,km) &
892!$OMP SHARED(rs)
893 DO k = rs%lb_local(3), rs%ub_local(3)
894 IF (k < rs%lb_real(3)) THEN
895 km = k + rs%desc%npts(3)
896 ELSE IF (k > rs%ub_real(3)) THEN
897 km = k - rs%desc%npts(3)
898 ELSE
899 km = k
900 END IF
901 DO j = rs%lb_local(2), rs%ub_local(2)
902 IF (j < rs%lb_real(2)) THEN
903 jm = j + rs%desc%npts(2)
904 ELSE IF (j > rs%ub_real(2)) THEN
905 jm = j - rs%desc%npts(2)
906 ELSE
907 jm = j
908 END IF
909 DO i = rs%lb_local(1), rs%ub_local(1)
910 IF (i < rs%lb_real(1)) THEN
911 im = i + rs%desc%npts(1)
912 ELSE IF (i > rs%ub_real(1)) THEN
913 im = i - rs%desc%npts(1)
914 ELSE
915 im = i
916 END IF
917 rs%r(i, j, k) = rs%r(im, jm, km)
918 END DO
919 END DO
920 END DO
921!$OMP END PARALLEL DO
922 END IF
923 END associate
924
925 DEALLOCATE (rcount)
926 DEALLOCATE (sendbuf)
927 DEALLOCATE (recvbuf)
928
929 END SUBROUTINE transfer_pw2rs_replicated
930
931! **************************************************************************************************
932!> \brief does the rs2pw transfer in the case where the rs grid is
933!> distributed (3D domain decomposition)
934!> \param rs ...
935!> \param pw ...
936!> \par History
937!> 12.2007 created [Matt Watkins]
938!> 9.2008 reduced amount of halo data sent [Iain Bethune]
939!> 10.2008 added non-blocking communication [Iain Bethune]
940!> 4.2009 added support for rank-reordering on the grid [Iain Bethune]
941!> 12.2009 added OMP and sparse alltoall [Iain Bethune]
942!> (c) The Numerical Algorithms Group (NAG) Ltd, 2008-2009 on behalf of the HECToR project
943!> \note
944!> the transfer is a two step procedure. For example, for the rs2pw transfer:
945!>
946!> 1) Halo-exchange in 3D so that the local part of the rs_grid contains the full data
947!> 2) an alltoall communication to redistribute the local rs_grid to the local pw_grid
948!>
949!> the halo exchange is most expensive on a large number of CPUs. Particular in this halo
950!> exchange is that the border region is rather large (e.g. 20 points) and that it might overlap
951!> with the central domain of several CPUs (i.e. next nearest neighbors)
952! **************************************************************************************************
953 SUBROUTINE transfer_rs2pw_distributed(rs, pw)
954 TYPE(realspace_grid_type), INTENT(IN) :: rs
955 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw
956
957 CHARACTER(LEN=200) :: error_string
958 INTEGER :: completed, dest_down, dest_up, i, idir, j, k, lb, my_id, my_pw_rank, my_rs_rank, &
959 n_shifts, nn, num_threads, position, source_down, source_up, ub, x, y, z
960 INTEGER, ALLOCATABLE, DIMENSION(:) :: dshifts, recv_disps, recv_sizes, &
961 send_disps, send_sizes, ushifts
962 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: bounds, recv_tasks, send_tasks
963 INTEGER, DIMENSION(2) :: neighbours, pos
964 INTEGER, DIMENSION(3) :: coords, lb_recv, lb_recv_down, lb_recv_up, lb_send, lb_send_down, &
965 lb_send_up, ub_recv, ub_recv_down, ub_recv_up, ub_send, ub_send_down, ub_send_up
966 LOGICAL, DIMENSION(3) :: halo_swapped
967 REAL(kind=dp) :: pw_sum, rs_sum
968 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: recv_buf_3d_down, recv_buf_3d_up, &
969 send_buf_3d_down, send_buf_3d_up
970 TYPE(cp_1d_r_p_type), ALLOCATABLE, DIMENSION(:) :: recv_bufs, send_bufs
971 TYPE(mp_request_type), ALLOCATABLE, DIMENSION(:) :: recv_reqs, send_reqs
972 TYPE(mp_request_type), DIMENSION(4) :: req
973
974 num_threads = 1
975 my_id = 0
976
977 ! safety check, to be removed once we're absolute sure the routine is correct
978 IF (debug_this_module) THEN
979 rs_sum = accurate_sum(rs%r)*abs(det_3x3(rs%desc%dh))
980 CALL rs%desc%group%sum(rs_sum)
981 END IF
982
983 halo_swapped = .false.
984 ! We don't need to send the 'edges' of the halos that have already been sent
985 ! Halos are contiguous in memory in z-direction only, so swap these first,
986 ! and send less data in the y and x directions which are more expensive
987
988 DO idir = 3, 1, -1
989
990 IF (rs%desc%perd(idir) /= 1) THEN
991
992 ALLOCATE (dshifts(0:rs%desc%neighbours(idir)))
993 ALLOCATE (ushifts(0:rs%desc%neighbours(idir)))
994
995 ushifts = 0
996 dshifts = 0
997
998 ! check that we don't try to send data to ourself
999 DO n_shifts = 1, min(rs%desc%neighbours(idir), rs%desc%group_dim(idir) - 1)
1000
1001 ! need to take into account the possible varying widths of neighbouring cells
1002 ! offset_up and offset_down hold the real size of the neighbouring cells
1003 position = modulo(rs%desc%virtual_group_coor(idir) - n_shifts, rs%desc%group_dim(idir))
1004 neighbours = get_limit(rs%desc%npts(idir), rs%desc%group_dim(idir), position)
1005 dshifts(n_shifts) = dshifts(n_shifts - 1) + (neighbours(2) - neighbours(1) + 1)
1006
1007 position = modulo(rs%desc%virtual_group_coor(idir) + n_shifts, rs%desc%group_dim(idir))
1008 neighbours = get_limit(rs%desc%npts(idir), rs%desc%group_dim(idir), position)
1009 ushifts(n_shifts) = ushifts(n_shifts - 1) + (neighbours(2) - neighbours(1) + 1)
1010
1011 ! The border data has to be send/received from the neighbours
1012 ! First we calculate the source and destination processes for the shift
1013 ! We do both shifts at once to allow for more overlap of communication and buffer packing/unpacking
1014
1015 CALL cart_shift(rs, idir, -1*n_shifts, source_down, dest_down)
1016
1017 lb_send_down(:) = rs%lb_local(:)
1018 lb_recv_down(:) = rs%lb_local(:)
1019 ub_recv_down(:) = rs%ub_local(:)
1020 ub_send_down(:) = rs%ub_local(:)
1021
1022 IF (dshifts(n_shifts - 1) <= rs%desc%border) THEN
1023 ub_send_down(idir) = lb_send_down(idir) + rs%desc%border - 1 - dshifts(n_shifts - 1)
1024 lb_send_down(idir) = max(lb_send_down(idir), &
1025 lb_send_down(idir) + rs%desc%border - dshifts(n_shifts))
1026
1027 ub_recv_down(idir) = ub_recv_down(idir) - rs%desc%border
1028 lb_recv_down(idir) = max(lb_recv_down(idir) + rs%desc%border, &
1029 ub_recv_down(idir) - rs%desc%border + 1 + ushifts(n_shifts - 1))
1030 ELSE
1031 lb_send_down(idir) = 0
1032 ub_send_down(idir) = -1
1033 lb_recv_down(idir) = 0
1034 ub_recv_down(idir) = -1
1035 END IF
1036
1037 DO i = 1, 3
1038 IF (halo_swapped(i)) THEN
1039 lb_send_down(i) = rs%lb_real(i)
1040 ub_send_down(i) = rs%ub_real(i)
1041 lb_recv_down(i) = rs%lb_real(i)
1042 ub_recv_down(i) = rs%ub_real(i)
1043 END IF
1044 END DO
1045
1046 ! post the receive
1047 ALLOCATE (recv_buf_3d_down(lb_recv_down(1):ub_recv_down(1), &
1048 lb_recv_down(2):ub_recv_down(2), lb_recv_down(3):ub_recv_down(3)))
1049 CALL rs%desc%group%irecv(recv_buf_3d_down, source_down, req(1))
1050
1051 ! now allocate, pack and send the send buffer
1052 nn = product(ub_send_down - lb_send_down + 1)
1053 ALLOCATE (send_buf_3d_down(lb_send_down(1):ub_send_down(1), &
1054 lb_send_down(2):ub_send_down(2), lb_send_down(3):ub_send_down(3)))
1055
1056!$OMP PARALLEL DEFAULT(NONE), &
1057!$OMP PRIVATE(lb,ub,my_id,NUM_THREADS), &
1058!$OMP SHARED(send_buf_3d_down,rs,lb_send_down,ub_send_down)
1059!$ num_threads = MIN(omp_get_max_threads(), ub_send_down(3) - lb_send_down(3) + 1)
1060!$ my_id = omp_get_thread_num()
1061 IF (my_id < num_threads) THEN
1062 lb = lb_send_down(3) + ((ub_send_down(3) - lb_send_down(3) + 1)*my_id)/num_threads
1063 ub = lb_send_down(3) + ((ub_send_down(3) - lb_send_down(3) + 1)*(my_id + 1))/num_threads - 1
1064
1065 send_buf_3d_down(lb_send_down(1):ub_send_down(1), lb_send_down(2):ub_send_down(2), &
1066 lb:ub) = rs%r(lb_send_down(1):ub_send_down(1), &
1067 lb_send_down(2):ub_send_down(2), lb:ub)
1068 END IF
1069!$OMP END PARALLEL
1070
1071 CALL rs%desc%group%isend(send_buf_3d_down, dest_down, req(3))
1072
1073 ! Now for the other direction
1074 CALL cart_shift(rs, idir, n_shifts, source_up, dest_up)
1075
1076 lb_send_up(:) = rs%lb_local(:)
1077 lb_recv_up(:) = rs%lb_local(:)
1078 ub_recv_up(:) = rs%ub_local(:)
1079 ub_send_up(:) = rs%ub_local(:)
1080
1081 IF (ushifts(n_shifts - 1) <= rs%desc%border) THEN
1082
1083 lb_send_up(idir) = ub_send_up(idir) - rs%desc%border + 1 + ushifts(n_shifts - 1)
1084 ub_send_up(idir) = min(ub_send_up(idir), &
1085 ub_send_up(idir) - rs%desc%border + ushifts(n_shifts))
1086
1087 lb_recv_up(idir) = lb_recv_up(idir) + rs%desc%border
1088 ub_recv_up(idir) = min(ub_recv_up(idir) - rs%desc%border, &
1089 lb_recv_up(idir) + rs%desc%border - 1 - dshifts(n_shifts - 1))
1090 ELSE
1091 lb_send_up(idir) = 0
1092 ub_send_up(idir) = -1
1093 lb_recv_up(idir) = 0
1094 ub_recv_up(idir) = -1
1095 END IF
1096
1097 DO i = 1, 3
1098 IF (halo_swapped(i)) THEN
1099 lb_send_up(i) = rs%lb_real(i)
1100 ub_send_up(i) = rs%ub_real(i)
1101 lb_recv_up(i) = rs%lb_real(i)
1102 ub_recv_up(i) = rs%ub_real(i)
1103 END IF
1104 END DO
1105
1106 ! post the receive
1107 ALLOCATE (recv_buf_3d_up(lb_recv_up(1):ub_recv_up(1), &
1108 lb_recv_up(2):ub_recv_up(2), lb_recv_up(3):ub_recv_up(3)))
1109 CALL rs%desc%group%irecv(recv_buf_3d_up, source_up, req(2))
1110
1111 ! now allocate,pack and send the send buffer
1112 nn = product(ub_send_up - lb_send_up + 1)
1113 ALLOCATE (send_buf_3d_up(lb_send_up(1):ub_send_up(1), &
1114 lb_send_up(2):ub_send_up(2), lb_send_up(3):ub_send_up(3)))
1115
1116!$OMP PARALLEL DEFAULT(NONE), &
1117!$OMP PRIVATE(lb,ub,my_id,NUM_THREADS), &
1118!$OMP SHARED(send_buf_3d_up,rs,lb_send_up,ub_send_up)
1119!$ num_threads = MIN(omp_get_max_threads(), ub_send_up(3) - lb_send_up(3) + 1)
1120!$ my_id = omp_get_thread_num()
1121 IF (my_id < num_threads) THEN
1122 lb = lb_send_up(3) + ((ub_send_up(3) - lb_send_up(3) + 1)*my_id)/num_threads
1123 ub = lb_send_up(3) + ((ub_send_up(3) - lb_send_up(3) + 1)*(my_id + 1))/num_threads - 1
1124
1125 send_buf_3d_up(lb_send_up(1):ub_send_up(1), lb_send_up(2):ub_send_up(2), &
1126 lb:ub) = rs%r(lb_send_up(1):ub_send_up(1), &
1127 lb_send_up(2):ub_send_up(2), lb:ub)
1128 END IF
1129!$OMP END PARALLEL
1130
1131 CALL rs%desc%group%isend(send_buf_3d_up, dest_up, req(4))
1132
1133 ! wait for a recv to complete, then we can unpack
1134
1135 DO i = 1, 2
1136
1137 CALL mp_waitany(req(1:2), completed)
1138
1139 IF (completed == 1) THEN
1140
1141 ! only some procs may need later shifts
1142 IF (ub_recv_down(idir) >= lb_recv_down(idir)) THEN
1143 ! Sum the data in the RS Grid
1144!$OMP PARALLEL DEFAULT(NONE), &
1145!$OMP PRIVATE(lb,ub,my_id,NUM_THREADS), &
1146!$OMP SHARED(recv_buf_3d_down,rs,lb_recv_down,ub_recv_down)
1147!$ num_threads = MIN(omp_get_max_threads(), ub_recv_down(3) - lb_recv_down(3) + 1)
1148!$ my_id = omp_get_thread_num()
1149 IF (my_id < num_threads) THEN
1150 lb = lb_recv_down(3) + ((ub_recv_down(3) - lb_recv_down(3) + 1)*my_id)/num_threads
1151 ub = lb_recv_down(3) + ((ub_recv_down(3) - lb_recv_down(3) + 1)*(my_id + 1))/num_threads - 1
1152
1153 rs%r(lb_recv_down(1):ub_recv_down(1), &
1154 lb_recv_down(2):ub_recv_down(2), lb:ub) = &
1155 rs%r(lb_recv_down(1):ub_recv_down(1), &
1156 lb_recv_down(2):ub_recv_down(2), lb:ub) + &
1157 recv_buf_3d_down(:, :, lb:ub)
1158 END IF
1159!$OMP END PARALLEL
1160 END IF
1161 DEALLOCATE (recv_buf_3d_down)
1162 ELSE
1163
1164 ! only some procs may need later shifts
1165 IF (ub_recv_up(idir) >= lb_recv_up(idir)) THEN
1166 ! Sum the data in the RS Grid
1167!$OMP PARALLEL DEFAULT(NONE), &
1168!$OMP PRIVATE(lb,ub,my_id,NUM_THREADS), &
1169!$OMP SHARED(recv_buf_3d_up,rs,lb_recv_up,ub_recv_up)
1170!$ num_threads = MIN(omp_get_max_threads(), ub_recv_up(3) - lb_recv_up(3) + 1)
1171!$ my_id = omp_get_thread_num()
1172 IF (my_id < num_threads) THEN
1173 lb = lb_recv_up(3) + ((ub_recv_up(3) - lb_recv_up(3) + 1)*my_id)/num_threads
1174 ub = lb_recv_up(3) + ((ub_recv_up(3) - lb_recv_up(3) + 1)*(my_id + 1))/num_threads - 1
1175
1176 rs%r(lb_recv_up(1):ub_recv_up(1), &
1177 lb_recv_up(2):ub_recv_up(2), lb:ub) = &
1178 rs%r(lb_recv_up(1):ub_recv_up(1), &
1179 lb_recv_up(2):ub_recv_up(2), lb:ub) + &
1180 recv_buf_3d_up(:, :, lb:ub)
1181 END IF
1182!$OMP END PARALLEL
1183 END IF
1184 DEALLOCATE (recv_buf_3d_up)
1185 END IF
1186
1187 END DO
1188
1189 ! make sure the sends have completed before we deallocate
1190
1191 CALL mp_waitall(req(3:4))
1192
1193 DEALLOCATE (send_buf_3d_down)
1194 DEALLOCATE (send_buf_3d_up)
1195 END DO
1196
1197 DEALLOCATE (dshifts)
1198 DEALLOCATE (ushifts)
1199
1200 END IF
1201
1202 halo_swapped(idir) = .true.
1203
1204 END DO
1205
1206 ! This is the real redistribution
1207 ALLOCATE (bounds(0:pw%pw_grid%para%group%num_pe - 1, 1:4))
1208
1209 ! work out the pw grid points each proc holds
1210 DO i = 0, pw%pw_grid%para%group%num_pe - 1
1211 bounds(i, 1:2) = pw%pw_grid%para%bo(1:2, 1, i, 1)
1212 bounds(i, 3:4) = pw%pw_grid%para%bo(1:2, 2, i, 1)
1213 bounds(i, 1:2) = bounds(i, 1:2) - pw%pw_grid%npts(1)/2 - 1
1214 bounds(i, 3:4) = bounds(i, 3:4) - pw%pw_grid%npts(2)/2 - 1
1215 END DO
1216
1217 ALLOCATE (send_tasks(0:pw%pw_grid%para%group%num_pe - 1, 1:6))
1218 ALLOCATE (send_sizes(0:pw%pw_grid%para%group%num_pe - 1))
1219 ALLOCATE (send_disps(0:pw%pw_grid%para%group%num_pe - 1))
1220 ALLOCATE (recv_tasks(0:pw%pw_grid%para%group%num_pe - 1, 1:6))
1221 ALLOCATE (recv_sizes(0:pw%pw_grid%para%group%num_pe - 1))
1222 ALLOCATE (recv_disps(0:pw%pw_grid%para%group%num_pe - 1))
1223 send_tasks(:, 1) = 1
1224 send_tasks(:, 2) = 0
1225 send_tasks(:, 3) = 1
1226 send_tasks(:, 4) = 0
1227 send_tasks(:, 5) = 1
1228 send_tasks(:, 6) = 0
1229 send_sizes = 0
1230 recv_sizes = 0
1231
1232 my_rs_rank = rs%desc%my_pos
1233 my_pw_rank = pw%pw_grid%para%group%mepos
1234
1235 ! find the processors that should hold our data
1236 ! should be part of the rs grid type
1237 ! this is a loop over real ranks (i.e. the in-order cartesian ranks)
1238 ! do the recv and send tasks in two separate loops which will
1239 ! load balance better for OpenMP with large numbers of MPI tasks
1240
1241!$OMP PARALLEL DO DEFAULT(NONE), &
1242!$OMP PRIVATE(coords,idir,pos,lb_send,ub_send), &
1243!$OMP SHARED(rs,bounds,my_rs_rank,recv_tasks,recv_sizes)
1244 DO i = 0, rs%desc%group_size - 1
1245
1246 coords(:) = rs%desc%rank2coord(:, rs%desc%real2virtual(i))
1247 !calculate the rs grid points on each processor
1248 !coords is the part of the grid that rank i actually holds
1249 DO idir = 1, 3
1250 pos(:) = get_limit(rs%desc%npts(idir), rs%desc%group_dim(idir), coords(idir))
1251 pos(:) = pos(:) - rs%desc%npts(idir)/2 - 1
1252 lb_send(idir) = pos(1)
1253 ub_send(idir) = pos(2)
1254 END DO
1255
1256 IF (lb_send(1) > bounds(my_rs_rank, 2)) cycle
1257 IF (ub_send(1) < bounds(my_rs_rank, 1)) cycle
1258 IF (lb_send(2) > bounds(my_rs_rank, 4)) cycle
1259 IF (ub_send(2) < bounds(my_rs_rank, 3)) cycle
1260
1261 recv_tasks(i, 1) = max(lb_send(1), bounds(my_rs_rank, 1))
1262 recv_tasks(i, 2) = min(ub_send(1), bounds(my_rs_rank, 2))
1263 recv_tasks(i, 3) = max(lb_send(2), bounds(my_rs_rank, 3))
1264 recv_tasks(i, 4) = min(ub_send(2), bounds(my_rs_rank, 4))
1265 recv_tasks(i, 5) = lb_send(3)
1266 recv_tasks(i, 6) = ub_send(3)
1267 recv_sizes(i) = (recv_tasks(i, 2) - recv_tasks(i, 1) + 1)* &
1268 (recv_tasks(i, 4) - recv_tasks(i, 3) + 1)*(recv_tasks(i, 6) - recv_tasks(i, 5) + 1)
1269
1270 END DO
1271!$OMP END PARALLEL DO
1272
1273 coords(:) = rs%desc%rank2coord(:, rs%desc%real2virtual(my_rs_rank))
1274 DO idir = 1, 3
1275 pos(:) = get_limit(rs%desc%npts(idir), rs%desc%group_dim(idir), coords(idir))
1276 pos(:) = pos(:) - rs%desc%npts(idir)/2 - 1
1277 lb_send(idir) = pos(1)
1278 ub_send(idir) = pos(2)
1279 END DO
1280
1281 lb_recv(:) = lb_send(:)
1282 ub_recv(:) = ub_send(:)
1283!$OMP PARALLEL DO DEFAULT(NONE), &
1284!$OMP SHARED(pw,lb_send,ub_send,bounds,send_tasks,send_sizes)
1285 DO j = 0, pw%pw_grid%para%group%num_pe - 1
1286
1287 IF (lb_send(1) > bounds(j, 2)) cycle
1288 IF (ub_send(1) < bounds(j, 1)) cycle
1289 IF (lb_send(2) > bounds(j, 4)) cycle
1290 IF (ub_send(2) < bounds(j, 3)) cycle
1291
1292 send_tasks(j, 1) = max(lb_send(1), bounds(j, 1))
1293 send_tasks(j, 2) = min(ub_send(1), bounds(j, 2))
1294 send_tasks(j, 3) = max(lb_send(2), bounds(j, 3))
1295 send_tasks(j, 4) = min(ub_send(2), bounds(j, 4))
1296 send_tasks(j, 5) = lb_send(3)
1297 send_tasks(j, 6) = ub_send(3)
1298 send_sizes(j) = (send_tasks(j, 2) - send_tasks(j, 1) + 1)* &
1299 (send_tasks(j, 4) - send_tasks(j, 3) + 1)*(send_tasks(j, 6) - send_tasks(j, 5) + 1)
1300
1301 END DO
1302!$OMP END PARALLEL DO
1303
1304 send_disps(0) = 0
1305 recv_disps(0) = 0
1306 DO i = 1, pw%pw_grid%para%group%num_pe - 1
1307 send_disps(i) = send_disps(i - 1) + send_sizes(i - 1)
1308 recv_disps(i) = recv_disps(i - 1) + recv_sizes(i - 1)
1309 END DO
1310
1311 cpassert(sum(send_sizes) == product(ub_recv - lb_recv + 1))
1312
1313 ALLOCATE (send_bufs(0:rs%desc%group_size - 1))
1314 ALLOCATE (recv_bufs(0:rs%desc%group_size - 1))
1315
1316 DO i = 0, rs%desc%group_size - 1
1317 IF (send_sizes(i) /= 0) THEN
1318 ALLOCATE (send_bufs(i)%array(send_sizes(i)))
1319 ELSE
1320 NULLIFY (send_bufs(i)%array)
1321 END IF
1322 IF (recv_sizes(i) /= 0) THEN
1323 ALLOCATE (recv_bufs(i)%array(recv_sizes(i)))
1324 ELSE
1325 NULLIFY (recv_bufs(i)%array)
1326 END IF
1327 END DO
1328
1329 ALLOCATE (recv_reqs(0:rs%desc%group_size - 1))
1330 recv_reqs = mp_request_null
1331
1332 DO i = 0, rs%desc%group_size - 1
1333 IF (recv_sizes(i) /= 0) THEN
1334 CALL rs%desc%group%irecv(recv_bufs(i)%array, i, recv_reqs(i))
1335 END IF
1336 END DO
1337
1338 ! do packing
1339!$OMP PARALLEL DO DEFAULT(NONE), &
1340!$OMP PRIVATE(k,z,y,x), &
1341!$OMP SHARED(rs,send_tasks,send_bufs,send_disps)
1342 DO i = 0, rs%desc%group_size - 1
1343 k = 0
1344 DO z = send_tasks(i, 5), send_tasks(i, 6)
1345 DO y = send_tasks(i, 3), send_tasks(i, 4)
1346 DO x = send_tasks(i, 1), send_tasks(i, 2)
1347 k = k + 1
1348 send_bufs(i)%array(k) = rs%r(x, y, z)
1349 END DO
1350 END DO
1351 END DO
1352 END DO
1353!$OMP END PARALLEL DO
1354
1355 ALLOCATE (send_reqs(0:rs%desc%group_size - 1))
1356 send_reqs = mp_request_null
1357
1358 DO i = 0, rs%desc%group_size - 1
1359 IF (send_sizes(i) /= 0) THEN
1360 CALL rs%desc%group%isend(send_bufs(i)%array, i, send_reqs(i))
1361 END IF
1362 END DO
1363
1364 ! do unpacking
1365 ! no OMP here so we can unpack each message as it arrives
1366 DO i = 0, rs%desc%group_size - 1
1367 IF (recv_sizes(i) == 0) cycle
1368
1369 CALL mp_waitany(recv_reqs, completed)
1370 k = 0
1371 DO z = recv_tasks(completed - 1, 5), recv_tasks(completed - 1, 6)
1372 DO y = recv_tasks(completed - 1, 3), recv_tasks(completed - 1, 4)
1373 DO x = recv_tasks(completed - 1, 1), recv_tasks(completed - 1, 2)
1374 k = k + 1
1375 pw%array(x, y, z) = recv_bufs(completed - 1)%array(k)
1376 END DO
1377 END DO
1378 END DO
1379 END DO
1380
1381 CALL mp_waitall(send_reqs)
1382
1383 DEALLOCATE (recv_reqs)
1384 DEALLOCATE (send_reqs)
1385
1386 DO i = 0, rs%desc%group_size - 1
1387 IF (ASSOCIATED(send_bufs(i)%array)) THEN
1388 DEALLOCATE (send_bufs(i)%array)
1389 END IF
1390 IF (ASSOCIATED(recv_bufs(i)%array)) THEN
1391 DEALLOCATE (recv_bufs(i)%array)
1392 END IF
1393 END DO
1394
1395 DEALLOCATE (send_bufs)
1396 DEALLOCATE (recv_bufs)
1397 DEALLOCATE (send_tasks)
1398 DEALLOCATE (send_sizes)
1399 DEALLOCATE (send_disps)
1400 DEALLOCATE (recv_tasks)
1401 DEALLOCATE (recv_sizes)
1402 DEALLOCATE (recv_disps)
1403
1404 IF (debug_this_module) THEN
1405 ! safety check, to be removed once we're absolute sure the routine is correct
1406 pw_sum = pw_integrate_function(pw)
1407 IF (abs(pw_sum - rs_sum)/max(1.0_dp, abs(pw_sum), abs(rs_sum)) > epsilon(rs_sum)*1000) THEN
1408 WRITE (error_string, '(A,6(1X,I4.4),3F25.16)') "rs_pw_transfer_distributed", &
1409 rs%desc%npts, rs%desc%group_dim, pw_sum, rs_sum, abs(pw_sum - rs_sum)
1410 CALL cp_abort(__location__, &
1411 error_string//" Please report this bug ... quick workaround: use "// &
1412 "DISTRIBUTION_TYPE REPLICATED")
1413 END IF
1414 END IF
1415
1416 END SUBROUTINE transfer_rs2pw_distributed
1417
1418! **************************************************************************************************
1419!> \brief does the pw2rs transfer in the case where the rs grid is
1420!> distributed (3D domain decomposition)
1421!> \param rs ...
1422!> \param pw ...
1423!> \par History
1424!> 12.2007 created [Matt Watkins]
1425!> 9.2008 reduced amount of halo data sent [Iain Bethune]
1426!> 10.2008 added non-blocking communication [Iain Bethune]
1427!> 4.2009 added support for rank-reordering on the grid [Iain Bethune]
1428!> 12.2009 added OMP and sparse alltoall [Iain Bethune]
1429!> (c) The Numerical Algorithms Group (NAG) Ltd, 2008-2009 on behalf of the HECToR project
1430!> \note
1431!> the transfer is a two step procedure. For example, for the rs2pw transfer:
1432!>
1433!> 1) Halo-exchange in 3D so that the local part of the rs_grid contains the full data
1434!> 2) an alltoall communication to redistribute the local rs_grid to the local pw_grid
1435!>
1436!> the halo exchange is most expensive on a large number of CPUs. Particular in this halo
1437!> exchange is that the border region is rather large (e.g. 20 points) and that it might overlap
1438!> with the central domain of several CPUs (i.e. next nearest neighbors)
1439! **************************************************************************************************
1440 SUBROUTINE transfer_pw2rs_distributed(rs, pw)
1441 TYPE(realspace_grid_type), INTENT(IN) :: rs
1442 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw
1443
1444 INTEGER :: completed, dest_down, dest_up, i, idir, j, k, lb, my_id, my_pw_rank, my_rs_rank, &
1445 n_shifts, nn, num_threads, position, source_down, source_up, ub, x, y, z
1446 INTEGER, ALLOCATABLE, DIMENSION(:) :: dshifts, recv_disps, recv_sizes, &
1447 send_disps, send_sizes, ushifts
1448 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: bounds, recv_tasks, send_tasks
1449 INTEGER, DIMENSION(2) :: neighbours, pos
1450 INTEGER, DIMENSION(3) :: coords, lb_recv, lb_recv_down, lb_recv_up, lb_send, lb_send_down, &
1451 lb_send_up, ub_recv, ub_recv_down, ub_recv_up, ub_send, ub_send_down, ub_send_up
1452 LOGICAL, DIMENSION(3) :: halo_swapped
1453 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: recv_buf_3d_down, recv_buf_3d_up, &
1454 send_buf_3d_down, send_buf_3d_up
1455 TYPE(cp_1d_r_p_type), ALLOCATABLE, DIMENSION(:) :: recv_bufs, send_bufs
1456 TYPE(mp_request_type), ALLOCATABLE, DIMENSION(:) :: recv_reqs, send_reqs
1457 TYPE(mp_request_type), DIMENSION(4) :: req
1458
1459 num_threads = 1
1460 my_id = 0
1461
1462 CALL rs_grid_zero(rs)
1463
1464 ! This is the real redistribution
1465
1466 ALLOCATE (bounds(0:pw%pw_grid%para%group%num_pe - 1, 1:4))
1467
1468 DO i = 0, pw%pw_grid%para%group%num_pe - 1
1469 bounds(i, 1:2) = pw%pw_grid%para%bo(1:2, 1, i, 1)
1470 bounds(i, 3:4) = pw%pw_grid%para%bo(1:2, 2, i, 1)
1471 bounds(i, 1:2) = bounds(i, 1:2) - pw%pw_grid%npts(1)/2 - 1
1472 bounds(i, 3:4) = bounds(i, 3:4) - pw%pw_grid%npts(2)/2 - 1
1473 END DO
1474
1475 ALLOCATE (send_tasks(0:pw%pw_grid%para%group%num_pe - 1, 1:6))
1476 ALLOCATE (send_sizes(0:pw%pw_grid%para%group%num_pe - 1))
1477 ALLOCATE (send_disps(0:pw%pw_grid%para%group%num_pe - 1))
1478 ALLOCATE (recv_tasks(0:pw%pw_grid%para%group%num_pe - 1, 1:6))
1479 ALLOCATE (recv_sizes(0:pw%pw_grid%para%group%num_pe - 1))
1480 ALLOCATE (recv_disps(0:pw%pw_grid%para%group%num_pe - 1))
1481
1482 send_tasks = 0
1483 send_tasks(:, 1) = 1
1484 send_tasks(:, 2) = 0
1485 send_tasks(:, 3) = 1
1486 send_tasks(:, 4) = 0
1487 send_tasks(:, 5) = 1
1488 send_tasks(:, 6) = 0
1489 send_sizes = 0
1490
1491 recv_tasks = 0
1492 recv_tasks(:, 1) = 1
1493 recv_tasks(:, 2) = 0
1494 send_tasks(:, 3) = 1
1495 send_tasks(:, 4) = 0
1496 send_tasks(:, 5) = 1
1497 send_tasks(:, 6) = 0
1498 recv_sizes = 0
1499
1500 my_rs_rank = rs%desc%my_pos
1501 my_pw_rank = pw%pw_grid%para%group%mepos
1502
1503 ! find the processors that should hold our data
1504 ! should be part of the rs grid type
1505 ! this is a loop over real ranks (i.e. the in-order cartesian ranks)
1506 ! do the recv and send tasks in two separate loops which will
1507 ! load balance better for OpenMP with large numbers of MPI tasks
1508
1509 ! this is the reverse of rs2pw: what were the sends are now the recvs
1510
1511!$OMP PARALLEL DO DEFAULT(NONE), &
1512!$OMP PRIVATE(coords,idir,pos,lb_send,ub_send), &
1513!$OMP SHARED(rs,bounds,my_rs_rank,send_tasks,send_sizes,pw)
1514 DO i = 0, pw%pw_grid%para%group%num_pe - 1
1515
1516 coords(:) = rs%desc%rank2coord(:, rs%desc%real2virtual(i))
1517 !calculate the real rs grid points on each processor
1518 !coords is the part of the grid that rank i actually holds
1519 DO idir = 1, 3
1520 pos(:) = get_limit(rs%desc%npts(idir), rs%desc%group_dim(idir), coords(idir))
1521 pos(:) = pos(:) - rs%desc%npts(idir)/2 - 1
1522 lb_send(idir) = pos(1)
1523 ub_send(idir) = pos(2)
1524 END DO
1525
1526 IF (ub_send(1) < bounds(my_rs_rank, 1)) cycle
1527 IF (lb_send(1) > bounds(my_rs_rank, 2)) cycle
1528 IF (ub_send(2) < bounds(my_rs_rank, 3)) cycle
1529 IF (lb_send(2) > bounds(my_rs_rank, 4)) cycle
1530
1531 send_tasks(i, 1) = max(lb_send(1), bounds(my_rs_rank, 1))
1532 send_tasks(i, 2) = min(ub_send(1), bounds(my_rs_rank, 2))
1533 send_tasks(i, 3) = max(lb_send(2), bounds(my_rs_rank, 3))
1534 send_tasks(i, 4) = min(ub_send(2), bounds(my_rs_rank, 4))
1535 send_tasks(i, 5) = lb_send(3)
1536 send_tasks(i, 6) = ub_send(3)
1537 send_sizes(i) = (send_tasks(i, 2) - send_tasks(i, 1) + 1)* &
1538 (send_tasks(i, 4) - send_tasks(i, 3) + 1)*(send_tasks(i, 6) - send_tasks(i, 5) + 1)
1539
1540 END DO
1541!$OMP END PARALLEL DO
1542
1543 coords(:) = rs%desc%rank2coord(:, rs%desc%real2virtual(my_rs_rank))
1544 DO idir = 1, 3
1545 pos(:) = get_limit(rs%desc%npts(idir), rs%desc%group_dim(idir), coords(idir))
1546 pos(:) = pos(:) - rs%desc%npts(idir)/2 - 1
1547 lb_send(idir) = pos(1)
1548 ub_send(idir) = pos(2)
1549 END DO
1550
1551 lb_recv(:) = lb_send(:)
1552 ub_recv(:) = ub_send(:)
1553
1554!$OMP PARALLEL DO DEFAULT(NONE), &
1555!$OMP SHARED(pw,lb_send,ub_send,bounds,recv_tasks,recv_sizes)
1556 DO j = 0, pw%pw_grid%para%group%num_pe - 1
1557
1558 IF (ub_send(1) < bounds(j, 1)) cycle
1559 IF (lb_send(1) > bounds(j, 2)) cycle
1560 IF (ub_send(2) < bounds(j, 3)) cycle
1561 IF (lb_send(2) > bounds(j, 4)) cycle
1562
1563 recv_tasks(j, 1) = max(lb_send(1), bounds(j, 1))
1564 recv_tasks(j, 2) = min(ub_send(1), bounds(j, 2))
1565 recv_tasks(j, 3) = max(lb_send(2), bounds(j, 3))
1566 recv_tasks(j, 4) = min(ub_send(2), bounds(j, 4))
1567 recv_tasks(j, 5) = lb_send(3)
1568 recv_tasks(j, 6) = ub_send(3)
1569 recv_sizes(j) = (recv_tasks(j, 2) - recv_tasks(j, 1) + 1)* &
1570 (recv_tasks(j, 4) - recv_tasks(j, 3) + 1)*(recv_tasks(j, 6) - recv_tasks(j, 5) + 1)
1571
1572 END DO
1573!$OMP END PARALLEL DO
1574
1575 send_disps(0) = 0
1576 recv_disps(0) = 0
1577 DO i = 1, pw%pw_grid%para%group%num_pe - 1
1578 send_disps(i) = send_disps(i - 1) + send_sizes(i - 1)
1579 recv_disps(i) = recv_disps(i - 1) + recv_sizes(i - 1)
1580 END DO
1581
1582 cpassert(sum(recv_sizes) == product(ub_recv - lb_recv + 1))
1583
1584 ALLOCATE (send_bufs(0:rs%desc%group_size - 1))
1585 ALLOCATE (recv_bufs(0:rs%desc%group_size - 1))
1586
1587 DO i = 0, rs%desc%group_size - 1
1588 IF (send_sizes(i) /= 0) THEN
1589 ALLOCATE (send_bufs(i)%array(send_sizes(i)))
1590 ELSE
1591 NULLIFY (send_bufs(i)%array)
1592 END IF
1593 IF (recv_sizes(i) /= 0) THEN
1594 ALLOCATE (recv_bufs(i)%array(recv_sizes(i)))
1595 ELSE
1596 NULLIFY (recv_bufs(i)%array)
1597 END IF
1598 END DO
1599
1600 ALLOCATE (recv_reqs(0:rs%desc%group_size - 1))
1601 recv_reqs = mp_request_null
1602
1603 DO i = 0, rs%desc%group_size - 1
1604 IF (recv_sizes(i) /= 0) THEN
1605 CALL rs%desc%group%irecv(recv_bufs(i)%array, i, recv_reqs(i))
1606 END IF
1607 END DO
1608
1609 ! do packing
1610!$OMP PARALLEL DO DEFAULT(NONE), &
1611!$OMP PRIVATE(k,z,y,x), &
1612!$OMP SHARED(pw,rs,send_tasks,send_bufs,send_disps)
1613 DO i = 0, rs%desc%group_size - 1
1614 k = 0
1615 DO z = send_tasks(i, 5), send_tasks(i, 6)
1616 DO y = send_tasks(i, 3), send_tasks(i, 4)
1617 DO x = send_tasks(i, 1), send_tasks(i, 2)
1618 k = k + 1
1619 send_bufs(i)%array(k) = pw%array(x, y, z)
1620 END DO
1621 END DO
1622 END DO
1623 END DO
1624!$OMP END PARALLEL DO
1625
1626 ALLOCATE (send_reqs(0:rs%desc%group_size - 1))
1627 send_reqs = mp_request_null
1628
1629 DO i = 0, rs%desc%group_size - 1
1630 IF (send_sizes(i) /= 0) THEN
1631 CALL rs%desc%group%isend(send_bufs(i)%array, i, send_reqs(i))
1632 END IF
1633 END DO
1634
1635 ! do unpacking
1636 ! no OMP here so we can unpack each message as it arrives
1637
1638 DO i = 0, rs%desc%group_size - 1
1639 IF (recv_sizes(i) == 0) cycle
1640
1641 CALL mp_waitany(recv_reqs, completed)
1642 k = 0
1643 DO z = recv_tasks(completed - 1, 5), recv_tasks(completed - 1, 6)
1644 DO y = recv_tasks(completed - 1, 3), recv_tasks(completed - 1, 4)
1645 DO x = recv_tasks(completed - 1, 1), recv_tasks(completed - 1, 2)
1646 k = k + 1
1647 rs%r(x, y, z) = recv_bufs(completed - 1)%array(k)
1648 END DO
1649 END DO
1650 END DO
1651 END DO
1652
1653 CALL mp_waitall(send_reqs)
1654
1655 DEALLOCATE (recv_reqs)
1656 DEALLOCATE (send_reqs)
1657
1658 DO i = 0, rs%desc%group_size - 1
1659 IF (ASSOCIATED(send_bufs(i)%array)) THEN
1660 DEALLOCATE (send_bufs(i)%array)
1661 END IF
1662 IF (ASSOCIATED(recv_bufs(i)%array)) THEN
1663 DEALLOCATE (recv_bufs(i)%array)
1664 END IF
1665 END DO
1666
1667 DEALLOCATE (send_bufs)
1668 DEALLOCATE (recv_bufs)
1669 DEALLOCATE (send_tasks)
1670 DEALLOCATE (send_sizes)
1671 DEALLOCATE (send_disps)
1672 DEALLOCATE (recv_tasks)
1673 DEALLOCATE (recv_sizes)
1674 DEALLOCATE (recv_disps)
1675
1676 ! now pass wings around
1677 halo_swapped = .false.
1678
1679 DO idir = 1, 3
1680
1681 IF (rs%desc%perd(idir) /= 1) THEN
1682
1683 ALLOCATE (dshifts(0:rs%desc%neighbours(idir)))
1684 ALLOCATE (ushifts(0:rs%desc%neighbours(idir)))
1685 ushifts = 0
1686 dshifts = 0
1687
1688 DO n_shifts = 1, rs%desc%neighbours(idir)
1689
1690 ! need to take into account the possible varying widths of neighbouring cells
1691 ! ushifts and dshifts hold the real size of the neighbouring cells
1692
1693 position = modulo(rs%desc%virtual_group_coor(idir) - n_shifts, rs%desc%group_dim(idir))
1694 neighbours = get_limit(rs%desc%npts(idir), rs%desc%group_dim(idir), position)
1695 dshifts(n_shifts) = dshifts(n_shifts - 1) + (neighbours(2) - neighbours(1) + 1)
1696
1697 position = modulo(rs%desc%virtual_group_coor(idir) + n_shifts, rs%desc%group_dim(idir))
1698 neighbours = get_limit(rs%desc%npts(idir), rs%desc%group_dim(idir), position)
1699 ushifts(n_shifts) = ushifts(n_shifts - 1) + (neighbours(2) - neighbours(1) + 1)
1700
1701 ! The border data has to be send/received from the neighbors
1702 ! First we calculate the source and destination processes for the shift
1703 ! The first shift is "downwards"
1704
1705 CALL cart_shift(rs, idir, -1*n_shifts, source_down, dest_down)
1706
1707 lb_send_down(:) = rs%lb_local(:)
1708 ub_send_down(:) = rs%ub_local(:)
1709 lb_recv_down(:) = rs%lb_local(:)
1710 ub_recv_down(:) = rs%ub_local(:)
1711
1712 IF (dshifts(n_shifts - 1) <= rs%desc%border) THEN
1713 lb_send_down(idir) = lb_send_down(idir) + rs%desc%border
1714 ub_send_down(idir) = min(ub_send_down(idir) - rs%desc%border, &
1715 lb_send_down(idir) + rs%desc%border - 1 - dshifts(n_shifts - 1))
1716
1717 lb_recv_down(idir) = ub_recv_down(idir) - rs%desc%border + 1 + ushifts(n_shifts - 1)
1718 ub_recv_down(idir) = min(ub_recv_down(idir), &
1719 ub_recv_down(idir) - rs%desc%border + ushifts(n_shifts))
1720 ELSE
1721 lb_send_down(idir) = 0
1722 ub_send_down(idir) = -1
1723 lb_recv_down(idir) = 0
1724 ub_recv_down(idir) = -1
1725 END IF
1726
1727 DO i = 1, 3
1728 IF (.NOT. (halo_swapped(i) .OR. i == idir)) THEN
1729 lb_send_down(i) = rs%lb_real(i)
1730 ub_send_down(i) = rs%ub_real(i)
1731 lb_recv_down(i) = rs%lb_real(i)
1732 ub_recv_down(i) = rs%ub_real(i)
1733 END IF
1734 END DO
1735
1736 ! allocate the recv buffer
1737 nn = product(ub_recv_down - lb_recv_down + 1)
1738 ALLOCATE (recv_buf_3d_down(lb_recv_down(1):ub_recv_down(1), &
1739 lb_recv_down(2):ub_recv_down(2), lb_recv_down(3):ub_recv_down(3)))
1740
1741 ! recv buffer is now ready, so post the receive
1742 CALL rs%desc%group%irecv(recv_buf_3d_down, source_down, req(1))
1743
1744 ! now allocate,pack and send the send buffer
1745 nn = product(ub_send_down - lb_send_down + 1)
1746 ALLOCATE (send_buf_3d_down(lb_send_down(1):ub_send_down(1), &
1747 lb_send_down(2):ub_send_down(2), lb_send_down(3):ub_send_down(3)))
1748
1749!$OMP PARALLEL DEFAULT(NONE), &
1750!$OMP PRIVATE(lb,ub,my_id,NUM_THREADS), &
1751!$OMP SHARED(send_buf_3d_down,rs,lb_send_down,ub_send_down)
1752!$ num_threads = MIN(omp_get_max_threads(), ub_send_down(3) - lb_send_down(3) + 1)
1753!$ my_id = omp_get_thread_num()
1754 IF (my_id < num_threads) THEN
1755 lb = lb_send_down(3) + ((ub_send_down(3) - lb_send_down(3) + 1)*my_id)/num_threads
1756 ub = lb_send_down(3) + ((ub_send_down(3) - lb_send_down(3) + 1)*(my_id + 1))/num_threads - 1
1757
1758 send_buf_3d_down(lb_send_down(1):ub_send_down(1), lb_send_down(2):ub_send_down(2), &
1759 lb:ub) = rs%r(lb_send_down(1):ub_send_down(1), &
1760 lb_send_down(2):ub_send_down(2), lb:ub)
1761 END IF
1762!$OMP END PARALLEL
1763
1764 CALL rs%desc%group%isend(send_buf_3d_down, dest_down, req(3))
1765
1766 ! Now for the other direction
1767
1768 CALL cart_shift(rs, idir, n_shifts, source_up, dest_up)
1769
1770 lb_send_up(:) = rs%lb_local(:)
1771 ub_send_up(:) = rs%ub_local(:)
1772 lb_recv_up(:) = rs%lb_local(:)
1773 ub_recv_up(:) = rs%ub_local(:)
1774
1775 IF (ushifts(n_shifts - 1) <= rs%desc%border) THEN
1776 ub_send_up(idir) = ub_send_up(idir) - rs%desc%border
1777 lb_send_up(idir) = max(lb_send_up(idir) + rs%desc%border, &
1778 ub_send_up(idir) - rs%desc%border + 1 + ushifts(n_shifts - 1))
1779
1780 ub_recv_up(idir) = lb_recv_up(idir) + rs%desc%border - 1 - dshifts(n_shifts - 1)
1781 lb_recv_up(idir) = max(lb_recv_up(idir), &
1782 lb_recv_up(idir) + rs%desc%border - dshifts(n_shifts))
1783 ELSE
1784 lb_send_up(idir) = 0
1785 ub_send_up(idir) = -1
1786 lb_recv_up(idir) = 0
1787 ub_recv_up(idir) = -1
1788 END IF
1789
1790 DO i = 1, 3
1791 IF (.NOT. (halo_swapped(i) .OR. i == idir)) THEN
1792 lb_send_up(i) = rs%lb_real(i)
1793 ub_send_up(i) = rs%ub_real(i)
1794 lb_recv_up(i) = rs%lb_real(i)
1795 ub_recv_up(i) = rs%ub_real(i)
1796 END IF
1797 END DO
1798
1799 ! allocate the recv buffer
1800 nn = product(ub_recv_up - lb_recv_up + 1)
1801 ALLOCATE (recv_buf_3d_up(lb_recv_up(1):ub_recv_up(1), &
1802 lb_recv_up(2):ub_recv_up(2), lb_recv_up(3):ub_recv_up(3)))
1803
1804 ! recv buffer is now ready, so post the receive
1805
1806 CALL rs%desc%group%irecv(recv_buf_3d_up, source_up, req(2))
1807
1808 ! now allocate,pack and send the send buffer
1809 nn = product(ub_send_up - lb_send_up + 1)
1810 ALLOCATE (send_buf_3d_up(lb_send_up(1):ub_send_up(1), &
1811 lb_send_up(2):ub_send_up(2), lb_send_up(3):ub_send_up(3)))
1812
1813!$OMP PARALLEL DEFAULT(NONE), &
1814!$OMP PRIVATE(lb,ub,my_id,NUM_THREADS), &
1815!$OMP SHARED(send_buf_3d_up,rs,lb_send_up,ub_send_up)
1816!$ num_threads = MIN(omp_get_max_threads(), ub_send_up(3) - lb_send_up(3) + 1)
1817!$ my_id = omp_get_thread_num()
1818 IF (my_id < num_threads) THEN
1819 lb = lb_send_up(3) + ((ub_send_up(3) - lb_send_up(3) + 1)*my_id)/num_threads
1820 ub = lb_send_up(3) + ((ub_send_up(3) - lb_send_up(3) + 1)*(my_id + 1))/num_threads - 1
1821
1822 send_buf_3d_up(lb_send_up(1):ub_send_up(1), lb_send_up(2):ub_send_up(2), &
1823 lb:ub) = rs%r(lb_send_up(1):ub_send_up(1), &
1824 lb_send_up(2):ub_send_up(2), lb:ub)
1825 END IF
1826!$OMP END PARALLEL
1827
1828 CALL rs%desc%group%isend(send_buf_3d_up, dest_up, req(4))
1829
1830 ! wait for a recv to complete, then we can unpack
1831
1832 DO i = 1, 2
1833
1834 CALL mp_waitany(req(1:2), completed)
1835
1836 IF (completed == 1) THEN
1837
1838 ! only some procs may need later shifts
1839 IF (ub_recv_down(idir) >= lb_recv_down(idir)) THEN
1840
1841 ! Add the data to the RS Grid
1842!$OMP PARALLEL DEFAULT(NONE), &
1843!$OMP PRIVATE(lb,ub,my_id,NUM_THREADS), &
1844!$OMP SHARED(recv_buf_3d_down,rs,lb_recv_down,ub_recv_down)
1845!$ num_threads = MIN(omp_get_max_threads(), ub_recv_down(3) - lb_recv_down(3) + 1)
1846!$ my_id = omp_get_thread_num()
1847 IF (my_id < num_threads) THEN
1848 lb = lb_recv_down(3) + ((ub_recv_down(3) - lb_recv_down(3) + 1)*my_id)/num_threads
1849 ub = lb_recv_down(3) + ((ub_recv_down(3) - lb_recv_down(3) + 1)*(my_id + 1))/num_threads - 1
1850
1851 rs%r(lb_recv_down(1):ub_recv_down(1), lb_recv_down(2):ub_recv_down(2), &
1852 lb:ub) = recv_buf_3d_down(:, :, lb:ub)
1853 END IF
1854!$OMP END PARALLEL
1855 END IF
1856
1857 DEALLOCATE (recv_buf_3d_down)
1858 ELSE
1859
1860 ! only some procs may need later shifts
1861 IF (ub_recv_up(idir) >= lb_recv_up(idir)) THEN
1862
1863 ! Add the data to the RS Grid
1864!$OMP PARALLEL DEFAULT(NONE), &
1865!$OMP PRIVATE(lb,ub,my_id,NUM_THREADS), &
1866!$OMP SHARED(recv_buf_3d_up,rs,lb_recv_up,ub_recv_up)
1867!$ num_threads = MIN(omp_get_max_threads(), ub_recv_up(3) - lb_recv_up(3) + 1)
1868!$ my_id = omp_get_thread_num()
1869 IF (my_id < num_threads) THEN
1870 lb = lb_recv_up(3) + ((ub_recv_up(3) - lb_recv_up(3) + 1)*my_id)/num_threads
1871 ub = lb_recv_up(3) + ((ub_recv_up(3) - lb_recv_up(3) + 1)*(my_id + 1))/num_threads - 1
1872
1873 rs%r(lb_recv_up(1):ub_recv_up(1), lb_recv_up(2):ub_recv_up(2), &
1874 lb:ub) = recv_buf_3d_up(:, :, lb:ub)
1875 END IF
1876!$OMP END PARALLEL
1877 END IF
1878
1879 DEALLOCATE (recv_buf_3d_up)
1880 END IF
1881 END DO
1882
1883 CALL mp_waitall(req(3:4))
1884
1885 DEALLOCATE (send_buf_3d_down)
1886 DEALLOCATE (send_buf_3d_up)
1887 END DO
1888
1889 DEALLOCATE (ushifts)
1890 DEALLOCATE (dshifts)
1891 END IF
1892
1893 halo_swapped(idir) = .true.
1894
1895 END DO
1896
1897 END SUBROUTINE transfer_pw2rs_distributed
1898
1899! **************************************************************************************************
1900!> \brief Initialize grid to zero
1901!> \param rs ...
1902!> \par History
1903!> none
1904!> \author JGH (23-Mar-2002)
1905! **************************************************************************************************
1906 SUBROUTINE rs_grid_zero(rs)
1907
1908 TYPE(realspace_grid_type), INTENT(IN) :: rs
1909
1910 CHARACTER(len=*), PARAMETER :: routinen = 'rs_grid_zero'
1911
1912 INTEGER :: handle, i, j, k, l(3), u(3)
1913
1914 CALL timeset(routinen, handle)
1915 l(1) = lbound(rs%r, 1); l(2) = lbound(rs%r, 2); l(3) = lbound(rs%r, 3)
1916 u(1) = ubound(rs%r, 1); u(2) = ubound(rs%r, 2); u(3) = ubound(rs%r, 3)
1917!$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(3) &
1918!$OMP PRIVATE(i,j,k) &
1919!$OMP SHARED(rs,l,u)
1920 DO k = l(3), u(3)
1921 DO j = l(2), u(2)
1922 DO i = l(1), u(1)
1923 rs%r(i, j, k) = 0.0_dp
1924 END DO
1925 END DO
1926 END DO
1927!$OMP END PARALLEL DO
1928 CALL timestop(handle)
1929
1930 END SUBROUTINE rs_grid_zero
1931
1932! **************************************************************************************************
1933!> \brief rs1(i) = rs1(i) + rs2(i)*rs3(i)
1934!> \param rs1 ...
1935!> \param rs2 ...
1936!> \param rs3 ...
1937!> \param scalar ...
1938!> \par History
1939!> none
1940!> \author
1941! **************************************************************************************************
1942 SUBROUTINE rs_grid_mult_and_add(rs1, rs2, rs3, scalar)
1943
1944 TYPE(realspace_grid_type), INTENT(IN) :: rs1, rs2, rs3
1945 REAL(dp), INTENT(IN) :: scalar
1946
1947 CHARACTER(len=*), PARAMETER :: routinen = 'rs_grid_mult_and_add'
1948
1949 INTEGER :: handle, i, j, k, l(3), u(3)
1950
1951!-----------------------------------------------------------------------------!
1952
1953 CALL timeset(routinen, handle)
1954 IF (scalar /= 0.0_dp) THEN
1955 l(1) = lbound(rs1%r, 1); l(2) = lbound(rs1%r, 2); l(3) = lbound(rs1%r, 3)
1956 u(1) = ubound(rs1%r, 1); u(2) = ubound(rs1%r, 2); u(3) = ubound(rs1%r, 3)
1957!$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(3) &
1958!$OMP PRIVATE(i,j,k) &
1959!$OMP SHARED(rs1,rs2,rs3,scalar,l,u)
1960 DO k = l(3), u(3)
1961 DO j = l(2), u(2)
1962 DO i = l(1), u(1)
1963 rs1%r(i, j, k) = rs1%r(i, j, k) + scalar*rs2%r(i, j, k)*rs3%r(i, j, k)
1964 END DO
1965 END DO
1966 END DO
1967!$OMP END PARALLEL DO
1968 END IF
1969 CALL timestop(handle)
1970 END SUBROUTINE rs_grid_mult_and_add
1971
1972! **************************************************************************************************
1973!> \brief Set box matrix info for real space grid
1974!> This is needed for variable cell simulations
1975!> \param pw_grid ...
1976!> \param rs ...
1977!> \par History
1978!> none
1979!> \author JGH (15-May-2007)
1980! **************************************************************************************************
1981 SUBROUTINE rs_grid_set_box(pw_grid, rs)
1982
1983 TYPE(pw_grid_type), INTENT(IN), TARGET :: pw_grid
1984 TYPE(realspace_grid_type), INTENT(IN) :: rs
1985
1986 cpassert(ASSOCIATED(rs%desc%pw, pw_grid))
1987 rs%desc%dh = pw_grid%dh
1988 rs%desc%dh_inv = pw_grid%dh_inv
1989
1990 END SUBROUTINE rs_grid_set_box
1991
1992! **************************************************************************************************
1993!> \brief retains the given rs grid descriptor (see doc/ReferenceCounting.html)
1994!> \param rs_desc the grid descriptor to retain
1995!> \par History
1996!> 04.2009 created [Iain Bethune]
1997!> (c) The Numerical Algorithms Group (NAG) Ltd, 2009 on behalf of the HECToR project
1998! **************************************************************************************************
1999 SUBROUTINE rs_grid_retain_descriptor(rs_desc)
2000 TYPE(realspace_grid_desc_type), INTENT(INOUT) :: rs_desc
2001
2002 cpassert(rs_desc%ref_count > 0)
2003 rs_desc%ref_count = rs_desc%ref_count + 1
2004 END SUBROUTINE rs_grid_retain_descriptor
2005
2006! **************************************************************************************************
2007!> \brief releases the given rs grid (see doc/ReferenceCounting.html)
2008!> \param rs_grid the rs grid to release
2009!> \par History
2010!> 03.2003 created [fawzi]
2011!> \author fawzi
2012! **************************************************************************************************
2013 SUBROUTINE rs_grid_release(rs_grid)
2014 TYPE(realspace_grid_type), INTENT(INOUT) :: rs_grid
2015
2016 CALL rs_grid_release_descriptor(rs_grid%desc)
2017
2018 CALL offload_free_buffer(rs_grid%buffer)
2019 NULLIFY (rs_grid%r)
2020
2021 IF (ALLOCATED(rs_grid%px)) DEALLOCATE (rs_grid%px)
2022 IF (ALLOCATED(rs_grid%py)) DEALLOCATE (rs_grid%py)
2023 IF (ALLOCATED(rs_grid%pz)) DEALLOCATE (rs_grid%pz)
2024 END SUBROUTINE rs_grid_release
2025
2026! **************************************************************************************************
2027!> \brief releases the given rs grid descriptor (see doc/ReferenceCounting.html)
2028!> \param rs_desc the rs grid descriptor to release
2029!> \par History
2030!> 04.2009 created [Iain Bethune]
2031!> (c) The Numerical Algorithms Group (NAG) Ltd, 2009 on behalf of the HECToR project
2032! **************************************************************************************************
2033 SUBROUTINE rs_grid_release_descriptor(rs_desc)
2034 TYPE(realspace_grid_desc_type), POINTER :: rs_desc
2035
2036 IF (ASSOCIATED(rs_desc)) THEN
2037 cpassert(rs_desc%ref_count > 0)
2038 rs_desc%ref_count = rs_desc%ref_count - 1
2039 IF (rs_desc%ref_count == 0) THEN
2040
2041 CALL pw_grid_release(rs_desc%pw)
2042
2043 IF (rs_desc%parallel) THEN
2044 ! release the group communicator
2045 CALL rs_desc%group%free()
2046
2047 DEALLOCATE (rs_desc%virtual2real)
2048 DEALLOCATE (rs_desc%real2virtual)
2049 END IF
2050
2051 IF (rs_desc%distributed) THEN
2052 DEALLOCATE (rs_desc%rank2coord)
2053 DEALLOCATE (rs_desc%coord2rank)
2054 DEALLOCATE (rs_desc%lb_global)
2055 DEALLOCATE (rs_desc%ub_global)
2056 DEALLOCATE (rs_desc%x2coord)
2057 DEALLOCATE (rs_desc%y2coord)
2058 DEALLOCATE (rs_desc%z2coord)
2059 END IF
2060
2061 DEALLOCATE (rs_desc)
2062 END IF
2063 END IF
2064 NULLIFY (rs_desc)
2065 END SUBROUTINE rs_grid_release_descriptor
2066
2067! **************************************************************************************************
2068!> \brief emulates the function of an MPI_cart_shift operation, but the shift is
2069!> done in virtual coordinates, and the corresponding real ranks are returned
2070!> \param rs_grid ...
2071!> \param dir ...
2072!> \param disp ...
2073!> \param source ...
2074!> \param dest ...
2075!> \par History
2076!> 04.2009 created [Iain Bethune]
2077!> (c) The Numerical Algorithms Group (NAG) Ltd, 2009 on behalf of the HECToR project
2078! **************************************************************************************************
2079 PURE SUBROUTINE cart_shift(rs_grid, dir, disp, source, dest)
2080
2081 TYPE(realspace_grid_type), INTENT(IN) :: rs_grid
2082 INTEGER, INTENT(IN) :: dir, disp
2083 INTEGER, INTENT(OUT) :: source, dest
2084
2085 INTEGER, DIMENSION(3) :: shift_coords
2086
2087 shift_coords = rs_grid%desc%virtual_group_coor
2088 shift_coords(dir) = modulo(shift_coords(dir) + disp, rs_grid%desc%group_dim(dir))
2089 dest = rs_grid%desc%virtual2real(rs_grid%desc%coord2rank(shift_coords(1), shift_coords(2), shift_coords(3)))
2090 shift_coords = rs_grid%desc%virtual_group_coor
2091 shift_coords(dir) = modulo(shift_coords(dir) - disp, rs_grid%desc%group_dim(dir))
2092 source = rs_grid%desc%virtual2real(rs_grid%desc%coord2rank(shift_coords(1), shift_coords(2), shift_coords(3)))
2093
2094 END SUBROUTINE cart_shift
2095
2096! **************************************************************************************************
2097!> \brief returns the maximum number of points in the local grid of any process
2098!> to account for the case where the grid may later be reordered
2099!> \param desc ...
2100!> \return ...
2101!> \par History
2102!> 10.2011 created [Iain Bethune]
2103! **************************************************************************************************
2104 FUNCTION rs_grid_max_ngpts(desc) RESULT(max_ngpts)
2105 TYPE(realspace_grid_desc_type), INTENT(IN) :: desc
2106 INTEGER :: max_ngpts
2107
2108 CHARACTER(len=*), PARAMETER :: routinen = 'rs_grid_max_ngpts'
2109
2110 INTEGER :: handle, i
2111 INTEGER, DIMENSION(3) :: lb, ub
2112
2113 CALL timeset(routinen, handle)
2114
2115 max_ngpts = 0
2116 IF ((desc%pw%para%mode == pw_mode_local) .OR. &
2117 (all(desc%group_dim == 1))) THEN
2118 cpassert(product(int(desc%npts, kind=int_8)) < huge(1))
2119 max_ngpts = product(desc%npts)
2120 ELSE
2121 DO i = 0, desc%group_size - 1
2122 lb = desc%lb_global(:, i)
2123 ub = desc%ub_global(:, i)
2124 lb = lb - desc%border*(1 - desc%perd)
2125 ub = ub + desc%border*(1 - desc%perd)
2126 cpassert(product(int(ub - lb + 1, kind=int_8)) < huge(1))
2127 max_ngpts = max(max_ngpts, product(ub - lb + 1))
2128 END DO
2129 END IF
2130
2131 CALL timestop(handle)
2132
2133 END FUNCTION rs_grid_max_ngpts
2134
2135! **************************************************************************************************
2136!> \brief ...
2137!> \param rs_grid ...
2138!> \param h_inv ...
2139!> \param ra ...
2140!> \param offset ...
2141!> \param group_size ...
2142!> \param my_pos ...
2143!> \return ...
2144! **************************************************************************************************
2145 PURE LOGICAL FUNCTION map_gaussian_here(rs_grid, h_inv, ra, offset, group_size, my_pos) RESULT(res)
2146 TYPE(realspace_grid_type), INTENT(IN) :: rs_grid
2147 REAL(kind=dp), DIMENSION(3, 3), INTENT(IN) :: h_inv
2148 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: ra
2149 INTEGER, INTENT(IN), OPTIONAL :: offset, group_size, my_pos
2150
2151 INTEGER :: dir, lb(3), location(3), tp(3), ub(3)
2152
2153 res = .false.
2154
2155 IF (.NOT. all(rs_grid%desc%perd == 1)) THEN
2156 DO dir = 1, 3
2157 ! bounds of local grid (i.e. removing the 'wings'), if periodic
2158 tp(dir) = floor(dot_product(h_inv(dir, :), ra)*rs_grid%desc%npts(dir))
2159 tp(dir) = modulo(tp(dir), rs_grid%desc%npts(dir))
2160 IF (rs_grid%desc%perd(dir) /= 1) THEN
2161 lb(dir) = rs_grid%lb_local(dir) + rs_grid%desc%border
2162 ub(dir) = rs_grid%ub_local(dir) - rs_grid%desc%border
2163 ELSE
2164 lb(dir) = rs_grid%lb_local(dir)
2165 ub(dir) = rs_grid%ub_local(dir)
2166 END IF
2167 ! distributed grid, only map if it is local to the grid
2168 location(dir) = tp(dir) + rs_grid%desc%lb(dir)
2169 END DO
2170 IF (all(lb(:) <= location(:)) .AND. all(location(:) <= ub(:))) THEN
2171 res = .true.
2172 END IF
2173 ELSE
2174 IF (PRESENT(offset) .AND. PRESENT(group_size) .AND. PRESENT(my_pos)) THEN
2175 ! not distributed, just a round-robin distribution over the full set of CPUs
2176 IF (modulo(offset, group_size) == my_pos) res = .true.
2177 END IF
2178 END IF
2179
2180 END FUNCTION map_gaussian_here
2181
2182END MODULE realspace_grid_types
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
various utilities that regard array of different kinds: output, allocation,... maybe it is not a good...
various routines to log and control the output. The idea is that decisions about where to log should ...
sums arrays of real/complex numbers with much reduced round-off as compared to a naive implementation...
Definition kahan_sum.F:29
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
subroutine, public m_memory(mem)
Returns the total amount of memory [bytes] in use, if known, zero otherwise.
Definition machine.F:440
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
Interface to the message passing library MPI.
type(mp_comm_type), parameter, public mp_comm_null
subroutine, public mp_waitany(requests, completed)
waits for completion of any of the given requests
type(mp_request_type), parameter, public mp_request_null
Fortran API for the offload package, which is written in C.
Definition offload_api.F:12
subroutine, public offload_free_buffer(buffer)
Deallocates given buffer.
subroutine, public offload_create_buffer(length, buffer)
Allocates a buffer of given length, ie. number of elements.
integer, parameter, public pw_mode_local
This module defines the grid data type and some basic operations on it.
Definition pw_grids.F:36
subroutine, public pw_grid_release(pw_grid)
releases the given pw grid
Definition pw_grids.F:2163
subroutine, public pw_grid_retain(pw_grid)
retains the given pw grid
Definition pw_grids.F:2147
subroutine, public rs_grid_print(rs, iounit)
Print information on grids to output.
integer, parameter, public rsgrid_replicated
subroutine, public rs_grid_create(rs, desc)
...
subroutine, public rs_grid_create_descriptor(desc, pw_grid, input_settings, border_points)
Determine the setup of real space grids - this is divided up into the creation of a descriptor and th...
pure integer function, public rs_grid_locate_rank(rs_desc, rank_in, shift)
returns the 1D rank of the task which is a cartesian shift away from 1D rank rank_in only possible if...
subroutine, public transfer_pw2rs(rs, pw)
...
integer, parameter, public rsgrid_automatic
pure subroutine, public rs_grid_reorder_ranks(desc, real2virtual)
Defines a new ordering of ranks on this realspace grid, recalculating the data bounds and reallocatin...
subroutine, public rs_grid_mult_and_add(rs1, rs2, rs3, scalar)
rs1(i) = rs1(i) + rs2(i)*rs3(i)
subroutine, public rs_grid_set_box(pw_grid, rs)
Set box matrix info for real space grid This is needed for variable cell simulations.
subroutine, public rs_grid_retain_descriptor(rs_desc)
retains the given rs grid descriptor (see doc/ReferenceCounting.html)
subroutine, public rs_grid_release_descriptor(rs_desc)
releases the given rs grid descriptor (see doc/ReferenceCounting.html)
integer function, public rs_grid_max_ngpts(desc)
returns the maximum number of points in the local grid of any process to account for the case where t...
integer, parameter, public rsgrid_distributed
pure logical function, public map_gaussian_here(rs_grid, h_inv, ra, offset, group_size, my_pos)
...
subroutine, public transfer_rs2pw(rs, pw)
...
subroutine, public rs_grid_release(rs_grid)
releases the given rs grid (see doc/ReferenceCounting.html)
subroutine, public rs_grid_zero(rs)
Initialize grid to zero.
All kind of helpful little routines.
Definition util.F:14
pure integer function, dimension(2), public get_limit(m, n, me)
divide m entries into n parts, return size of part me
Definition util.F:333
void offload_free_buffer(offload_buffer *buffer)
Deallocate given buffer.
represent a pointer to a 1d array