-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_list_methods.F
3022 lines (2614 loc) · 130 KB
/
task_list_methods.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief generate the tasks lists used by collocate and integrate routines
!> \par History
!> 01.2008 [Joost VandeVondele] refactered out of qs_collocate / qs_integrate
!> \author Joost VandeVondele
! **************************************************************************************************
MODULE task_list_methods
USE offload_api, ONLY: offload_create_buffer, offload_buffer_type
USE grid_api, ONLY: grid_create_basis_set, grid_create_task_list
USE ao_util, ONLY: exp_radius_very_extended
USE basis_set_types, ONLY: get_gto_basis_set, &
gto_basis_set_p_type, &
gto_basis_set_type
USE cell_types, ONLY: cell_type, &
pbc
USE cp_control_types, ONLY: dft_control_type
USE cube_utils, ONLY: compute_cube_center, &
cube_info_type, &
return_cube, &
return_cube_nonortho
USE cp_dbcsr_api, ONLY: dbcsr_convert_sizes_to_offsets, &
dbcsr_finalize, &
dbcsr_get_block_p, &
dbcsr_get_info, &
dbcsr_p_type, &
dbcsr_put_block, &
dbcsr_type, &
dbcsr_work_create
USE gaussian_gridlevels, ONLY: gaussian_gridlevel, &
gridlevel_info_type
USE kinds, ONLY: default_string_length, &
dp, &
int_8
USE kpoint_types, ONLY: get_kpoint_info, &
kpoint_type
USE memory_utilities, ONLY: reallocate
USE message_passing, ONLY: &
mp_comm_type
USE particle_types, ONLY: particle_type
USE particle_methods, ONLY: get_particle_set
USE pw_env_types, ONLY: pw_env_get, &
pw_env_type
USE qs_kind_types, ONLY: get_qs_kind, &
qs_kind_type
USE qs_ks_types, ONLY: get_ks_env, &
qs_ks_env_type
USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
USE realspace_grid_types, ONLY: realspace_grid_desc_p_type, &
realspace_grid_desc_type, &
rs_grid_create, &
rs_grid_locate_rank, &
rs_grid_release, &
rs_grid_reorder_ranks, realspace_grid_type
USE task_list_types, ONLY: deserialize_task, &
reallocate_tasks, &
serialize_task, &
task_list_type, &
atom_pair_type, &
task_size_in_int8, &
task_type
USE util, ONLY: sort
!$ USE OMP_LIB, ONLY: omp_destroy_lock, omp_get_num_threads, omp_init_lock, &
!$ omp_lock_kind, omp_set_lock, omp_unset_lock, omp_get_max_threads
#include "./base/base_uses.f90"
#:include './common/array_sort.fypp'
IMPLICIT NONE
LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .FALSE.
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'task_list_methods'
PUBLIC :: generate_qs_task_list, &
task_list_inner_loop
PUBLIC :: distribute_tasks, &
rs_distribute_matrix, &
rs_scatter_matrices, &
rs_gather_matrices, &
rs_copy_to_buffer, &
rs_copy_to_matrices
CONTAINS
! **************************************************************************************************
!> \brief ...
!> \param ks_env ...
!> \param task_list ...
!> \param reorder_rs_grid_ranks Flag that indicates if this routine should
!> or should not overwrite the rs descriptor (see comment below)
!> \param skip_load_balance_distributed ...
!> \param soft_valid ...
!> \param basis_type ...
!> \param pw_env_external ...
!> \param sab_orb_external ...
!> \par History
!> 01.2008 factored out of calculate_rho_elec [Joost VandeVondele]
!> 04.2010 divides tasks into grid levels and atom pairs for integrate/collocate [Iain Bethune]
!> (c) The Numerical Algorithms Group (NAG) Ltd, 2010 on behalf of the HECToR project
!> 06.2015 adjusted to be used with multiple images (k-points) [JGH]
!> \note If this routine is called several times with different task lists,
!> the default behaviour is to re-optimize the grid ranks and overwrite
!> the rs descriptor and grids. reorder_rs_grid_ranks = .FALSE. prevents the code
!> of performing a new optimization by leaving the rank order in
!> its current state.
! **************************************************************************************************
SUBROUTINE generate_qs_task_list(ks_env, task_list, &
reorder_rs_grid_ranks, skip_load_balance_distributed, &
soft_valid, basis_type, pw_env_external, sab_orb_external)
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(task_list_type), POINTER :: task_list
LOGICAL, INTENT(IN) :: reorder_rs_grid_ranks, &
skip_load_balance_distributed
LOGICAL, INTENT(IN), OPTIONAL :: soft_valid
CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: basis_type
TYPE(pw_env_type), OPTIONAL, POINTER :: pw_env_external
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
OPTIONAL, POINTER :: sab_orb_external
CHARACTER(LEN=*), PARAMETER :: routineN = 'generate_qs_task_list'
INTEGER, PARAMETER :: max_tasks = 2000
CHARACTER(LEN=default_string_length) :: my_basis_type
INTEGER :: cindex, curr_tasks, handle, i, iatom, iatom_old, igrid_level, igrid_level_old, &
ikind, ilevel, img, img_old, ipair, ipgf, iset, itask, jatom, jatom_old, jkind, jpgf, &
jset, maxpgf, maxset, natoms, nimages, nkind, nseta, nsetb, slot
INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: blocks
INTEGER, DIMENSION(3) :: cellind
INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, npgfa, &
npgfb, nsgf
INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
LOGICAL :: dokp, my_soft
REAL(KIND=dp) :: kind_radius_a, kind_radius_b
REAL(KIND=dp), DIMENSION(3) :: ra, rab
REAL(KIND=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
REAL(KIND=dp), DIMENSION(:, :), POINTER :: rpgfa, rpgfb, zeta, zetb
TYPE(cell_type), POINTER :: cell
TYPE(cube_info_type), DIMENSION(:), POINTER :: cube_info
TYPE(dft_control_type), POINTER :: dft_control
TYPE(gridlevel_info_type), POINTER :: gridlevel_info
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b, orb_basis_set
TYPE(kpoint_type), POINTER :: kpoints
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_orb
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(pw_env_type), POINTER :: pw_env
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_kind_type), POINTER :: qs_kind
TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
POINTER :: rs_descs
TYPE(realspace_grid_type), DIMENSION(:), POINTER :: rs_grids
TYPE(task_type), DIMENSION(:), POINTER :: tasks
CALL timeset(routineN, handle)
CALL get_ks_env(ks_env, &
qs_kind_set=qs_kind_set, &
cell=cell, &
particle_set=particle_set, &
dft_control=dft_control)
! OPTION 1) basis is set through input
! OPTION 2) soft orb basis is requested
! OPTION 3) by default, the full orb basis density is calculated
my_soft = .FALSE.
IF (PRESENT(soft_valid)) my_soft = soft_valid
IF (PRESENT(basis_type)) THEN
CPASSERT(.NOT. my_soft)
my_basis_type = basis_type
ELSEIF (my_soft) THEN
my_basis_type = "ORB_SOFT"
ELSE
my_basis_type = "ORB"
END IF
CALL get_ks_env(ks_env, sab_orb=sab_orb)
IF (PRESENT(sab_orb_external)) sab_orb => sab_orb_external
CALL get_ks_env(ks_env, pw_env=pw_env)
IF (PRESENT(pw_env_external)) pw_env => pw_env_external
CALL pw_env_get(pw_env, rs_descs=rs_descs, rs_grids=rs_grids)
! *** assign from pw_env
gridlevel_info => pw_env%gridlevel_info
cube_info => pw_env%cube_info
! find maximum numbers
nkind = SIZE(qs_kind_set)
natoms = SIZE(particle_set)
maxset = 0
maxpgf = 0
DO ikind = 1, nkind
qs_kind => qs_kind_set(ikind)
CALL get_qs_kind(qs_kind=qs_kind, &
basis_set=orb_basis_set, basis_type=my_basis_type)
IF (.NOT. ASSOCIATED(orb_basis_set)) CYCLE
CALL get_gto_basis_set(gto_basis_set=orb_basis_set, npgf=npgfa, nset=nseta)
maxset = MAX(nseta, maxset)
maxpgf = MAX(MAXVAL(npgfa), maxpgf)
END DO
! kpoint related
nimages = dft_control%nimages
IF (nimages > 1) THEN
dokp = .TRUE.
NULLIFY (kpoints)
CALL get_ks_env(ks_env=ks_env, kpoints=kpoints)
CALL get_kpoint_info(kpoint=kpoints, cell_to_index=cell_to_index)
ELSE
dokp = .FALSE.
NULLIFY (cell_to_index)
END IF
! free the atom_pair lists if allocated
IF (ASSOCIATED(task_list%atom_pair_send)) DEALLOCATE (task_list%atom_pair_send)
IF (ASSOCIATED(task_list%atom_pair_recv)) DEALLOCATE (task_list%atom_pair_recv)
! construct a list of all tasks
IF (.NOT. ASSOCIATED(task_list%tasks)) THEN
CALL reallocate_tasks(task_list%tasks, max_tasks)
END IF
task_list%ntasks = 0
curr_tasks = SIZE(task_list%tasks)
ALLOCATE (basis_set_list(nkind))
DO ikind = 1, nkind
qs_kind => qs_kind_set(ikind)
CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a, &
basis_type=my_basis_type)
IF (ASSOCIATED(basis_set_a)) THEN
basis_set_list(ikind)%gto_basis_set => basis_set_a
ELSE
NULLIFY (basis_set_list(ikind)%gto_basis_set)
END IF
END DO
!!$OMP PARALLEL DEFAULT(NONE) &
!!$OMP SHARED (sab_orb, dokp, basis_set_list, task_list, rs_descs, dft_control, cube_info, gridlevel_info, &
!!$OMP curr_tasks, maxpgf, maxset, natoms, nimages, particle_set, cell_to_index, cell) &
!!$OMP PRIVATE (ikind, jkind, iatom, jatom, rab, cellind, basis_set_a, basis_set_b, ra, &
!!$OMP la_max, la_min, npgfa, nseta, rpgfa, set_radius_a, kind_radius_a, zeta, &
!!$OMP lb_max, lb_min, npgfb, nsetb, rpgfb, set_radius_b, kind_radius_b, zetb, &
!!$OMP cindex, slot)
! Loop over neighbor list
!!$OMP DO SCHEDULE(GUIDED)
DO slot = 1, sab_orb(1)%nl_size
ikind = sab_orb(1)%nlist_task(slot)%ikind
jkind = sab_orb(1)%nlist_task(slot)%jkind
iatom = sab_orb(1)%nlist_task(slot)%iatom
jatom = sab_orb(1)%nlist_task(slot)%jatom
rab(1:3) = sab_orb(1)%nlist_task(slot)%r(1:3)
cellind(1:3) = sab_orb(1)%nlist_task(slot)%cell(1:3)
basis_set_a => basis_set_list(ikind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
basis_set_b => basis_set_list(jkind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
ra(:) = pbc(particle_set(iatom)%r, cell)
! basis ikind
la_max => basis_set_a%lmax
la_min => basis_set_a%lmin
npgfa => basis_set_a%npgf
nseta = basis_set_a%nset
rpgfa => basis_set_a%pgf_radius
set_radius_a => basis_set_a%set_radius
kind_radius_a = basis_set_a%kind_radius
zeta => basis_set_a%zet
! basis jkind
lb_max => basis_set_b%lmax
lb_min => basis_set_b%lmin
npgfb => basis_set_b%npgf
nsetb = basis_set_b%nset
rpgfb => basis_set_b%pgf_radius
set_radius_b => basis_set_b%set_radius
kind_radius_b = basis_set_b%kind_radius
zetb => basis_set_b%zet
IF (dokp) THEN
cindex = cell_to_index(cellind(1), cellind(2), cellind(3))
ELSE
cindex = 1
END IF
CALL task_list_inner_loop(task_list%tasks, task_list%ntasks, curr_tasks, &
rs_descs, dft_control, cube_info, gridlevel_info, cindex, &
iatom, jatom, rpgfa, rpgfb, zeta, zetb, kind_radius_b, &
set_radius_a, set_radius_b, ra, rab, &
la_max, la_min, lb_max, lb_min, npgfa, npgfb, nseta, nsetb)
END DO
!!$OMP END PARALLEL
! redistribute the task list so that all tasks map on the local rs grids
CALL distribute_tasks( &
rs_descs=rs_descs, ntasks=task_list%ntasks, natoms=natoms, &
tasks=task_list%tasks, atom_pair_send=task_list%atom_pair_send, &
atom_pair_recv=task_list%atom_pair_recv, symmetric=.TRUE., &
reorder_rs_grid_ranks=reorder_rs_grid_ranks, &
skip_load_balance_distributed=skip_load_balance_distributed)
! compute offsets for rs_scatter_matrix / rs_copy_matrix
ALLOCATE (nsgf(natoms))
CALL get_particle_set(particle_set, qs_kind_set, basis=basis_set_list, nsgf=nsgf)
IF (ASSOCIATED(task_list%atom_pair_send)) THEN
! only needed when there is a distributed grid
CALL rs_calc_offsets(pairs=task_list%atom_pair_send, &
nsgf=nsgf, &
group_size=rs_descs(1)%rs_desc%group_size, &
pair_offsets=task_list%pair_offsets_send, &
rank_offsets=task_list%rank_offsets_send, &
rank_sizes=task_list%rank_sizes_send, &
buffer_size=task_list%buffer_size_send)
END IF
CALL rs_calc_offsets(pairs=task_list%atom_pair_recv, &
nsgf=nsgf, &
group_size=rs_descs(1)%rs_desc%group_size, &
pair_offsets=task_list%pair_offsets_recv, &
rank_offsets=task_list%rank_offsets_recv, &
rank_sizes=task_list%rank_sizes_recv, &
buffer_size=task_list%buffer_size_recv)
DEALLOCATE (basis_set_list, nsgf)
! If the rank order has changed, reallocate any of the distributed rs_grids
IF (reorder_rs_grid_ranks) THEN
DO i = 1, gridlevel_info%ngrid_levels
IF (rs_descs(i)%rs_desc%distributed) THEN
CALL rs_grid_release(rs_grids(i))
CALL rs_grid_create(rs_grids(i), rs_descs(i)%rs_desc)
END IF
END DO
END IF
CALL create_grid_task_list(task_list=task_list, &
qs_kind_set=qs_kind_set, &
particle_set=particle_set, &
cell=cell, &
basis_type=my_basis_type, &
rs_grids=rs_grids)
! Now we have the final list of tasks, setup the task_list with the
! data needed for the loops in integrate_v/calculate_rho
IF (ASSOCIATED(task_list%taskstart)) THEN
DEALLOCATE (task_list%taskstart)
END IF
IF (ASSOCIATED(task_list%taskstop)) THEN
DEALLOCATE (task_list%taskstop)
END IF
IF (ASSOCIATED(task_list%npairs)) THEN
DEALLOCATE (task_list%npairs)
END IF
! First, count the number of unique atom pairs per grid level
ALLOCATE (task_list%npairs(SIZE(rs_descs)))
iatom_old = -1; jatom_old = -1; igrid_level_old = -1; img_old = -1
ipair = 0
task_list%npairs = 0
DO i = 1, task_list%ntasks
igrid_level = task_list%tasks(i)%grid_level
img = task_list%tasks(i)%image
iatom = task_list%tasks(i)%iatom
jatom = task_list%tasks(i)%jatom
iset = task_list%tasks(i)%iset
jset = task_list%tasks(i)%jset
ipgf = task_list%tasks(i)%ipgf
jpgf = task_list%tasks(i)%jpgf
IF (igrid_level .NE. igrid_level_old) THEN
IF (igrid_level_old .NE. -1) THEN
task_list%npairs(igrid_level_old) = ipair
END IF
ipair = 1
igrid_level_old = igrid_level
iatom_old = iatom
jatom_old = jatom
img_old = img
ELSE IF (iatom .NE. iatom_old .OR. jatom .NE. jatom_old .OR. img .NE. img_old) THEN
ipair = ipair + 1
iatom_old = iatom
jatom_old = jatom
img_old = img
END IF
END DO
! Take care of the last iteration
IF (task_list%ntasks /= 0) THEN
task_list%npairs(igrid_level) = ipair
END IF
! Second, for each atom pair, find the indices in the task list
! of the first and last task
! Array sized for worst case
ALLOCATE (task_list%taskstart(MAXVAL(task_list%npairs), SIZE(rs_descs)))
ALLOCATE (task_list%taskstop(MAXVAL(task_list%npairs), SIZE(rs_descs)))
iatom_old = -1; jatom_old = -1; igrid_level_old = -1; img_old = -1
ipair = 0
task_list%taskstart = 0
task_list%taskstop = 0
DO i = 1, task_list%ntasks
igrid_level = task_list%tasks(i)%grid_level
img = task_list%tasks(i)%image
iatom = task_list%tasks(i)%iatom
jatom = task_list%tasks(i)%jatom
iset = task_list%tasks(i)%iset
jset = task_list%tasks(i)%jset
ipgf = task_list%tasks(i)%ipgf
jpgf = task_list%tasks(i)%jpgf
IF (igrid_level .NE. igrid_level_old) THEN
IF (igrid_level_old .NE. -1) THEN
task_list%taskstop(ipair, igrid_level_old) = i - 1
END IF
ipair = 1
task_list%taskstart(ipair, igrid_level) = i
igrid_level_old = igrid_level
iatom_old = iatom
jatom_old = jatom
img_old = img
ELSE IF (iatom .NE. iatom_old .OR. jatom .NE. jatom_old .OR. img .NE. img_old) THEN
ipair = ipair + 1
task_list%taskstart(ipair, igrid_level) = i
task_list%taskstop(ipair - 1, igrid_level) = i - 1
iatom_old = iatom
jatom_old = jatom
img_old = img
END IF
END DO
! Take care of the last iteration
IF (task_list%ntasks /= 0) THEN
task_list%taskstop(ipair, igrid_level) = task_list%ntasks
END IF
! Debug task destribution
IF (debug_this_module) THEN
tasks => task_list%tasks
WRITE (6, *)
WRITE (6, *) "Total number of tasks ", task_list%ntasks
DO igrid_level = 1, gridlevel_info%ngrid_levels
WRITE (6, *) "Total number of pairs(grid_level) ", &
igrid_level, task_list%npairs(igrid_level)
END DO
WRITE (6, *)
DO igrid_level = 1, gridlevel_info%ngrid_levels
ALLOCATE (blocks(natoms, natoms, nimages))
blocks = -1
DO ipair = 1, task_list%npairs(igrid_level)
itask = task_list%taskstart(ipair, igrid_level)
ilevel = task_list%tasks(itask)%grid_level
img = task_list%tasks(itask)%image
iatom = task_list%tasks(itask)%iatom
jatom = task_list%tasks(itask)%jatom
iset = task_list%tasks(itask)%iset
jset = task_list%tasks(itask)%jset
ipgf = task_list%tasks(itask)%ipgf
jpgf = task_list%tasks(itask)%jpgf
IF (blocks(iatom, jatom, img) == -1 .AND. blocks(jatom, iatom, img) == -1) THEN
blocks(iatom, jatom, img) = 1
blocks(jatom, iatom, img) = 1
ELSE
WRITE (6, *) "TASK LIST CONFLICT IN PAIR ", ipair
WRITE (6, *) "Reuse of iatom, jatom, image ", iatom, jatom, img
END IF
iatom_old = iatom
jatom_old = jatom
img_old = img
DO itask = task_list%taskstart(ipair, igrid_level), task_list%taskstop(ipair, igrid_level)
ilevel = task_list%tasks(itask)%grid_level
img = task_list%tasks(itask)%image
iatom = task_list%tasks(itask)%iatom
jatom = task_list%tasks(itask)%jatom
iset = task_list%tasks(itask)%iset
jset = task_list%tasks(itask)%jset
ipgf = task_list%tasks(itask)%ipgf
jpgf = task_list%tasks(itask)%jpgf
IF (iatom /= iatom_old .OR. jatom /= jatom_old .OR. img /= img_old) THEN
WRITE (6, *) "TASK LIST CONFLICT IN TASK ", itask
WRITE (6, *) "Inconsistent iatom, jatom, image ", iatom, jatom, img
WRITE (6, *) "Should be iatom, jatom, image ", iatom_old, jatom_old, img_old
END IF
END DO
END DO
DEALLOCATE (blocks)
END DO
END IF
CALL timestop(handle)
END SUBROUTINE generate_qs_task_list
! **************************************************************************************************
!> \brief Sends the task list data to the grid API.
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE create_grid_task_list(task_list, qs_kind_set, particle_set, cell, basis_type, rs_grids)
TYPE(task_list_type), POINTER :: task_list
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(cell_type), POINTER :: cell
CHARACTER(LEN=default_string_length) :: basis_type
TYPE(realspace_grid_type), DIMENSION(:), POINTER :: rs_grids
TYPE(gto_basis_set_type), POINTER :: orb_basis_set
INTEGER :: nset, natoms, nkinds, ntasks, &
ikind, iatom, itask, nsgf
INTEGER, DIMENSION(:), ALLOCATABLE :: atom_kinds, level_list, iatom_list, jatom_list, &
iset_list, jset_list, ipgf_list, jpgf_list, &
border_mask_list, block_num_list
REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: radius_list
REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: rab_list, atom_positions
TYPE(task_type), DIMENSION(:), POINTER :: tasks
INTEGER, DIMENSION(:, :), POINTER :: first_sgf
REAL(KIND=dp), DIMENSION(:, :), POINTER :: sphi, zet
INTEGER, DIMENSION(:), POINTER :: lmax, lmin, npgf, nsgf_set
nkinds = SIZE(qs_kind_set)
natoms = SIZE(particle_set)
ntasks = task_list%ntasks
tasks => task_list%tasks
IF (.NOT. ASSOCIATED(task_list%grid_basis_sets)) THEN
! Basis sets do not change during simulation - only need to create them once.
ALLOCATE (task_list%grid_basis_sets(nkinds))
DO ikind = 1, nkinds
CALL get_qs_kind(qs_kind_set(ikind), basis_type=basis_type, basis_set=orb_basis_set)
CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
nset=nset, &
nsgf=nsgf, &
nsgf_set=nsgf_set, &
npgf=npgf, &
first_sgf=first_sgf, &
lmax=lmax, &
lmin=lmin, &
sphi=sphi, &
zet=zet)
CALL grid_create_basis_set(nset=nset, &
nsgf=nsgf, &
maxco=SIZE(sphi, 1), &
maxpgf=SIZE(zet, 1), &
lmin=lmin, &
lmax=lmax, &
npgf=npgf, &
nsgf_set=nsgf_set, &
first_sgf=first_sgf, &
sphi=sphi, &
zet=zet, &
basis_set=task_list%grid_basis_sets(ikind))
END DO
END IF
! Pack task list infos
ALLOCATE (atom_kinds(natoms), atom_positions(3, natoms))
DO iatom = 1, natoms
atom_kinds(iatom) = particle_set(iatom)%atomic_kind%kind_number
atom_positions(:, iatom) = pbc(particle_set(iatom)%r, cell)
END DO
ALLOCATE (level_list(ntasks), iatom_list(ntasks), jatom_list(ntasks))
ALLOCATE (iset_list(ntasks), jset_list(ntasks), ipgf_list(ntasks), jpgf_list(ntasks))
ALLOCATE (border_mask_list(ntasks), block_num_list(ntasks))
ALLOCATE (radius_list(ntasks), rab_list(3, ntasks))
DO itask = 1, ntasks
level_list(itask) = tasks(itask)%grid_level
iatom_list(itask) = tasks(itask)%iatom
jatom_list(itask) = tasks(itask)%jatom
iset_list(itask) = tasks(itask)%iset
jset_list(itask) = tasks(itask)%jset
ipgf_list(itask) = tasks(itask)%ipgf
jpgf_list(itask) = tasks(itask)%jpgf
IF (tasks(itask)%dist_type == 2) THEN
border_mask_list(itask) = IAND(63, NOT(tasks(itask)%subpatch_pattern)) ! invert last 6 bits
ELSE
border_mask_list(itask) = 0 ! no masking
END IF
block_num_list(itask) = tasks(itask)%pair_index ! change of nomenclature pair_index -> block_num
radius_list(itask) = tasks(itask)%radius
rab_list(:, itask) = tasks(itask)%rab(:)
END DO
CALL grid_create_task_list(ntasks=ntasks, &
natoms=natoms, &
nkinds=nkinds, &
nblocks=SIZE(task_list%pair_offsets_recv), &
block_offsets=task_list%pair_offsets_recv, &
atom_positions=atom_positions, &
atom_kinds=atom_kinds, &
basis_sets=task_list%grid_basis_sets, &
level_list=level_list, &
iatom_list=iatom_list, &
jatom_list=jatom_list, &
iset_list=iset_list, &
jset_list=jset_list, &
ipgf_list=ipgf_list, &
jpgf_list=jpgf_list, &
border_mask_list=border_mask_list, &
block_num_list=block_num_list, &
radius_list=radius_list, &
rab_list=rab_list, &
rs_grids=rs_grids, &
task_list=task_list%grid_task_list)
CALL offload_create_buffer(task_list%buffer_size_recv, task_list%pab_buffer)
CALL offload_create_buffer(task_list%buffer_size_recv, task_list%hab_buffer)
END SUBROUTINE create_grid_task_list
! **************************************************************************************************
!> \brief ...
!> \param tasks ...
!> \param ntasks ...
!> \param curr_tasks ...
!> \param rs_descs ...
!> \param dft_control ...
!> \param cube_info ...
!> \param gridlevel_info ...
!> \param cindex ...
!> \param iatom ...
!> \param jatom ...
!> \param rpgfa ...
!> \param rpgfb ...
!> \param zeta ...
!> \param zetb ...
!> \param kind_radius_b ...
!> \param set_radius_a ...
!> \param set_radius_b ...
!> \param ra ...
!> \param rab ...
!> \param la_max ...
!> \param la_min ...
!> \param lb_max ...
!> \param lb_min ...
!> \param npgfa ...
!> \param npgfb ...
!> \param nseta ...
!> \param nsetb ...
!> \par History
!> Joost VandeVondele: 10.2008 refactored
! **************************************************************************************************
SUBROUTINE task_list_inner_loop(tasks, ntasks, curr_tasks, rs_descs, dft_control, &
cube_info, gridlevel_info, cindex, &
iatom, jatom, rpgfa, rpgfb, zeta, zetb, kind_radius_b, set_radius_a, set_radius_b, ra, rab, &
la_max, la_min, lb_max, lb_min, npgfa, npgfb, nseta, nsetb)
TYPE(task_type), DIMENSION(:), POINTER :: tasks
INTEGER :: ntasks, curr_tasks
TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
POINTER :: rs_descs
TYPE(dft_control_type), POINTER :: dft_control
TYPE(cube_info_type), DIMENSION(:), POINTER :: cube_info
TYPE(gridlevel_info_type), POINTER :: gridlevel_info
INTEGER :: cindex, iatom, jatom
REAL(KIND=dp), DIMENSION(:, :), POINTER :: rpgfa, rpgfb, zeta, zetb
REAL(KIND=dp) :: kind_radius_b
REAL(KIND=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
REAL(KIND=dp), DIMENSION(3) :: ra, rab
INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, npgfa, &
npgfb
INTEGER :: nseta, nsetb
INTEGER :: cube_center(3), igrid_level, ipgf, iset, &
jpgf, jset, lb_cube(3), ub_cube(3)
REAL(KIND=dp) :: dab, rab2, radius, zetp
rab2 = rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3)
dab = SQRT(rab2)
loop_iset: DO iset = 1, nseta
IF (set_radius_a(iset) + kind_radius_b < dab) CYCLE
loop_jset: DO jset = 1, nsetb
IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
loop_ipgf: DO ipgf = 1, npgfa(iset)
IF (rpgfa(ipgf, iset) + set_radius_b(jset) < dab) CYCLE
loop_jpgf: DO jpgf = 1, npgfb(jset)
IF (rpgfa(ipgf, iset) + rpgfb(jpgf, jset) < dab) CYCLE
zetp = zeta(ipgf, iset) + zetb(jpgf, jset)
igrid_level = gaussian_gridlevel(gridlevel_info, zetp)
CALL compute_pgf_properties(cube_center, lb_cube, ub_cube, radius, &
rs_descs(igrid_level)%rs_desc, cube_info(igrid_level), &
la_max(iset), zeta(ipgf, iset), la_min(iset), &
lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
ra, rab, rab2, dft_control%qs_control%eps_rho_rspace)
CALL pgf_to_tasks(tasks, ntasks, curr_tasks, &
rab, cindex, iatom, jatom, iset, jset, ipgf, jpgf, &
la_max(iset), lb_max(jset), rs_descs(igrid_level)%rs_desc, &
igrid_level, gridlevel_info%ngrid_levels, cube_center, &
lb_cube, ub_cube, radius)
END DO loop_jpgf
END DO loop_ipgf
END DO loop_jset
END DO loop_iset
END SUBROUTINE task_list_inner_loop
! **************************************************************************************************
!> \brief combines the calculation of several basic properties of a given pgf:
!> its center, the bounding cube, the radius, the cost,
!> tries to predict the time needed for processing this task
!> in this way an improved load balance might be obtained
!> \param cube_center ...
!> \param lb_cube ...
!> \param ub_cube ...
!> \param radius ...
!> \param rs_desc ...
!> \param cube_info ...
!> \param la_max ...
!> \param zeta ...
!> \param la_min ...
!> \param lb_max ...
!> \param zetb ...
!> \param lb_min ...
!> \param ra ...
!> \param rab ...
!> \param rab2 ...
!> \param eps ...
!> \par History
!> 10.2008 refactored [Joost VandeVondele]
!> \note
!> -) this requires the radius to be computed in the same way as
!> collocate_pgf_product, we should factor that part into a subroutine
!> -) we're assuming that integrate_pgf and collocate_pgf are the same cost for load balancing
!> this is more or less true for map_consistent
!> -) in principle, the computed radius could be recycled in integrate_pgf/collocate_pgf if it is certainly
!> the same, this could lead to a small speedup
!> -) 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)
!> fitting the measured data on an opteron/g95 using the expression
!> a*(l+b)(r+c)**3+d which is based on the innerloop of the collocating routines
! **************************************************************************************************
SUBROUTINE compute_pgf_properties(cube_center, lb_cube, ub_cube, radius, &
rs_desc, cube_info, la_max, zeta, la_min, lb_max, zetb, lb_min, ra, rab, rab2, eps)
INTEGER, DIMENSION(3), INTENT(OUT) :: cube_center, lb_cube, ub_cube
REAL(KIND=dp), INTENT(OUT) :: radius
TYPE(realspace_grid_desc_type), POINTER :: rs_desc
TYPE(cube_info_type), INTENT(IN) :: cube_info
INTEGER, INTENT(IN) :: la_max
REAL(KIND=dp), INTENT(IN) :: zeta
INTEGER, INTENT(IN) :: la_min, lb_max
REAL(KIND=dp), INTENT(IN) :: zetb
INTEGER, INTENT(IN) :: lb_min
REAL(KIND=dp), INTENT(IN) :: ra(3), rab(3), rab2, eps
INTEGER :: extent(3)
INTEGER, DIMENSION(:), POINTER :: sphere_bounds
REAL(KIND=dp) :: cutoff, f, prefactor, rb(3), zetp
REAL(KIND=dp), DIMENSION(3) :: rp
! the radius for this task
zetp = zeta + zetb
rp(:) = ra(:) + zetb/zetp*rab(:)
rb(:) = ra(:) + rab(:)
cutoff = 1.0_dp
f = zetb/zetp
prefactor = EXP(-zeta*f*rab2)
radius = exp_radius_very_extended(la_min, la_max, lb_min, lb_max, ra=ra, rb=rb, rp=rp, &
zetp=zetp, eps=eps, prefactor=prefactor, cutoff=cutoff)
CALL compute_cube_center(cube_center, rs_desc, zeta, zetb, ra, rab)
! compute cube_center, the center of the gaussian product to map (folded to within the unit cell)
cube_center(:) = MODULO(cube_center(:), rs_desc%npts(:))
cube_center(:) = cube_center(:) + rs_desc%lb(:)
IF (rs_desc%orthorhombic) THEN
CALL return_cube(cube_info, radius, lb_cube, ub_cube, sphere_bounds)
ELSE
CALL return_cube_nonortho(cube_info, radius, lb_cube, ub_cube, rp)
!? unclear if extent is computed correctly.
extent(:) = ub_cube(:) - lb_cube(:)
lb_cube(:) = -extent(:)/2 - 1
ub_cube(:) = extent(:)/2
END IF
END SUBROUTINE compute_pgf_properties
! **************************************************************************************************
!> \brief predicts the cost of a task in kcycles for a given task
!> the model is based on a fit of actual data, and might need updating
!> as collocate_pgf_product changes (or CPUs/compilers change)
!> maybe some dynamic approach, improving the cost model on the fly could
!> work as well
!> the cost model does not yet take into account the fraction of space
!> that is mapped locally for a given cube and rs_grid (generalised tasks)
!> \param lb_cube ...
!> \param ub_cube ...
!> \param fraction ...
!> \param lmax ...
!> \param is_ortho ...
!> \return ...
! **************************************************************************************************
INTEGER FUNCTION cost_model(lb_cube, ub_cube, fraction, lmax, is_ortho)
INTEGER, DIMENSION(3), INTENT(IN) :: lb_cube, ub_cube
REAL(KIND=dp), INTENT(IN) :: fraction
INTEGER :: lmax
LOGICAL :: is_ortho
INTEGER :: cmax
REAL(KIND=dp) :: v1, v2, v3, v4, v5
cmax = MAXVAL(((ub_cube - lb_cube) + 1)/2)
IF (is_ortho) THEN
v1 = 1.504760E+00_dp
v2 = 3.126770E+00_dp
v3 = 5.074106E+00_dp
v4 = 1.091568E+00_dp
v5 = 1.070187E+00_dp
ELSE
v1 = 7.831105E+00_dp
v2 = 2.675174E+00_dp
v3 = 7.546553E+00_dp
v4 = 6.122446E-01_dp
v5 = 3.886382E+00_dp
END IF
cost_model = CEILING(((lmax + v1)*(cmax + v2)**3*v3*fraction + v4 + v5*lmax**7)/1000.0_dp)
END FUNCTION cost_model
! **************************************************************************************************
!> \brief pgf_to_tasks converts a given pgf to one or more tasks, in particular
!> this determines by which CPUs a given pgf gets collocated
!> the format of the task array is as follows
!> tasks(1,i) := destination
!> tasks(2,i) := source
!> tasks(3,i) := compressed type (iatom, jatom, ....)
!> tasks(4,i) := type (0: replicated, 1: distributed local, 2: distributed generalised)
!> tasks(5,i) := cost
!> tasks(6,i) := alternate destination code (0 if none available)
!>
!> \param tasks ...
!> \param ntasks ...
!> \param curr_tasks ...
!> \param rab ...
!> \param cindex ...
!> \param iatom ...
!> \param jatom ...
!> \param iset ...
!> \param jset ...
!> \param ipgf ...
!> \param jpgf ...
!> \param la_max ...
!> \param lb_max ...
!> \param rs_desc ...
!> \param igrid_level ...
!> \param n_levels ...
!> \param cube_center ...
!> \param lb_cube ...
!> \param ub_cube ...
!> \par History
!> 10.2008 Refactored based on earlier routines by MattW [Joost VandeVondele]
! **************************************************************************************************
SUBROUTINE pgf_to_tasks(tasks, ntasks, curr_tasks, &
rab, cindex, iatom, jatom, iset, jset, ipgf, jpgf, &
la_max, lb_max, rs_desc, igrid_level, n_levels, &
cube_center, lb_cube, ub_cube, radius)
TYPE(task_type), DIMENSION(:), POINTER :: tasks
INTEGER, INTENT(INOUT) :: ntasks, curr_tasks
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rab
INTEGER, INTENT(IN) :: cindex, iatom, jatom, iset, jset, ipgf, &
jpgf, la_max, lb_max
TYPE(realspace_grid_desc_type), POINTER :: rs_desc
INTEGER, INTENT(IN) :: igrid_level, n_levels
INTEGER, DIMENSION(3), INTENT(IN) :: cube_center, lb_cube, ub_cube
REAL(KIND=dp), INTENT(IN) :: radius
INTEGER, PARAMETER :: add_tasks = 1000
REAL(kind=dp), PARAMETER :: mult_tasks = 2.0_dp
INTEGER :: added_tasks, cost, j, lmax
LOGICAL :: is_ortho
REAL(KIND=dp) :: tfraction
!$OMP SINGLE
ntasks = ntasks + 1
IF (ntasks > curr_tasks) THEN
curr_tasks = INT((curr_tasks + add_tasks)*mult_tasks)
CALL reallocate_tasks(tasks, curr_tasks)
END IF
!$OMP END SINGLE
IF (rs_desc%distributed) THEN
! finds the node(s) that need to process this task
! on exit tasks(:)%dist_type is 1 for distributed tasks and 2 for generalised tasks
CALL rs_find_node(rs_desc, igrid_level, n_levels, cube_center, &
ntasks=ntasks, tasks=tasks, lb_cube=lb_cube, ub_cube=ub_cube, added_tasks=added_tasks)
ELSE
tasks(ntasks)%destination = encode_rank(rs_desc%my_pos, igrid_level, n_levels)
tasks(ntasks)%dist_type = 0
tasks(ntasks)%subpatch_pattern = 0
added_tasks = 1
END IF
lmax = la_max + lb_max
is_ortho = (tasks(ntasks)%dist_type == 0 .OR. tasks(ntasks)%dist_type == 1) .AND. rs_desc%orthorhombic
! we assume the load is shared equally between processes dealing with a generalised Gaussian.
! this could be refined in the future
tfraction = 1.0_dp/added_tasks
cost = cost_model(lb_cube, ub_cube, tfraction, lmax, is_ortho)
DO j = 1, added_tasks
tasks(ntasks - added_tasks + j)%source = encode_rank(rs_desc%my_pos, igrid_level, n_levels)
tasks(ntasks - added_tasks + j)%cost = cost
tasks(ntasks - added_tasks + j)%grid_level = igrid_level
tasks(ntasks - added_tasks + j)%image = cindex
tasks(ntasks - added_tasks + j)%iatom = iatom
tasks(ntasks - added_tasks + j)%jatom = jatom
tasks(ntasks - added_tasks + j)%iset = iset
tasks(ntasks - added_tasks + j)%jset = jset
tasks(ntasks - added_tasks + j)%ipgf = ipgf
tasks(ntasks - added_tasks + j)%jpgf = jpgf
tasks(ntasks - added_tasks + j)%rab = rab
tasks(ntasks - added_tasks + j)%radius = radius
END DO
END SUBROUTINE pgf_to_tasks
! **************************************************************************************************
!> \brief performs load balancing of the tasks on the distributed grids
!> \param tasks ...
!> \param ntasks ...
!> \param rs_descs ...
!> \param grid_level ...
!> \param natoms ...
!> \par History
!> created 2008-10-03 [Joost VandeVondele]
! **************************************************************************************************
SUBROUTINE load_balance_distributed(tasks, ntasks, rs_descs, grid_level, natoms)
TYPE(task_type), DIMENSION(:), POINTER :: tasks
INTEGER :: ntasks
TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
POINTER :: rs_descs
INTEGER :: grid_level, natoms
CHARACTER(LEN=*), PARAMETER :: routineN = 'load_balance_distributed'
INTEGER :: handle
INTEGER, DIMENSION(:, :, :), POINTER :: list
CALL timeset(routineN, handle)
NULLIFY (list)
! here we create for each cpu (0:ncpu-1) a list of possible destinations.
! if a destination would not be in this list, it is a bug
CALL create_destination_list(list, rs_descs, grid_level)
! now, walk over the tasks, filling in the loads of each destination
CALL compute_load_list(list, rs_descs, grid_level, tasks, ntasks, natoms, create_list=.TRUE.)
! optimize loads & fluxes
CALL optimize_load_list(list, rs_descs(1)%rs_desc%group, rs_descs(1)%rs_desc%my_pos)
! now, walk over the tasks, using the list to set the destinations
CALL compute_load_list(list, rs_descs, grid_level, tasks, ntasks, natoms, create_list=.FALSE.)
DEALLOCATE (list)
CALL timestop(handle)
END SUBROUTINE load_balance_distributed
! **************************************************************************************************
!> \brief this serial routine adjusts the fluxes in the global list