Repository navigation
Expand file tree
/
Copy pathbse_matvec.F
More file actions
850 lines (740 loc) · 44.1 KB
/
Copy pathbse_matvec.F
File metadata and controls
850 lines (740 loc) · 44.1 KB
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
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Matrix-free application of the BSE matrices A and B to trial vectors from RI slabs that
!> are sliced along the RI index over all MPI ranks
!> \par History
!> 09.2026 created [Maximilian Graml]
! **************************************************************************************************
MODULE bse_matvec
USE bse_util, ONLY: ia_of_occ_virt,&
occ_of_ia,&
virt_of_ia
USE cp_blacs_env, ONLY: cp_blacs_env_create,&
cp_blacs_env_release,&
cp_blacs_env_type
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_create,&
cp_fm_get_info,&
cp_fm_get_submatrix,&
cp_fm_release,&
cp_fm_set_all,&
cp_fm_to_fm_submat_general,&
cp_fm_type
USE input_constants, ONLY: bse_precond_full_diag
USE kinds, ONLY: dp
USE message_passing, ONLY: mp_mem_avail_per_rank_GB,&
mp_mem_used_per_rank_GB,&
mp_para_env_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'bse_matvec'
! the detected MEMORY_PER_PROC claims this share of the free memory on top of the occupied memory,
! the share resolve_memory_per_proc of the GW claims; the probe counts reclaimable cache
REAL(KIND=dp), PARAMETER :: detected_memory_fraction = 0.8_dp, &
fallback_memory_per_proc_GB = 2.0_dp
PUBLIC :: bse_matvec_env_type, bse_matvec_create, bse_matvec_release, bse_matvec_apply, &
bse_memory_left_GB, bse_resolve_memory_per_proc, bse_matvec_diagonal, &
bse_matvec_subblock, bse_matvec_vector_struct, bse_matvec_selfcheck
! **************************************************************************************************
!> \brief RI slabs of one spin channel, every rank holding n_ri_loc whole RI slices
!> \param alpha prefactor of the exchange term (2 singlet, 0 triplet)
!> \param w_fac prefactor of the screened term (0 switches it off)
!> \param eps_diff ε_a - ε_i at the compound index ia = (i-1)*virt + a of bse_util's ia_of_occ_virt
!> \param B_ia B^P_ia at (a, i, P)
!> \param B_bar_ia \bar{B}^P_ia at (a, i, P), only for do_abba
!> \param B_bar_ij \bar{B}^P_ij at (j, i, P)
!> \param B_ab B^P_ab at (b, a, P)
!> \param row_count rows of a trial vector owned by each rank, in rank order
!> \param row_displ first row of each rank minus one, so that row_count and row_displ describe
!> the contiguous ia range per rank that allgatherv and sum_scatter need
!> \param blacs_env npe x 1 process grid of the slabs and of all trial vectors
!> \param block_cols trial vectors per pass in bse_matvec_apply, -1 to size it from the memory left
!> \param memory_per_proc_gb the resolved MEMORY_PER_PROC of the BSE, which block_cols -1 sizes the pass from
! **************************************************************************************************
TYPE bse_matvec_env_type
INTEGER :: homo = 0, virt = 0, n_ov = 0, &
n_ri_loc = 0, block_cols = -1
REAL(KIND=dp) :: alpha = 0.0_dp, w_fac = 0.0_dp, &
memory_per_proc_gb = -1.0_dp
LOGICAL :: do_abba = .FALSE.
INTEGER, ALLOCATABLE, DIMENSION(:) :: row_count, row_displ
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eps_diff
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: B_ia, B_bar_ia, B_bar_ij, B_ab
TYPE(cp_blacs_env_type), POINTER :: blacs_env => NULL()
TYPE(mp_para_env_type), POINTER :: para_env => NULL()
END TYPE bse_matvec_env_type
CONTAINS
! **************************************************************************************************
!> \brief MEMORY_PER_PROC of the BSE as a number: the keyword's value, or for -1 the memory the rank
!> holds plus the share detected_memory_fraction of the free memory, read once at the first
!> consumer; when the free memory cannot be read, the GW's fallback of 2 GB with a warning
!> \param memory_per_proc_gb the keyword's value on entry, the memory per MPI rank for the BSE on exit
!> \param para_env the ranks that share the node's memory
!> \param unit_nr output unit, positive on the writing rank only
! **************************************************************************************************
SUBROUTINE bse_resolve_memory_per_proc(memory_per_proc_gb, para_env, unit_nr)
REAL(KIND=dp), INTENT(INOUT) :: memory_per_proc_gb
TYPE(mp_para_env_type), POINTER :: para_env
INTEGER, INTENT(IN) :: unit_nr
REAL(KIND=dp) :: mem_free_GB, mem_occ_GB
IF (memory_per_proc_gb >= 0.0_dp) RETURN
CALL mp_mem_avail_per_rank_GB(para_env, mem_free_GB)
CALL mp_mem_used_per_rank_GB(para_env, mem_occ_GB)
IF (mem_free_GB > 0.0_dp) THEN
memory_per_proc_gb = mem_occ_GB + detected_memory_fraction*mem_free_GB
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A4,T7,A,T67,F14.3)') 'BSE|', 'Memory per MPI rank for the BSE, detected (GB)', &
memory_per_proc_gb
END IF
ELSE
memory_per_proc_gb = fallback_memory_per_proc_GB
CALL cp_warn(__LOCATION__, &
"BSE: the free memory per MPI rank could not be detected; MEMORY_PER_PROC falls back "// &
"to 2 GB. Set MEMORY_PER_PROC in the BSE section for a run that fits the node.")
END IF
END SUBROUTINE bse_resolve_memory_per_proc
! **************************************************************************************************
!> \brief What MEMORY_PER_PROC leaves to a step: the resolved memory per MPI rank minus the memory the
!> rank holds now, never negative; both probes reduce over the ranks, so every rank gets the same
!> \param memory_per_proc_gb the resolved MEMORY_PER_PROC
!> \param para_env the ranks that share the node's memory
!> \return memory left per MPI rank in GB
! **************************************************************************************************
FUNCTION bse_memory_left_GB(memory_per_proc_gb, para_env) RESULT(mem_left_GB)
REAL(KIND=dp), INTENT(IN) :: memory_per_proc_gb
TYPE(mp_para_env_type), POINTER :: para_env
REAL(KIND=dp) :: mem_left_GB
REAL(KIND=dp) :: mem_occ_GB
! a consumer before the resolution would size itself from the keyword's -1
CPASSERT(memory_per_proc_gb >= 0.0_dp)
CALL mp_mem_used_per_rank_GB(para_env, mem_occ_GB)
mem_left_GB = MAX(memory_per_proc_gb - mem_occ_GB, 0.0_dp)
END FUNCTION bse_memory_left_GB
! **************************************************************************************************
!> \brief Moves the RI slabs onto an npe x 1 process grid, such that every rank owns whole RI
!> slices, and stores them as local 3-index arrays: the contraction over each P in
!> bse_matvec_apply is then a local DGEMM and only the final sum over P crosses the ranks
!> \param mv_env the environment created here
!> \param fm_S_ia B^P_ia, N_RI x homo*virt
!> \param fm_S_bar_ij \bar{B}^P_ij, the slab that enters W_ij,ab, N_RI x homo*homo
!> \param fm_S_ab B^P_ab, N_RI x virt*virt
!> \param fm_S_bar_ia \bar{B}^P_ia, the slab that enters W_ib,aj, N_RI x homo*virt
!> \param eps_reduced quasiparticle energies of the homo+virt active levels
!> \param homo occupied levels of the active window
!> \param virt virtual levels of the active window
!> \param alpha prefactor of the exchange term, 2 singlet, 0 triplet
!> \param w_fac prefactor of the screened term, 0 switches it off
!> \param do_abba slice \bar{B}^P_ia as well, for the application of B
!> \param unit_nr output unit, positive on the writing rank only
!> \param block_cols trial vectors per pass of bse_matvec_apply; absent or -1 sizes it from the memory left
!> \param memory_per_proc_gb the resolved MEMORY_PER_PROC of the BSE, what block_cols -1 sizes the pass from
! **************************************************************************************************
SUBROUTINE bse_matvec_create(mv_env, fm_S_ia, fm_S_bar_ij, fm_S_ab, fm_S_bar_ia, eps_reduced, &
homo, virt, alpha, w_fac, do_abba, unit_nr, block_cols, memory_per_proc_gb)
TYPE(bse_matvec_env_type), INTENT(OUT) :: mv_env
TYPE(cp_fm_type), INTENT(IN) :: fm_S_ia, fm_S_bar_ij, fm_S_ab, &
fm_S_bar_ia
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: eps_reduced
INTEGER, INTENT(IN) :: homo, virt
REAL(KIND=dp), INTENT(IN) :: alpha, w_fac
LOGICAL, INTENT(IN) :: do_abba
INTEGER, INTENT(IN) :: unit_nr
INTEGER, INTENT(IN), OPTIONAL :: block_cols
REAL(KIND=dp), INTENT(IN), OPTIONAL :: memory_per_proc_gb
CHARACTER(LEN=*), PARAMETER :: routineN = 'bse_matvec_create'
CHARACTER(LEN=12) :: n_idle_str, n_ri_str, npe_str
INTEGER :: a, handle, i, n_ri, n_ri_slab, &
n_row_block
REAL(KIND=dp) :: mem_slabs_GB, n_slab_entries
CALL timeset(routineN, handle)
mv_env%homo = homo
mv_env%virt = virt
IF (PRESENT(block_cols)) mv_env%block_cols = block_cols
IF (PRESENT(memory_per_proc_gb)) mv_env%memory_per_proc_gb = memory_per_proc_gb
mv_env%n_ov = homo*virt
mv_env%alpha = alpha
mv_env%w_fac = w_fac
mv_env%do_abba = do_abba
mv_env%para_env => fm_S_ia%matrix_struct%para_env
CALL cp_blacs_env_create(mv_env%blacs_env, mv_env%para_env, &
grid_2d=[mv_env%para_env%num_pe, 1])
! one contiguous ia range per rank, so that a trial vector is gathered with allgatherv and
! its image scattered back with sum_scatter, rather than replicated by two allreduces
n_row_block = contiguous_row_block(mv_env%n_ov, mv_env%para_env%num_pe)
ALLOCATE (mv_env%row_count(mv_env%para_env%num_pe), mv_env%row_displ(mv_env%para_env%num_pe))
DO i = 1, mv_env%para_env%num_pe
mv_env%row_displ(i) = MIN(mv_env%n_ov, (i - 1)*n_row_block)
mv_env%row_count(i) = MIN(mv_env%n_ov, i*n_row_block) - mv_env%row_displ(i)
END DO
! eps_diff_ia = ε_a - ε_i at the compound index ia = (i-1)*virt + a, the row index of every trial vector
ALLOCATE (mv_env%eps_diff(mv_env%n_ov))
DO i = 1, homo
DO a = 1, virt
mv_env%eps_diff(ia_of_occ_virt(i, a, virt)) = eps_reduced(homo + a) - eps_reduced(i)
END DO
END DO
! every slab is sliced on the same grid, so every rank owns the same RI indices of each slab
CALL slice_slab(mv_env, fm_S_ia, virt, homo, mv_env%B_ia, mv_env%n_ri_loc)
IF (w_fac /= 0.0_dp) THEN
CALL slice_slab(mv_env, fm_S_bar_ij, homo, homo, mv_env%B_bar_ij, n_ri_slab)
CPASSERT(n_ri_slab == mv_env%n_ri_loc)
CALL slice_slab(mv_env, fm_S_ab, virt, virt, mv_env%B_ab, n_ri_slab)
CPASSERT(n_ri_slab == mv_env%n_ri_loc)
IF (do_abba) THEN
CALL slice_slab(mv_env, fm_S_bar_ia, virt, homo, mv_env%B_bar_ia, n_ri_slab)
CPASSERT(n_ri_slab == mv_env%n_ri_loc)
END IF
END IF
CALL cp_fm_get_info(fm_S_ia, nrow_global=n_ri)
IF (mv_env%para_env%num_pe > n_ri .AND. unit_nr > 0) THEN
WRITE (npe_str, '(I0)') mv_env%para_env%num_pe
WRITE (n_ri_str, '(I0)') n_ri
WRITE (n_idle_str, '(I0)') mv_env%para_env%num_pe - n_ri
CALL cp_warn(__LOCATION__, &
"BSE iterative solver: more MPI ranks ("//TRIM(npe_str)// &
") than RI basis functions ("//TRIM(n_ri_str)//"); "// &
TRIM(n_idle_str)//" ranks hold no RI slice and idle in the kernel application.")
END IF
IF (unit_nr > 0) THEN
! pair entries of the sliced slabs per RI index
n_slab_entries = REAL(homo*virt, dp)
IF (w_fac /= 0.0_dp) THEN
n_slab_entries = n_slab_entries + REAL(homo, dp)**2 + REAL(virt, dp)**2
IF (do_abba) n_slab_entries = n_slab_entries + REAL(homo*virt, dp)
END IF
mem_slabs_GB = n_slab_entries*REAL(mv_env%n_ri_loc, dp)*8.0E-9_dp
WRITE (unit_nr, '(T2,A4,T7,A,T71,I10)') 'BSE|', &
'Max. number of RI functions per MPI rank', mv_env%n_ri_loc
WRITE (unit_nr, '(T2,A4,T7,A,T67,F14.3)') 'BSE|', &
'Memory of the RI slabs per MPI rank (GB)', mem_slabs_GB
END IF
CALL timestop(handle)
END SUBROUTINE bse_matvec_create
! **************************************************************************************************
!> \brief Rows of a trial vector per rank, the smallest block that leaves no rank beyond the last
!> \param n_ov number of transitions, the global row count
!> \param npe ranks of the npe x 1 grid
!> \return rows of every full block; the last rank takes what remains, possibly none
! **************************************************************************************************
PURE FUNCTION contiguous_row_block(n_ov, npe) RESULT(n_row_block)
INTEGER, INTENT(IN) :: n_ov, npe
INTEGER :: n_row_block
n_row_block = (n_ov + npe - 1)/npe
END FUNCTION contiguous_row_block
! **************************************************************************************************
!> \brief Redistributes one N_RI x n_fast*n_slow slab to whole RI rows per rank and copies the owned
!> rows into a contiguous (n_fast, n_slow, n_ri_loc) array, slab_loc(x, y, p) = slab(P_p, (y-1)*n_fast + x)
!> for the p-th owned RI index P_p, so that every slice slab_loc(:, :, p) is a n_fast x n_slow DGEMM operand
!> \param mv_env process grid and communicator of the slices
!> \param fm_slab the slab on the grid of the caller, N_RI x n_fast*n_slow
!> \param n_fast fast index of the pair index of the slab
!> \param n_slow slow index of the pair index of the slab
!> \param slab_loc the owned slices, allocated here
!> \param n_ri_loc RI indices owned by this rank, the third extent of slab_loc
! **************************************************************************************************
SUBROUTINE slice_slab(mv_env, fm_slab, n_fast, n_slow, slab_loc, n_ri_loc)
TYPE(bse_matvec_env_type), INTENT(IN) :: mv_env
TYPE(cp_fm_type), INTENT(IN) :: fm_slab
INTEGER, INTENT(IN) :: n_fast, n_slow
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
INTENT(OUT) :: slab_loc
INTEGER, INTENT(OUT) :: n_ri_loc
CHARACTER(LEN=*), PARAMETER :: routineN = 'slice_slab'
INTEGER :: handle, n_ri, npair, p
TYPE(cp_fm_struct_type), POINTER :: fm_struct
TYPE(cp_fm_type) :: fm_sliced
CALL timeset(routineN, handle)
! row count from the struct: the slabs may carry fewer rows than the full RI basis
CALL cp_fm_get_info(fm_slab, nrow_global=n_ri, ncol_global=npair)
CPASSERT(npair == n_fast*n_slow)
! one-row blocks on the npe x 1 grid spread the RI rows cyclically over the ranks; the copy
! below turns each strided local_data row into a contiguous (n_fast, n_slow) slice
NULLIFY (fm_struct)
CALL cp_fm_struct_create(fm_struct, para_env=mv_env%para_env, context=mv_env%blacs_env, &
nrow_global=n_ri, ncol_global=npair, nrow_block=1, &
force_block=.TRUE.)
CALL cp_fm_create(fm_sliced, fm_struct, name="fm_slab_ri_sliced")
CALL cp_fm_struct_release(fm_struct)
CALL cp_fm_to_fm_submat_general(fm_slab, fm_sliced, n_ri, npair, 1, 1, 1, 1, &
fm_slab%matrix_struct%context)
CALL cp_fm_get_info(fm_sliced, nrow_local=n_ri_loc)
ALLOCATE (slab_loc(n_fast, n_slow, n_ri_loc))
DO p = 1, n_ri_loc
slab_loc(:, :, p) = RESHAPE(fm_sliced%local_data(p, 1:npair), [n_fast, n_slow])
END DO
CALL cp_fm_release(fm_sliced)
CALL timestop(handle)
END SUBROUTINE slice_slab
! **************************************************************************************************
!> \brief Frees the sliced slabs, the transition energies and the process grid of mv_env
!> \param mv_env the environment released
! **************************************************************************************************
SUBROUTINE bse_matvec_release(mv_env)
TYPE(bse_matvec_env_type), INTENT(INOUT) :: mv_env
IF (ALLOCATED(mv_env%row_count)) DEALLOCATE (mv_env%row_count)
IF (ALLOCATED(mv_env%row_displ)) DEALLOCATE (mv_env%row_displ)
IF (ALLOCATED(mv_env%eps_diff)) DEALLOCATE (mv_env%eps_diff)
IF (ALLOCATED(mv_env%B_ia)) DEALLOCATE (mv_env%B_ia)
IF (ALLOCATED(mv_env%B_bar_ia)) DEALLOCATE (mv_env%B_bar_ia)
IF (ALLOCATED(mv_env%B_bar_ij)) DEALLOCATE (mv_env%B_bar_ij)
IF (ALLOCATED(mv_env%B_ab)) DEALLOCATE (mv_env%B_ab)
IF (ASSOCIATED(mv_env%blacs_env)) CALL cp_blacs_env_release(mv_env%blacs_env)
NULLIFY (mv_env%para_env)
END SUBROUTINE bse_matvec_release
! **************************************************************************************************
!> \brief Matrix structure of a block of trial vectors: rows ia distributed, all columns local
!> \param mv_env process grid and communicator of the trial vectors
!> \param ncol_global number of trial vectors
!> \param fm_struct created here, released by the caller
! **************************************************************************************************
SUBROUTINE bse_matvec_vector_struct(mv_env, ncol_global, fm_struct)
TYPE(bse_matvec_env_type), INTENT(IN) :: mv_env
INTEGER, INTENT(IN) :: ncol_global
TYPE(cp_fm_struct_type), POINTER :: fm_struct
NULLIFY (fm_struct)
! the row block matches mv_env%row_count, which bse_matvec_apply's collectives rely on
CALL cp_fm_struct_create(fm_struct, para_env=mv_env%para_env, context=mv_env%blacs_env, &
nrow_global=mv_env%n_ov, ncol_global=ncol_global, &
nrow_block=contiguous_row_block(mv_env%n_ov, mv_env%para_env%num_pe), &
force_block=.TRUE.)
END SUBROUTINE bse_matvec_vector_struct
! **************************************************************************************************
!> \brief Applies A (and B) to ncol trial vectors without forming an N_ov x N_ov object,
!> (A Z)_ia = (ε_a-ε_i) Z_ia + α sum_P B^P_ia sum_jb B^P_jb Z_jb - w sum_Pjb \bar{B}^P_ij B^P_ab Z_jb
!> (B Z)_ia = α sum_P B^P_ia sum_jb B^P_jb Z_jb - w sum_Pjb \bar{B}^P_ib B^P_ja Z_jb
!> α: exchange prefactor (2 singlet, 0 triplet), w: prefactor of the screened term,
!> \bar{B}: slab bound by the caller of bse_matvec_create (B itself for TDHF and ALPHA
!> screening, sum_Q [1+Q(0)]^-1_PQ B^Q otherwise); the sums over P run over the local RI
!> slices and are completed by a sum over the ranks
!> \param mv_env slabs, prefactors and transition energies
!> \param fm_Z trial vectors, columns first_col .. first_col+ncol-1 are read
!> \param first_col first trial vector read, and first column written
!> \param ncol number of trial vectors
!> \param fm_AZ receives A Z in the same columns
!> \param fm_BZ receives B Z in the same columns, or from first_col_BZ on
!> \param first_col_BZ first column of fm_BZ written, first_col by default
! **************************************************************************************************
SUBROUTINE bse_matvec_apply(mv_env, fm_Z, first_col, ncol, fm_AZ, fm_BZ, first_col_BZ)
TYPE(bse_matvec_env_type), INTENT(IN), TARGET :: mv_env
TYPE(cp_fm_type), INTENT(IN) :: fm_Z
INTEGER, INTENT(IN) :: first_col, ncol
TYPE(cp_fm_type), INTENT(IN) :: fm_AZ
TYPE(cp_fm_type), INTENT(IN), OPTIONAL :: fm_BZ
INTEGER, INTENT(IN), OPTIONAL :: first_col_BZ
CHARACTER(LEN=*), PARAMETER :: routineN = 'bse_matvec_apply'
INTEGER :: a, col, col_shift_B, first_row, handle, handle2, homo, i, ia, iloc, k, last_row, &
me, n_outputs, n_small, n_work_bufs, nb, nb_max, nrow_local, nvec, offset, p, peer, rows, &
virt
INTEGER, DIMENSION(:), POINTER :: row_indices
LOGICAL :: do_B, do_screened, has_exchange
REAL(KIND=dp) :: mem_avail_GB
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), TARGET :: result_buf, work_buf
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: T_Pk
REAL(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
POINTER :: B_ia_2d, local_result, packed, RA, &
RA_virt, RB_occ, RB_virt, T_occ, &
T_small_cols, T_small_rows, T_virt, Z, &
Z_occ, Z_virt
REAL(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
POINTER :: RA_screen, RB_screen, Z_screen
INTEGER :: displs(SIZE(mv_env%row_displ)), &
counts(SIZE(mv_env%row_count))
CALL timeset(routineN, handle)
homo = mv_env%homo
virt = mv_env%virt
n_small = MIN(homo, virt)
do_B = PRESENT(fm_BZ)
do_screened = mv_env%w_fac /= 0.0_dp .AND. mv_env%n_ri_loc > 0
has_exchange = mv_env%alpha /= 0.0_dp .AND. mv_env%n_ri_loc > 0
IF (do_B) THEN
! B Z needs the \bar{B}^P_ia slab, which is sliced for do_abba only
CPASSERT(mv_env%do_abba)
END IF
col_shift_B = 0
IF (PRESENT(first_col_BZ)) col_shift_B = first_col_BZ - first_col
CALL cp_fm_get_info(fm_Z, nrow_local=nrow_local, row_indices=row_indices)
! the collectives below read the rows of one rank as one range, as bse_matvec_vector_struct
! lays them out; a vector built elsewhere would scatter into the wrong rows
me = mv_env%para_env%mepos + 1
CPASSERT(nrow_local == mv_env%row_count(me))
CPASSERT(nrow_local == 0 .OR. row_indices(1) == mv_env%row_displ(me) + 1)
! columns per pass from three/four replicated blocks: trial vectors, scratch and A (and B)
n_outputs = 1
IF (do_B) n_outputs = 2
n_work_bufs = 2 + n_outputs
IF (mv_env%block_cols > 0) THEN
! pinned by input: the replicated work arrays are then a known n_work_bufs*n_ov*nb doubles
nb_max = MIN(ncol, mv_env%block_cols)
ELSE
! the pass takes what MEMORY_PER_PROC leaves after the solver's arrays
mem_avail_GB = bse_memory_left_GB(mv_env%memory_per_proc_gb, mv_env%para_env)
nb_max = INT(MIN(REAL(ncol, dp), &
mem_avail_GB*1.0E9_dp/(8.0_dp*REAL(n_work_bufs, dp)*REAL(mv_env%n_ov, dp))))
END IF
nb_max = MAX(nb_max, 1)
! The paired work buffer and all collective counts/displacements use default INTEGER.
nb_max = MIN(nb_max, (HUGE(0)/2)/mv_env%n_ov)
CPASSERT(nb_max >= 1)
! Keep the same three/four full buffers, reused by communication, exchange and screening.
! Canonical Z and the exchange image start in the first slots of work_buf and result_buf.
ALLOCATE (work_buf(2*mv_env%n_ov*nb_max), result_buf(n_outputs*mv_env%n_ov*nb_max))
ALLOCATE (T_Pk(mv_env%n_ri_loc, nb_max))
B_ia_2d(1:mv_env%n_ov, 1:mv_env%n_ri_loc) => mv_env%B_ia
DO col = first_col, first_col + ncol - 1, nb_max
nb = MIN(nb_max, first_col + ncol - col)
nvec = mv_env%n_ov*nb
! Canonical ia-by-k views for the exchange block GEMMs.
Z(1:mv_env%n_ov, 1:nb) => work_buf(1:nvec)
RA(1:mv_env%n_ov, 1:nb) => result_buf(1:nvec)
! Z_ia,k on every rank, by design: each rank contracts its whole RI slices against all of Z (header of
! bse_matvec_create), so the trial vectors are the one replicated buffer, n_ov x nb per matvec at any rank
! count. Gather the entire block in rank-major order, then unpack the contiguous row ranges.
! result_buf is free until the exchange; the second work slot receives the gathered data.
CALL timeset(routineN//"_gather", handle2)
DO k = 1, nb
offset = (k - 1)*nrow_local
result_buf(offset + 1:offset + nrow_local) = fm_Z%local_data(1:nrow_local, col + k - 1)
END DO
counts = mv_env%row_count*nb
displs = mv_env%row_displ*nb
CALL mv_env%para_env%allgatherv(result_buf(1:nrow_local*nb), work_buf(nvec + 1:2*nvec), counts, displs)
DO peer = 1, mv_env%para_env%num_pe
rows = mv_env%row_count(peer)
first_row = mv_env%row_displ(peer) + 1
last_row = mv_env%row_displ(peer) + rows
DO k = 1, nb
offset = nvec + displs(peer) + (k - 1)*rows
Z(first_row:last_row, k) = work_buf(offset + 1:offset + rows)
END DO
END DO
CALL timestop(handle2)
! exchange, identical in A Z and B Z: R_ia,k = α sum_P B^P_ia t_Pk, t_Pk = sum_jb B^P_jb Z_jb,k
CALL timeset(routineN//"_exchange", handle2)
IF (has_exchange) THEN
CALL DGEMM('T', 'N', mv_env%n_ri_loc, nb, mv_env%n_ov, 1.0_dp, B_ia_2d, mv_env%n_ov, Z, mv_env%n_ov, &
0.0_dp, T_Pk, mv_env%n_ri_loc)
END IF
! Without screening, Z is no longer needed after forming T_Pk. Reuse its slot
! for the canonical exchange image, leaving result_buf free for the reduction.
IF (.NOT. do_screened) RA(1:mv_env%n_ov, 1:nb) => work_buf(1:nvec)
IF (has_exchange) THEN
CALL DGEMM('N', 'N', mv_env%n_ov, nb, mv_env%n_ri_loc, mv_env%alpha, B_ia_2d, mv_env%n_ov, T_Pk, mv_env%n_ri_loc, &
0.0_dp, RA, mv_env%n_ov)
ELSE IF (.NOT. do_screened) THEN
RA(:, :) = 0.0_dp
END IF
CALL timestop(handle2)
CALL timeset(routineN//"_pack", handle2)
IF (do_screened) THEN
! Put the trial-vector index between the two orbital indices. One flat block then has
! both (i, (k a)) and ((i k), a) views, so neither contraction needs a per-vector GEMM.
! TDA keeps this Z in work slot 2; ABBA uses result slot 2, leaving both work slots for A/B.
IF (do_B) THEN
Z_screen(1:homo, 1:nb, 1:virt) => result_buf(nvec + 1:2*nvec)
ELSE
Z_screen(1:homo, 1:nb, 1:virt) => work_buf(nvec + 1:2*nvec)
END IF
Z_occ(1:homo, 1:nb*virt) => Z_screen
Z_virt(1:homo*nb, 1:virt) => Z_screen
DO a = 1, virt
DO k = 1, nb
DO i = 1, homo
Z_screen(i, k, a) = Z(ia_of_occ_virt(i, a, virt), k)
END DO
END DO
END DO
! Copy the shared exchange image into the two screened output layouts, then its original
! result slot is free for all RI-slice intermediates. These permutations happen once per pass.
RA_screen(1:homo, 1:nb, 1:virt) => work_buf(1:nvec)
RA_virt(1:homo*nb, 1:virt) => work_buf(1:nvec)
IF (do_B) THEN
RB_screen(1:virt, 1:nb, 1:homo) => work_buf(nvec + 1:2*nvec)
RB_occ(1:virt, 1:nb*homo) => work_buf(nvec + 1:2*nvec)
RB_virt(1:virt*nb, 1:homo) => work_buf(nvec + 1:2*nvec)
END IF
IF (has_exchange) THEN
DO a = 1, virt
DO k = 1, nb
DO i = 1, homo
ia = ia_of_occ_virt(i, a, virt)
RA_screen(i, k, a) = RA(ia, k)
IF (do_B) RB_screen(a, k, i) = RA(ia, k)
END DO
END DO
END DO
ELSE
RA_virt(:, :) = 0.0_dp
IF (do_B) RB_occ(:, :) = 0.0_dp
END IF
T_occ(1:homo, 1:nb*virt) => result_buf(1:nvec)
T_virt(1:homo*nb, 1:virt) => result_buf(1:nvec)
! The B association chooses the smaller square intermediate: n_small^2*nb <= nvec.
T_small_cols(1:n_small, 1:nb*n_small) => result_buf(1:n_small*n_small*nb)
T_small_rows(1:n_small*nb, 1:n_small) => result_buf(1:n_small*n_small*nb)
END IF
CALL timestop(handle2)
CALL timeset(routineN//"_W", handle2)
IF (do_screened) THEN
DO p = 1, mv_env%n_ri_loc
! A: T_ikb = sum_j \bar{B}^P_ji Z_bjk, R^A_ika -= w sum_b T_ikb B^P_ba.
! Only the view of T changes between the occupied and virtual block GEMMs.
CALL DGEMM('T', 'N', homo, nb*virt, homo, 1.0_dp, mv_env%B_bar_ij(:, :, p), homo, &
Z_occ, homo, 0.0_dp, T_occ, homo)
CALL DGEMM('N', 'N', homo*nb, virt, virt, -mv_env%w_fac, T_virt, homo*nb, &
mv_env%B_ab(:, :, p), virt, 1.0_dp, RA_virt, homo*nb)
IF (do_B) THEN
! B: R^B_aik -= w sum_jb B^P_aj Z_bjk \bar{B}^P_bi.
! Associate the products as B (Z^T \bar{B}) for homo <= virt, otherwise (B Z^T) \bar{B}.
IF (homo <= virt) THEN
CALL DGEMM('N', 'N', homo*nb, homo, virt, 1.0_dp, Z_virt, homo*nb, &
mv_env%B_bar_ia(:, :, p), virt, 0.0_dp, T_small_rows, homo*nb)
CALL DGEMM('N', 'N', virt, nb*homo, homo, -mv_env%w_fac, mv_env%B_ia(:, :, p), virt, &
T_small_cols, homo, 1.0_dp, RB_occ, virt)
ELSE
CALL DGEMM('N', 'N', virt, nb*virt, homo, 1.0_dp, mv_env%B_ia(:, :, p), virt, &
Z_occ, homo, 0.0_dp, T_small_cols, virt)
CALL DGEMM('N', 'N', virt*nb, homo, virt, -mv_env%w_fac, T_small_rows, virt*nb, &
mv_env%B_bar_ia(:, :, p), virt, 1.0_dp, RB_virt, virt*nb)
END IF
END IF
END DO
END IF
CALL timestop(handle2)
! Pack by destination rank. Screening has distinct A/B layouts; without it,
! both outputs share the canonical exchange image. result_buf is free in either case.
CALL timeset(routineN//"_reduce", handle2)
DO peer = 1, mv_env%para_env%num_pe
rows = mv_env%row_count(peer)
first_row = mv_env%row_displ(peer) + 1
DO k = 1, nb
offset = n_outputs*displs(peer) + (k - 1)*rows
IF (do_screened) THEN
DO iloc = 1, rows
ia = first_row + iloc - 1
i = occ_of_ia(ia, virt)
a = virt_of_ia(ia, virt)
result_buf(offset + iloc) = RA_screen(i, k, a)
IF (do_B) result_buf(offset + rows*nb + iloc) = RB_screen(a, k, i)
END DO
ELSE
last_row = first_row + rows - 1
result_buf(offset + 1:offset + rows) = RA(first_row:last_row, k)
IF (do_B) result_buf(offset + rows*nb + 1:offset + rows*nb + rows) = RA(first_row:last_row, k)
END IF
END DO
END DO
counts = counts*n_outputs
! The wrapper's serial branch also expects a single-column flattened send buffer.
packed(1:n_outputs*nvec, 1:1) => result_buf(1:n_outputs*nvec)
CALL mv_env%para_env%sum_scatter(packed, work_buf(1:n_outputs*nrow_local*nb), counts)
local_result(1:nrow_local, 1:n_outputs*nb) => work_buf(1:n_outputs*nrow_local*nb)
DO k = 1, nb
DO iloc = 1, nrow_local
ia = row_indices(iloc)
fm_AZ%local_data(iloc, col + k - 1) = local_result(iloc, k) + &
mv_env%eps_diff(ia)*fm_Z%local_data(iloc, col + k - 1)
END DO
IF (do_B) fm_BZ%local_data(1:nrow_local, col + col_shift_B + k - 1) = local_result(:, nb + k)
END DO
CALL timestop(handle2)
END DO
DEALLOCATE (work_buf, result_buf, T_Pk)
CALL timestop(handle)
END SUBROUTINE bse_matvec_apply
! **************************************************************************************************
!> \brief Diagonal used by the Davidson correction, either ε_a-ε_i or the full diagonal
!> A_ia,ia = ε_a-ε_i + α sum_P (B^P_ia)^2 - w sum_P \bar{B}^P_ii B^P_aa
!> with α, w and \bar{B} as in bse_matvec_apply
!> \param mv_env slabs, prefactors and transition energies
!> \param precond_kind bse_precond_full_diag or bse_precond_qp_diff
!> \param diag replicated, size N_ov
! **************************************************************************************************
SUBROUTINE bse_matvec_diagonal(mv_env, precond_kind, diag)
TYPE(bse_matvec_env_type), INTENT(IN) :: mv_env
INTEGER, INTENT(IN) :: precond_kind
REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: diag
CHARACTER(LEN=*), PARAMETER :: routineN = 'bse_matvec_diagonal'
INTEGER :: a, handle, i, ia, p
CALL timeset(routineN, handle)
diag(:) = 0.0_dp
IF (precond_kind == bse_precond_full_diag) THEN
DO p = 1, mv_env%n_ri_loc
DO i = 1, mv_env%homo
DO a = 1, mv_env%virt
ia = ia_of_occ_virt(i, a, mv_env%virt)
diag(ia) = diag(ia) + mv_env%alpha*mv_env%B_ia(a, i, p)**2
IF (mv_env%w_fac /= 0.0_dp) THEN
diag(ia) = diag(ia) - mv_env%w_fac*mv_env%B_bar_ij(i, i, p)*mv_env%B_ab(a, a, p)
END IF
END DO
END DO
END DO
! one n_ov vector, summed once per solve; replicated on every rank on purpose
CALL mv_env%para_env%sum(diag)
END IF
diag(:) = diag(:) + mv_env%eps_diff(:)
CALL timestop(handle)
END SUBROUTINE bse_matvec_diagonal
! **************************************************************************************************
!> \brief Exact A (and B) on a list of transitions, replicated on every rank,
!> A_kl = δ_kl (ε_a-ε_i) + α sum_P B^P_ia B^P_jb - w sum_P \bar{B}^P_ij B^P_ab
!> B_kl = α sum_P B^P_ia B^P_jb - w sum_P \bar{B}^P_ib B^P_ja
!> with k = (i,a), l = (j,b) and α, w, \bar{B} as in bse_matvec_apply
!> \param mv_env slabs, prefactors and transition energies
!> \param ia_list global transition indices ia = (i-1)*virt + a of the block
!> \param A_sub SIZE(ia_list) x SIZE(ia_list)
!> \param B_sub same, only formed when present
! **************************************************************************************************
SUBROUTINE bse_matvec_subblock(mv_env, ia_list, A_sub, B_sub)
TYPE(bse_matvec_env_type), INTENT(IN) :: mv_env
INTEGER, DIMENSION(:), INTENT(IN) :: ia_list
REAL(KIND=dp), DIMENSION(:, :), INTENT(OUT) :: A_sub
REAL(KIND=dp), DIMENSION(:, :), INTENT(OUT), &
OPTIONAL :: B_sub
CHARACTER(LEN=*), PARAMETER :: routineN = 'bse_matvec_subblock'
INTEGER :: handle, k, l, n_sub, p
INTEGER, ALLOCATABLE, DIMENSION(:) :: a_virt, i_occ
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: B_Pk
CALL timeset(routineN, handle)
n_sub = SIZE(ia_list)
ALLOCATE (i_occ(n_sub), a_virt(n_sub), B_Pk(MAX(mv_env%n_ri_loc, 1), n_sub))
DO k = 1, n_sub
i_occ(k) = occ_of_ia(ia_list(k), mv_env%virt)
a_virt(k) = virt_of_ia(ia_list(k), mv_env%virt)
END DO
! exchange, identical in A and B: α sum_P B_Pk B_Pl with B_Pk = B^P_ia at ia = ia_list(k)
A_sub(:, :) = 0.0_dp
IF (mv_env%alpha /= 0.0_dp .AND. mv_env%n_ri_loc > 0) THEN
! P outermost: the gathered entries of one slice lie within one contiguous (a, i) block
DO p = 1, mv_env%n_ri_loc
DO k = 1, n_sub
B_Pk(p, k) = mv_env%B_ia(a_virt(k), i_occ(k), p)
END DO
END DO
CALL DGEMM('T', 'N', n_sub, n_sub, mv_env%n_ri_loc, mv_env%alpha, B_Pk, SIZE(B_Pk, 1), B_Pk, SIZE(B_Pk, 1), &
0.0_dp, A_sub, n_sub)
END IF
IF (PRESENT(B_sub)) B_sub(:, :) = A_sub(:, :)
IF (mv_env%w_fac /= 0.0_dp) THEN
DO p = 1, mv_env%n_ri_loc
DO l = 1, n_sub
DO k = 1, n_sub
A_sub(k, l) = A_sub(k, l) - mv_env%w_fac*mv_env%B_bar_ij(i_occ(l), i_occ(k), p)*mv_env%B_ab(a_virt(l), a_virt(k), p)
END DO
END DO
IF (PRESENT(B_sub)) THEN
DO l = 1, n_sub
DO k = 1, n_sub
B_sub(k, l) = B_sub(k, l) - mv_env%w_fac*mv_env%B_ia(a_virt(k), i_occ(l), p)*mv_env%B_bar_ia(a_virt(l), i_occ(k), p)
END DO
END DO
END IF
END DO
END IF
CALL mv_env%para_env%sum(A_sub)
IF (PRESENT(B_sub)) CALL mv_env%para_env%sum(B_sub)
DO k = 1, n_sub
A_sub(k, k) = A_sub(k, k) + mv_env%eps_diff(ia_list(k))
END DO
DEALLOCATE (i_occ, a_virt, B_Pk)
CALL timestop(handle)
END SUBROUTINE bse_matvec_subblock
! **************************************************************************************************
!> \brief Debug check of the matrix-free application against the explicit matrices A (and B),
!> dev = max_ia,k |(A Z)_ia,k - sum_jb A_ia,jb Z_jb,k| over up to eight unit vectors Z_ia,k = δ_ia,k
!> and one dense vector Z_ia = sin(ia), the same for B, and max_ia |d_ia - A_ia,ia| for the diagonal
!> \param mv_env the environment under test
!> \param fm_A_explicit A from create_A_and_B, N_ov x N_ov on the grid of the caller
!> \param unit_nr output unit, positive on the writing rank only
!> \param fm_B_explicit B, present for an ABBA run
! **************************************************************************************************
SUBROUTINE bse_matvec_selfcheck(mv_env, fm_A_explicit, unit_nr, fm_B_explicit)
TYPE(bse_matvec_env_type), INTENT(IN) :: mv_env
TYPE(cp_fm_type), INTENT(IN) :: fm_A_explicit
INTEGER, INTENT(IN) :: unit_nr
TYPE(cp_fm_type), INTENT(IN), OPTIONAL :: fm_B_explicit
CHARACTER(LEN=*), PARAMETER :: routineN = 'bse_matvec_selfcheck'
INTEGER :: handle, ia, iloc, k, n_ov, n_unit, ncol, &
nrow_local
INTEGER, DIMENSION(:), POINTER :: row_indices
REAL(KIND=dp) :: dev_A, dev_B, dev_diag
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: diag
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: mv_result, ref_matrix, ref_result, Z
TYPE(cp_fm_struct_type), POINTER :: fm_struct
TYPE(cp_fm_type) :: fm_AZ, fm_BZ, fm_Z
CALL timeset(routineN, handle)
n_ov = mv_env%n_ov
! eight unit vectors read eight columns of the explicit matrix exactly; the dense column covers the whole contraction
n_unit = MIN(n_ov, 8)
dev_B = 0.0_dp
ncol = n_unit + 1
! trial vectors: e_k for k <= n_unit, then one dense column Z_ia = sin(ia)
ALLOCATE (Z(n_ov, ncol))
Z(:, :) = 0.0_dp
DO k = 1, n_unit
Z(k, k) = 1.0_dp
END DO
DO ia = 1, n_ov
Z(ia, ncol) = SIN(REAL(ia, dp))
END DO
CALL bse_matvec_vector_struct(mv_env, ncol, fm_struct)
CALL cp_fm_create(fm_Z, fm_struct, name="fm_Z_selfcheck")
CALL cp_fm_create(fm_AZ, fm_struct, name="fm_AZ_selfcheck")
CALL cp_fm_set_all(fm_AZ, 0.0_dp)
IF (PRESENT(fm_B_explicit)) THEN
CALL cp_fm_create(fm_BZ, fm_struct, name="fm_BZ_selfcheck")
CALL cp_fm_set_all(fm_BZ, 0.0_dp)
END IF
CALL cp_fm_struct_release(fm_struct)
CALL cp_fm_get_info(fm_Z, nrow_local=nrow_local, row_indices=row_indices)
DO k = 1, ncol
DO iloc = 1, nrow_local
fm_Z%local_data(iloc, k) = Z(row_indices(iloc), k)
END DO
END DO
IF (PRESENT(fm_B_explicit)) THEN
CALL bse_matvec_apply(mv_env, fm_Z, 1, ncol, fm_AZ, fm_BZ)
ELSE
CALL bse_matvec_apply(mv_env, fm_Z, 1, ncol, fm_AZ)
END IF
ALLOCATE (ref_matrix(n_ov, n_ov), mv_result(n_ov, ncol), ref_result(n_ov, ncol), diag(n_ov))
CALL cp_fm_get_submatrix(fm_A_explicit, ref_matrix)
CALL cp_fm_get_submatrix(fm_AZ, mv_result)
ref_result(:, :) = MATMUL(ref_matrix, Z)
dev_A = MAXVAL(ABS(mv_result - ref_result))
CALL bse_matvec_diagonal(mv_env, bse_precond_full_diag, diag)
dev_diag = 0.0_dp
DO ia = 1, n_ov
dev_diag = MAX(dev_diag, ABS(diag(ia) - ref_matrix(ia, ia)))
END DO
IF (PRESENT(fm_B_explicit)) THEN
CALL cp_fm_get_submatrix(fm_B_explicit, ref_matrix)
CALL cp_fm_get_submatrix(fm_BZ, mv_result)
ref_result(:, :) = MATMUL(ref_matrix, Z)
dev_B = MAXVAL(ABS(mv_result - ref_result))
END IF
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A10,T13,A,T59,ES22.6)') 'BSE|DEBUG|', &
'Max deviation matvec vs explicit A', dev_A
IF (PRESENT(fm_B_explicit)) THEN
WRITE (unit_nr, '(T2,A10,T13,A,T59,ES22.6)') 'BSE|DEBUG|', &
'Max deviation matvec vs explicit B', dev_B
END IF
WRITE (unit_nr, '(T2,A10,T13,A,T59,ES22.6)') 'BSE|DEBUG|', &
'Max deviation diagonal vs explicit A', dev_diag
END IF
DEALLOCATE (Z, ref_matrix, mv_result, ref_result, diag)
CALL cp_fm_release(fm_Z)
CALL cp_fm_release(fm_AZ)
IF (PRESENT(fm_B_explicit)) CALL cp_fm_release(fm_BZ)
CALL timestop(handle)
END SUBROUTINE bse_matvec_selfcheck
END MODULE bse_matvec