-
Notifications
You must be signed in to change notification settings - Fork 1
/
hfx_load_balance_methods.F
2515 lines (2239 loc) · 115 KB
/
hfx_load_balance_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 Routines for optimizing load balance between processes in HFX calculations
!> \par History
!> 04.2008 created [Manuel Guidon]
!> \author Manuel Guidon
! **************************************************************************************************
MODULE hfx_load_balance_methods
USE cell_types, ONLY: cell_type
USE cp_files, ONLY: close_file, &
open_file
USE message_passing, ONLY: mp_para_env_type
USE hfx_pair_list_methods, ONLY: build_atomic_pair_list, &
build_pair_list
USE hfx_types, ONLY: &
hfx_basis_type, hfx_block_range_type, hfx_distribution, hfx_load_balance_type, hfx_p_kind, &
hfx_screen_coeff_type, hfx_set_distr_energy, hfx_set_distr_forces, hfx_type, &
pair_list_type, pair_set_list_type
USE input_constants, ONLY: hfx_do_eval_energy, &
hfx_do_eval_forces
USE kinds, ONLY: dp, &
int_8
USE message_passing, ONLY: mp_waitall, mp_request_type
USE parallel_rng_types, ONLY: UNIFORM, &
rng_stream_type
USE particle_types, ONLY: particle_type
USE util, ONLY: sort
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
PUBLIC :: hfx_load_balance, &
hfx_update_load_balance, &
collect_load_balance_info, cost_model, p1_energy, p2_energy, p3_energy
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'hfx_load_balance_methods'
REAL(KIND=dp), PARAMETER :: p1_energy(12) = (/2.9461408209700424_dp, 1.0624718662999657_dp, &
-1.91570128356921242E-002_dp, -1.6668495454436603_dp, &
1.7512639006523709_dp, -9.76074323945336081E-002_dp, &
2.6230786127311889_dp, -0.31870737623014189_dp, &
7.9588203912690973_dp, 1.8331423413134813_dp, &
-0.15427618665346299_dp, 0.19749436090711650_dp/)
REAL(KIND=dp), PARAMETER :: p2_energy(12) = (/2.3104682960662593_dp, 1.8744052737304417_dp, &
-9.36564055598656797E-002_dp, 0.64284973765086939_dp, &
1.0137565430060556_dp, -6.80088178288954567E-003_dp, &
1.1692629207374552_dp, -2.6314710080507573_dp, &
19.237814781880786_dp, 1.0505934173661349_dp, &
0.80382371955699250_dp, 0.49903401991818103_dp/)
REAL(KIND=dp), PARAMETER :: p3_energy(2) = (/7.82336287670072350E-002_dp, 0.38073304105744837_dp/)
REAL(KIND=dp), PARAMETER :: p1_forces(12) = (/2.5746279948798874_dp, 1.3420575378609276_dp, &
-9.41673106447732111E-002_dp, 0.94568006899317825_dp, &
-1.4511897117448544_dp, 0.59178934677316952_dp, &
2.7291149361757236_dp, -0.50555512044800210_dp, &
8.3508180969609871_dp, 1.6829982496141809_dp, &
-0.74895370472152600_dp, 0.43801726744197500_dp/)
REAL(KIND=dp), PARAMETER :: p2_forces(12) = (/2.6398568961569020_dp, 2.3024918834564101_dp, &
5.33216585432061581E-003_dp, 0.45572145697283628_dp, &
1.8119743851500618_dp, -0.12533918548421166_dp, &
-1.4040312084552751_dp, -4.5331650463917859_dp, &
12.593431549069477_dp, 1.1311978374487595_dp, &
1.4245996087624646_dp, 1.1425350529853495_dp/)
REAL(KIND=dp), PARAMETER :: p3_forces(2) = (/0.12051930516830946_dp, 1.3828051586144336_dp/)
!***
CONTAINS
! **************************************************************************************************
!> \brief Distributes the computation of eri's to all available processes.
!> \param x_data Object that stores the indices array
!> \param eps_schwarz screening parameter
!> \param particle_set , atomic_kind_set, para_env ...
!> \param max_set Maximum number of set to be considered
!> \param para_env para_env
!> \param coeffs_set screening functions
!> \param coeffs_kind screening functions
!> \param is_assoc_atomic_block_global KS-matrix sparsity
!> \param do_periodic flag for periodicity
!> \param load_balance_parameter Parameters for Monte-Carlo routines
!> \param kind_of helper array for mapping
!> \param basis_parameter Basis set parameters
!> \param pmax_set Initial screening matrix
!> \param pmax_atom ...
!> \param i_thread Process ID of current Thread
!> \param n_threads Total Number of Threads
!> \param cell cell
!> \param do_p_screening Flag for initial p screening
!> \param map_atom_to_kind_atom ...
!> \param nkind ...
!> \param eval_type ...
!> \param pmax_block ...
!> \param use_virial ...
!> \par History
!> 06.2007 created [Manuel Guidon]
!> 08.2007 new parallel scheme [Manuel Guidon]
!> 09.2007 new 'modulo' parellel scheme and Monte Carlo step [Manuel Guidon]
!> 11.2007 parallelize load balance on box_idx1 [Manuel Guidon]
!> 02.2009 completely refactored [Manuel Guidon]
!> \author Manuel Guidon
!> \note
!> The optimization is done via a binning procedure followed by simple
!> Monte Carlo procedure:
!> In a first step the total amount of integrals in the system is calculated,
!> taking into account the sparsity of the KS-matrix , the screening based
!> on near/farfield approximations and if desired the screening on an initial
!> density matrix.
!> In a second step, bins are generate that contain approximately the same number
!> of integrals, and a cost for these bins is estimated (currently the number of integrals)
!> In a third step, a Monte Carlo procedure optimizes the assignment
!> of the different loads to each process
!> At the end each process owns an unique array of *atomic* indices-ranges
!> that are used to decide whether a process has to calculate a certain
!> bunch of integrals or not
! **************************************************************************************************
SUBROUTINE hfx_load_balance(x_data, eps_schwarz, particle_set, max_set, para_env, &
coeffs_set, coeffs_kind, &
is_assoc_atomic_block_global, do_periodic, &
load_balance_parameter, kind_of, basis_parameter, pmax_set, &
pmax_atom, i_thread, n_threads, cell, &
do_p_screening, map_atom_to_kind_atom, nkind, eval_type, &
pmax_block, use_virial)
TYPE(hfx_type), POINTER :: x_data
REAL(dp), INTENT(IN) :: eps_schwarz
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
INTEGER, INTENT(IN) :: max_set
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(hfx_screen_coeff_type), &
DIMENSION(:, :, :, :), POINTER :: coeffs_set
TYPE(hfx_screen_coeff_type), DIMENSION(:, :), &
POINTER :: coeffs_kind
INTEGER, DIMENSION(:, :) :: is_assoc_atomic_block_global
LOGICAL :: do_periodic
TYPE(hfx_load_balance_type), POINTER :: load_balance_parameter
INTEGER :: kind_of(*)
TYPE(hfx_basis_type), DIMENSION(:), POINTER :: basis_parameter
TYPE(hfx_p_kind), DIMENSION(:), POINTER :: pmax_set
REAL(dp), DIMENSION(:, :), POINTER :: pmax_atom
INTEGER, INTENT(IN) :: i_thread, n_threads
TYPE(cell_type), POINTER :: cell
LOGICAL, INTENT(IN) :: do_p_screening
INTEGER, DIMENSION(:), POINTER :: map_atom_to_kind_atom
INTEGER, INTENT(IN) :: nkind, eval_type
REAL(dp), DIMENSION(:, :), POINTER :: pmax_block
LOGICAL, INTENT(IN) :: use_virial
CHARACTER(LEN=*), PARAMETER :: routineN = 'hfx_load_balance'
CHARACTER(LEN=512) :: error_msg
INTEGER :: block_size, current_block_id, data_from, dest, handle, handle_inner, &
handle_range, i, iatom_block, iatom_end, iatom_start, ibin, icpu, j, jatom_block, &
jatom_end, jatom_start, katom_block, katom_end, katom_start, latom_block, latom_end, &
latom_start, mepos, my_process_id, n_processes, natom, nbins, nblocks, ncpu, &
new_iatom_end, new_iatom_start, new_jatom_end, new_jatom_start, non_empty_blocks, &
objective_block_size, objective_nblocks, source, total_blocks
TYPE(mp_request_type), DIMENSION(2) :: req
INTEGER(int_8) :: atom_block, cost_per_bin, cost_per_core, current_cost, &
distribution_counter_end, distribution_counter_start, global_quartet_counter, &
local_quartet_counter, self_cost_per_block, tmp_block, total_block_self_cost
INTEGER(int_8), ALLOCATABLE, DIMENSION(:) :: buffer_in, buffer_out
INTEGER(int_8), DIMENSION(:), POINTER :: local_cost_matrix, recbuffer, &
sendbuffer, swapbuffer
INTEGER(int_8), DIMENSION(:), POINTER, SAVE :: cost_matrix
INTEGER(int_8), SAVE :: shm_global_quartet_counter, &
shm_local_quartet_counter
INTEGER, ALLOCATABLE, DIMENSION(:) :: rcount, rdispl, tmp_index, tmp_pos, &
to_be_sorted
INTEGER, DIMENSION(:), POINTER, SAVE :: shm_distribution_vector
INTEGER, SAVE :: shm_nblocks
LOGICAL :: changed, last_bin_needs_to_be_filled, &
optimized
LOGICAL, DIMENSION(:, :), POINTER, SAVE :: atomic_pair_list
REAL(dp) :: coeffs_kind_max0, log10_eps_schwarz, &
log_2, pmax_blocks
TYPE(hfx_block_range_type), DIMENSION(:), POINTER :: blocks_guess, tmp_blocks, tmp_blocks2
TYPE(hfx_block_range_type), DIMENSION(:), &
POINTER, SAVE :: shm_blocks
TYPE(hfx_distribution), DIMENSION(:), POINTER :: binned_dist, ptr_to_tmp_dist, tmp_dist
TYPE(hfx_distribution), DIMENSION(:, :), POINTER, &
SAVE :: full_dist
TYPE(pair_list_type) :: list_ij, list_kl
TYPE(pair_set_list_type), ALLOCATABLE, &
DIMENSION(:) :: set_list_ij, set_list_kl
!$OMP BARRIER
!$OMP MASTER
CALL timeset(routineN, handle)
!$OMP END MASTER
!$OMP BARRIER
log10_eps_schwarz = LOG10(eps_schwarz)
log_2 = LOG10(2.0_dp)
coeffs_kind_max0 = MAXVAL(coeffs_kind(:, :)%x(2))
ncpu = para_env%num_pe
n_processes = ncpu*n_threads
natom = SIZE(particle_set)
block_size = load_balance_parameter%block_size
ALLOCATE (set_list_ij((max_set*block_size)**2))
ALLOCATE (set_list_kl((max_set*block_size)**2))
IF (.NOT. load_balance_parameter%blocks_initialized) THEN
!$OMP BARRIER
!$OMP MASTER
CALL timeset(routineN//"_range", handle_range)
nblocks = MAX((natom + block_size - 1)/block_size, 1)
ALLOCATE (blocks_guess(nblocks))
ALLOCATE (tmp_blocks(natom))
ALLOCATE (tmp_blocks2(natom))
pmax_blocks = 0.0_dp
SELECT CASE (eval_type)
CASE (hfx_do_eval_energy)
atomic_pair_list => x_data%atomic_pair_list
CASE (hfx_do_eval_forces)
atomic_pair_list => x_data%atomic_pair_list_forces
END SELECT
atomic_pair_list = .TRUE.
CALL init_blocks(nkind, para_env, natom, block_size, nblocks, blocks_guess, &
list_ij, list_kl, set_list_ij, set_list_kl, &
particle_set, &
coeffs_set, coeffs_kind, &
is_assoc_atomic_block_global, do_periodic, &
kind_of, basis_parameter, pmax_set, pmax_atom, &
pmax_blocks, cell, &
do_p_screening, map_atom_to_kind_atom, eval_type, &
log10_eps_schwarz, log_2, coeffs_kind_max0, use_virial, atomic_pair_list)
total_block_self_cost = 0
DO i = 1, nblocks
total_block_self_cost = total_block_self_cost + blocks_guess(i)%cost
END DO
CALL para_env%sum(total_block_self_cost)
objective_block_size = load_balance_parameter%block_size
objective_nblocks = MAX((natom + objective_block_size - 1)/objective_block_size, 1)
self_cost_per_block = (total_block_self_cost + objective_nblocks - 1)/(objective_nblocks)
DO i = 1, nblocks
tmp_blocks2(i) = blocks_guess(i)
END DO
optimized = .FALSE.
i = 0
DO WHILE (.NOT. optimized)
i = i + 1
current_block_id = 0
changed = .FALSE.
DO atom_block = 1, nblocks
current_block_id = current_block_id + 1
iatom_start = tmp_blocks2(atom_block)%istart
iatom_end = tmp_blocks2(atom_block)%iend
IF (tmp_blocks2(atom_block)%cost > 1.5_dp*self_cost_per_block .AND. iatom_end - iatom_start > 0) THEN
changed = .TRUE.
new_iatom_start = iatom_start
new_iatom_end = (iatom_end - iatom_start + 1)/2 + iatom_start - 1
new_jatom_start = new_iatom_end + 1
new_jatom_end = iatom_end
tmp_blocks(current_block_id)%istart = new_iatom_start
tmp_blocks(current_block_id)%iend = new_iatom_end
tmp_blocks(current_block_id)%cost = estimate_block_cost( &
natom, nkind, list_ij, list_kl, set_list_ij, set_list_kl, &
new_iatom_start, new_iatom_end, new_iatom_start, new_iatom_end, &
new_iatom_start, new_iatom_end, new_iatom_start, new_iatom_end, &
particle_set, &
coeffs_set, coeffs_kind, &
is_assoc_atomic_block_global, do_periodic, &
kind_of, basis_parameter, pmax_set, pmax_atom, pmax_blocks, &
cell, &
do_p_screening, map_atom_to_kind_atom, eval_type, &
log10_eps_schwarz, log_2, coeffs_kind_max0, use_virial, atomic_pair_list)
current_block_id = current_block_id + 1
tmp_blocks(current_block_id)%istart = new_jatom_start
tmp_blocks(current_block_id)%iend = new_jatom_end
tmp_blocks(current_block_id)%cost = estimate_block_cost( &
natom, nkind, list_ij, list_kl, set_list_ij, set_list_kl, &
new_jatom_start, new_jatom_end, new_jatom_start, new_jatom_end, &
new_jatom_start, new_jatom_end, new_jatom_start, new_jatom_end, &
particle_set, &
coeffs_set, coeffs_kind, &
is_assoc_atomic_block_global, do_periodic, &
kind_of, basis_parameter, pmax_set, pmax_atom, pmax_blocks, &
cell, &
do_p_screening, map_atom_to_kind_atom, eval_type, &
log10_eps_schwarz, log_2, coeffs_kind_max0, use_virial, atomic_pair_list)
ELSE
tmp_blocks(current_block_id)%istart = iatom_start
tmp_blocks(current_block_id)%iend = iatom_end
tmp_blocks(current_block_id)%cost = tmp_blocks2(atom_block)%cost
END IF
END DO
IF (.NOT. changed) optimized = .TRUE.
IF (i > 20) optimized = .TRUE.
nblocks = current_block_id
DO atom_block = 1, nblocks
tmp_blocks2(atom_block) = tmp_blocks(atom_block)
END DO
END DO
DEALLOCATE (tmp_blocks2)
! ** count number of non empty blocks on each node
non_empty_blocks = 0
DO atom_block = 1, nblocks
IF (tmp_blocks(atom_block)%istart == 0) CYCLE
non_empty_blocks = non_empty_blocks + 1
END DO
ALLOCATE (rcount(ncpu))
rcount = 0
rcount(para_env%mepos + 1) = non_empty_blocks
CALL para_env%sum(rcount)
! ** sum all non_empty_blocks
total_blocks = 0
DO i = 1, ncpu
total_blocks = total_blocks + rcount(i)
END DO
! ** calculate offsets
ALLOCATE (rdispl(ncpu))
rcount(:) = rcount(:)*3
rdispl(1) = 0
DO i = 2, ncpu
rdispl(i) = rdispl(i - 1) + rcount(i - 1)
END DO
ALLOCATE (buffer_in(3*non_empty_blocks))
non_empty_blocks = 0
DO atom_block = 1, nblocks
IF (tmp_blocks(atom_block)%istart == 0) CYCLE
buffer_in(non_empty_blocks*3 + 1) = tmp_blocks(atom_block)%istart
buffer_in(non_empty_blocks*3 + 2) = tmp_blocks(atom_block)%iend
buffer_in(non_empty_blocks*3 + 3) = tmp_blocks(atom_block)%cost
non_empty_blocks = non_empty_blocks + 1
END DO
nblocks = total_blocks
ALLOCATE (tmp_blocks2(nblocks))
ALLOCATE (buffer_out(3*nblocks))
! ** Gather all three arrays
CALL para_env%allgatherv(buffer_in, buffer_out, rcount, rdispl)
DO i = 1, nblocks
tmp_blocks2(i)%istart = INT(buffer_out((i - 1)*3 + 1))
tmp_blocks2(i)%iend = INT(buffer_out((i - 1)*3 + 2))
tmp_blocks2(i)%cost = buffer_out((i - 1)*3 + 3)
END DO
! ** Now we sort the blocks
ALLOCATE (to_be_sorted(nblocks))
ALLOCATE (tmp_index(nblocks))
DO atom_block = 1, nblocks
to_be_sorted(atom_block) = tmp_blocks2(atom_block)%istart
END DO
CALL sort(to_be_sorted, nblocks, tmp_index)
ALLOCATE (x_data%blocks(nblocks))
DO atom_block = 1, nblocks
x_data%blocks(atom_block) = tmp_blocks2(tmp_index(atom_block))
END DO
shm_blocks => x_data%blocks
shm_nblocks = nblocks
! ** Set nblocks in structure
load_balance_parameter%nblocks = nblocks
DEALLOCATE (blocks_guess, tmp_blocks, tmp_blocks2)
DEALLOCATE (rcount, rdispl, buffer_in, buffer_out, to_be_sorted, tmp_index)
load_balance_parameter%blocks_initialized = .TRUE.
x_data%blocks = shm_blocks
load_balance_parameter%nblocks = shm_nblocks
load_balance_parameter%blocks_initialized = .TRUE.
ALLOCATE (x_data%pmax_block(shm_nblocks, shm_nblocks))
x_data%pmax_block = 0.0_dp
pmax_block => x_data%pmax_block
CALL timestop(handle_range)
!$OMP END MASTER
!$OMP BARRIER
IF (.NOT. load_balance_parameter%blocks_initialized) THEN
ALLOCATE (x_data%blocks(shm_nblocks))
x_data%blocks = shm_blocks
load_balance_parameter%nblocks = shm_nblocks
load_balance_parameter%blocks_initialized = .TRUE.
END IF
!! ** precalculate maximum density matrix elements in blocks
!$OMP BARRIER
END IF
!$OMP BARRIER
!$OMP MASTER
pmax_block => x_data%pmax_block
pmax_block = 0.0_dp
IF (do_p_screening) THEN
DO iatom_block = 1, shm_nblocks
iatom_start = x_data%blocks(iatom_block)%istart
iatom_end = x_data%blocks(iatom_block)%iend
DO jatom_block = 1, shm_nblocks
jatom_start = x_data%blocks(jatom_block)%istart
jatom_end = x_data%blocks(jatom_block)%iend
pmax_block(iatom_block, jatom_block) = MAXVAL(pmax_atom(iatom_start:iatom_end, jatom_start:jatom_end))
END DO
END DO
END IF
SELECT CASE (eval_type)
CASE (hfx_do_eval_energy)
atomic_pair_list => x_data%atomic_pair_list
CASE (hfx_do_eval_forces)
atomic_pair_list => x_data%atomic_pair_list_forces
END SELECT
CALL build_atomic_pair_list(natom, atomic_pair_list, kind_of, basis_parameter, particle_set, &
do_periodic, coeffs_kind, coeffs_kind_max0, log10_eps_schwarz, cell, &
x_data%blocks)
!$OMP END MASTER
!$OMP BARRIER
!! If there is only 1 cpu skip the binning
IF (n_processes == 1) THEN
ALLOCATE (tmp_dist(1))
tmp_dist(1)%number_of_atom_quartets = HUGE(tmp_dist(1)%number_of_atom_quartets)
tmp_dist(1)%istart = 0_int_8
ptr_to_tmp_dist => tmp_dist(:)
SELECT CASE (eval_type)
CASE (hfx_do_eval_energy)
CALL hfx_set_distr_energy(ptr_to_tmp_dist, x_data)
CASE (hfx_do_eval_forces)
CALL hfx_set_distr_forces(ptr_to_tmp_dist, x_data)
END SELECT
DEALLOCATE (tmp_dist)
ELSE
!! Calculate total numbers of integrals that have to be calculated (wrt screening and symmetry)
!$OMP BARRIER
!$OMP MASTER
CALL timeset(routineN//"_count", handle_inner)
!$OMP END MASTER
!$OMP BARRIER
cost_per_core = 0_int_8
my_process_id = para_env%mepos*n_threads + i_thread
nblocks = load_balance_parameter%nblocks
DO atom_block = my_process_id, INT(nblocks, KIND=int_8)**4 - 1, n_processes
latom_block = INT(MODULO(atom_block, INT(nblocks, KIND=int_8))) + 1
tmp_block = atom_block/nblocks
katom_block = INT(MODULO(tmp_block, INT(nblocks, KIND=int_8))) + 1
IF (latom_block < katom_block) CYCLE
tmp_block = tmp_block/nblocks
jatom_block = INT(MODULO(tmp_block, INT(nblocks, KIND=int_8))) + 1
tmp_block = tmp_block/nblocks
iatom_block = INT(MODULO(tmp_block, INT(nblocks, KIND=int_8))) + 1
IF (jatom_block < iatom_block) CYCLE
iatom_start = x_data%blocks(iatom_block)%istart
iatom_end = x_data%blocks(iatom_block)%iend
jatom_start = x_data%blocks(jatom_block)%istart
jatom_end = x_data%blocks(jatom_block)%iend
katom_start = x_data%blocks(katom_block)%istart
katom_end = x_data%blocks(katom_block)%iend
latom_start = x_data%blocks(latom_block)%istart
latom_end = x_data%blocks(latom_block)%iend
SELECT CASE (eval_type)
CASE (hfx_do_eval_energy)
pmax_blocks = MAX(pmax_block(katom_block, iatom_block), &
pmax_block(latom_block, jatom_block), &
pmax_block(latom_block, iatom_block), &
pmax_block(katom_block, jatom_block))
CASE (hfx_do_eval_forces)
pmax_blocks = MAX(pmax_block(katom_block, iatom_block) + &
pmax_block(latom_block, jatom_block), &
pmax_block(latom_block, iatom_block) + &
pmax_block(katom_block, jatom_block))
END SELECT
IF (2.0_dp*coeffs_kind_max0 + pmax_blocks < log10_eps_schwarz) CYCLE
cost_per_core = cost_per_core &
+ estimate_block_cost(natom, nkind, list_ij, list_kl, set_list_ij, set_list_kl, &
iatom_start, iatom_end, jatom_start, jatom_end, &
katom_start, katom_end, latom_start, latom_end, &
particle_set, &
coeffs_set, coeffs_kind, &
is_assoc_atomic_block_global, do_periodic, &
kind_of, basis_parameter, pmax_set, pmax_atom, pmax_blocks, &
cell, &
do_p_screening, map_atom_to_kind_atom, eval_type, &
log10_eps_schwarz, log_2, coeffs_kind_max0, use_virial, atomic_pair_list)
END DO ! atom_block
nbins = load_balance_parameter%nbins
cost_per_bin = (cost_per_core + nbins - 1)/(nbins)
!$OMP BARRIER
!$OMP MASTER
CALL timestop(handle_inner)
!$OMP END MASTER
!$OMP BARRIER
! new load balancing test
IF (.FALSE.) THEN
CALL hfx_recursive_load_balance(n_processes, my_process_id, nblocks, &
natom, nkind, list_ij, list_kl, set_list_ij, set_list_kl, &
particle_set, &
coeffs_set, coeffs_kind, &
is_assoc_atomic_block_global, do_periodic, &
kind_of, basis_parameter, pmax_set, pmax_atom, pmax_blocks, &
cell, x_data, para_env, pmax_block, &
do_p_screening, map_atom_to_kind_atom, eval_type, &
log10_eps_schwarz, log_2, coeffs_kind_max0, use_virial, atomic_pair_list)
END IF
!$OMP BARRIER
!$OMP MASTER
CALL timeset(routineN//"_bin", handle_inner)
!$OMP END MASTER
!$OMP BARRIER
ALLOCATE (binned_dist(nbins))
binned_dist(:)%istart = -1_int_8
binned_dist(:)%number_of_atom_quartets = 0_int_8
binned_dist(:)%cost = 0_int_8
binned_dist(:)%time_first_scf = 0.0_dp
binned_dist(:)%time_other_scf = 0.0_dp
binned_dist(:)%time_forces = 0.0_dp
current_cost = 0
mepos = 1
distribution_counter_start = 1
distribution_counter_end = 0
ibin = 1
global_quartet_counter = 0
local_quartet_counter = 0
last_bin_needs_to_be_filled = .FALSE.
DO atom_block = my_process_id, INT(nblocks, KIND=int_8)**4 - 1, n_processes
latom_block = INT(MODULO(atom_block, INT(nblocks, KIND=int_8))) + 1
tmp_block = atom_block/nblocks
katom_block = INT(MODULO(tmp_block, INT(nblocks, KIND=int_8))) + 1
IF (latom_block < katom_block) CYCLE
tmp_block = tmp_block/nblocks
jatom_block = INT(MODULO(tmp_block, INT(nblocks, KIND=int_8))) + 1
tmp_block = tmp_block/nblocks
iatom_block = INT(MODULO(tmp_block, INT(nblocks, KIND=int_8))) + 1
IF (jatom_block < iatom_block) CYCLE
distribution_counter_end = distribution_counter_end + 1
global_quartet_counter = global_quartet_counter + 1
last_bin_needs_to_be_filled = .TRUE.
IF (binned_dist(ibin)%istart == -1_int_8) binned_dist(ibin)%istart = atom_block
iatom_start = x_data%blocks(iatom_block)%istart
iatom_end = x_data%blocks(iatom_block)%iend
jatom_start = x_data%blocks(jatom_block)%istart
jatom_end = x_data%blocks(jatom_block)%iend
katom_start = x_data%blocks(katom_block)%istart
katom_end = x_data%blocks(katom_block)%iend
latom_start = x_data%blocks(latom_block)%istart
latom_end = x_data%blocks(latom_block)%iend
SELECT CASE (eval_type)
CASE (hfx_do_eval_energy)
pmax_blocks = MAX(pmax_block(katom_block, iatom_block), &
pmax_block(latom_block, jatom_block), &
pmax_block(latom_block, iatom_block), &
pmax_block(katom_block, jatom_block))
CASE (hfx_do_eval_forces)
pmax_blocks = MAX(pmax_block(katom_block, iatom_block) + &
pmax_block(latom_block, jatom_block), &
pmax_block(latom_block, iatom_block) + &
pmax_block(katom_block, jatom_block))
END SELECT
IF (2.0_dp*coeffs_kind_max0 + pmax_blocks < log10_eps_schwarz) CYCLE
current_cost = current_cost &
+ estimate_block_cost(natom, nkind, list_ij, list_kl, set_list_ij, set_list_kl, &
iatom_start, iatom_end, jatom_start, jatom_end, &
katom_start, katom_end, latom_start, latom_end, &
particle_set, &
coeffs_set, coeffs_kind, &
is_assoc_atomic_block_global, do_periodic, &
kind_of, basis_parameter, pmax_set, pmax_atom, pmax_blocks, &
cell, &
do_p_screening, map_atom_to_kind_atom, eval_type, &
log10_eps_schwarz, log_2, coeffs_kind_max0, use_virial, atomic_pair_list)
IF (current_cost >= cost_per_bin) THEN
IF (ibin == nbins) THEN
binned_dist(ibin)%number_of_atom_quartets = binned_dist(ibin)%number_of_atom_quartets + &
distribution_counter_end - distribution_counter_start + 1
ELSE
binned_dist(ibin)%number_of_atom_quartets = distribution_counter_end - distribution_counter_start + 1
END IF
binned_dist(ibin)%cost = binned_dist(ibin)%cost + current_cost
ibin = MIN(ibin + 1, nbins)
distribution_counter_start = distribution_counter_end + 1
current_cost = 0
last_bin_needs_to_be_filled = .FALSE.
END IF
END DO
!$OMP BARRIER
!$OMP MASTER
CALL timestop(handle_inner)
CALL timeset(routineN//"_dist", handle_inner)
!$OMP END MASTER
!$OMP BARRIER
!! Fill the last bin if necessary
IF (last_bin_needs_to_be_filled) THEN
binned_dist(ibin)%cost = binned_dist(ibin)%cost + current_cost
IF (ibin == nbins) THEN
binned_dist(ibin)%number_of_atom_quartets = binned_dist(ibin)%number_of_atom_quartets + &
distribution_counter_end - distribution_counter_start + 1
ELSE
binned_dist(ibin)%number_of_atom_quartets = distribution_counter_end - distribution_counter_start + 1
END IF
END IF
!! Sanity-Check
DO ibin = 1, nbins
local_quartet_counter = local_quartet_counter + binned_dist(ibin)%number_of_atom_quartets
END DO
!$OMP BARRIER
!$OMP MASTER
shm_local_quartet_counter = 0
shm_global_quartet_counter = 0
!$OMP END MASTER
!$OMP BARRIER
!$OMP ATOMIC
shm_local_quartet_counter = shm_local_quartet_counter + local_quartet_counter
!$OMP ATOMIC
shm_global_quartet_counter = shm_global_quartet_counter + global_quartet_counter
!$OMP BARRIER
!$OMP MASTER
CALL para_env%sum(shm_local_quartet_counter)
CALL para_env%sum(shm_global_quartet_counter)
IF (para_env%is_source()) THEN
IF (shm_local_quartet_counter /= shm_global_quartet_counter) THEN
WRITE (error_msg, '(A,I0,A,I0,A)') "HFX Sanity check for parallel distribution failed. "// &
"Number of local quartets (", shm_local_quartet_counter, &
") and number of global quartets (", shm_global_quartet_counter, &
") are different. Please send in a bug report."
CPABORT(error_msg)
END IF
END IF
!$OMP END MASTER
!$OMP BARRIER
!$OMP MASTER
ALLOCATE (cost_matrix(ncpu*nbins*n_threads))
cost_matrix = 0
!$OMP END MASTER
!$OMP BARRIER
icpu = para_env%mepos + 1
DO i = 1, nbins
cost_matrix((icpu - 1)*nbins*n_threads + i_thread*nbins + i) = binned_dist(i)%cost
END DO
mepos = para_env%mepos
!$OMP BARRIER
!$OMP MASTER
! sync before/after ring of isendrecv
CALL para_env%sync()
ALLOCATE (sendbuffer(nbins*n_threads))
ALLOCATE (recbuffer(nbins*n_threads))
sendbuffer = cost_matrix(mepos*nbins*n_threads + 1:mepos*nbins*n_threads + nbins*n_threads)
dest = MODULO(mepos + 1, ncpu)
source = MODULO(mepos - 1, ncpu)
DO icpu = 0, ncpu - 1
IF (icpu .NE. ncpu - 1) THEN
CALL para_env%isendrecv(sendbuffer, dest, recbuffer, source, &
req(1), req(2), 13)
END IF
data_from = MODULO(mepos - icpu, ncpu)
cost_matrix(data_from*nbins*n_threads + 1:data_from*nbins*n_threads + nbins*n_threads) = sendbuffer
IF (icpu .NE. ncpu - 1) THEN
CALL mp_waitall(req)
END IF
swapbuffer => sendbuffer
sendbuffer => recbuffer
recbuffer => swapbuffer
END DO
DEALLOCATE (recbuffer, sendbuffer)
!$OMP END MASTER
!$OMP BARRIER
!$OMP BARRIER
!$OMP MASTER
CALL timestop(handle_inner)
CALL timeset(routineN//"_opt", handle_inner)
!$OMP END MASTER
!$OMP BARRIER
!! Find an optimal distribution i.e. assign each element of the cost matrix to a certain process
!$OMP BARRIER
ALLOCATE (local_cost_matrix(SIZE(cost_matrix, 1)))
local_cost_matrix = cost_matrix
!$OMP MASTER
ALLOCATE (shm_distribution_vector(ncpu*nbins*n_threads))
CALL optimize_distribution(ncpu*nbins*n_threads, ncpu*n_threads, local_cost_matrix, &
shm_distribution_vector, x_data%load_balance_parameter%do_randomize)
CALL timestop(handle_inner)
CALL timeset(routineN//"_redist", handle_inner)
!! Collect local data to global array
ALLOCATE (full_dist(ncpu*n_threads, nbins))
full_dist(:, :)%istart = 0_int_8
full_dist(:, :)%number_of_atom_quartets = 0_int_8
full_dist(:, :)%cost = 0_int_8
full_dist(:, :)%time_first_scf = 0.0_dp
full_dist(:, :)%time_other_scf = 0.0_dp
full_dist(:, :)%time_forces = 0.0_dp
!$OMP END MASTER
!$OMP BARRIER
mepos = para_env%mepos + 1
full_dist((mepos - 1)*n_threads + i_thread + 1, :) = binned_dist(:)
!$OMP BARRIER
!$OMP MASTER
ALLOCATE (sendbuffer(3*nbins*n_threads))
ALLOCATE (recbuffer(3*nbins*n_threads))
mepos = para_env%mepos
DO j = 1, n_threads
DO i = 1, nbins
sendbuffer((j - 1)*3*nbins + (i - 1)*3 + 1) = full_dist(mepos*n_threads + j, i)%istart
sendbuffer((j - 1)*3*nbins + (i - 1)*3 + 2) = full_dist(mepos*n_threads + j, i)%number_of_atom_quartets
sendbuffer((j - 1)*3*nbins + (i - 1)*3 + 3) = full_dist(mepos*n_threads + j, i)%cost
END DO
END DO
! sync before/after ring of isendrecv
CALL para_env%sync()
dest = MODULO(mepos + 1, ncpu)
source = MODULO(mepos - 1, ncpu)
DO icpu = 0, ncpu - 1
IF (icpu .NE. ncpu - 1) THEN
CALL para_env%isendrecv(sendbuffer, dest, recbuffer, source, &
req(1), req(2), 13)
END IF
data_from = MODULO(mepos - icpu, ncpu)
DO j = 1, n_threads
DO i = 1, nbins
full_dist(data_from*n_threads + j, i)%istart = sendbuffer((j - 1)*3*nbins + (i - 1)*3 + 1)
full_dist(data_from*n_threads + j, i)%number_of_atom_quartets = sendbuffer((j - 1)*3*nbins + (i - 1)*3 + 2)
full_dist(data_from*n_threads + j, i)%cost = sendbuffer((j - 1)*3*nbins + (i - 1)*3 + 3)
END DO
END DO
IF (icpu .NE. ncpu - 1) THEN
CALL mp_waitall(req)
END IF
swapbuffer => sendbuffer
sendbuffer => recbuffer
recbuffer => swapbuffer
END DO
DEALLOCATE (recbuffer, sendbuffer)
! sync before/after ring of isendrecv
CALL para_env%sync()
!$OMP END MASTER
!$OMP BARRIER
!! reorder the distribution according to the distribution vector
ALLOCATE (tmp_pos(ncpu*n_threads))
tmp_pos = 1
ALLOCATE (tmp_dist(nbins*ncpu*n_threads))
tmp_dist(:)%istart = 0_int_8
tmp_dist(:)%number_of_atom_quartets = 0_int_8
tmp_dist(:)%cost = 0_int_8
tmp_dist(:)%time_first_scf = 0.0_dp
tmp_dist(:)%time_other_scf = 0.0_dp
tmp_dist(:)%time_forces = 0.0_dp
DO icpu = 1, n_processes
DO i = 1, nbins
mepos = my_process_id + 1
IF (shm_distribution_vector((icpu - 1)*nbins + i) == mepos) THEN
tmp_dist(tmp_pos(mepos)) = full_dist(icpu, i)
tmp_pos(mepos) = tmp_pos(mepos) + 1
END IF
END DO
END DO
!! Assign the load to each process
NULLIFY (ptr_to_tmp_dist)
mepos = my_process_id + 1
ptr_to_tmp_dist => tmp_dist(1:tmp_pos(mepos) - 1)
SELECT CASE (eval_type)
CASE (hfx_do_eval_energy)
CALL hfx_set_distr_energy(ptr_to_tmp_dist, x_data)
CASE (hfx_do_eval_forces)
CALL hfx_set_distr_forces(ptr_to_tmp_dist, x_data)
END SELECT
!$OMP BARRIER
!$OMP MASTER
DEALLOCATE (full_dist, cost_matrix, shm_distribution_vector)
!$OMP END MASTER
!$OMP BARRIER
DEALLOCATE (tmp_dist, tmp_pos)
DEALLOCATE (binned_dist, local_cost_matrix)
DEALLOCATE (set_list_ij, set_list_kl)
!$OMP BARRIER
!$OMP MASTER
CALL timestop(handle_inner)
!$OMP END MASTER
!$OMP BARRIER
END IF
!$OMP BARRIER
!$OMP MASTER
CALL timestop(handle)
!$OMP END MASTER
!$OMP BARRIER
END SUBROUTINE hfx_load_balance
! **************************************************************************************************
!> \brief Reference implementation of new recursive load balancing routine
!> Computes a local list of atom_blocks (p_atom_blocks,q_atom_blocks) for
!> each process in a P-Q grid such that every process has more or less the
!> same amount of work. Has no output at the moment (not used) but writes
!> its computed load balance values into a file. Possible output is ready
!> to use in the two arrays p_atom_blocks & q_atom_blocks
!> \param n_processes ...
!> \param my_process_id ...
!> \param nblocks ...
!> \param natom ...
!> \param nkind ...
!> \param list_ij ...
!> \param list_kl ...
!> \param set_list_ij ...
!> \param set_list_kl ...
!> \param particle_set ...
!> \param coeffs_set ...
!> \param coeffs_kind ...
!> \param is_assoc_atomic_block_global ...
!> \param do_periodic ...
!> \param kind_of ...
!> \param basis_parameter ...
!> \param pmax_set ...
!> \param pmax_atom ...
!> \param pmax_blocks ...
!> \param cell ...
!> \param x_data ...
!> \param para_env ...
!> \param pmax_block ...
!> \param do_p_screening ...
!> \param map_atom_to_kind_atom ...
!> \param eval_type ...
!> \param log10_eps_schwarz ...
!> \param log_2 ...
!> \param coeffs_kind_max0 ...
!> \param use_virial ...
!> \param atomic_pair_list ...
!> \par History
!> 03.2011 created [Michael Steinlechner]
!> \author Michael Steinlechner
! **************************************************************************************************
SUBROUTINE hfx_recursive_load_balance(n_processes, my_process_id, nblocks, &
natom, nkind, list_ij, list_kl, set_list_ij, set_list_kl, &
particle_set, &
coeffs_set, coeffs_kind, &
is_assoc_atomic_block_global, do_periodic, &
kind_of, basis_parameter, pmax_set, pmax_atom, pmax_blocks, &
cell, x_data, para_env, pmax_block, &
do_p_screening, map_atom_to_kind_atom, eval_type, &
log10_eps_schwarz, log_2, coeffs_kind_max0, use_virial, atomic_pair_list)
! input variables:
INTEGER, INTENT(IN) :: n_processes, my_process_id, nblocks, &
natom, nkind
TYPE(pair_list_type), INTENT(IN) :: list_ij, list_kl
TYPE(pair_set_list_type), ALLOCATABLE, &
DIMENSION(:), INTENT(IN) :: set_list_ij, set_list_kl
TYPE(particle_type), DIMENSION(:), INTENT(IN), &
POINTER :: particle_set
TYPE(hfx_screen_coeff_type), &
DIMENSION(:, :, :, :), INTENT(IN), POINTER :: coeffs_set
TYPE(hfx_screen_coeff_type), DIMENSION(:, :), &
INTENT(IN), POINTER :: coeffs_kind
INTEGER, DIMENSION(:, :), INTENT(IN) :: is_assoc_atomic_block_global
LOGICAL, INTENT(IN) :: do_periodic
INTEGER, INTENT(IN) :: kind_of(*)
TYPE(hfx_basis_type), DIMENSION(:), INTENT(IN), &
POINTER :: basis_parameter
TYPE(hfx_p_kind), DIMENSION(:), INTENT(IN), &
POINTER :: pmax_set
REAL(dp), DIMENSION(:, :), INTENT(IN), POINTER :: pmax_atom
REAL(dp) :: pmax_blocks
TYPE(cell_type), INTENT(IN), POINTER :: cell
TYPE(hfx_type), INTENT(IN), POINTER :: x_data
TYPE(mp_para_env_type), INTENT(IN) :: para_env
REAL(dp), DIMENSION(:, :), INTENT(IN), POINTER :: pmax_block
LOGICAL, INTENT(IN) :: do_p_screening
INTEGER, DIMENSION(:), INTENT(IN), POINTER :: map_atom_to_kind_atom
INTEGER, INTENT(IN) :: eval_type
REAL(dp), INTENT(IN) :: log10_eps_schwarz, log_2, &
coeffs_kind_max0
LOGICAL, INTENT(IN) :: use_virial
LOGICAL, DIMENSION(:, :), INTENT(IN), POINTER :: atomic_pair_list
CHARACTER(LEN=*), PARAMETER :: routineN = 'hfx_recursive_load_balance'
INTEGER :: handle, i, iatom_block, iatom_end, iatom_start, j, jatom_block, jatom_end, &
jatom_start, katom_block, katom_end, katom_start, latom_block, latom_end, latom_start, &
nP, nQ, numBins, p, q, sizeP, sizeQ, unit_nr
INTEGER(int_8) :: local_cost, pidx, qidx, sumP, sumQ
INTEGER(int_8), ALLOCATABLE, DIMENSION(:) :: local_cost_vector
INTEGER, ALLOCATABLE, DIMENSION(:) :: blocksize, p_atom_blocks, permute, &
q_atom_blocks
REAL(dp) :: maximum, mean
! internal variables:
!$OMP BARRIER
!$OMP MASTER
CALL timeset(routineN, handle)
!$OMP END MASTER
!$OMP BARRIER
! calculate best p/q distribution grid for the n_processes
CALL hfx_calculate_PQ(p, q, numBins, n_processes)
ALLOCATE (blocksize(numBins))
ALLOCATE (permute(nblocks**2))
DO i = 1, nblocks**2
permute(i) = i
END DO
! call the main recursive permutation routine.
! Output:
! blocksize :: vector (size numBins) with the sizes for each column/row block
! permute :: permutation vector
CALL hfx_recursive_permute(blocksize, 1, nblocks**2, numBins, &
permute, 1, &
my_process_id, n_processes, nblocks, &
natom, nkind, list_ij, list_kl, set_list_ij, set_list_kl, &
particle_set, &
coeffs_set, coeffs_kind, &
is_assoc_atomic_block_global, do_periodic, &
kind_of, basis_parameter, pmax_set, pmax_atom, pmax_blocks, &
cell, x_data, para_env, pmax_block, &
do_p_screening, map_atom_to_kind_atom, eval_type, &
log10_eps_schwarz, log_2, coeffs_kind_max0, use_virial, atomic_pair_list)
! number of blocks per processor in p-direction (vertical)
nP = numBins/p
! number of blocks per processor in q-direction (horizontal)
nQ = numBins/q
! calc own position in P-Q-processor grid (PQ-grid is column-major)
pidx = MODULO(INT(my_process_id), INT(p)) + 1
qidx = my_process_id/p + 1
sizeP = SUM(blocksize((nP*(pidx - 1) + 1):(nP*pidx)))
sizeQ = SUM(blocksize((nQ*(qidx - 1) + 1):(nQ*qidx)))
sumP = SUM(blocksize(1:(nP*(pidx - 1))))
sumQ = SUM(blocksize(1:(nQ*(qidx - 1))))
ALLOCATE (p_atom_blocks(sizeP))
ALLOCATE (q_atom_blocks(sizeQ))
p_atom_blocks(:) = permute((sumP + 1):(sumP + sizeP))