-
Notifications
You must be signed in to change notification settings - Fork 1
/
mp2_ri_gpw.F
2659 lines (2265 loc) · 130 KB
/
mp2_ri_gpw.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 to calculate RI-GPW-MP2 energy using pw
!> \par History
!> 06.2012 created [Mauro Del Ben]
!> 03.2019 Refactored from mp2_ri_gpw [Frederick Stein]
! **************************************************************************************************
MODULE mp2_ri_gpw
USE cp_log_handling, ONLY: cp_to_string
USE dgemm_counter_types, ONLY: dgemm_counter_init,&
dgemm_counter_start,&
dgemm_counter_stop,&
dgemm_counter_type,&
dgemm_counter_write
USE group_dist_types, ONLY: get_group_dist,&
group_dist_d1_type,&
maxsize,&
release_group_dist
USE kinds, ONLY: dp,&
int_8
USE libint_2c_3c, ONLY: compare_potential_types
USE local_gemm_api, ONLY: LOCAL_GEMM_PU_GPU
USE machine, ONLY: m_flush,&
m_memory,&
m_walltime
USE message_passing, ONLY: mp_comm_type,&
mp_para_env_type
USE mp2_ri_grad_util, ONLY: complete_gamma
USE mp2_types, ONLY: mp2_type,&
three_dim_real_array
!$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mp2_ri_gpw'
PUBLIC :: mp2_ri_gpw_compute_en
CONTAINS
! **************************************************************************************************
!> \brief ...
!> \param Emp2_Cou ...
!> \param Emp2_EX ...
!> \param Emp2_S ...
!> \param Emp2_T ...
!> \param BIb_C ...
!> \param mp2_env ...
!> \param para_env ...
!> \param para_env_sub ...
!> \param color_sub ...
!> \param gd_array ...
!> \param gd_B_virtual ...
!> \param Eigenval ...
!> \param nmo ...
!> \param homo ...
!> \param dimen_RI ...
!> \param unit_nr ...
!> \param calc_forces ...
!> \param calc_ex ...
! **************************************************************************************************
SUBROUTINE mp2_ri_gpw_compute_en(Emp2_Cou, Emp2_EX, Emp2_S, Emp2_T, BIb_C, mp2_env, para_env, para_env_sub, color_sub, &
gd_array, gd_B_virtual, &
Eigenval, nmo, homo, dimen_RI, unit_nr, calc_forces, calc_ex)
REAL(KIND=dp), INTENT(INOUT) :: Emp2_Cou, Emp2_EX, Emp2_S, Emp2_T
TYPE(three_dim_real_array), DIMENSION(:), &
INTENT(INOUT) :: BIb_C
TYPE(mp2_type) :: mp2_env
TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env, para_env_sub
INTEGER, INTENT(IN) :: color_sub
TYPE(group_dist_d1_type), INTENT(INOUT) :: gd_array
INTEGER, DIMENSION(:), INTENT(IN) :: homo
INTEGER, INTENT(IN) :: nmo
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: Eigenval
TYPE(group_dist_d1_type), DIMENSION(SIZE(homo)), &
INTENT(INOUT) :: gd_B_virtual
INTEGER, INTENT(IN) :: dimen_RI, unit_nr
LOGICAL, INTENT(IN) :: calc_forces, calc_ex
CHARACTER(LEN=*), PARAMETER :: routineN = 'mp2_ri_gpw_compute_en'
INTEGER :: a, a_global, b, b_global, block_size, decil, end_point, handle, handle2, handle3, &
iiB, ij_counter, ij_counter_send, ij_index, integ_group_size, ispin, jjB, jspin, &
max_ij_pairs, my_block_size, my_group_L_end, my_group_L_size, my_group_L_size_orig, &
my_group_L_start, my_i, my_ij_pairs, my_j, my_new_group_L_size, ngroup, nspins, &
num_integ_group, proc_receive, proc_send, proc_shift, rec_B_size, rec_B_virtual_end, &
rec_B_virtual_start, rec_L_size, send_B_size, send_B_virtual_end, send_B_virtual_start, &
send_i, send_ij_index, send_j, start_point, tag, total_ij_pairs
INTEGER, ALLOCATABLE, DIMENSION(:) :: integ_group_pos2color_sub, my_B_size, &
my_B_virtual_end, my_B_virtual_start, num_ij_pairs, sizes_array_orig, virtual
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: ij_map
INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: ranges_info_array
LOGICAL :: my_alpha_beta_case, my_beta_beta_case, &
my_open_shell_SS
REAL(KIND=dp) :: amp_fac, my_Emp2_Cou, my_Emp2_EX, &
sym_fac, t_new, t_start
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), TARGET :: buffer_1D
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
TARGET :: local_ab, local_ba, t_ab
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
TARGET :: local_i_aL, local_j_aL, Y_i_aP, Y_j_aP
REAL(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
POINTER :: external_ab, external_i_aL
REAL(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
POINTER :: BI_C_rec
TYPE(dgemm_counter_type) :: dgemm_counter
TYPE(mp_comm_type) :: comm_exchange, comm_rep
TYPE(three_dim_real_array), ALLOCATABLE, &
DIMENSION(:) :: B_ia_Q
CALL timeset(routineN, handle)
nspins = SIZE(homo)
ALLOCATE (virtual(nspins))
virtual(:) = nmo - homo(:)
ALLOCATE (my_B_size(nspins), my_B_virtual_start(nspins), my_B_virtual_end(nspins))
DO ispin = 1, nspins
CALL get_group_dist(gd_B_virtual(ispin), para_env_sub%mepos, &
my_B_virtual_start(ispin), my_B_virtual_end(ispin), my_B_size(ispin))
END DO
CALL get_group_dist(gd_array, color_sub, my_group_L_start, my_group_L_end, my_group_L_size)
CALL dgemm_counter_init(dgemm_counter, unit_nr, mp2_env%ri_mp2%print_dgemm_info)
! local_gemm_ctx has a very footprint the first time this routine is
! called.
CALL mp2_env%local_gemm_ctx%create(LOCAL_GEMM_PU_GPU)
CALL mp2_env%local_gemm_ctx%set_op_threshold_gpu(128*128*128*2)
CALL mp2_ri_get_integ_group_size( &
mp2_env, para_env, para_env_sub, gd_array, gd_B_virtual, &
homo, dimen_RI, unit_nr, &
integ_group_size, ngroup, &
num_integ_group, virtual, calc_forces)
! now create a group that contains all the proc that have the same virtual starting point
! in the integ group
CALL mp2_ri_create_group( &
para_env, para_env_sub, color_sub, &
gd_array%sizes, calc_forces, &
integ_group_size, my_group_L_end, &
my_group_L_size, my_group_L_size_orig, my_group_L_start, my_new_group_L_size, &
integ_group_pos2color_sub, sizes_array_orig, &
ranges_info_array, comm_exchange, comm_rep, num_integ_group)
! We cannot fix the tag because of the recv routine
tag = 42
DO jspin = 1, nspins
CALL replicate_iaK_2intgroup(BIb_C(jspin)%array, comm_exchange, comm_rep, &
homo(jspin), gd_array%sizes, my_B_size(jspin), &
my_group_L_size, ranges_info_array)
DO ispin = 1, jspin
IF (unit_nr > 0) THEN
IF (nspins == 1) THEN
WRITE (unit_nr, *) "Start loop run"
ELSE IF (ispin == 1 .AND. jspin == 1) THEN
WRITE (unit_nr, *) "Start loop run alpha-alpha"
ELSE IF (ispin == 1 .AND. jspin == 2) THEN
WRITE (unit_nr, *) "Start loop run alpha-beta"
ELSE IF (ispin == 2 .AND. jspin == 2) THEN
WRITE (unit_nr, *) "Start loop run beta-beta"
END IF
CALL m_flush(unit_nr)
END IF
my_open_shell_SS = (nspins == 2) .AND. (ispin == jspin)
! t_ab = amp_fac*(:,a|:,b)-(:,b|:,a)
! If we calculate the gradient we need to distinguish
! between alpha-alpha and beta-beta cases for UMP2
my_beta_beta_case = .FALSE.
my_alpha_beta_case = .FALSE.
IF (ispin /= jspin) THEN
my_alpha_beta_case = .TRUE.
ELSE IF (my_open_shell_SS) THEN
IF (ispin == 2) my_beta_beta_case = .TRUE.
END IF
amp_fac = mp2_env%scale_S + mp2_env%scale_T
IF (my_alpha_beta_case .OR. my_open_shell_SS) amp_fac = mp2_env%scale_T
CALL mp2_ri_allocate_no_blk(local_ab, t_ab, mp2_env, homo, virtual, my_B_size, &
my_group_L_size, calc_forces, ispin, jspin, local_ba)
CALL mp2_ri_get_block_size( &
mp2_env, para_env, para_env_sub, gd_array, gd_B_virtual(ispin:jspin), &
homo(ispin:jspin), virtual(ispin:jspin), dimen_RI, unit_nr, block_size, &
ngroup, num_integ_group, my_open_shell_ss, calc_forces, buffer_1D)
! *****************************************************************
! ********** REPLICATION-BLOCKED COMMUNICATION SCHEME ***********
! *****************************************************************
! introduce block size, the number of occupied orbitals has to be a
! multiple of the block size
! Calculate the maximum number of ij pairs that have to be computed
! among groups
CALL mp2_ri_communication(my_alpha_beta_case, total_ij_pairs, homo(ispin), homo(jspin), &
block_size, ngroup, ij_map, color_sub, my_ij_pairs, my_open_shell_SS, unit_nr)
ALLOCATE (num_ij_pairs(0:comm_exchange%num_pe - 1))
CALL comm_exchange%allgather(my_ij_pairs, num_ij_pairs)
max_ij_pairs = MAXVAL(num_ij_pairs)
! start real stuff
CALL mp2_ri_allocate_blk(dimen_RI, my_B_size, block_size, local_i_aL, &
local_j_aL, calc_forces, Y_i_aP, Y_j_aP, ispin, jspin)
CALL timeset(routineN//"_RI_loop", handle2)
my_Emp2_Cou = 0.0_dp
my_Emp2_EX = 0.0_dp
t_start = m_walltime()
DO ij_index = 1, max_ij_pairs
! Prediction is unreliable if we are in the first step of the loop
IF (unit_nr > 0 .AND. ij_index > 1) THEN
decil = ij_index*10/max_ij_pairs
IF (decil /= (ij_index - 1)*10/max_ij_pairs) THEN
t_new = m_walltime()
t_new = (t_new - t_start)/60.0_dp*(max_ij_pairs - ij_index + 1)/(ij_index - 1)
WRITE (unit_nr, FMT="(T3,A)") "Percentage of finished loop: "// &
cp_to_string(decil*10)//". Minutes left: "//cp_to_string(t_new)
CALL m_flush(unit_nr)
END IF
END IF
IF (calc_forces) THEN
Y_i_aP = 0.0_dp
Y_j_aP = 0.0_dp
END IF
IF (ij_index <= my_ij_pairs) THEN
! We have work to do
ij_counter = (ij_index - MIN(1, color_sub))*ngroup + color_sub
my_i = ij_map(1, ij_counter)
my_j = ij_map(2, ij_counter)
my_block_size = ij_map(3, ij_counter)
local_i_aL = 0.0_dp
CALL fill_local_i_aL(local_i_aL(:, :, 1:my_block_size), ranges_info_array(:, :, comm_exchange%mepos), &
BIb_C(ispin)%array(:, :, my_i:my_i + my_block_size - 1))
local_j_aL = 0.0_dp
CALL fill_local_i_aL(local_j_aL(:, :, 1:my_block_size), ranges_info_array(:, :, comm_exchange%mepos), &
BIb_C(jspin)%array(:, :, my_j:my_j + my_block_size - 1))
! collect data from other proc
CALL timeset(routineN//"_comm", handle3)
DO proc_shift = 1, comm_exchange%num_pe - 1
proc_send = MODULO(comm_exchange%mepos + proc_shift, comm_exchange%num_pe)
proc_receive = MODULO(comm_exchange%mepos - proc_shift, comm_exchange%num_pe)
send_ij_index = num_ij_pairs(proc_send)
CALL get_group_dist(gd_array, proc_receive, sizes=rec_L_size)
IF (ij_index <= send_ij_index) THEN
ij_counter_send = (ij_index - MIN(1, integ_group_pos2color_sub(proc_send)))*ngroup + &
integ_group_pos2color_sub(proc_send)
send_i = ij_map(1, ij_counter_send)
send_j = ij_map(2, ij_counter_send)
! occupied i
BI_C_rec(1:rec_L_size, 1:my_B_size(ispin), 1:my_block_size) => &
buffer_1D(1:rec_L_size*my_B_size(ispin)*my_block_size)
BI_C_rec = 0.0_dp
CALL comm_exchange%sendrecv(BIb_C(ispin)%array(:, :, send_i:send_i + my_block_size - 1), &
proc_send, BI_C_rec, proc_receive, tag)
CALL fill_local_i_aL(local_i_aL(:, :, 1:my_block_size), ranges_info_array(:, :, proc_receive), &
BI_C_rec(:, 1:my_B_size(ispin), :))
! occupied j
BI_C_rec(1:rec_L_size, 1:my_B_size(jspin), 1:my_block_size) => &
buffer_1D(1:INT(rec_L_size, int_8)*my_B_size(jspin)*my_block_size)
BI_C_rec = 0.0_dp
CALL comm_exchange%sendrecv(BIb_C(jspin)%array(:, :, send_j:send_j + my_block_size - 1), &
proc_send, BI_C_rec, proc_receive, tag)
CALL fill_local_i_aL(local_j_aL(:, :, 1:my_block_size), ranges_info_array(:, :, proc_receive), &
BI_C_rec(:, 1:my_B_size(jspin), :))
ELSE
! we send nothing while we know that we have to receive something
! occupied i
BI_C_rec(1:rec_L_size, 1:my_B_size(ispin), 1:my_block_size) => &
buffer_1D(1:INT(rec_L_size, int_8)*my_B_size(ispin)*my_block_size)
BI_C_rec = 0.0_dp
CALL comm_exchange%recv(BI_C_rec, proc_receive, tag)
CALL fill_local_i_aL(local_i_aL(:, :, 1:my_block_size), ranges_info_array(:, :, proc_receive), &
BI_C_rec(:, 1:my_B_size(ispin), 1:my_block_size))
! occupied j
BI_C_rec(1:rec_L_size, 1:my_B_size(jspin), 1:my_block_size) => &
buffer_1D(1:INT(rec_L_size, int_8)*my_B_size(jspin)*my_block_size)
BI_C_rec = 0.0_dp
CALL comm_exchange%recv(BI_C_rec, proc_receive, tag)
CALL fill_local_i_aL(local_j_aL(:, :, 1:my_block_size), ranges_info_array(:, :, proc_receive), &
BI_C_rec(:, 1:my_B_size(jspin), 1:my_block_size))
END IF
END DO
CALL timestop(handle3)
! loop over the block elements
DO iiB = 1, my_block_size
DO jjB = 1, my_block_size
CALL timeset(routineN//"_expansion", handle3)
ASSOCIATE (my_local_i_aL => local_i_aL(:, :, iiB), my_local_j_aL => local_j_aL(:, :, jjB))
! calculate the integrals (ia|jb) strating from my local data ...
local_ab = 0.0_dp
IF ((my_alpha_beta_case) .AND. (calc_forces)) THEN
local_ba = 0.0_dp
END IF
CALL dgemm_counter_start(dgemm_counter)
CALL mp2_env%local_gemm_ctx%gemm('T', 'N', my_B_size(ispin), my_B_size(jspin), dimen_RI, 1.0_dp, &
my_local_i_aL, dimen_RI, my_local_j_aL, dimen_RI, &
0.0_dp, local_ab(my_B_virtual_start(ispin):my_B_virtual_end(ispin), :), &
my_B_size(ispin))
! Additional integrals only for alpha_beta case and forces
IF (my_alpha_beta_case .AND. calc_forces) THEN
local_ba(my_B_virtual_start(jspin):my_B_virtual_end(jspin), :) = &
TRANSPOSE(local_ab(my_B_virtual_start(ispin):my_B_virtual_end(ispin), :))
END IF
! ... and from the other of my subgroup
DO proc_shift = 1, para_env_sub%num_pe - 1
proc_send = MODULO(para_env_sub%mepos + proc_shift, para_env_sub%num_pe)
proc_receive = MODULO(para_env_sub%mepos - proc_shift, para_env_sub%num_pe)
CALL get_group_dist(gd_B_virtual(ispin), proc_receive, rec_B_virtual_start, &
rec_B_virtual_end, rec_B_size)
external_i_aL(1:dimen_RI, 1:rec_B_size) => buffer_1D(1:INT(dimen_RI, int_8)*rec_B_size)
external_i_aL = 0.0_dp
CALL para_env_sub%sendrecv(my_local_i_aL, proc_send, &
external_i_aL, proc_receive, tag)
CALL mp2_env%local_gemm_ctx%gemm( &
'T', 'N', rec_B_size, my_B_size(jspin), dimen_RI, 1.0_dp, &
external_i_aL, dimen_RI, my_local_j_aL, dimen_RI, &
0.0_dp, local_ab(rec_B_virtual_start:rec_B_virtual_end, 1:my_B_size(jspin)), rec_B_size)
! Additional integrals only for alpha_beta case and forces
IF (my_alpha_beta_case .AND. calc_forces) THEN
CALL get_group_dist(gd_B_virtual(jspin), proc_receive, rec_B_virtual_start, &
rec_B_virtual_end, rec_B_size)
external_i_aL(1:dimen_RI, 1:rec_B_size) => buffer_1D(1:INT(dimen_RI, int_8)*rec_B_size)
external_i_aL = 0.0_dp
CALL para_env_sub%sendrecv(my_local_j_aL, proc_send, &
external_i_aL, proc_receive, tag)
CALL mp2_env%local_gemm_ctx%gemm('T', 'N', rec_B_size, my_B_size(ispin), dimen_RI, 1.0_dp, &
external_i_aL, dimen_RI, my_local_i_aL, dimen_RI, &
0.0_dp, local_ba(rec_B_virtual_start:rec_B_virtual_end, 1:my_B_size(ispin)), rec_B_size)
END IF
END DO
IF (my_alpha_beta_case .AND. calc_forces) THEN
! Is just an approximation, but the call does not allow it, it ought to be (virtual_i*B_size_j+virtual_j*B_size_i)*dimen_RI
CALL dgemm_counter_stop(dgemm_counter, virtual(ispin), my_B_size(ispin) + my_B_size(jspin), dimen_RI)
ELSE
CALL dgemm_counter_stop(dgemm_counter, virtual(ispin), my_B_size(jspin), dimen_RI)
END IF
CALL timestop(handle3)
!sample peak memory
CALL m_memory()
CALL timeset(routineN//"_ener", handle3)
! calculate coulomb only MP2
sym_fac = 2.0_dp
IF (my_i == my_j) sym_fac = 1.0_dp
IF (my_alpha_beta_case) sym_fac = 0.5_dp
DO b = 1, my_B_size(jspin)
b_global = b + my_B_virtual_start(jspin) - 1
DO a = 1, virtual(ispin)
my_Emp2_Cou = my_Emp2_Cou - sym_fac*2.0_dp*local_ab(a, b)**2/ &
(Eigenval(homo(ispin) + a, ispin) + Eigenval(homo(jspin) + b_global, jspin) - &
Eigenval(my_i + iiB - 1, ispin) - Eigenval(my_j + jjB - 1, jspin))
END DO
END DO
IF (calc_ex) THEN
! contract integrals with orbital energies for exchange MP2 energy
! starting with local ...
IF (calc_forces .AND. (.NOT. my_alpha_beta_case)) t_ab = 0.0_dp
DO b = 1, my_B_size(ispin)
b_global = b + my_B_virtual_start(ispin) - 1
DO a = 1, my_B_size(ispin)
a_global = a + my_B_virtual_start(ispin) - 1
my_Emp2_Ex = my_Emp2_Ex + sym_fac*local_ab(a_global, b)*local_ab(b_global, a)/ &
(Eigenval(homo(ispin) + a_global, ispin) + Eigenval(homo(ispin) + b_global, ispin) - &
Eigenval(my_i + iiB - 1, ispin) - Eigenval(my_j + jjB - 1, ispin))
IF (calc_forces .AND. (.NOT. my_alpha_beta_case)) THEN
t_ab(a_global, b) = -(amp_fac*local_ab(a_global, b) - mp2_env%scale_T*local_ab(b_global, a))/ &
(Eigenval(homo(ispin) + a_global, ispin) + &
Eigenval(homo(ispin) + b_global, ispin) - &
Eigenval(my_i + iiB - 1, ispin) - Eigenval(my_j + jjB - 1, ispin))
END IF
END DO
END DO
! ... and then with external data
DO proc_shift = 1, para_env_sub%num_pe - 1
proc_send = MODULO(para_env_sub%mepos + proc_shift, para_env_sub%num_pe)
proc_receive = MODULO(para_env_sub%mepos - proc_shift, para_env_sub%num_pe)
CALL get_group_dist(gd_B_virtual(ispin), proc_receive, &
rec_B_virtual_start, rec_B_virtual_end, rec_B_size)
CALL get_group_dist(gd_B_virtual(ispin), proc_send, &
send_B_virtual_start, send_B_virtual_end, send_B_size)
external_ab(1:my_B_size(ispin), 1:rec_B_size) => &
buffer_1D(1:INT(rec_B_size, int_8)*my_B_size(ispin))
external_ab = 0.0_dp
CALL para_env_sub%sendrecv(local_ab(send_B_virtual_start:send_B_virtual_end, 1:my_B_size(ispin)), proc_send, &
external_ab(1:my_B_size(ispin), 1:rec_B_size), proc_receive, tag)
DO b = 1, my_B_size(ispin)
b_global = b + my_B_virtual_start(ispin) - 1
DO a = 1, rec_B_size
a_global = a + rec_B_virtual_start - 1
my_Emp2_Ex = my_Emp2_Ex + sym_fac*local_ab(a_global, b)*external_ab(b, a)/ &
(Eigenval(homo(ispin) + a_global, ispin) + Eigenval(homo(ispin) + b_global, ispin) - &
Eigenval(my_i + iiB - 1, ispin) - Eigenval(my_j + jjB - 1, ispin))
IF (calc_forces .AND. (.NOT. my_alpha_beta_case)) &
t_ab(a_global, b) = -(amp_fac*local_ab(a_global, b) - mp2_env%scale_T*external_ab(b, a))/ &
(Eigenval(homo(ispin) + a_global, ispin) + &
Eigenval(homo(ispin) + b_global, ispin) - &
Eigenval(my_i + iiB - 1, ispin) - Eigenval(my_j + jjB - 1, ispin))
END DO
END DO
END DO
END IF
CALL timestop(handle3)
IF (calc_forces) THEN
! update P_ab, Gamma_P_ia
CALL mp2_update_P_gamma(mp2_env, para_env_sub, gd_B_virtual, &
Eigenval, homo, dimen_RI, iiB, jjB, my_B_size, &
my_B_virtual_end, my_B_virtual_start, my_i, my_j, virtual, &
local_ab, t_ab, my_local_i_aL, my_local_j_aL, &
my_open_shell_ss, Y_i_aP(:, :, iiB), Y_j_aP(:, :, jjB), local_ba, &
ispin, jspin, dgemm_counter, buffer_1D)
END IF
END ASSOCIATE
END DO ! jjB
END DO ! iiB
ELSE
! We need it later in case of gradients
my_block_size = 1
CALL timeset(routineN//"_comm", handle3)
! No work to do and we know that we have to receive nothing, but send something
! send data to other proc
DO proc_shift = 1, comm_exchange%num_pe - 1
proc_send = MODULO(comm_exchange%mepos + proc_shift, comm_exchange%num_pe)
proc_receive = MODULO(comm_exchange%mepos - proc_shift, comm_exchange%num_pe)
send_ij_index = num_ij_pairs(proc_send)
IF (ij_index <= send_ij_index) THEN
! something to send
ij_counter_send = (ij_index - MIN(1, integ_group_pos2color_sub(proc_send)))*ngroup + &
integ_group_pos2color_sub(proc_send)
send_i = ij_map(1, ij_counter_send)
send_j = ij_map(2, ij_counter_send)
! occupied i
CALL comm_exchange%send(BIb_C(ispin)%array(:, :, send_i:send_i + my_block_size - 1), &
proc_send, tag)
! occupied j
CALL comm_exchange%send(BIb_C(jspin)%array(:, :, send_j:send_j + my_block_size - 1), &
proc_send, tag)
END IF
END DO
CALL timestop(handle3)
END IF
! redistribute gamma
IF (calc_forces) THEN
CALL mp2_redistribute_gamma(mp2_env%ri_grad%Gamma_P_ia(ispin)%array, ij_index, my_B_size(ispin), &
my_block_size, my_group_L_size, my_i, my_ij_pairs, ngroup, &
num_integ_group, integ_group_pos2color_sub, num_ij_pairs, &
ij_map, ranges_info_array, Y_i_aP(:, :, 1:my_block_size), comm_exchange, &
gd_array%sizes, 1, buffer_1D)
CALL mp2_redistribute_gamma(mp2_env%ri_grad%Gamma_P_ia(jspin)%array, ij_index, my_B_size(jspin), &
my_block_size, my_group_L_size, my_j, my_ij_pairs, ngroup, &
num_integ_group, integ_group_pos2color_sub, num_ij_pairs, &
ij_map, ranges_info_array, Y_j_aP(:, :, 1:my_block_size), comm_exchange, &
gd_array%sizes, 2, buffer_1D)
END IF
END DO
CALL timestop(handle2)
DEALLOCATE (local_i_aL)
DEALLOCATE (local_j_aL)
DEALLOCATE (ij_map)
DEALLOCATE (num_ij_pairs)
DEALLOCATE (local_ab)
IF (calc_forces) THEN
DEALLOCATE (Y_i_aP)
DEALLOCATE (Y_j_aP)
IF (ALLOCATED(t_ab)) THEN
DEALLOCATE (t_ab)
END IF
DEALLOCATE (local_ba)
! here we check if there are almost degenerate ij
! pairs and we update P_ij with these contribution.
! If all pairs are degenerate with each other this step will scale O(N^6),
! if the number of degenerate pairs scales linearly with the system size
! this step will scale O(N^5).
! Start counting the number of almost degenerate ij pairs according
! to eps_canonical
CALL quasi_degenerate_P_ij( &
mp2_env, Eigenval(:, ispin:jspin), homo(ispin:jspin), virtual(ispin:jspin), my_open_shell_ss, &
my_beta_beta_case, Bib_C(ispin:jspin), unit_nr, dimen_RI, &
my_B_size(ispin:jspin), ngroup, my_group_L_size, &
color_sub, ranges_info_array, comm_exchange, para_env_sub, para_env, &
my_B_virtual_start(ispin:jspin), my_B_virtual_end(ispin:jspin), gd_array%sizes, gd_B_virtual(ispin:jspin), &
integ_group_pos2color_sub, dgemm_counter, buffer_1D)
END IF
DEALLOCATE (buffer_1D)
! Dereplicate BIb_C and Gamma_P_ia to save memory
! These matrices will not be needed in that fashion anymore
! B_ia_Q will needed later
IF (calc_forces .AND. jspin == nspins) THEN
IF (.NOT. ALLOCATED(B_ia_Q)) ALLOCATE (B_ia_Q(nspins))
ALLOCATE (B_ia_Q(ispin)%array(homo(ispin), my_B_size(ispin), my_group_L_size_orig))
B_ia_Q(ispin)%array = 0.0_dp
DO jjB = 1, homo(ispin)
DO iiB = 1, my_B_size(ispin)
B_ia_Q(ispin)%array(jjB, iiB, 1:my_group_L_size_orig) = &
BIb_C(ispin)%array(1:my_group_L_size_orig, iiB, jjB)
END DO
END DO
DEALLOCATE (BIb_C(ispin)%array)
! sum Gamma and dereplicate
ALLOCATE (BIb_C(ispin)%array(my_B_size(ispin), homo(ispin), my_group_L_size_orig))
DO proc_shift = 1, comm_rep%num_pe - 1
! invert order
proc_send = MODULO(comm_rep%mepos - proc_shift, comm_rep%num_pe)
proc_receive = MODULO(comm_rep%mepos + proc_shift, comm_rep%num_pe)
start_point = ranges_info_array(3, proc_shift, comm_exchange%mepos)
end_point = ranges_info_array(4, proc_shift, comm_exchange%mepos)
CALL comm_rep%sendrecv(mp2_env%ri_grad%Gamma_P_ia(ispin)%array(:, :, start_point:end_point), &
proc_send, BIb_C(ispin)%array, proc_receive, tag)
!$OMP PARALLEL WORKSHARE DEFAULT(NONE) &
!$OMP SHARED(mp2_env,BIb_C,ispin,homo,my_B_size,my_group_L_size_orig)
mp2_env%ri_grad%Gamma_P_ia(ispin)%array(:, :, 1:my_group_L_size_orig) = &
mp2_env%ri_grad%Gamma_P_ia(ispin)%array(:, :, 1:my_group_L_size_orig) &
+ BIb_C(ispin)%array(:, :, :)
!$OMP END PARALLEL WORKSHARE
END DO
BIb_C(ispin)%array(:, :, :) = mp2_env%ri_grad%Gamma_P_ia(ispin)%array(:, :, 1:my_group_L_size_orig)
DEALLOCATE (mp2_env%ri_grad%Gamma_P_ia(ispin)%array)
CALL MOVE_ALLOC(BIb_C(ispin)%array, mp2_env%ri_grad%Gamma_P_ia(ispin)%array)
ELSE IF (jspin == nspins) THEN
DEALLOCATE (BIb_C(ispin)%array)
END IF
CALL para_env%sum(my_Emp2_Cou)
CALL para_env%sum(my_Emp2_Ex)
IF (my_open_shell_SS .OR. my_alpha_beta_case) THEN
IF (my_alpha_beta_case) THEN
Emp2_S = Emp2_S + my_Emp2_Cou
Emp2_Cou = Emp2_Cou + my_Emp2_Cou
ELSE
my_Emp2_Cou = my_Emp2_Cou*0.25_dp
my_Emp2_EX = my_Emp2_EX*0.5_dp
Emp2_T = Emp2_T + my_Emp2_Cou + my_Emp2_EX
Emp2_Cou = Emp2_Cou + my_Emp2_Cou
Emp2_EX = Emp2_EX + my_Emp2_EX
END IF
ELSE
Emp2_Cou = Emp2_Cou + my_Emp2_Cou
Emp2_EX = Emp2_EX + my_Emp2_EX
END IF
END DO
END DO
DEALLOCATE (integ_group_pos2color_sub)
DEALLOCATE (ranges_info_array)
CALL comm_exchange%free()
CALL comm_rep%free()
IF (calc_forces) THEN
! recover original information (before replication)
DEALLOCATE (gd_array%sizes)
iiB = SIZE(sizes_array_orig)
ALLOCATE (gd_array%sizes(0:iiB - 1))
gd_array%sizes(:) = sizes_array_orig
DEALLOCATE (sizes_array_orig)
! Remove replication from BIb_C and reorder the matrix
my_group_L_size = my_group_L_size_orig
! B_ia_Q(ispin)%array will be deallocated inside of complete_gamma
DO ispin = 1, nspins
CALL complete_gamma(mp2_env, B_ia_Q(ispin)%array, dimen_RI, homo(ispin), &
virtual(ispin), para_env, para_env_sub, ngroup, &
my_group_L_size, my_group_L_start, my_group_L_end, &
my_B_size(ispin), my_B_virtual_start(ispin), &
gd_array, gd_B_virtual(ispin), &
ispin)
END DO
DEALLOCATE (B_ia_Q)
IF (nspins == 1) mp2_env%ri_grad%P_ab(1)%array(:, :) = mp2_env%ri_grad%P_ab(1)%array(:, :)*2.0_dp
BLOCK
TYPE(mp_comm_type) :: comm
CALL comm%from_split(para_env, para_env_sub%mepos)
DO ispin = 1, nspins
! P_ab is only replicated over all subgroups
CALL comm%sum(mp2_env%ri_grad%P_ab(ispin)%array)
! P_ij is replicated over all processes
CALL para_env%sum(mp2_env%ri_grad%P_ij(ispin)%array)
END DO
CALL comm%free()
END BLOCK
END IF
CALL release_group_dist(gd_array)
DO ispin = 1, nspins
IF (ALLOCATED(BIb_C(ispin)%array)) DEALLOCATE (BIb_C(ispin)%array)
CALL release_group_dist(gd_B_virtual(ispin))
END DO
! We do not need this matrix later, so deallocate it here to safe memory
IF (calc_forces) DEALLOCATE (mp2_env%ri_grad%PQ_half)
IF (calc_forces .AND. .NOT. compare_potential_types(mp2_env%ri_metric, mp2_env%potential_parameter)) &
DEALLOCATE (mp2_env%ri_grad%operator_half)
CALL dgemm_counter_write(dgemm_counter, para_env)
! release memory allocated by local_gemm when run on GPU. local_gemm_ctx is null on cpu only runs
CALL mp2_env%local_gemm_ctx%destroy()
CALL timestop(handle)
END SUBROUTINE mp2_ri_gpw_compute_en
! **************************************************************************************************
!> \brief ...
!> \param local_i_aL ...
!> \param ranges_info_array ...
!> \param BIb_C_rec ...
! **************************************************************************************************
SUBROUTINE fill_local_i_aL(local_i_aL, ranges_info_array, BIb_C_rec)
REAL(KIND=dp), DIMENSION(:, :, :), INTENT(INOUT) :: local_i_aL
INTEGER, DIMENSION(:, :), INTENT(IN) :: ranges_info_array
REAL(KIND=dp), DIMENSION(:, :, :), INTENT(IN) :: BIb_C_rec
CHARACTER(LEN=*), PARAMETER :: routineN = 'fill_local_i_aL'
INTEGER :: end_point, handle, irep, Lend_pos, &
Lstart_pos, start_point
CALL timeset(routineN, handle)
DO irep = 1, SIZE(ranges_info_array, 2)
Lstart_pos = ranges_info_array(1, irep)
Lend_pos = ranges_info_array(2, irep)
start_point = ranges_info_array(3, irep)
end_point = ranges_info_array(4, irep)
!$OMP PARALLEL WORKSHARE DEFAULT(NONE) &
!$OMP SHARED(BIb_C_rec,local_i_aL,Lstart_pos,Lend_pos,start_point,end_point)
local_i_aL(Lstart_pos:Lend_pos, :, :) = BIb_C_rec(start_point:end_point, :, :)
!$OMP END PARALLEL WORKSHARE
END DO
CALL timestop(handle)
END SUBROUTINE fill_local_i_aL
! **************************************************************************************************
!> \brief ...
!> \param local_i_aL ...
!> \param ranges_info_array ...
!> \param BIb_C_rec ...
! **************************************************************************************************
SUBROUTINE fill_local_i_aL_2D(local_i_aL, ranges_info_array, BIb_C_rec)
REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: local_i_aL
INTEGER, DIMENSION(:, :), INTENT(IN) :: ranges_info_array
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: BIb_C_rec
CHARACTER(LEN=*), PARAMETER :: routineN = 'fill_local_i_aL_2D'
INTEGER :: end_point, handle, irep, Lend_pos, &
Lstart_pos, start_point
CALL timeset(routineN, handle)
DO irep = 1, SIZE(ranges_info_array, 2)
Lstart_pos = ranges_info_array(1, irep)
Lend_pos = ranges_info_array(2, irep)
start_point = ranges_info_array(3, irep)
end_point = ranges_info_array(4, irep)
!$OMP PARALLEL WORKSHARE DEFAULT(NONE) &
!$OMP SHARED(BIb_C_rec,local_i_aL,Lstart_pos,Lend_pos,start_point,end_point)
local_i_aL(Lstart_pos:Lend_pos, :) = BIb_C_rec(start_point:end_point, :)
!$OMP END PARALLEL WORKSHARE
END DO
CALL timestop(handle)
END SUBROUTINE fill_local_i_aL_2D
! **************************************************************************************************
!> \brief ...
!> \param BIb_C ...
!> \param comm_exchange ...
!> \param comm_rep ...
!> \param homo ...
!> \param sizes_array ...
!> \param my_B_size ...
!> \param my_group_L_size ...
!> \param ranges_info_array ...
! **************************************************************************************************
SUBROUTINE replicate_iaK_2intgroup(BIb_C, comm_exchange, comm_rep, homo, sizes_array, my_B_size, &
my_group_L_size, ranges_info_array)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
INTENT(INOUT) :: BIb_C
TYPE(mp_comm_type), INTENT(IN) :: comm_exchange, comm_rep
INTEGER, INTENT(IN) :: homo
INTEGER, DIMENSION(:), INTENT(IN) :: sizes_array
INTEGER, INTENT(IN) :: my_B_size, my_group_L_size
INTEGER, DIMENSION(:, 0:, 0:), INTENT(IN) :: ranges_info_array
CHARACTER(LEN=*), PARAMETER :: routineN = 'replicate_iaK_2intgroup'
INTEGER :: end_point, handle, max_L_size, &
proc_receive, proc_shift, start_point
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: BIb_C_copy
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: BIb_C_gather
CALL timeset(routineN, handle)
! replication scheme using mpi_allgather
! get the max L size of the
max_L_size = MAXVAL(sizes_array)
ALLOCATE (BIb_C_copy(max_L_size, my_B_size, homo))
BIb_C_copy = 0.0_dp
BIb_C_copy(1:SIZE(BIb_C, 1), 1:my_B_size, 1:homo) = BIb_C
DEALLOCATE (BIb_C)
ALLOCATE (BIb_C_gather(max_L_size, my_B_size, homo, 0:comm_rep%num_pe - 1))
BIb_C_gather = 0.0_dp
CALL comm_rep%allgather(BIb_C_copy, BIb_C_gather)
DEALLOCATE (BIb_C_copy)
ALLOCATE (BIb_C(my_group_L_size, my_B_size, homo))
BIb_C = 0.0_dp
! reorder data
DO proc_shift = 0, comm_rep%num_pe - 1
proc_receive = MODULO(comm_rep%mepos - proc_shift, comm_rep%num_pe)
start_point = ranges_info_array(3, proc_shift, comm_exchange%mepos)
end_point = ranges_info_array(4, proc_shift, comm_exchange%mepos)
BIb_C(start_point:end_point, 1:my_B_size, 1:homo) = &
BIb_C_gather(1:end_point - start_point + 1, 1:my_B_size, 1:homo, proc_receive)
END DO
DEALLOCATE (BIb_C_gather)
CALL timestop(handle)
END SUBROUTINE replicate_iaK_2intgroup
! **************************************************************************************************
!> \brief ...
!> \param local_ab ...
!> \param t_ab ...
!> \param mp2_env ...
!> \param homo ...
!> \param virtual ...
!> \param my_B_size ...
!> \param my_group_L_size ...
!> \param calc_forces ...
!> \param ispin ...
!> \param jspin ...
!> \param local_ba ...
! **************************************************************************************************
SUBROUTINE mp2_ri_allocate_no_blk(local_ab, t_ab, mp2_env, homo, virtual, my_B_size, &
my_group_L_size, calc_forces, ispin, jspin, local_ba)
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
INTENT(OUT) :: local_ab, t_ab
TYPE(mp2_type) :: mp2_env
INTEGER, INTENT(IN) :: homo(2), virtual(2), my_B_size(2), &
my_group_L_size
LOGICAL, INTENT(IN) :: calc_forces
INTEGER, INTENT(IN) :: ispin, jspin
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
INTENT(OUT) :: local_ba
CHARACTER(LEN=*), PARAMETER :: routineN = 'mp2_ri_allocate_no_blk'
INTEGER :: handle
CALL timeset(routineN, handle)
ALLOCATE (local_ab(virtual(ispin), my_B_size(jspin)))
local_ab = 0.0_dp
IF (calc_forces) THEN
IF (.NOT. ALLOCATED(mp2_env%ri_grad%P_ij(jspin)%array)) THEN
ALLOCATE (mp2_env%ri_grad%P_ij(jspin)%array(homo(ispin), homo(ispin)))
mp2_env%ri_grad%P_ij(jspin)%array = 0.0_dp
END IF
IF (.NOT. ALLOCATED(mp2_env%ri_grad%P_ab(jspin)%array)) THEN
ALLOCATE (mp2_env%ri_grad%P_ab(jspin)%array(my_B_size(jspin), virtual(jspin)))
mp2_env%ri_grad%P_ab(jspin)%array = 0.0_dp
END IF
IF (.NOT. ALLOCATED(mp2_env%ri_grad%Gamma_P_ia(jspin)%array)) THEN
ALLOCATE (mp2_env%ri_grad%Gamma_P_ia(jspin)%array(my_B_size(jspin), homo(jspin), my_group_L_size))
mp2_env%ri_grad%Gamma_P_ia(jspin)%array = 0.0_dp
END IF
IF (ispin == jspin) THEN
! For non-alpha-beta case we need amplitudes
ALLOCATE (t_ab(virtual(ispin), my_B_size(jspin)))
! That is just a dummy. In that way, we can pass it as array to other routines w/o requirement for allocatable array
ALLOCATE (local_ba(1, 1))
ELSE
! We need more integrals
ALLOCATE (local_ba(virtual(jspin), my_B_size(ispin)))
END IF
END IF
!
CALL timestop(handle)
END SUBROUTINE mp2_ri_allocate_no_blk
! **************************************************************************************************
!> \brief ...
!> \param dimen_RI ...
!> \param my_B_size ...
!> \param block_size ...
!> \param local_i_aL ...
!> \param local_j_aL ...
!> \param calc_forces ...
!> \param Y_i_aP ...
!> \param Y_j_aP ...
!> \param ispin ...
!> \param jspin ...
! **************************************************************************************************
SUBROUTINE mp2_ri_allocate_blk(dimen_RI, my_B_size, block_size, &
local_i_aL, local_j_aL, calc_forces, &
Y_i_aP, Y_j_aP, ispin, jspin)
INTEGER, INTENT(IN) :: dimen_RI, my_B_size(2), block_size
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
INTENT(OUT) :: local_i_aL, local_j_aL
LOGICAL, INTENT(IN) :: calc_forces
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
INTENT(OUT) :: Y_i_aP, Y_j_aP
INTEGER, INTENT(IN) :: ispin, jspin
CHARACTER(LEN=*), PARAMETER :: routineN = 'mp2_ri_allocate_blk'
INTEGER :: handle
CALL timeset(routineN, handle)
ALLOCATE (local_i_aL(dimen_RI, my_B_size(ispin), block_size))
local_i_aL = 0.0_dp
ALLOCATE (local_j_aL(dimen_RI, my_B_size(jspin), block_size))
local_j_aL = 0.0_dp
IF (calc_forces) THEN
ALLOCATE (Y_i_aP(my_B_size(ispin), dimen_RI, block_size))
Y_i_aP = 0.0_dp
! For closed-shell, alpha-alpha and beta-beta my_B_size_beta=my_b_size
! Not for alpha-beta case: Y_j_aP_beta is sent and received as Y_j_aP
ALLOCATE (Y_j_aP(my_B_size(jspin), dimen_RI, block_size))
Y_j_aP = 0.0_dp
END IF
!
CALL timestop(handle)
END SUBROUTINE mp2_ri_allocate_blk
! **************************************************************************************************
!> \brief ...
!> \param my_alpha_beta_case ...
!> \param total_ij_pairs ...
!> \param homo ...
!> \param homo_beta ...
!> \param block_size ...
!> \param ngroup ...
!> \param ij_map ...
!> \param color_sub ...
!> \param my_ij_pairs ...
!> \param my_open_shell_SS ...
!> \param unit_nr ...
! **************************************************************************************************
SUBROUTINE mp2_ri_communication(my_alpha_beta_case, total_ij_pairs, homo, homo_beta, &
block_size, ngroup, ij_map, color_sub, my_ij_pairs, my_open_shell_SS, unit_nr)
LOGICAL, INTENT(IN) :: my_alpha_beta_case
INTEGER, INTENT(OUT) :: total_ij_pairs
INTEGER, INTENT(IN) :: homo, homo_beta, block_size, ngroup
INTEGER, ALLOCATABLE, DIMENSION(:, :), INTENT(OUT) :: ij_map
INTEGER, INTENT(IN) :: color_sub
INTEGER, INTENT(OUT) :: my_ij_pairs
LOGICAL, INTENT(IN) :: my_open_shell_SS
INTEGER, INTENT(IN) :: unit_nr
CHARACTER(LEN=*), PARAMETER :: routineN = 'mp2_ri_communication'
INTEGER :: assigned_blocks, first_I_block, first_J_block, handle, iiB, ij_block_counter, &
ij_counter, jjB, last_i_block, last_J_block, num_block_per_group, num_IJ_blocks, &
num_IJ_blocks_beta, total_ij_block, total_ij_pairs_blocks
LOGICAL, ALLOCATABLE, DIMENSION(:, :) :: ij_marker
! Calculate the maximum number of ij pairs that have to be computed
! among groups
CALL timeset(routineN, handle)
IF (.NOT. my_open_shell_ss .AND. .NOT. my_alpha_beta_case) THEN
total_ij_pairs = homo*(1 + homo)/2
num_IJ_blocks = homo/block_size - 1
first_I_block = 1
last_i_block = block_size*(num_IJ_blocks - 1)
first_J_block = block_size + 1
last_J_block = block_size*(num_IJ_blocks + 1)
ij_block_counter = 0
DO iiB = first_I_block, last_i_block, block_size
DO jjB = iiB + block_size, last_J_block, block_size
ij_block_counter = ij_block_counter + 1
END DO
END DO
total_ij_block = ij_block_counter
num_block_per_group = total_ij_block/ngroup
assigned_blocks = num_block_per_group*ngroup
total_ij_pairs_blocks = assigned_blocks + (total_ij_pairs - assigned_blocks*(block_size**2))
ALLOCATE (ij_marker(homo, homo))
ij_marker = .TRUE.
ALLOCATE (ij_map(3, total_ij_pairs_blocks))