-
Notifications
You must be signed in to change notification settings - Fork 1
/
distribution_methods.F
1382 lines (1180 loc) · 64.9 KB
/
distribution_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 Distribution methods for atoms, particles, or molecules
!> \par History
!> - 1d-distribution of molecules and particles (Sep. 2003, MK)
!> - 2d-distribution for Quickstep updated with molecules (Oct. 2003, MK)
!> \author MK (22.08.2003)
! **************************************************************************************************
MODULE distribution_methods
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind,&
get_atomic_kind_set
USE basis_set_types, ONLY: get_gto_basis_set,&
gto_basis_set_type
USE cell_types, ONLY: cell_type,&
pbc,&
real_to_scaled,&
scaled_to_real
USE cp_array_utils, ONLY: cp_1d_i_p_type
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_dbcsr_api, ONLY: dbcsr_distribution_get_num_images
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_io_unit,&
cp_logger_get_default_unit_nr,&
cp_logger_type
USE cp_min_heap, ONLY: cp_heap_fill,&
cp_heap_get_first,&
cp_heap_new,&
cp_heap_release,&
cp_heap_reset_first,&
cp_heap_type
USE cp_output_handling, ONLY: cp_p_file,&
cp_print_key_finished_output,&
cp_print_key_should_output,&
cp_print_key_unit_nr
USE distribution_1d_types, ONLY: distribution_1d_create,&
distribution_1d_type
USE distribution_2d_types, ONLY: distribution_2d_create,&
distribution_2d_type,&
distribution_2d_write
USE input_constants, ONLY: model_block_count,&
model_block_lmax
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE kinds, ONLY: dp,&
int_8
USE machine, ONLY: m_flush
USE mathconstants, ONLY: pi
USE mathlib, ONLY: gcd,&
lcm
USE molecule_kind_types, ONLY: get_molecule_kind,&
get_molecule_kind_set,&
molecule_kind_type
USE molecule_types, ONLY: molecule_type
USE parallel_rng_types, ONLY: UNIFORM,&
rng_stream_type
USE particle_types, ONLY: particle_type
USE qs_kind_types, ONLY: get_qs_kind,&
qs_kind_type
USE util, ONLY: sort
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
! *** Global parameters (in this module) ***
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'distribution_methods'
! *** Public subroutines ***
PUBLIC :: distribute_molecules_1d, &
distribute_molecules_2d
CONTAINS
! **************************************************************************************************
!> \brief Distribute molecules and particles
!> \param atomic_kind_set particle (atomic) kind information
!> \param particle_set particle information
!> \param local_particles distribution of particles created by this routine
!> \param molecule_kind_set molecule kind information
!> \param molecule_set molecule information
!> \param local_molecules distribution of molecules created by this routine
!> \param force_env_section ...
!> \param prev_molecule_kind_set previous molecule kind information, used with
!> prev_local_molecules
!> \param prev_local_molecules previous distribution of molecules, new one will
!> be identical if all the prev_* arguments are present and associated
!> \par History
!> none
!> \author MK (Jun. 2003)
! **************************************************************************************************
SUBROUTINE distribute_molecules_1d(atomic_kind_set, particle_set, &
local_particles, &
molecule_kind_set, molecule_set, &
local_molecules, force_env_section, &
prev_molecule_kind_set, &
prev_local_molecules)
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(distribution_1d_type), POINTER :: local_particles
TYPE(molecule_kind_type), DIMENSION(:), POINTER :: molecule_kind_set
TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
TYPE(distribution_1d_type), POINTER :: local_molecules
TYPE(section_vals_type), POINTER :: force_env_section
TYPE(molecule_kind_type), DIMENSION(:), OPTIONAL, &
POINTER :: prev_molecule_kind_set
TYPE(distribution_1d_type), OPTIONAL, POINTER :: prev_local_molecules
CHARACTER(len=*), PARAMETER :: routineN = 'distribute_molecules_1d'
INTEGER :: atom_a, bin, handle, iatom, imolecule, imolecule_kind, imolecule_local, &
imolecule_prev_kind, iparticle_kind, ipe, iw, kind_a, molecule_a, n, natom, nbins, nload, &
nmolecule, nmolecule_kind, nparticle_kind, nsgf, output_unit
INTEGER(int_8) :: bin_price
INTEGER(int_8), ALLOCATABLE, DIMENSION(:) :: workload_count, workload_fill
INTEGER, ALLOCATABLE, DIMENSION(:) :: nmolecule_local, nparticle_local, work
INTEGER, DIMENSION(:), POINTER :: molecule_list
LOGICAL :: found, has_prev_subsys_info, is_local
TYPE(cp_1d_i_p_type), ALLOCATABLE, DIMENSION(:) :: local_molecule
TYPE(cp_heap_type) :: bin_heap_count, bin_heap_fill
TYPE(cp_logger_type), POINTER :: logger
TYPE(molecule_kind_type), POINTER :: molecule_kind
CALL timeset(routineN, handle)
has_prev_subsys_info = .FALSE.
IF (PRESENT(prev_local_molecules) .AND. &
PRESENT(prev_molecule_kind_set)) THEN
IF (ASSOCIATED(prev_local_molecules) .AND. &
ASSOCIATED(prev_molecule_kind_set)) THEN
has_prev_subsys_info = .TRUE.
END IF
END IF
logger => cp_get_default_logger()
ASSOCIATE (group => logger%para_env, mype => logger%para_env%mepos + 1, &
npe => logger%para_env%num_pe)
ALLOCATE (workload_count(npe))
workload_count(:) = 0
ALLOCATE (workload_fill(npe))
workload_fill(:) = 0
nmolecule_kind = SIZE(molecule_kind_set)
ALLOCATE (nmolecule_local(nmolecule_kind))
nmolecule_local(:) = 0
ALLOCATE (local_molecule(nmolecule_kind))
nparticle_kind = SIZE(atomic_kind_set)
ALLOCATE (nparticle_local(nparticle_kind))
nparticle_local(:) = 0
nbins = npe
CALL cp_heap_new(bin_heap_count, nbins)
CALL cp_heap_fill(bin_heap_count, workload_count)
CALL cp_heap_new(bin_heap_fill, nbins)
CALL cp_heap_fill(bin_heap_fill, workload_fill)
DO imolecule_kind = 1, nmolecule_kind
molecule_kind => molecule_kind_set(imolecule_kind)
NULLIFY (molecule_list)
! *** Get the number of molecules and the number of ***
! *** atoms in each molecule of that molecular kind ***
CALL get_molecule_kind(molecule_kind=molecule_kind, &
molecule_list=molecule_list, &
natom=natom, &
nsgf=nsgf)
! *** Consider the number of atoms or basis ***
! *** functions which depends on the method ***
nload = MAX(natom, nsgf)
nmolecule = SIZE(molecule_list)
! *** Get the number of local molecules of the current molecule kind ***
DO imolecule = 1, nmolecule
IF (has_prev_subsys_info) THEN
DO imolecule_prev_kind = 1, SIZE(prev_molecule_kind_set)
IF (ANY(prev_local_molecules%list(imolecule_prev_kind)%array( &
1:prev_local_molecules%n_el(imolecule_prev_kind)) == molecule_list(imolecule))) THEN
! molecule used to be local
nmolecule_local(imolecule_kind) = nmolecule_local(imolecule_kind) + 1
END IF
END DO
ELSE
CALL cp_heap_get_first(bin_heap_count, bin, bin_price, found)
IF (.NOT. found) &
CPABORT("No topmost heap element found.")
ipe = bin
IF (bin_price /= workload_count(ipe)) &
CPABORT("inconsistent heap")
workload_count(ipe) = workload_count(ipe) + nload
IF (ipe == mype) THEN
nmolecule_local(imolecule_kind) = nmolecule_local(imolecule_kind) + 1
END IF
bin_price = workload_count(ipe)
CALL cp_heap_reset_first(bin_heap_count, bin_price)
END IF
END DO
! *** Distribute the molecules ***
n = nmolecule_local(imolecule_kind)
IF (n > 0) THEN
ALLOCATE (local_molecule(imolecule_kind)%array(n))
ELSE
NULLIFY (local_molecule(imolecule_kind)%array)
END IF
imolecule_local = 0
DO imolecule = 1, nmolecule
is_local = .FALSE.
IF (has_prev_subsys_info) THEN
DO imolecule_prev_kind = 1, SIZE(prev_molecule_kind_set)
IF (ANY(prev_local_molecules%list(imolecule_prev_kind)%array( &
1:prev_local_molecules%n_el(imolecule_prev_kind)) == molecule_list(imolecule))) THEN
is_local = .TRUE.
END IF
END DO
ELSE
CALL cp_heap_get_first(bin_heap_fill, bin, bin_price, found)
IF (.NOT. found) &
CPABORT("No topmost heap element found.")
ipe = bin
IF (bin_price /= workload_fill(ipe)) &
CPABORT("inconsistent heap")
workload_fill(ipe) = workload_fill(ipe) + nload
is_local = (ipe == mype)
END IF
IF (is_local) THEN
imolecule_local = imolecule_local + 1
molecule_a = molecule_list(imolecule)
local_molecule(imolecule_kind)%array(imolecule_local) = molecule_a
DO iatom = 1, natom
atom_a = molecule_set(molecule_a)%first_atom + iatom - 1
CALL get_atomic_kind(atomic_kind=particle_set(atom_a)%atomic_kind, &
kind_number=kind_a)
nparticle_local(kind_a) = nparticle_local(kind_a) + 1
END DO
END IF
IF (.NOT. has_prev_subsys_info) THEN
bin_price = workload_fill(ipe)
CALL cp_heap_reset_first(bin_heap_fill, bin_price)
END IF
END DO
END DO
IF (ANY(workload_fill .NE. workload_count)) &
CPABORT("Inconsistent heaps encountered")
CALL cp_heap_release(bin_heap_count)
CALL cp_heap_release(bin_heap_fill)
! *** Create the local molecule structure ***
CALL distribution_1d_create(local_molecules, &
n_el=nmolecule_local, &
para_env=logger%para_env)
! *** Create the local particle structure ***
CALL distribution_1d_create(local_particles, &
n_el=nparticle_local, &
para_env=logger%para_env)
! *** Store the generated local molecule and particle distributions ***
nparticle_local(:) = 0
DO imolecule_kind = 1, nmolecule_kind
IF (nmolecule_local(imolecule_kind) == 0) CYCLE
local_molecules%list(imolecule_kind)%array(:) = &
local_molecule(imolecule_kind)%array(:)
molecule_kind => molecule_kind_set(imolecule_kind)
CALL get_molecule_kind(molecule_kind=molecule_kind, &
natom=natom)
DO imolecule = 1, nmolecule_local(imolecule_kind)
molecule_a = local_molecule(imolecule_kind)%array(imolecule)
DO iatom = 1, natom
atom_a = molecule_set(molecule_a)%first_atom + iatom - 1
CALL get_atomic_kind(atomic_kind=particle_set(atom_a)%atomic_kind, &
kind_number=kind_a)
nparticle_local(kind_a) = nparticle_local(kind_a) + 1
local_particles%list(kind_a)%array(nparticle_local(kind_a)) = atom_a
END DO
END DO
END DO
! *** Print distribution, if requested ***
IF (BTEST(cp_print_key_should_output(logger%iter_info, &
force_env_section, "PRINT%DISTRIBUTION1D"), cp_p_file)) THEN
output_unit = cp_print_key_unit_nr(logger, force_env_section, "PRINT%DISTRIBUTION1D", &
extension=".Log")
iw = output_unit
IF (output_unit < 0) iw = cp_logger_get_default_unit_nr(logger, LOCAL=.TRUE.)
! *** Print molecule distribution ***
ALLOCATE (work(npe))
work(:) = 0
work(mype) = SUM(nmolecule_local)
CALL group%sum(work)
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, &
FMT="(/, T2, A, T51, A, /, (T52, I6, T73, I8))") &
"DISTRIBUTION OF THE MOLECULES", &
"Process Number of molecules", &
(ipe - 1, work(ipe), ipe=1, npe)
WRITE (UNIT=output_unit, FMT="(T55, A3, T73, I8)") &
"Sum", SUM(work)
CALL m_flush(output_unit)
END IF
CALL group%sync()
DO ipe = 1, npe
IF (ipe == mype) THEN
WRITE (UNIT=iw, FMT="(/, T3, A)") &
"Process Kind Local molecules (global indices)"
DO imolecule_kind = 1, nmolecule_kind
IF (imolecule_kind == 1) THEN
WRITE (UNIT=iw, FMT="(T4, I6, 2X, I5, (T21, 10I6))") &
ipe - 1, imolecule_kind, &
(local_molecules%list(imolecule_kind)%array(imolecule), &
imolecule=1, nmolecule_local(imolecule_kind))
ELSE
WRITE (UNIT=iw, FMT="(T12, I5, (T21, 10I6))") &
imolecule_kind, &
(local_molecules%list(imolecule_kind)%array(imolecule), &
imolecule=1, nmolecule_local(imolecule_kind))
END IF
END DO
END IF
CALL m_flush(iw)
CALL group%sync()
END DO
! *** Print particle distribution ***
work(:) = 0
work(mype) = SUM(nparticle_local)
CALL group%sum(work)
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, &
FMT="(/, T2, A, T51, A, /, (T52, I6, T73, I8))") &
"DISTRIBUTION OF THE PARTICLES", &
"Process Number of particles", &
(ipe - 1, work(ipe), ipe=1, npe)
WRITE (UNIT=output_unit, FMT="(T55, A3, T73, I8)") &
"Sum", SUM(work)
CALL m_flush(output_unit)
END IF
CALL group%sync()
DO ipe = 1, npe
IF (ipe == mype) THEN
WRITE (UNIT=iw, FMT="(/, T3, A)") &
"Process Kind Local particles (global indices)"
DO iparticle_kind = 1, nparticle_kind
IF (iparticle_kind == 1) THEN
WRITE (UNIT=iw, FMT="(T4, I6, 2X, I5, (T20, 10I6))") &
ipe - 1, iparticle_kind, &
(local_particles%list(iparticle_kind)%array(iatom), &
iatom=1, nparticle_local(iparticle_kind))
ELSE
WRITE (UNIT=iw, FMT="(T12, I5, (T20, 10I6))") &
iparticle_kind, &
(local_particles%list(iparticle_kind)%array(iatom), &
iatom=1, nparticle_local(iparticle_kind))
END IF
END DO
END IF
CALL m_flush(iw)
CALL group%sync()
END DO
DEALLOCATE (work)
CALL cp_print_key_finished_output(output_unit, logger, force_env_section, &
"PRINT%DISTRIBUTION1D")
END IF
END ASSOCIATE
! *** Release work storage ***
DEALLOCATE (workload_count)
DEALLOCATE (workload_fill)
DEALLOCATE (nmolecule_local)
DEALLOCATE (nparticle_local)
DO imolecule_kind = 1, nmolecule_kind
IF (ASSOCIATED(local_molecule(imolecule_kind)%array)) THEN
DEALLOCATE (local_molecule(imolecule_kind)%array)
END IF
END DO
DEALLOCATE (local_molecule)
CALL timestop(handle)
END SUBROUTINE distribute_molecules_1d
! **************************************************************************************************
!> \brief Distributes the particle pairs creating a 2d distribution optimally
!> suited for quickstep
!> \param cell ...
!> \param atomic_kind_set ...
!> \param particle_set ...
!> \param qs_kind_set ...
!> \param molecule_kind_set ...
!> \param molecule_set ...
!> \param distribution_2d the distribution that will be created by this
!> method
!> \param blacs_env the parallel environment at the basis of the
!> distribution
!> \param force_env_section ...
!> \par History
!> - local_rows & cols blocksize optimizations (Aug. 2003, MK)
!> - cleanup of distribution_2d (Sep. 2003, fawzi)
!> - update for molecules (Oct. 2003, MK)
!> \author fawzi (Feb. 2003)
!> \note
!> Intermediate generation of a 2d distribution of the molecules, but
!> only the corresponding particle (atomic) distribution is currently
!> used. The 2d distribution of the molecules is deleted, but may easily
!> be recovered (MK).
! **************************************************************************************************
SUBROUTINE distribute_molecules_2d(cell, atomic_kind_set, particle_set, &
qs_kind_set, molecule_kind_set, molecule_set, &
distribution_2d, blacs_env, force_env_section)
TYPE(cell_type), POINTER :: cell
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(molecule_kind_type), DIMENSION(:), POINTER :: molecule_kind_set
TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
TYPE(distribution_2d_type), POINTER :: distribution_2d
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(section_vals_type), POINTER :: force_env_section
CHARACTER(len=*), PARAMETER :: routineN = 'distribute_molecules_2d'
INTEGER :: cluster_price, cost_model, handle, iatom, iatom_mol, iatom_one, ikind, imol, &
imolecule, imolecule_kind, iparticle_kind, ipcol, iprow, iw, kind_a, n, natom, natom_mol, &
nclusters, nmolecule, nmolecule_kind, nparticle_kind, nsgf, output_unit
INTEGER, ALLOCATABLE, DIMENSION(:) :: cluster_list, cluster_prices, &
nparticle_local_col, &
nparticle_local_row, work
INTEGER, DIMENSION(:), POINTER :: lmax_basis, molecule_list
INTEGER, DIMENSION(:, :), POINTER :: cluster_col_distribution, &
cluster_row_distribution, &
col_distribution, row_distribution
LOGICAL :: basic_cluster_optimization, basic_optimization, basic_spatial_optimization, &
molecular_distribution, skip_optimization
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: coords, pbc_scaled_coords
REAL(KIND=dp), DIMENSION(3) :: center
TYPE(cp_1d_i_p_type), DIMENSION(:), POINTER :: local_particle_col, local_particle_row
TYPE(cp_logger_type), POINTER :: logger
TYPE(gto_basis_set_type), POINTER :: orb_basis_set
TYPE(molecule_kind_type), POINTER :: molecule_kind
TYPE(section_vals_type), POINTER :: distribution_section
!...
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
distribution_section => section_vals_get_subs_vals(force_env_section, "DFT%QS%DISTRIBUTION")
CALL section_vals_val_get(distribution_section, "2D_MOLECULAR_DISTRIBUTION", l_val=molecular_distribution)
CALL section_vals_val_get(distribution_section, "SKIP_OPTIMIZATION", l_val=skip_optimization)
CALL section_vals_val_get(distribution_section, "BASIC_OPTIMIZATION", l_val=basic_optimization)
CALL section_vals_val_get(distribution_section, "BASIC_SPATIAL_OPTIMIZATION", l_val=basic_spatial_optimization)
CALL section_vals_val_get(distribution_section, "BASIC_CLUSTER_OPTIMIZATION", l_val=basic_cluster_optimization)
CALL section_vals_val_get(distribution_section, "COST_MODEL", i_val=cost_model)
!
ASSOCIATE (group => blacs_env%para_env, myprow => blacs_env%mepos(1) + 1, mypcol => blacs_env%mepos(2) + 1, &
nprow => blacs_env%num_pe(1), npcol => blacs_env%num_pe(2))
nmolecule_kind = SIZE(molecule_kind_set)
CALL get_molecule_kind_set(molecule_kind_set, nmolecule=nmolecule)
nparticle_kind = SIZE(atomic_kind_set)
CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, natom=natom)
!
! we need to generate two representations of the distribution, one as a straight array with global particles
! one ordered wrt to kinds and only listing the local particles
!
ALLOCATE (row_distribution(natom, 2))
ALLOCATE (col_distribution(natom, 2))
! Initialize the distributions to -1, as the second dimension only gets set with cluster optimization
! but the information is needed by dbcsr
row_distribution = -1; col_distribution = -1
ALLOCATE (local_particle_col(nparticle_kind))
ALLOCATE (local_particle_row(nparticle_kind))
ALLOCATE (nparticle_local_row(nparticle_kind))
ALLOCATE (nparticle_local_col(nparticle_kind))
IF (basic_optimization .OR. basic_spatial_optimization .OR. basic_cluster_optimization) THEN
IF (molecular_distribution) THEN
nclusters = nmolecule
ELSE
nclusters = natom
END IF
ALLOCATE (cluster_list(nclusters))
ALLOCATE (cluster_prices(nclusters))
ALLOCATE (cluster_row_distribution(nclusters, 2))
ALLOCATE (cluster_col_distribution(nclusters, 2))
cluster_row_distribution = -1; cluster_col_distribution = -1
! Fill in the clusters and their prices
CALL section_vals_val_get(distribution_section, "COST_MODEL", i_val=cost_model)
IF (.NOT. molecular_distribution) THEN
DO iatom = 1, natom
IF (iatom .GT. nclusters) &
CPABORT("Bounds error")
CALL get_atomic_kind(particle_set(iatom)%atomic_kind, kind_number=ikind)
cluster_list(iatom) = iatom
SELECT CASE (cost_model)
CASE (model_block_count)
CALL get_qs_kind(qs_kind_set(ikind), nsgf=nsgf)
cluster_price = nsgf
CASE (model_block_lmax)
CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
CALL get_gto_basis_set(orb_basis_set, lmax=lmax_basis)
cluster_price = MAXVAL(lmax_basis)
CASE default
CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
CALL get_gto_basis_set(orb_basis_set, lmax=lmax_basis)
cluster_price = 8 + (MAXVAL(lmax_basis)**2)
END SELECT
cluster_prices(iatom) = cluster_price
END DO
ELSE
imol = 0
DO imolecule_kind = 1, nmolecule_kind
molecule_kind => molecule_kind_set(imolecule_kind)
CALL get_molecule_kind(molecule_kind=molecule_kind, molecule_list=molecule_list, natom=natom_mol)
DO imolecule = 1, SIZE(molecule_list)
imol = imol + 1
cluster_list(imol) = imol
cluster_price = 0
DO iatom_mol = 1, natom_mol
iatom = molecule_set(molecule_list(imolecule))%first_atom + iatom_mol - 1
CALL get_atomic_kind(particle_set(iatom)%atomic_kind, kind_number=ikind)
SELECT CASE (cost_model)
CASE (model_block_count)
CALL get_qs_kind(qs_kind_set(ikind), nsgf=nsgf)
cluster_price = cluster_price + nsgf
CASE (model_block_lmax)
CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
CALL get_gto_basis_set(orb_basis_set, lmax=lmax_basis)
cluster_price = cluster_price + MAXVAL(lmax_basis)
CASE default
CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
CALL get_gto_basis_set(orb_basis_set, lmax=lmax_basis)
cluster_price = cluster_price + 8 + (MAXVAL(lmax_basis)**2)
END SELECT
END DO
cluster_prices(imol) = cluster_price
END DO
END DO
END IF
! And distribute
IF (basic_optimization) THEN
CALL make_basic_distribution(cluster_list, cluster_prices, &
nprow, cluster_row_distribution(:, 1), npcol, cluster_col_distribution(:, 1))
ELSE
IF (basic_cluster_optimization) THEN
IF (molecular_distribution) &
CPABORT("clustering and molecular blocking NYI")
ALLOCATE (pbc_scaled_coords(3, natom), coords(3, natom))
DO iatom = 1, natom
CALL real_to_scaled(pbc_scaled_coords(:, iatom), pbc(particle_set(iatom)%r(:), cell), cell)
coords(:, iatom) = pbc(particle_set(iatom)%r(:), cell)
END DO
CALL make_cluster_distribution(coords, pbc_scaled_coords, cell, cluster_prices, &
nprow, cluster_row_distribution, npcol, cluster_col_distribution)
ELSE ! basic_spatial_optimization
ALLOCATE (pbc_scaled_coords(3, nclusters))
IF (.NOT. molecular_distribution) THEN
! just scaled coords
DO iatom = 1, natom
CALL real_to_scaled(pbc_scaled_coords(:, iatom), pbc(particle_set(iatom)%r(:), cell), cell)
END DO
ELSE
! use scaled coords of geometric center, folding when appropriate
imol = 0
DO imolecule_kind = 1, nmolecule_kind
molecule_kind => molecule_kind_set(imolecule_kind)
CALL get_molecule_kind(molecule_kind=molecule_kind, molecule_list=molecule_list, natom=natom_mol)
DO imolecule = 1, SIZE(molecule_list)
imol = imol + 1
iatom_one = molecule_set(molecule_list(imolecule))%first_atom
center = 0.0_dp
DO iatom_mol = 1, natom_mol
iatom = molecule_set(molecule_list(imolecule))%first_atom + iatom_mol - 1
center = center + &
pbc(particle_set(iatom)%r(:) - particle_set(iatom_one)%r(:), cell) + particle_set(iatom_one)%r(:)
END DO
center = center/natom_mol
CALL real_to_scaled(pbc_scaled_coords(:, imol), pbc(center, cell), cell)
END DO
END DO
END IF
CALL make_basic_spatial_distribution(pbc_scaled_coords, cluster_prices, &
nprow, cluster_row_distribution(:, 1), npcol, cluster_col_distribution(:, 1))
DEALLOCATE (pbc_scaled_coords)
END IF
END IF
! And assign back
IF (.NOT. molecular_distribution) THEN
row_distribution = cluster_row_distribution
col_distribution = cluster_col_distribution
ELSE
imol = 0
DO imolecule_kind = 1, nmolecule_kind
molecule_kind => molecule_kind_set(imolecule_kind)
CALL get_molecule_kind(molecule_kind=molecule_kind, molecule_list=molecule_list, natom=natom_mol)
DO imolecule = 1, SIZE(molecule_list)
imol = imol + 1
DO iatom_mol = 1, natom_mol
iatom = molecule_set(molecule_list(imolecule))%first_atom + iatom_mol - 1
row_distribution(iatom, :) = cluster_row_distribution(imol, :)
col_distribution(iatom, :) = cluster_col_distribution(imol, :)
END DO
END DO
END DO
END IF
! cleanup
DEALLOCATE (cluster_list)
DEALLOCATE (cluster_prices)
DEALLOCATE (cluster_row_distribution)
DEALLOCATE (cluster_col_distribution)
ELSE
! expects nothing else
CPABORT("")
END IF
! prepare the lists of local particles
! count local particles of a given kind
nparticle_local_col = 0
nparticle_local_row = 0
DO iatom = 1, natom
CALL get_atomic_kind(atomic_kind=particle_set(iatom)%atomic_kind, kind_number=kind_a)
IF (row_distribution(iatom, 1) == myprow) nparticle_local_row(kind_a) = nparticle_local_row(kind_a) + 1
IF (col_distribution(iatom, 1) == mypcol) nparticle_local_col(kind_a) = nparticle_local_col(kind_a) + 1
END DO
! allocate space
DO iparticle_kind = 1, nparticle_kind
n = nparticle_local_row(iparticle_kind)
ALLOCATE (local_particle_row(iparticle_kind)%array(n))
n = nparticle_local_col(iparticle_kind)
ALLOCATE (local_particle_col(iparticle_kind)%array(n))
END DO
! store
nparticle_local_col = 0
nparticle_local_row = 0
DO iatom = 1, natom
CALL get_atomic_kind(atomic_kind=particle_set(iatom)%atomic_kind, kind_number=kind_a)
IF (row_distribution(iatom, 1) == myprow) THEN
nparticle_local_row(kind_a) = nparticle_local_row(kind_a) + 1
local_particle_row(kind_a)%array(nparticle_local_row(kind_a)) = iatom
END IF
IF (col_distribution(iatom, 1) == mypcol) THEN
nparticle_local_col(kind_a) = nparticle_local_col(kind_a) + 1
local_particle_col(kind_a)%array(nparticle_local_col(kind_a)) = iatom
END IF
END DO
! *** Generate the 2d distribution structure but take care of the zero offsets required
row_distribution(:, 1) = row_distribution(:, 1) - 1
col_distribution(:, 1) = col_distribution(:, 1) - 1
CALL distribution_2d_create(distribution_2d, &
row_distribution_ptr=row_distribution, &
col_distribution_ptr=col_distribution, &
local_rows_ptr=local_particle_row, &
local_cols_ptr=local_particle_col, &
blacs_env=blacs_env)
NULLIFY (local_particle_row)
NULLIFY (local_particle_col)
NULLIFY (row_distribution)
NULLIFY (col_distribution)
! *** Print distribution, if requested ***
IF (BTEST(cp_print_key_should_output(logger%iter_info, &
force_env_section, "PRINT%DISTRIBUTION"), cp_p_file)) THEN
output_unit = cp_print_key_unit_nr(logger, force_env_section, "PRINT%DISTRIBUTION", &
extension=".Log")
! *** Print row distribution ***
ALLOCATE (work(nprow))
work(:) = 0
IF (mypcol == 1) work(myprow) = SUM(distribution_2d%n_local_rows)
CALL group%sum(work)
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, &
FMT="(/, T2, A, /, T15, A, /, (T16, I10, T41, I10, T71, I10))") &
"DISTRIBUTION OF THE PARTICLES (ROWS)", &
"Process row Number of particles Number of matrix rows", &
(iprow - 1, work(iprow), -1, iprow=1, nprow)
WRITE (UNIT=output_unit, FMT="(T23, A3, T41, I10, T71, I10)") &
"Sum", SUM(work), -1
CALL m_flush(output_unit)
END IF
DEALLOCATE (work)
! *** Print column distribution ***
ALLOCATE (work(npcol))
work(:) = 0
IF (myprow == 1) work(mypcol) = SUM(distribution_2d%n_local_cols)
CALL group%sum(work)
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, &
FMT="(/, T2, A, /, T15, A, /, (T16, I10, T41, I10, T71, I10))") &
"DISTRIBUTION OF THE PARTICLES (COLUMNS)", &
"Process col Number of particles Number of matrix columns", &
(ipcol - 1, work(ipcol), -1, ipcol=1, npcol)
WRITE (UNIT=output_unit, FMT="(T23, A3, T41, I10, T71, I10)") &
"Sum", SUM(work), -1
CALL m_flush(output_unit)
END IF
DEALLOCATE (work)
CALL cp_print_key_finished_output(output_unit, logger, force_env_section, &
"PRINT%DISTRIBUTION")
END IF
END ASSOCIATE
IF (BTEST(cp_print_key_should_output(logger%iter_info, &
force_env_section, "PRINT%DISTRIBUTION2D"), cp_p_file)) THEN
iw = cp_logger_get_default_unit_nr(logger, LOCAL=.TRUE.)
CALL distribution_2d_write(distribution_2d, &
unit_nr=iw, &
local=.TRUE., &
long_description=.TRUE.)
END IF
! *** Release work storage ***
DEALLOCATE (nparticle_local_row)
DEALLOCATE (nparticle_local_col)
CALL timestop(handle)
END SUBROUTINE distribute_molecules_2d
! **************************************************************************************************
!> \brief Creates a basic distribution
!> \param cluster_list ...
!> \param cluster_prices ...
!> \param nprows ...
!> \param row_distribution ...
!> \param npcols ...
!> \param col_distribution ...
!> \par History
!> - Created 2010-08-06 UB
! **************************************************************************************************
SUBROUTINE make_basic_distribution(cluster_list, cluster_prices, &
nprows, row_distribution, npcols, col_distribution)
INTEGER, DIMENSION(:), INTENT(INOUT) :: cluster_list, cluster_prices
INTEGER, INTENT(IN) :: nprows
INTEGER, DIMENSION(:), INTENT(OUT) :: row_distribution
INTEGER, INTENT(IN) :: npcols
INTEGER, DIMENSION(:), INTENT(OUT) :: col_distribution
CHARACTER(len=*), PARAMETER :: routineN = 'make_basic_distribution'
INTEGER :: bin, cluster, cluster_index, &
cluster_price, nbins, nclusters, pcol, &
pgrid_gcd, prow, timing_handle
INTEGER(int_8) :: bin_price
LOGICAL :: found
TYPE(cp_heap_type) :: bin_heap
! ---------------------------------------------------------------------------
CALL timeset(routineN, timing_handle)
nbins = lcm(nprows, npcols)
pgrid_gcd = gcd(nprows, npcols)
CALL sort(cluster_prices, SIZE(cluster_list), cluster_list)
CALL cp_heap_new(bin_heap, nbins)
CALL cp_heap_fill(bin_heap, (/(0_int_8, bin=1, nbins)/))
!
nclusters = SIZE(cluster_list)
! Put the most expensive cluster in the bin with the smallest
! price and repeat.
DO cluster_index = nclusters, 1, -1
cluster = cluster_list(cluster_index)
CALL cp_heap_get_first(bin_heap, bin, bin_price, found)
IF (.NOT. found) &
CPABORT("No topmost heap element found.")
!
prow = INT((bin - 1)*pgrid_gcd/npcols)
IF (prow .GE. nprows) &
CPABORT("Invalid process row.")
pcol = INT((bin - 1)*pgrid_gcd/nprows)
IF (pcol .GE. npcols) &
CPABORT("Invalid process column.")
row_distribution(cluster) = prow + 1
col_distribution(cluster) = pcol + 1
!
cluster_price = cluster_prices(cluster_index)
bin_price = bin_price + cluster_price
CALL cp_heap_reset_first(bin_heap, bin_price)
END DO
CALL cp_heap_release(bin_heap)
CALL timestop(timing_handle)
END SUBROUTINE make_basic_distribution
! **************************************************************************************************
!> \brief Creates a basic spatial distribution
!> that tries to make the corresponding blocks as homogeneous as possible
!> \param pbc_scaled_coords ...
!> \param costs ...
!> \param nprows ...
!> \param row_distribution ...
!> \param npcols ...
!> \param col_distribution ...
!> \par History
!> - Created 2010-11-11 Joost VandeVondele
! **************************************************************************************************
SUBROUTINE make_basic_spatial_distribution(pbc_scaled_coords, costs, &
nprows, row_distribution, npcols, col_distribution)
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: pbc_scaled_coords
INTEGER, DIMENSION(:), INTENT(IN) :: costs
INTEGER, INTENT(IN) :: nprows
INTEGER, DIMENSION(:), INTENT(OUT) :: row_distribution
INTEGER, INTENT(IN) :: npcols
INTEGER, DIMENSION(:), INTENT(OUT) :: col_distribution
CHARACTER(len=*), PARAMETER :: routineN = 'make_basic_spatial_distribution'
INTEGER :: handle, iatom, natoms, nbins, pgrid_gcd
INTEGER, ALLOCATABLE, DIMENSION(:) :: bin_costs, distribution
CALL timeset(routineN, handle)
natoms = SIZE(costs)
nbins = lcm(nprows, npcols)
pgrid_gcd = gcd(nprows, npcols)
ALLOCATE (bin_costs(nbins), distribution(natoms))
bin_costs = 0
CALL spatial_recurse(pbc_scaled_coords, costs, (/(iatom, iatom=1, natoms)/), bin_costs, distribution, 0)
! WRITE(*, *) "Final bin costs: ", bin_costs
! final row_distribution / col_distribution
DO iatom = 1, natoms
row_distribution(iatom) = (distribution(iatom) - 1)*pgrid_gcd/npcols + 1
col_distribution(iatom) = (distribution(iatom) - 1)*pgrid_gcd/nprows + 1
END DO
DEALLOCATE (bin_costs, distribution)
CALL timestop(handle)
END SUBROUTINE make_basic_spatial_distribution
! **************************************************************************************************
!> \brief ...
!> \param pbc_scaled_coords ...
!> \param costs ...
!> \param indices ...
!> \param bin_costs ...
!> \param distribution ...
!> \param level ...
! **************************************************************************************************
RECURSIVE SUBROUTINE spatial_recurse(pbc_scaled_coords, costs, indices, bin_costs, distribution, level)
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: pbc_scaled_coords
INTEGER, DIMENSION(:), INTENT(IN) :: costs, indices
INTEGER, DIMENSION(:), INTENT(INOUT) :: bin_costs, distribution
INTEGER, INTENT(IN) :: level
INTEGER :: iatom, ibin, natoms, nbins, nhalf
INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_costs_sorted, atom_permutation, &
bin_costs_sorted, permutation
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: coord
natoms = SIZE(costs)
nbins = SIZE(bin_costs)
nhalf = (natoms + 1)/2
IF (natoms <= nbins) THEN
! assign the most expensive atom to the least costly bin
ALLOCATE (bin_costs_sorted(nbins), permutation(nbins))
bin_costs_sorted(:) = bin_costs
CALL sort(bin_costs_sorted, nbins, permutation)
ALLOCATE (atom_costs_sorted(natoms), atom_permutation(natoms))
atom_costs_sorted(:) = costs
CALL sort(atom_costs_sorted, natoms, atom_permutation)
ibin = 0
! WRITE(*, *) "Dealing with a new bunch of atoms "
DO iatom = natoms, 1, -1
ibin = ibin + 1
! WRITE(*, *) "atom", indices(atom_permutation(iatom)), "cost", atom_costs_sorted(iatom), &
! "bin", permutation(ibin), "its cost", bin_costs(permutation(ibin))
! WRITE(100, '(A, I0, 3F12.6)') "A", permutation(ibin), pbc_scaled_coords(:, atom_permutation(iatom))
bin_costs(permutation(ibin)) = bin_costs(permutation(ibin)) + atom_costs_sorted(iatom)
distribution(indices(atom_permutation(iatom))) = permutation(ibin)
END DO
DEALLOCATE (bin_costs_sorted, permutation, atom_costs_sorted, atom_permutation)
ELSE
! divide atoms in two subsets, sorting according to their coordinates, alternatively x, y, z
! recursively do this for both subsets
ALLOCATE (coord(natoms), permutation(natoms))
coord(:) = pbc_scaled_coords(MOD(level, 3) + 1, :)
CALL sort(coord, natoms, permutation)
CALL spatial_recurse(pbc_scaled_coords(:, permutation(1:nhalf)), costs(permutation(1:nhalf)), &
indices(permutation(1:nhalf)), bin_costs, distribution, level + 1)
CALL spatial_recurse(pbc_scaled_coords(:, permutation(nhalf + 1:)), costs(permutation(nhalf + 1:)), &
indices(permutation(nhalf + 1:)), bin_costs, distribution, level + 1)
DEALLOCATE (coord, permutation)
END IF
END SUBROUTINE spatial_recurse
! **************************************************************************************************
!> \brief creates a distribution placing close by atoms into clusters and
!> putting them on the same processors. Load balancing is
!> performed by balancing sum of the cluster costs per processor
!> \param coords coordinates of the system
!> \param scaled_coords scaled coordinates