Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Green functions, screening and self-energy for periodic RI-RS GW.
10 : !> \author Jan Wilhelm (with help of ChatGPT, GPT-6 Sol and GPT-6 Astra)
11 : ! **************************************************************************************************
12 : MODULE gw_ri_rs_small_cell_full_kp_utils
13 : USE cp_blacs_env, ONLY: cp_blacs_env_create,&
14 : cp_blacs_env_release,&
15 : cp_blacs_env_type
16 : USE cp_cfm_basic_linalg, ONLY: cp_cfm_add_on_diag,&
17 : cp_cfm_scale_and_add,&
18 : cp_cfm_transpose,&
19 : cp_cfm_uplo_to_full
20 : USE cp_cfm_cholesky, ONLY: cp_cfm_cholesky_decompose,&
21 : cp_cfm_cholesky_invert
22 : USE cp_cfm_diag, ONLY: cp_cfm_power
23 : USE cp_cfm_types, ONLY: cp_cfm_create,&
24 : cp_cfm_get_submatrix,&
25 : cp_cfm_release,&
26 : cp_cfm_set_submatrix,&
27 : cp_cfm_type
28 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
29 : cp_fm_struct_release,&
30 : cp_fm_struct_type
31 : USE cp_fm_types, ONLY: cp_fm_get_info,&
32 : cp_fm_release,&
33 : cp_fm_type
34 : USE cp_log_handling, ONLY: cp_logger_get_default_io_unit
35 : USE fft_tools, ONLY: BWFFT,&
36 : fft_allocate,&
37 : fft_deallocate
38 : USE gw_ri_rs_small_cell_full_kp_lattice,ONLY: &
39 : assemble_AA_contraction, block_range, build_full_cell_grid, build_ri_2c_lattice, &
40 : complex_bytes, gamma_auxiliary_charges, grid_pair_gemm_panel, grid_pair_indices, &
41 : grid_pair_panel_cols, grid_pair_shift, grid_pair_stencil_denominator, &
42 : grid_pair_stencil_weight, grid_pair_upper_multiply, image_weight, owned_mo_coefficients, &
43 : real_bytes, ri_2c_lattice_to_kp, ri_metric_head_zero
44 : USE gw_ri_rs_small_cell_full_kp_q0_corr,ONLY: add_gamma_corr_pairs,&
45 : gamma_point_correction,&
46 : q_mesh_and_gamma_shell_average,&
47 : singular_head_cell_factors
48 : USE gw_ri_rs_small_cell_full_kp_types,ONLY: gwkp_type,&
49 : ri_q_matrix_block_type,&
50 : sigma_projection_type
51 : USE gw_utils_fm, ONLY: local_complex_power
52 : USE input_constants, ONLY: G0W0,&
53 : evGW0
54 : USE kinds, ONLY: dp,&
55 : int_1,&
56 : int_1_size,&
57 : int_8
58 : USE kpoint_coulomb_2c, ONLY: add_ewald_reciprocal
59 : USE kpoint_lattice_fft, ONLY: &
60 : fft_profile_enabled, k_grid_to_cell_execute, k_grid_to_cell_panel_width, &
61 : k_grid_to_cell_prepare, k_grid_to_cell_release, k_grid_to_cell_work_type, &
62 : lattice_fft_execute_pair_major, lattice_fft_profile_type, lattice_fft_real_sink_type
63 : USE kpoint_transform, ONLY: cell_to_k_transform
64 : USE machine, ONLY: m_flush,&
65 : m_walltime
66 : USE mathconstants, ONLY: fourpi,&
67 : gaussi,&
68 : twopi,&
69 : z_one,&
70 : z_zero
71 : USE message_passing, ONLY: mp_comm_type,&
72 : mp_mem_avail_per_rank_GB,&
73 : mp_para_env_release,&
74 : mp_para_env_type,&
75 : mp_request_type,&
76 : mp_waitall
77 : USE parallel_gemm_api, ONLY: parallel_gemm
78 : USE post_scf_bandstructure_types, ONLY: post_scf_bandstructure_type
79 : USE qs_environment_types, ONLY: qs_environment_type
80 : USE ri_rs_grid_setup, ONLY: setup_ri_rs_grid
81 : USE ri_rs_kp_fitting, ONLY: compute_Z_lP_q,&
82 : evaluate_mu_rl_R_values
83 : USE ri_rs_kp_integrals, ONLY: write_ri_rs_progress
84 : USE ri_rs_utils, ONLY: precompute_ri_rs_radii
85 :
86 : !$ USE omp_lib, ONLY: omp_get_max_threads, omp_get_thread_num
87 : #include "./base/base_uses.f90"
88 :
89 : IMPLICIT NONE
90 : PRIVATE
91 :
92 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_ri_rs_small_cell_full_kp_utils'
93 :
94 : PUBLIC :: compute_Sigma_x, &
95 : compute_g_and_chi, &
96 : compute_W0, &
97 : compute_sigma_c_from_w_pq, &
98 : prepare_ri_rs, &
99 : release_gw_work
100 :
101 : ! Tolerance for a sum of fractional k-point coordinates to be a reciprocal lattice vector.
102 : REAL(KIND=dp), PARAMETER :: eps_kpoint = 1.0E-8_dp
103 :
104 : ! Blocking limits and named slots in the six existing CP2K matrix workspaces.
105 : INTEGER, PARAMETER :: column_block_cols = 32
106 : INTEGER, PARAMETER :: band_kpoints_per_pass = 512, pair_batch_size = 256, sigma_rhs_cols = 64
107 : INTEGER, PARAMETER :: w_chi = 1, w_metric = 2, w_tmp = 3, w_dielectric = 4, &
108 : w_output = 5, n_w_matrices = 5
109 :
110 : ! Memory estimates: bytes per MiB and per GB (mp_mem_avail_per_rank_GB).
111 : REAL(KIND=dp), PARAMETER :: bytes_per_MiB = 1024.0_dp**2, bytes_per_GB = 1.0E9_dp
112 : ! A single stage may use stage_memory_share of the available memory. Stages that keep earlier
113 : ! data alive reserve reserve_share of it (at least min_reserve_GB) and add library_bytes.
114 : REAL(KIND=dp), PARAMETER :: stage_memory_share = 0.5_dp, reserve_share = 0.1_dp, &
115 : min_reserve_GB = 1.0_dp, library_bytes = 256.0_dp*bytes_per_MiB
116 : ! Workspace limits for AO-image panels, received chi batches, and Σ(k) exchange rounds.
117 : REAL(KIND=dp), PARAMETER :: projection_workspace = 64.0_dp*bytes_per_MiB, &
118 : projection_round_bytes = 512.0_dp*bytes_per_MiB
119 : INTEGER(KIND=int_8), PARAMETER :: fft_panel_bytes = 32_int_8*1024_int_8**2
120 :
121 : ! One stencil representation: signed bytes with denominator 64 and a shared cell phase.
122 : TYPE gw_pair_fft_batch_type
123 : INTEGER(KIND=int_1), ALLOCATABLE :: weight(:, :)
124 : COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: input(:) => NULL()
125 : END TYPE gw_pair_fft_batch_type
126 :
127 : TYPE pair_segment_type
128 : INTEGER :: rank = -1, first_row = 0, last_row = 0, displacement = 0
129 : END TYPE pair_segment_type
130 :
131 : TYPE gw_pair_fft_work_type
132 : PRIVATE
133 : INTEGER :: n_pairs = 0, width = 0, k_batch = 0, green_reuses = 0, nmesh(3) = 0
134 : INTEGER(KIND=int_8) :: nfft_grid = 0_int_8, spectrum_columns = 0_int_8
135 : LOGICAL :: cache_hit = .FALSE.
136 : COMPLEX(KIND=dp), ALLOCATABLE :: product_phase(:)
137 : INTEGER, ALLOCATABLE :: grid_linear(:), partner_linear(:), extract_q(:), extract_partner(:), mirror_cell(:)
138 : INTEGER, ALLOCATABLE :: input_to_native(:), green_source(:), pair_counts(:), pair_displs(:), column_segments(:)
139 : INTEGER, ALLOCATABLE :: q_source(:), q_slot(:)
140 : TYPE(pair_segment_type), ALLOCATABLE :: segments(:)
141 : LOGICAL, ALLOCATABLE :: diagonal(:)
142 : TYPE(k_grid_to_cell_work_type) :: inverse
143 : TYPE(gw_pair_fft_batch_type), ALLOCATABLE :: batches(:)
144 : TYPE(lattice_fft_profile_type) :: profile = lattice_fft_profile_type()
145 : REAL(KIND=dp) :: setup_seconds = 0.0_dp
146 : END TYPE gw_pair_fft_work_type
147 :
148 : TYPE, EXTENDS(lattice_fft_real_sink_type) :: gw_pair_extraction_type
149 : LOGICAL :: product = .FALSE.
150 : REAL(KIND=dp) :: scale = 1.0_dp
151 : REAL(KIND=dp), ALLOCATABLE :: norms(:, :)
152 : REAL(KIND=dp), CONTIGUOUS, POINTER :: values(:, :, :) => NULL(), multiply(:, :, :) => NULL()
153 : TYPE(gw_pair_fft_work_type), POINTER :: fft => NULL()
154 : CONTAINS
155 : PROCEDURE :: consume => extract_grid_pairs
156 : END TYPE gw_pair_extraction_type
157 :
158 : CONTAINS
159 :
160 : ! **************************************************************************************************
161 : !> \brief Release the inverse FFT workspace and the aligned inputs of the product panels.
162 : !> \param work ...
163 : ! **************************************************************************************************
164 42 : SUBROUTINE release_pair_fft(work)
165 : TYPE(gw_pair_fft_work_type), INTENT(INOUT) :: work
166 :
167 : INTEGER :: ibatch
168 :
169 42 : CALL k_grid_to_cell_release(work%inverse)
170 42 : IF (ALLOCATED(work%batches)) THEN
171 107 : DO ibatch = 1, SIZE(work%batches)
172 107 : IF (ASSOCIATED(work%batches(ibatch)%input)) CALL fft_deallocate(work%batches(ibatch)%input)
173 : END DO
174 0 : DEALLOCATE (work%batches, work%product_phase, work%grid_linear, work%partner_linear, &
175 0 : work%extract_q, work%extract_partner, work%mirror_cell, work%diagonal, work%input_to_native, &
176 107 : work%green_source, work%pair_counts, work%pair_displs, work%column_segments, work%segments)
177 : END IF
178 42 : IF (ALLOCATED(work%q_source)) DEALLOCATE (work%q_source, work%q_slot)
179 588 : work%profile = lattice_fft_profile_type()
180 42 : work%setup_seconds = 0.0_dp
181 42 : work%cache_hit = .FALSE.
182 42 : work%green_reuses = 0
183 42 : END SUBROUTINE release_pair_fft
184 :
185 : ! **************************************************************************************************
186 : !> \brief Consume K2R spectra once: original Green norms, physical output and next-FFT input.
187 : !> Chi stores its first Green in the input panel and replaces it in place with the weighted
188 : !> product on the second call. Self-energy retains the physical product for band interpolation.
189 : !> \param sink extraction recipe and result
190 : !> \param first first local pair in this FFT panel
191 : !> \param nbatch physical pairs in this panel
192 : !> \param spectrum pair-major FFT output
193 : !> \param cell_slot FFT slot of each physical cell
194 : !> \param cell_phase common inverse-transform phase and normalization
195 : ! **************************************************************************************************
196 930 : SUBROUTINE extract_grid_pairs(sink, first, nbatch, spectrum, cell_slot, cell_phase)
197 : CLASS(gw_pair_extraction_type), INTENT(INOUT) :: sink
198 : INTEGER(KIND=int_8), INTENT(IN) :: first
199 : INTEGER, INTENT(IN) :: nbatch, cell_slot(:)
200 : COMPLEX(KIND=dp), INTENT(IN) :: spectrum(:, :), cell_phase(:)
201 :
202 : COMPLEX(KIND=dp) :: input_phase, phase, raw, saved
203 930 : COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: input(:, :)
204 : INTEGER :: fft_count, i, ia, ib, icell, ipair, jpair, slot
205 : REAL(KIND=dp) :: a, b, norm, offnorm, product_a, product_b
206 : LOGICAL :: collect_norm
207 : TYPE(gw_pair_fft_batch_type), POINTER :: batch
208 : TYPE(gw_pair_fft_work_type), POINTER :: plan
209 :
210 930 : fft_count = (nbatch + 1)/2
211 930 : IF (.NOT. sink%two_real) THEN
212 0 : CPASSERT(nbatch == 1)
213 : END IF
214 930 : NULLIFY (input, batch)
215 930 : collect_norm = ALLOCATED(sink%norms)
216 930 : plan => sink%fft
217 930 : IF (ASSOCIATED(plan)) THEN
218 729 : CPASSERT(MODULO(INT(first) - 1, plan%width) == 0)
219 729 : batch => plan%batches((INT(first) - 1)/plan%width + 1)
220 729 : input(1:fft_count, 1:INT(plan%nfft_grid)) => batch%input
221 : END IF
222 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
223 : !$OMP SHARED(sink, first, nbatch, fft_count, spectrum, cell_slot, cell_phase, plan, batch, input) &
224 : !$OMP SHARED(collect_norm) &
225 : !$OMP PRIVATE(icell, slot, phase, i, ia, ib, ipair, jpair, raw, saved, A, b, product_a, product_b, norm, offnorm) &
226 930 : !$OMP PRIVATE(input_phase)
227 : DO icell = 1, SIZE(cell_slot)
228 : slot = cell_slot(icell)
229 : phase = MERGE(0.5_dp, 1.0_dp, sink%two_real)*cell_phase(icell)
230 : norm = 0.0_dp
231 : offnorm = 0.0_dp
232 : IF (sink%product) input_phase = plan%product_phase(icell)
233 : !$OMP SIMD REDUCTION(+:norm, offnorm) &
234 : !$OMP PRIVATE(ia, ib, ipair, jpair, raw, saved, A, b, product_a, product_b)
235 : DO i = 1, fft_count
236 : ia = 2*i - 1
237 : ib = MIN(2*i, nbatch)
238 : ipair = INT(first) + ia - 1
239 : jpair = INT(first) + ib - 1
240 : raw = phase*spectrum(i, slot)
241 : a = REAL(raw, dp)
242 : b = 0.0_dp
243 : IF (ib > ia) b = AIMAG(raw)
244 : IF (collect_norm) THEN
245 : norm = norm + a*a + b*b
246 : IF (.NOT. plan%diagonal(ipair)) offnorm = offnorm + a*a
247 : IF (ib > ia) THEN
248 : IF (.NOT. plan%diagonal(jpair)) offnorm = offnorm + b*b
249 : END IF
250 : END IF
251 : product_a = a
252 : product_b = b
253 : IF (ASSOCIATED(sink%multiply)) THEN
254 : product_a = (sink%scale*a)*sink%multiply(ipair, 1, icell)
255 : IF (ib > ia) product_b = (sink%scale*b)*sink%multiply(jpair, 1, icell)
256 : ELSE IF (sink%product) THEN
257 : saved = input(i, slot)
258 : product_a = (sink%scale*REAL(saved, dp))*a
259 : product_b = (sink%scale*AIMAG(saved))*b
260 : END IF
261 : IF (ASSOCIATED(sink%values)) THEN
262 : sink%values(ipair, 1, icell) = product_a
263 : IF (ib > ia) sink%values(jpair, 1, icell) = product_b
264 : END IF
265 : IF (.NOT. ASSOCIATED(plan)) CYCLE
266 : IF (.NOT. sink%product) THEN
267 : input(i, slot) = CMPLX(a, b, dp)
268 : CYCLE
269 : END IF
270 : input(i, slot) = input_phase* &
271 : CMPLX(REAL(batch%weight(ia, icell), dp)*product_a, REAL(batch%weight(ib, icell), dp)*product_b, dp)
272 : END DO
273 : IF (collect_norm) THEN
274 : sink%norms(1, icell) = sink%norms(1, icell) + norm
275 : sink%norms(2, icell) = sink%norms(2, icell) + offnorm
276 : END IF
277 : END DO
278 : !$OMP END PARALLEL DO
279 930 : END SUBROUTINE extract_grid_pairs
280 :
281 : ! **************************************************************************************************
282 : !> \brief Single GW K2R pipeline for unordered pairs and fused product consumers.
283 : !> The squared Frobenius norm counts each upper pair at R and its missing lower partner at -R;
284 : !> a diagonal is counted once. The MP boundary twist drops out of this squared norm.
285 : !> \param values_k unordered pairs on the input mesh
286 : !> \param work shared lattice geometry and next-FFT input
287 : !> \param values_R optional physical field or product required by a later consumer
288 : !> \param multiply_R second physical field for the self-energy product
289 : !> \param product_scale finish the R2K stencil using multiply_R or the saved chi Green
290 : !> \param norm_squared original full-matrix squared norm per cell
291 : ! **************************************************************************************************
292 788 : SUBROUTINE k_to_grid_pairs(values_k, work, values_R, multiply_R, product_scale, norm_squared)
293 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(IN) :: values_k(:, :, :)
294 : TYPE(gw_pair_fft_work_type), INTENT(INOUT), TARGET :: work
295 : REAL(KIND=dp), CONTIGUOUS, INTENT(INOUT), &
296 : OPTIONAL, TARGET :: values_R(:, :, :)
297 : REAL(KIND=dp), CONTIGUOUS, INTENT(IN), OPTIONAL, &
298 : TARGET :: multiply_R(:, :, :)
299 : REAL(KIND=dp), INTENT(IN), OPTIONAL :: product_scale
300 : REAL(KIND=dp), INTENT(OUT), OPTIONAL :: norm_squared(:)
301 :
302 : CHARACTER(LEN=14), PARAMETER :: stages(9) = [CHARACTER(LEN=14) :: 'initialize', 'geometry', &
303 : 'buffers', 'grid-fill', 'FFT-X/plan', 'FFT-Y/plan', 'FFT-Z/plan', 'extract', 'release']
304 :
305 : INTEGER :: i, n_cells, unit_nr
306 : REAL(KIND=dp) :: total_started
307 788 : TYPE(gw_pair_extraction_type) :: sink
308 788 : TYPE(lattice_fft_profile_type), ALLOCATABLE :: profile
309 :
310 : IF (fft_profile_enabled) total_started = m_walltime()
311 788 : CPASSERT(ALLOCATED(work%batches))
312 788 : CPASSERT(SIZE(values_k, 1) == work%n_pairs .AND. SIZE(values_k, 2) == 1)
313 788 : n_cells = SIZE(work%inverse%cell_slot)
314 788 : sink%product = PRESENT(product_scale)
315 788 : IF (PRESENT(product_scale)) sink%scale = product_scale
316 788 : IF (sink%product .OR. .NOT. PRESENT(values_R)) sink%fft => work
317 470 : IF (PRESENT(values_R)) THEN
318 1936 : CPASSERT(ALL(SHAPE(values_R) == [work%n_pairs, 1, n_cells]))
319 484 : sink%values => values_R
320 : END IF
321 788 : IF (PRESENT(multiply_R)) THEN
322 318 : CPASSERT(sink%product)
323 1272 : CPASSERT(ALL(SHAPE(multiply_R) == [work%n_pairs, 1, n_cells]))
324 318 : sink%multiply => multiply_R
325 : END IF
326 788 : IF (PRESENT(norm_squared)) THEN
327 608 : CPASSERT(ASSOCIATED(sink%fft))
328 608 : CPASSERT(SIZE(norm_squared) == n_cells)
329 44544 : ALLOCATE (sink%norms(2, n_cells), SOURCE=0.0_dp)
330 : END IF
331 : IF (fft_profile_enabled) ALLOCATE (profile)
332 : CALL k_grid_to_cell_execute(work%inverse, values_k, require_fft=.TRUE., profile=profile, &
333 788 : max_workspace_bytes=fft_panel_bytes, real_sink=sink)
334 788 : IF (PRESENT(norm_squared)) THEN
335 29088 : norm_squared = sink%norms(1, :) + sink%norms(2, work%mirror_cell)
336 : END IF
337 788 : unit_nr = 0
338 : IF (fft_profile_enabled) unit_nr = cp_logger_get_default_io_unit()
339 : IF (unit_nr > 0) THEN
340 : WRITE (unit_nr, '(T2,A)') 'K2R_FFT_PROFILE| Unordered grid pairs; logger-rank wall seconds, no MPI reduction'
341 : WRITE (unit_nr, '(T2,A,3I10)') 'K2R_FFT_PROFILE| source shape: ', SHAPE(values_k)
342 : WRITE (unit_nr, '(T2,A,6I8)') 'K2R_FFT_PROFILE| grid / padded: ', work%nmesh, profile%nfft
343 : WRITE (unit_nr, '(T2,A,L2)') 'K2R_FFT_PROFILE| two real outputs per complex FFT: ', profile%two_real
344 : WRITE (unit_nr, '(T2,A,L2)') 'K2R_FFT_PROFILE| extraction feeds next FFT input: ', ASSOCIATED(sink%fft)
345 : WRITE (unit_nr, '(T2,A,2I16)') 'K2R_FFT_PROFILE| FFT transforms / real outputs: ', profile%transforms, profile%elements
346 : WRITE (unit_nr, '(T2,A,2I16)') 'K2R_FFT_PROFILE| complex FFT elements / full: ', &
347 : PRODUCT(INT(profile%nfft, int_8))*profile%transforms, PRODUCT(INT(profile%nfft, int_8))*profile%elements
348 : WRITE (unit_nr, '(T2,A,2I16,2I8)') 'K2R_FFT_PROFILE| elements/batches/tile min/max: ', &
349 : profile%elements, profile%batches, profile%tile_min, profile%tile_max
350 : WRITE (unit_nr, '(T2,A,L2)') 'K2R_FFT_PROFILE| pair-major FFT input/output: ', profile%pair_major
351 : WRITE (unit_nr, '(T2,A)') 'K2R_FFT_PROFILE| FFT-X/plan reports full 3D batched FFT (Y/Z entries zero)'
352 : DO i = 1, SIZE(stages)
353 : WRITE (unit_nr, '(T2,A,A14,F14.6)') 'K2R_FFT_PROFILE| ', stages(i), profile%seconds(i)
354 : END DO
355 : WRITE (unit_nr, '(T2,A,F14.6)') 'K2R_FFT_PROFILE| total ', m_walltime() - total_started
356 : END IF
357 788 : END SUBROUTINE k_to_grid_pairs
358 :
359 : ! **************************************************************************************************
360 : !> \brief Prepare RI-RS radial data, grid values, and Bloch orbitals.
361 : !> \param bs_env Band-structure settings and physical results.
362 : !> \param gwkp_env Temporary periodic GW work data.
363 : ! **************************************************************************************************
364 14 : SUBROUTINE prepare_ri_rs(bs_env, gwkp_env)
365 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
366 : TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
367 :
368 : CHARACTER(LEN=*), PARAMETER :: routineN = 'prepare_ri_rs'
369 :
370 14 : COMPLEX(dp), ALLOCATABLE :: phi_lmu_k(:, :, :)
371 : INTEGER :: handle
372 :
373 14 : CALL timeset(routineN, handle)
374 :
375 : CALL precompute_ri_rs_radii(bs_env%ri_rs, bs_env%basis_set_AO, bs_env%basis_set_RI, &
376 14 : bs_env%eps_filter, bs_env%unit_nr)
377 14 : CALL setup_ri_rs_grid(bs_env, bs_env%ri_rs%grid_points)
378 14 : CALL minus_q_partners(bs_env%kpoints_chi_eps_W%xkp(:, :bs_env%nkp_scf_desymm), gwkp_env%q_partner)
379 14 : CALL compact_q_slots(gwkp_env%q_partner, bs_env%para_env%num_pe, gwkp_env%q_slot)
380 : CALL compute_Z_lP_q(bs_env, bs_env%ri_rs%grid_points, gwkp_env%q_slot, &
381 14 : Z_lP_q=gwkp_env%Z_lP_q, Z_lP_zero=gwkp_env%Z_lP_zero)
382 14 : CALL build_all_phi_k(bs_env, phi_lmu_k)
383 14 : CALL build_all_psi_k(bs_env, phi_lmu_k, gwkp_env%psi_lm_k)
384 14 : DEALLOCATE (phi_lmu_k)
385 14 : IF (bs_env%gw_flavour == evGW0) THEN
386 0 : CPASSERT(ALLOCATED(gwkp_env%eigenvalues_G))
387 0 : CPASSERT(ALLOCATED(bs_env%eigenval_evGW0))
388 0 : CPASSERT(SIZE(bs_env%eigenval_scf, 2) >= bs_env%nkp_scf_desymm)
389 0 : bs_env%eigenval_evGW0(:, :, :) = bs_env%eigenval_scf
390 : END IF
391 :
392 14 : CALL timestop(handle)
393 :
394 14 : END SUBROUTINE prepare_ri_rs
395 :
396 : ! **************************************************************************************************
397 : !> \brief Form the grid Green functions and RI polarizability.
398 : !> \param qs_env Quickstep data required by existing integral/lifecycle interfaces.
399 : !> \param bs_env Band-structure settings and physical results.
400 : !> \param gwkp_env Temporary periodic GW work data.
401 : ! **************************************************************************************************
402 14 : SUBROUTINE compute_G_and_chi(qs_env, bs_env, gwkp_env)
403 : TYPE(qs_environment_type), POINTER :: qs_env
404 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
405 : TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
406 :
407 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_G_and_chi'
408 :
409 : INTEGER :: handle
410 :
411 14 : CALL timeset(routineN, handle)
412 :
413 14 : CPASSERT(.NOT. ASSOCIATED(gwkp_env%group_env))
414 14 : gwkp_env%group_env => bs_env%para_env
415 14 : CALL gwkp_env%group_env%retain()
416 14 : gwkp_env%ngroups = 1
417 14 : gwkp_env%my_group = 0
418 :
419 14 : CALL gamma_point_correction(bs_env, qs_env, gwkp_env)
420 14 : CPASSERT(.NOT. ALLOCATED(gwkp_env%band_orbitals))
421 14 : CALL compute_chi(bs_env, gwkp_env)
422 :
423 14 : CALL timestop(handle)
424 :
425 14 : END SUBROUTINE compute_G_and_chi
426 :
427 : ! **************************************************************************************************
428 : !> \brief Compute ε, Wᶜ and its imaginary-time transform, Eqs. (7)-(8).
429 : !> \param qs_env Required by the existing two-centre integral interfaces.
430 : !> \param bs_env Band-structure settings and physical results.
431 : !> \param gwkp_env Periodic GW work data.
432 : ! **************************************************************************************************
433 14 : SUBROUTINE compute_W0(qs_env, bs_env, gwkp_env)
434 : TYPE(qs_environment_type), POINTER :: qs_env
435 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
436 : TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
437 :
438 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_W0'
439 :
440 : COMPLEX(KIND=dp), ALLOCATABLE :: chi_k_w(:, :), MWM_k_w(:, :)
441 : INTEGER :: handle, i_batch, i_group_size, ikp, &
442 : ikp_local, j_w, my_qgroup, n_qgroups, &
443 : n_RI, nkp, nkp_local, q_group, &
444 : q_owner, q_root
445 14 : INTEGER, ALLOCATABLE :: group_load(:), q_batch(:, :), q_roots(:)
446 : REAL(KIND=dp) :: available_GB, required_bytes, step_start
447 14 : REAL(KIND=dp), ALLOCATABLE :: frequency_elapsed(:)
448 : TYPE(cp_blacs_env_type), POINTER :: w_blacs
449 84 : TYPE(cp_cfm_type) :: w_matrices(n_w_matrices)
450 : TYPE(cp_fm_struct_type), POINTER :: w_matrix_struct
451 : TYPE(mp_para_env_type), POINTER :: w_group_env
452 :
453 14 : CALL timeset(routineN, handle)
454 :
455 14 : CALL compute_metric_coulomb_factor(bs_env, qs_env, gwkp_env)
456 : ! Σˣ and Σᶜ use the band-path orbitals, which include the SCF mesh.
457 14 : DEALLOCATE (gwkp_env%psi_lm_k)
458 14 : n_RI = bs_env%n_RI
459 14 : nkp = bs_env%nkp_scf_desymm
460 14 : IF (bs_env%nkp_chi_eps_W_orig_plus_extra /= nkp) THEN
461 0 : CPABORT("Periodic RI-RS GW requires the complete SCF transfer mesh")
462 : END IF
463 56 : IF (ANY(bs_env%nkp_grid_chi_eps_W_orig /= &
464 : bs_env%kpoints_scf_desymm%nkp_grid)) THEN
465 0 : CPABORT("Periodic RI-RS GW requires the SCF mesh dimensions for W")
466 : END IF
467 14 : nkp_local = SIZE(gwkp_env%metric_coulomb_factor)
468 : ! Distributed solver matrices plus equal workspace reserve; owner buffers remain local.
469 : ! Select the smallest feasible divisor, independently of the number of q points.
470 14 : CALL mp_mem_avail_per_rank_GB(bs_env%para_env, available_GB)
471 14 : DO i_group_size = 1, bs_env%para_env%num_pe
472 14 : IF (MODULO(bs_env%para_env%num_pe, i_group_size) /= 0) CYCLE
473 : required_bytes = REAL(n_RI, dp)**2*(2.0_dp*complex_bytes + &
474 : 2.0_dp*complex_bytes*n_w_matrices/REAL(i_group_size, dp)) + &
475 14 : complex_bytes*REAL(n_RI, dp)*REAL(column_block_cols, dp)
476 14 : IF (required_bytes <= stage_memory_share*MAX(0.0_dp, available_GB)*bytes_per_GB) EXIT
477 : END DO
478 14 : IF (i_group_size > bs_env%para_env%num_pe) THEN
479 0 : CPABORT('Insufficient node memory for the distributed GW W(q) solver')
480 : END IF
481 14 : n_qgroups = bs_env%para_env%num_pe/i_group_size
482 14 : CALL grouped_w_partition(bs_env%para_env, n_qgroups, my_qgroup, q_roots)
483 : ! Store W only at the first q of each inversion pair. Its conjugate is
484 : ! reconstructed after projecting to grid pairs in the correlation step.
485 42 : ALLOCATE (group_load(0:n_qgroups - 1), SOURCE=0)
486 262 : DO ikp = 1, nkp
487 262 : IF (gwkp_env%q_slot(ikp) > 0) group_load(MODULO(ikp, n_qgroups)) = group_load(MODULO(ikp, n_qgroups)) + 1
488 : END DO
489 84 : ALLOCATE (q_batch(MAXVAL(group_load), 0:n_qgroups - 1), SOURCE=0)
490 14 : group_load(:) = 0
491 262 : DO ikp = 1, nkp
492 248 : IF (gwkp_env%q_slot(ikp) == 0) CYCLE
493 126 : q_group = MODULO(ikp, n_qgroups)
494 126 : group_load(q_group) = group_load(q_group) + 1
495 262 : q_batch(group_load(q_group), q_group) = ikp
496 : END DO
497 105 : ALLOCATE (gwkp_env%W_PQ_tau(nkp_local))
498 14 : IF (nkp_local > 0 .OR. bs_env%para_env%mepos == q_roots(my_qgroup + 1)) THEN
499 84 : ALLOCATE (chi_k_w(n_RI, n_RI), MWM_k_w(n_RI, n_RI))
500 : ELSE
501 0 : ALLOCATE (chi_k_w(0, 0), MWM_k_w(0, 0))
502 : END IF
503 :
504 14 : ALLOCATE (w_group_env)
505 14 : CALL w_group_env%from_split(bs_env%para_env, my_qgroup)
506 14 : CALL grouped_w_create(w_matrices, w_blacs, w_matrix_struct, w_group_env, n_RI)
507 42 : ALLOCATE (frequency_elapsed(bs_env%num_time_freq_points), SOURCE=0.0_dp)
508 : ! Each group holds one q at a time, reusing K(q) for all frequencies. Group MODULO(q, n_qgroups)
509 : ! handles q, so with one rank per group the owner of q solves it without communication.
510 : ! Ordered world transfers also cover incomplete batches and idle groups.
511 78 : DO i_batch = 1, SIZE(q_batch, 1)
512 192 : DO q_group = 0, n_qgroups - 1
513 128 : ikp = q_batch(i_batch, q_group)
514 128 : IF (ikp == 0) CYCLE
515 126 : q_root = q_roots(q_group + 1)
516 126 : q_owner = MODULO(ikp, bs_env%para_env%num_pe)
517 126 : ikp_local = gwkp_env%q_slot(ikp)
518 126 : IF (bs_env%para_env%mepos == q_owner) THEN
519 : CALL grouped_w_copy_matrix(bs_env%para_env, n_RI, q_owner, q_root, &
520 63 : gwkp_env%metric_coulomb_factor(ikp_local)%values(:, :, 1), chi_k_w)
521 0 : ALLOCATE (gwkp_env%W_PQ_tau(ikp_local)%values( &
522 266227 : n_RI, n_RI, bs_env%num_time_freq_points), SOURCE=z_zero)
523 : ELSE
524 63 : CALL grouped_w_copy_matrix(bs_env%para_env, n_RI, q_owner, q_root, MWM_k_w, chi_k_w)
525 : END IF
526 190 : IF (q_group == my_qgroup) THEN
527 63 : CALL set_owner_matrix(w_matrices(w_metric), chi_k_w, w_group_env, 0, n_RI)
528 : END IF
529 : END DO
530 1872 : DO j_w = 1, bs_env%num_time_freq_points
531 1808 : step_start = m_walltime()
532 5424 : DO q_group = 0, n_qgroups - 1
533 3616 : ikp = q_batch(i_batch, q_group)
534 3616 : IF (ikp == 0) CYCLE
535 3600 : q_owner = MODULO(ikp, bs_env%para_env%num_pe)
536 3600 : ikp_local = gwkp_env%q_slot(ikp)
537 3600 : IF (bs_env%para_env%mepos == q_owner) THEN
538 265912 : MWM_k_w(:, :) = gwkp_env%chi_q_frequency(ikp_local)%values(:, :, j_w)
539 : ! The RI fit gives occupied-virtual pair densities a spurious charge m_ia that makes
540 : ! ε_00 - 1 ~ C(ω)/q and the q sum log-divergent. As in the Γ model (P = 1 - c c^H),
541 : ! remove it from the fitted coefficients, d' = d - (c/|c|) m, i.e. Z^H ρ' = Z^H ρ - e m:
542 : ! χ' = χ - e X^H - X e^H + L0 e e^H. m and d are taken at q = 0, so the physical
543 : ! O(q) dipole charge is kept; the error is O(q).
544 : CALL zgerc(n_RI, n_RI, CMPLX(gwkp_env%leak_l0(j_w), 0.0_dp, dp), &
545 : gwkp_env%leak_e(:, ikp_local), 1, gwkp_env%leak_e(:, ikp_local), 1, &
546 1800 : MWM_k_w, n_RI)
547 : CALL zgerc(n_RI, n_RI, -z_one, gwkp_env%leak_e(:, ikp_local), 1, &
548 1800 : gwkp_env%leak_x(:, j_w, ikp_local), 1, MWM_k_w, n_RI)
549 : CALL zgerc(n_RI, n_RI, -z_one, gwkp_env%leak_x(:, j_w, ikp_local), 1, &
550 1800 : gwkp_env%leak_e(:, ikp_local), 1, MWM_k_w, n_RI)
551 : END IF
552 5424 : CALL grouped_w_copy_matrix(bs_env%para_env, n_RI, q_owner, q_roots(q_group + 1), MWM_k_w, chi_k_w)
553 : END DO
554 1808 : IF (q_batch(i_batch, my_qgroup) > 0) THEN
555 : CALL grouped_w_solve(w_matrices, w_group_env, n_RI, 0, chi_k_w, &
556 1800 : bs_env%eps_eigval_mat_RI, MWM_k_w)
557 : END IF
558 5424 : DO q_group = 0, n_qgroups - 1
559 3616 : ikp = q_batch(i_batch, q_group)
560 3616 : IF (ikp == 0) CYCLE
561 3600 : q_owner = MODULO(ikp, bs_env%para_env%num_pe)
562 3600 : ikp_local = gwkp_env%q_slot(ikp)
563 3600 : CALL grouped_w_copy_matrix(bs_env%para_env, n_RI, q_roots(q_group + 1), q_owner, MWM_k_w, chi_k_w)
564 5408 : IF (bs_env%para_env%mepos == q_owner) THEN
565 265912 : gwkp_env%W_PQ_tau(ikp_local)%values(:, :, j_w) = chi_k_w
566 1800 : IF (j_w == bs_env%num_time_freq_points) DEALLOCATE (gwkp_env%chi_q_frequency(ikp_local)%values)
567 : END IF
568 : END DO
569 1872 : frequency_elapsed(j_w) = frequency_elapsed(j_w) + m_walltime() - step_start
570 : END DO
571 206 : DO q_group = 0, n_qgroups - 1
572 128 : ikp = q_batch(i_batch, q_group)
573 128 : IF (ikp == 0) CYCLE
574 126 : IF (bs_env%para_env%mepos /= MODULO(ikp, bs_env%para_env%num_pe)) CYCLE
575 : CALL cosine_transform_ri_q(gwkp_env%W_PQ_tau(gwkp_env%q_slot(ikp)), &
576 192 : bs_env%time_frequency_grid%cosine_frequency_to_time)
577 : END DO
578 : END DO
579 14 : IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, '(A)') ''
580 318 : DO j_w = 1, bs_env%num_time_freq_points
581 304 : CALL bs_env%para_env%max(frequency_elapsed(j_w))
582 318 : IF (bs_env%unit_nr > 0) THEN
583 : WRITE (bs_env%unit_nr, '(T2,A,I3,A,I0,T56,A,T70,F10.1,A)') &
584 152 : 'GW W(iω) point ', j_w, ' / ', bs_env%num_time_freq_points, &
585 304 : 'Exec. time:', frequency_elapsed(j_w), ' s'
586 152 : CALL m_flush(bs_env%unit_nr)
587 : END IF
588 : END DO
589 14 : DEALLOCATE (frequency_elapsed)
590 :
591 14 : CALL grouped_w_release(w_matrices, w_matrix_struct, w_blacs)
592 14 : CALL mp_para_env_release(w_group_env)
593 :
594 : ! Release frequency-domain matrices after constructing W(q,tau).
595 14 : DEALLOCATE (chi_k_w, MWM_k_w)
596 :
597 14 : CPASSERT(gwkp_env%ngroups == 1 .AND. gwkp_env%my_group == 0)
598 14 : CPASSERT(gwkp_env%group_env%num_pe == bs_env%para_env%num_pe)
599 14 : CPASSERT(gwkp_env%group_env%mepos == bs_env%para_env%mepos)
600 :
601 14 : DEALLOCATE (group_load, q_batch, gwkp_env%leak_e, gwkp_env%leak_x, gwkp_env%leak_l0)
602 77 : IF (ALLOCATED(gwkp_env%chi_q_frequency)) DEALLOCATE (gwkp_env%chi_q_frequency)
603 :
604 14 : CALL timestop(handle)
605 :
606 28 : END SUBROUTINE compute_W0
607 :
608 : ! **************************************************************************************************
609 : !> \brief Evaluate exchange and prepare band orbitals for correlation.
610 : !> \param bs_env Band-structure settings and physical results.
611 : !> \param gwkp_env Temporary periodic GW work data.
612 : ! **************************************************************************************************
613 14 : SUBROUTINE compute_Sigma_x(bs_env, gwkp_env)
614 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
615 : TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
616 :
617 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_Sigma_x'
618 :
619 : INTEGER :: handle
620 :
621 14 : CALL timeset(routineN, handle)
622 :
623 14 : CALL collect_band_bloch_orbitals(bs_env, gwkp_env%band_orbitals)
624 14 : CALL prepare_sigma_projection(bs_env, bs_env%para_env, gwkp_env%band_orbitals, gwkp_env%sigma_projection)
625 14 : CALL compute_Sigma_x_from_V(bs_env, gwkp_env)
626 14 : CALL prepare_self_energy_groups(bs_env, gwkp_env)
627 14 : IF (gwkp_env%ngroups > 1) THEN
628 14 : DEALLOCATE (gwkp_env%sigma_projection%partner_orbitals)
629 14 : CALL band_orbitals_to_groups(bs_env, gwkp_env)
630 14 : CALL prepare_sigma_projection(bs_env, gwkp_env%group_env, gwkp_env%band_orbitals, gwkp_env%sigma_projection)
631 : END IF
632 :
633 14 : CALL timestop(handle)
634 :
635 14 : END SUBROUTINE compute_Sigma_x
636 :
637 : ! **************************************************************************************************
638 : !> \brief Retain Z and fixed W in the smallest memory-feasible time groups.
639 : !>
640 : !> Each group owns τ = group + 1, group + 1 + ngroups, ... . Within a group,
641 : !> Z and W retain the representative q points, in native order within owner MOD(q,g).
642 : !> \param bs_env Band-structure settings.
643 : !> \param gwkp_env Work arrays and their MPI distribution.
644 : ! **************************************************************************************************
645 14 : SUBROUTINE prepare_self_energy_groups(bs_env, gwkp_env)
646 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
647 : TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
648 :
649 : CHARACTER(LEN=*), PARAMETER :: routineN = 'prepare_self_energy_groups'
650 :
651 14 : COMPLEX(KIND=dp), ALLOCATABLE :: Z_group(:, :, :)
652 : INTEGER :: handle, i_dest, i_group_size, i_root, i_source, i_tag, iq, iq_local, it, &
653 : it_local, n_ao, n_aux, n_grid, n_images, n_jobs, n_k, n_local_k, n_local_q, n_local_time, &
654 : n_offmesh, n_q_exchange, n_spin, n_stored_q, n_threads, n_time, n_world
655 : REAL(KIND=dp) :: available_GB, block_bytes, budget_bytes, grid_bytes, input_bytes, n_pairs, &
656 : peak_bytes, persistent_bytes, result_bytes, sigma_geometry_bytes
657 : TYPE(mp_comm_type) :: column_env
658 14 : TYPE(ri_q_matrix_block_type), ALLOCATABLE :: W_group(:)
659 :
660 14 : CALL timeset(routineN, handle)
661 :
662 : n_threads = 1
663 14 : !$ n_threads = omp_get_max_threads()
664 14 : n_world = bs_env%para_env%num_pe
665 14 : n_grid = bs_env%ri_rs%n_grid_points
666 14 : n_ao = bs_env%n_ao
667 14 : n_spin = bs_env%n_spin
668 14 : n_aux = bs_env%n_RI
669 14 : n_k = bs_env%nkp_scf_desymm
670 14 : n_time = bs_env%num_time_freq_points
671 42 : n_jobs = SUM(gwkp_env%sigma_projection%counts)
672 304 : n_offmesh = COUNT(gwkp_env%sigma_projection%mesh_index == 0)
673 56 : n_images = PRODUCT(bs_env%kpoints_scf_desymm%nkp_grid + 2)
674 14 : CALL mp_mem_avail_per_rank_GB(bs_env%para_env, available_GB)
675 14 : CALL bs_env%para_env%min(available_GB)
676 : input_bytes = complex_bytes*(REAL(SIZE(gwkp_env%Z_lP_q, KIND=int_8), dp) + &
677 56 : REAL(ri_q_storage_elements(gwkp_env%W_PQ_tau), dp))
678 14 : CALL bs_env%para_env%min(input_bytes)
679 : ! World inputs remain alive during redistribution. Count the complete new
680 : ! group storage in addition to measured usage; retain a library reserve.
681 14 : budget_bytes = budget_after_reserve(available_GB)
682 14 : block_bytes = complex_bytes*REAL(n_grid, dp)*REAL(n_aux, dp)
683 : result_bytes = 2.0_dp*complex_bytes*REAL(n_ao, dp)*REAL(SIZE(bs_env%eigenval_scf, 2), dp)* &
684 14 : REAL(n_time*n_spin, dp)
685 14 : DO i_group_size = MIN(2, MAX(1, n_world/2)), n_world
686 14 : IF (MODULO(n_world, i_group_size) /= 0) CYCLE
687 14 : gwkp_env%ngroups = n_world/i_group_size
688 14 : IF (gwkp_env%ngroups > n_time) CYCLE
689 14 : n_local_k = (n_k + i_group_size - 1)/i_group_size
690 14 : n_q_exchange = pair_exchange_width(n_grid, n_k, i_group_size)
691 14 : n_local_time = (n_time + gwkp_env%ngroups - 1)/gwkp_env%ngroups
692 14 : n_stored_q = 0
693 28 : DO iq = 1, MIN(n_k, i_group_size)
694 276 : n_stored_q = MAX(n_stored_q, COUNT(gwkp_env%q_slot(iq:n_k:i_group_size) > 0))
695 : END DO
696 : persistent_bytes = complex_bytes*(REAL(n_stored_q, dp)*REAL(n_grid, dp)*REAL(n_aux, dp) + &
697 14 : REAL(n_aux, dp)**2*REAL(n_stored_q*n_local_time, dp))
698 14 : n_pairs = REAL(CEILING(REAL(n_grid, dp)*REAL(n_grid + 1, dp)/(2.0_dp*REAL(i_group_size, dp)), KIND=int_8), dp)
699 14 : sigma_geometry_bytes = 0.0_dp
700 14 : IF (n_offmesh > 0) THEN
701 : sigma_geometry_bytes = 3.0_dp*n_pairs*(real_bytes + REAL(int_1_size + STORAGE_SIZE(0)/8, dp)) + &
702 14 : complex_bytes*REAL(n_offmesh, dp)*REAL(n_images, dp)
703 : END IF
704 : ! G(k), W(R), the retained off-mesh product, and the prepared FFT input coexist.
705 : ! Count the band orbitals of the owned band k points alongside these panels.
706 : ! q matrices, band projection, and Green blocks belong to separate stages.
707 : grid_bytes = (n_pairs*(complex_bytes + 3.0_dp*real_bytes) + real_bytes)*REAL(n_k, dp) + &
708 : REAL(int_1_size, dp)*n_pairs*REAL(n_k, dp) + &
709 : MAX(REAL(fft_panel_bytes, dp), 3.0_dp*complex_bytes*REAL(n_k, dp)) + &
710 : real_bytes*(n_pairs + REAL(n_k, dp)) + &
711 : complex_bytes*REAL(n_grid, dp)*REAL(n_ao, dp)*REAL(n_spin, dp)* &
712 : REAL((bs_env%nkp_bs_and_DOS + i_group_size - 1)/i_group_size + n_stored_q, dp) + &
713 : MAX(complex_bytes*REAL(n_grid, dp)*REAL(n_grid + 1, dp)*REAL(n_local_k, dp)/2.0_dp + &
714 : complex_bytes*REAL(n_aux, dp)**2*REAL(n_local_k, dp) + &
715 : complex_bytes*REAL(n_threads, dp)*REAL(n_grid, dp)* &
716 : REAL(n_ao + n_aux + MIN(n_grid, grid_pair_panel_cols), dp) + &
717 : block_bytes*REAL(MAX(0, n_q_exchange - n_threads), dp), &
718 : projection_workspace_bytes(n_grid, i_group_size, bs_env%nkp_bs_and_DOS, &
719 : n_jobs, n_offmesh, n_images, n_ao, n_threads)) + &
720 14 : 2.0_dp*complex_bytes*REAL(n_threads*n_k, dp) + sigma_geometry_bytes + library_bytes
721 : peak_bytes = MAX(persistent_bytes + block_bytes, &
722 14 : persistent_bytes - input_bytes + grid_bytes + result_bytes)
723 14 : IF (gwkp_env%ngroups == 1) peak_bytes = grid_bytes + result_bytes
724 14 : IF (peak_bytes <= budget_bytes) EXIT
725 : END DO
726 14 : IF (i_group_size > n_world) THEN
727 0 : CPABORT('Insufficient memory per MPI rank for GW self-energy time groups')
728 : END IF
729 14 : gwkp_env%ngroups = n_world/i_group_size
730 14 : gwkp_env%my_group = bs_env%para_env%mepos/i_group_size
731 14 : CALL mp_para_env_release(gwkp_env%group_env)
732 14 : ALLOCATE (gwkp_env%group_env)
733 14 : CALL gwkp_env%group_env%from_split(bs_env%para_env, gwkp_env%my_group)
734 14 : CPASSERT(gwkp_env%group_env%num_pe == i_group_size)
735 14 : IF (gwkp_env%ngroups == 1) THEN
736 0 : CALL timestop(handle)
737 0 : RETURN
738 : END IF
739 14 : CALL column_env%from_split(bs_env%para_env, gwkp_env%group_env%mepos)
740 14 : CALL gather_group_columns(bs_env, gwkp_env, column_env, i_group_size, gwkp_env%group_env%mepos, Z_group)
741 14 : CALL MOVE_ALLOC(Z_group, gwkp_env%Z_lP_q)
742 14 : n_local_q = SIZE(gwkp_env%Z_lP_q, 3)
743 14 : n_local_time = (n_time - gwkp_env%my_group + gwkp_env%ngroups - 1)/gwkp_env%ngroups
744 168 : ALLOCATE (W_group(n_local_q))
745 14 : iq_local = 0
746 262 : DO iq = MODULO(gwkp_env%group_env%mepos - 1, i_group_size) + 1, n_k, i_group_size
747 248 : IF (gwkp_env%q_slot(iq) == 0) CYCLE
748 126 : i_root = MODULO(iq, n_world)/i_group_size
749 126 : iq_local = iq_local + 1
750 630 : ALLOCATE (W_group(iq_local)%values(n_aux, n_aux, n_local_time))
751 3726 : DO it = 1, n_time
752 3600 : i_dest = MODULO(it - 1, gwkp_env%ngroups)
753 3726 : IF (column_env%mepos == i_root) THEN
754 1800 : IF (i_dest == i_root) THEN
755 900 : it_local = (it - 1)/gwkp_env%ngroups + 1
756 132956 : W_group(iq_local)%values(:, :, it_local) = gwkp_env%W_PQ_tau(gwkp_env%q_slot(iq))%values(:, :, it)
757 : ELSE
758 900 : CALL column_env%send(gwkp_env%W_PQ_tau(gwkp_env%q_slot(iq))%values(:, :, it), i_dest, 0)
759 : END IF
760 1800 : ELSE IF (column_env%mepos == i_dest) THEN
761 900 : i_source = i_root
762 900 : i_tag = 0
763 900 : it_local = (it - 1)/gwkp_env%ngroups + 1
764 900 : CALL column_env%recv(W_group(iq_local)%values(:, :, it_local), i_source, i_tag)
765 : END IF
766 : END DO
767 140 : IF (column_env%mepos == i_root) DEALLOCATE (gwkp_env%W_PQ_tau(gwkp_env%q_slot(iq))%values)
768 : END DO
769 14 : CALL column_env%free()
770 77 : CALL MOVE_ALLOC(W_group, gwkp_env%W_PQ_tau)
771 :
772 14 : CALL timestop(handle)
773 :
774 28 : END SUBROUTINE prepare_self_energy_groups
775 :
776 : ! **************************************************************************************************
777 : !> \brief Copy the Z(q), and optionally the orbitals ψ(k), needed by one rank of a time group
778 : !> from their world owners.
779 : !> World rank r owns k with MOD(k-1,n_world)=r and q with MOD(q,n_world)=r. Group rank i
780 : !> receives k with MOD(k-1,g)=i and q with MOD(q,g)=i. column_env connects the ranks with
781 : !> the same group rank; its rank is the time-group index.
782 : !> \param bs_env Band-structure settings.
783 : !> \param gwkp_env World-distributed ψ(k) and Z(q).
784 : !> \param column_env Ranks with the same within-group index.
785 : !> \param group_size Ranks per time group g.
786 : !> \param group_rank Within-group index i.
787 : !> \param Z_group Z(q) of this group rank, indexed (grid, RI, local q).
788 : !> \param psi_group ψ(k) of this group rank, indexed (grid, AO, local k, spin).
789 : ! **************************************************************************************************
790 28 : SUBROUTINE gather_group_columns(bs_env, gwkp_env, column_env, group_size, group_rank, Z_group, psi_group)
791 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
792 : TYPE(gwkp_type), INTENT(IN) :: gwkp_env
793 : TYPE(mp_comm_type), INTENT(IN) :: column_env
794 : INTEGER, INTENT(IN) :: group_size, group_rank
795 : COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: Z_group(:, :, :)
796 : COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT), &
797 : OPTIONAL :: psi_group(:, :, :, :)
798 :
799 : CHARACTER(LEN=*), PARAMETER :: routineN = 'gather_group_columns'
800 :
801 28 : COMPLEX(KIND=dp), ALLOCATABLE :: psi_block(:, :, :)
802 : INTEGER :: handle, i_root, ik, ik_local, iq, &
803 : iq_first, iq_local, n_grid, n_k, &
804 : n_world
805 :
806 28 : CALL timeset(routineN, handle)
807 :
808 28 : n_world = bs_env%para_env%num_pe
809 28 : n_grid = bs_env%ri_rs%n_grid_points
810 28 : n_k = bs_env%nkp_scf_desymm
811 28 : IF (PRESENT(psi_group)) THEN
812 0 : ALLOCATE (psi_group(n_grid, bs_env%n_ao, COUNT([(MODULO(ik - 1, group_size) == group_rank, ik=1, n_k)]), &
813 594 : bs_env%n_spin))
814 70 : ALLOCATE (psi_block(n_grid, bs_env%n_ao, bs_env%n_spin))
815 14 : ik_local = 0
816 262 : DO ik = group_rank + 1, n_k, group_size
817 248 : i_root = MODULO(ik - 1, n_world)/group_size
818 24364 : IF (column_env%mepos == i_root) psi_block(:, :, :) = gwkp_env%psi_lm_k(:, :, (ik - 1)/n_world + 1, :)
819 248 : CALL column_env%bcast(psi_block, i_root)
820 248 : ik_local = ik_local + 1
821 48494 : psi_group(:, :, ik_local, :) = psi_block
822 : END DO
823 14 : DEALLOCATE (psi_block)
824 : END IF
825 28 : iq_first = MODULO(group_rank - 1, group_size) + 1
826 636 : ALLOCATE (Z_group(n_grid, bs_env%n_RI, COUNT(gwkp_env%q_slot(iq_first:n_k:group_size) > 0)))
827 28 : iq_local = 0
828 524 : DO iq = iq_first, n_k, group_size
829 496 : IF (gwkp_env%q_slot(iq) == 0) CYCLE
830 252 : i_root = MODULO(iq, n_world)/group_size
831 252 : iq_local = iq_local + 1
832 47864 : IF (column_env%mepos == i_root) Z_group(:, :, iq_local) = gwkp_env%Z_lP_q(:, :, gwkp_env%q_slot(iq))
833 524 : CALL column_env%bcast(Z_group(:, :, iq_local), i_root)
834 : END DO
835 :
836 28 : CALL timestop(handle)
837 :
838 56 : END SUBROUTINE gather_group_columns
839 :
840 : ! **************************************************************************************************
841 : !> \brief Compute chi_PQ^R(i tau) through the periodic real-space grid representation.
842 : !> \param bs_env Band-structure settings and physical results.
843 : !> \param gwkp_env Periodic GW work data.
844 : ! **************************************************************************************************
845 14 : SUBROUTINE compute_chi(bs_env, gwkp_env)
846 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
847 : TYPE(gwkp_type), INTENT(INOUT), TARGET :: gwkp_env
848 :
849 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_chi'
850 :
851 14 : COMPLEX(KIND=dp), ALLOCATABLE :: chi_grid_q(:, :, :), chi_q(:, :, :), &
852 14 : G_grid_k(:, :, :)
853 14 : COMPLEX(KIND=dp), ALLOCATABLE, TARGET :: psi_time(:, :, :, :), Z_group(:, :, :)
854 14 : COMPLEX(KIND=dp), POINTER :: psi_values(:, :, :, :), Z_time(:, :, :)
855 : INTEGER :: first_pair, handle, i_aux, i_group, i_needed, i_q_world, i_rank, i_root, i_size, &
856 : i_source, i_t, i_tag, i_time_group, i_time_local, iq, iq_local, ispin, n_aux, n_cells, &
857 : n_grid, n_groups, n_local, n_needed_local, n_pairs_local, n_threads, n_time_local, &
858 : n_world, q_batch, unit_nr
859 : INTEGER(KIND=int_8) :: n_pairs_total
860 14 : INTEGER, ALLOCATABLE :: full_cells(:, :), needed_q(:)
861 14 : LOGICAL, ALLOCATABLE :: cell_active(:), needed(:)
862 : REAL(KIND=dp) :: available_GB, budget, chi_seconds(7), chi_started, green_bytes, &
863 : input_bytes, live_bytes, n_pair_max, n_q_max, peak, q_bytes, rows_bytes, stage_started, &
864 : tau, time_bytes
865 14 : REAL(KIND=dp), ALLOCATABLE :: green_norms(:, :)
866 266 : TYPE(gw_pair_fft_work_type) :: fft_stencil_work
867 : TYPE(mp_comm_type) :: column_env
868 : TYPE(mp_para_env_type), POINTER :: chi_env
869 14 : TYPE(ri_q_matrix_block_type), ALLOCATABLE :: chi_partial(:)
870 :
871 14 : CALL timeset(routineN, handle)
872 :
873 14 : CPASSERT(gwkp_env%ngroups == 1 .AND. gwkp_env%my_group == 0)
874 14 : CALL build_full_cell_grid(bs_env, full_cells)
875 14 : n_grid = bs_env%ri_rs%n_grid_points
876 14 : n_pairs_total = INT(n_grid, int_8)*(n_grid + 1)/2
877 14 : n_aux = bs_env%n_RI
878 14 : n_world = gwkp_env%group_env%num_pe
879 42 : ALLOCATE (needed(bs_env%nkp_scf_desymm))
880 262 : needed(:) = gwkp_env%q_slot > 0
881 290 : ALLOCATE (needed_q(COUNT(needed)))
882 510 : needed_q(:) = PACK([(iq, iq=1, SIZE(needed))], needed)
883 14 : CALL mp_mem_avail_per_rank_GB(gwkp_env%group_env, available_GB)
884 14 : budget = budget_after_reserve(available_GB)
885 14 : n_cells = SIZE(full_cells, 2)
886 : n_threads = 1
887 14 : !$ n_threads = omp_get_max_threads()
888 14 : CALL gwkp_env%group_env%max(n_threads)
889 : ! Keep received q matrices near their consumer; cap a batch at 64 MiB, or one matrix.
890 14 : q_batch = MAX(1, MIN(4*n_threads, INT(projection_workspace/(complex_bytes*REAL(n_pairs_total, dp)))))
891 14 : DO i_size = MIN(2, MAX(1, n_world/2)), n_world
892 14 : IF (MODULO(n_world, i_size) /= 0) CYCLE
893 14 : n_groups = n_world/i_size
894 14 : IF (n_groups > bs_env%num_time_freq_points) CYCLE
895 14 : i_rank = MODULO(gwkp_env%group_env%mepos, i_size)
896 14 : i_group = gwkp_env%group_env%mepos/i_size
897 14 : n_time_local = (bs_env%num_time_freq_points + n_groups - 1 - i_group)/n_groups
898 14 : n_pair_max = REAL(n_pairs_total*(i_rank + 1)/i_size - n_pairs_total*i_rank/i_size, dp)
899 : n_q_max = REAL(COUNT([(MODULO(iq, i_size) == i_rank .AND. needed(iq), &
900 510 : iq=1, bs_env%nkp_scf_desymm)]), dp)
901 14 : input_bytes = 0.0_dp
902 14 : IF (n_groups > 1) THEN
903 14 : input_bytes = complex_bytes*REAL(n_grid, dp)*REAL(n_aux, dp)*n_q_max
904 : input_bytes = input_bytes + &
905 : complex_bytes*REAL(n_grid, dp)*REAL(bs_env%n_ao*bs_env%n_spin, dp)* &
906 14 : REAL((bs_env%nkp_scf_desymm + i_size - 1 - i_rank)/i_size, dp)
907 : END IF
908 14 : time_bytes = complex_bytes*REAL(n_aux, dp)**2*n_q_max*REAL(n_time_local, dp)
909 : ! Keep only this group's time slices, not a full frequency set per group.
910 : ! The shared lattice backend uses bounded matrix tiles; library_bytes reserves its scratch.
911 : ! The occupied K2R stores directly in the persistent input; the virtual K2R updates it.
912 : rows_bytes = n_pair_max*complex_bytes*REAL(bs_env%nkp_scf_desymm, dp) + &
913 14 : real_bytes*(n_pair_max + 1.0_dp)*REAL(n_cells, dp)
914 14 : q_bytes = complex_bytes*REAL(n_pairs_total, dp)*MIN(REAL(q_batch, dp), n_q_max)
915 : ! The pair-local FFT output remains live while a bounded received batch is projected.
916 : green_bytes = complex_bytes*REAL(n_pairs_total, dp)* &
917 14 : REAL(pair_exchange_width(n_grid, bs_env%nkp_scf_desymm, i_size), dp)
918 : live_bytes = MAX(rows_bytes + green_bytes + &
919 : complex_bytes*REAL(n_threads, dp)*REAL(n_grid, dp)*REAL(bs_env%n_ao + 32, dp), &
920 : real_bytes*(n_pair_max + 1.0_dp)*REAL(n_cells, dp) + &
921 : complex_bytes*n_pair_max*REAL(SIZE(needed_q), dp) + q_bytes + &
922 : complex_bytes*REAL(n_grid, dp)*REAL(n_aux + MIN(n_grid, grid_pair_panel_cols), dp)* &
923 : MAX(1.0_dp, MIN(REAL(n_threads, dp), n_q_max, REAL(q_batch, dp)))) + &
924 : REAL(int_1_size, dp)*n_pair_max*REAL(n_cells, dp) + &
925 : MAX(REAL(fft_panel_bytes, dp), 3.0_dp*complex_bytes*REAL(n_cells, dp)) + &
926 14 : real_bytes*(n_pair_max + REAL(n_cells, dp))
927 : peak = input_bytes + time_bytes + complex_bytes*REAL(n_aux, dp)**2*n_q_max + &
928 : MAX(live_bytes, complex_bytes*REAL(n_aux, dp)**2*REAL(bs_env%num_time_freq_points, dp)) + &
929 14 : 2.0_dp*complex_bytes*REAL(n_threads*bs_env%nkp_scf_desymm, dp) + library_bytes
930 14 : CALL gwkp_env%group_env%max(peak)
931 14 : IF (peak <= budget) EXIT
932 : END DO
933 14 : IF (i_size > n_world) THEN
934 0 : CPABORT("Insufficient memory per MPI rank for GW time groups")
935 : END IF
936 14 : n_groups = n_world/i_size
937 14 : i_group = gwkp_env%group_env%mepos/i_size
938 14 : ALLOCATE (chi_env)
939 14 : CALL chi_env%from_split(gwkp_env%group_env, i_group)
940 14 : CALL column_env%from_split(gwkp_env%group_env, chi_env%mepos)
941 14 : IF (n_groups > 1) THEN
942 14 : CALL gather_group_columns(bs_env, gwkp_env, column_env, i_size, chi_env%mepos, Z_group, psi_time)
943 14 : psi_values => psi_time
944 14 : Z_time => Z_group
945 : ELSE
946 0 : psi_values => gwkp_env%psi_lm_k
947 0 : Z_time => gwkp_env%Z_lP_q
948 : END IF
949 14 : n_time_local = (bs_env%num_time_freq_points + n_groups - 1 - i_group)/n_groups
950 14 : CALL block_range(INT(INT(n_grid, int_8)*(n_grid + 1)/2), chi_env%mepos, chi_env%num_pe, first_pair, n_pairs_local)
951 14 : n_needed_local = SIZE(Z_time, 3)
952 168 : ALLOCATE (chi_partial(n_needed_local))
953 140 : DO iq_local = 1, n_needed_local
954 266556 : ALLOCATE (chi_partial(iq_local)%values(n_aux, n_aux, n_time_local), SOURCE=z_zero)
955 : END DO
956 70 : ALLOCATE (chi_q(n_aux, n_aux, n_needed_local))
957 :
958 70 : ALLOCATE (green_norms(n_cells, 2), cell_active(n_cells))
959 : CALL prepare_pair_fft(bs_env, full_cells, first_pair, n_pairs_local, chi_env%num_pe, &
960 14 : fft_stencil_work, .FALSE., needed_q)
961 : CALL start_time_loop_progress(bs_env, 'χ(iτ)', 'k points of G and q points of χ', n_groups, i_size, &
962 : INT(n_time_local*bs_env%n_spin, int_8)* &
963 262 : INT(2*bs_env%nkp_scf_desymm + COUNT(needed), int_8))
964 166 : DO i_t = i_group + 1, bs_env%num_time_freq_points, n_groups
965 152 : i_time_local = (i_t - 1)/n_groups + 1
966 152 : tau = bs_env%time_frequency_grid%imaginary_time(i_t)
967 152 : chi_q = z_zero
968 304 : DO ispin = 1, bs_env%n_spin
969 152 : chi_seconds = 0.0_dp
970 : IF (fft_profile_enabled) THEN
971 : chi_started = m_walltime()
972 : stage_started = chi_started
973 : END IF
974 608 : ALLOCATE (G_grid_k(n_pairs_local, 1, bs_env%nkp_scf_desymm))
975 : CALL build_green_pairs(bs_env, psi_values(:, :, :, ispin), chi_env, tau, ispin, &
976 152 : .TRUE., fft_stencil_work, G_grid_k)
977 : IF (fft_profile_enabled) chi_seconds(1) = m_walltime() - stage_started
978 : IF (fft_profile_enabled) stage_started = m_walltime()
979 152 : CALL k_to_grid_pairs(G_grid_k, fft_stencil_work, norm_squared=green_norms(:, 1))
980 : IF (fft_profile_enabled) chi_seconds(2) = m_walltime() - stage_started
981 : IF (fft_profile_enabled) stage_started = m_walltime()
982 : CALL build_green_pairs(bs_env, psi_values(:, :, :, ispin), chi_env, tau, ispin, &
983 152 : .FALSE., fft_stencil_work, G_grid_k)
984 : IF (fft_profile_enabled) chi_seconds(1) = chi_seconds(1) + m_walltime() - stage_started
985 : IF (fft_profile_enabled) stage_started = m_walltime()
986 : CALL k_to_grid_pairs(G_grid_k, fft_stencil_work, norm_squared=green_norms(:, 2), &
987 152 : product_scale=bs_env%spin_degeneracy)
988 : IF (fft_profile_enabled) chi_seconds(2) = chi_seconds(2) + m_walltime() - stage_started
989 : IF (fft_profile_enabled) stage_started = m_walltime()
990 : ! Each original Green is filtered by its own full Frobenius norm.
991 : ! Scalar cell masks commute with their product: (m_o G_o)(m_v G_v)=m_o m_v G_o G_v.
992 152 : CPASSERT(bs_env%n_atom > 0)
993 152 : CALL chi_env%sum(green_norms)
994 : cell_active(:) = .NOT. (SQRT(green_norms(:, 1))/REAL(bs_env%n_atom, dp)**2 < bs_env%eps_filter) .AND. &
995 3712 : .NOT. (SQRT(green_norms(:, 2))/REAL(bs_env%n_atom, dp)**2 < bs_env%eps_filter)
996 : IF (fft_profile_enabled) chi_seconds(3) = m_walltime() - stage_started
997 : IF (fft_profile_enabled) stage_started = m_walltime()
998 152 : DEALLOCATE (G_grid_k)
999 608 : ALLOCATE (chi_grid_q(n_pairs_local, 1, SIZE(needed_q)))
1000 : CALL execute_pair_fft(fft_stencil_work, chi_grid_q, cell_active, &
1001 152 : 'GW chi (K2R extraction feeds R2K input)')
1002 : IF (fft_profile_enabled) chi_seconds(4) = m_walltime() - stage_started
1003 :
1004 : IF (fft_profile_enabled) stage_started = m_walltime()
1005 : CALL project_fft_pairs_to_chi(chi_grid_q, chi_env, needed, Z_time, &
1006 152 : chi_q, q_batch, chi_seconds(6))
1007 152 : DEALLOCATE (chi_grid_q)
1008 : IF (fft_profile_enabled) chi_seconds(5) = MAX(0.0_dp, m_walltime() - stage_started - chi_seconds(6))
1009 : IF (fft_profile_enabled) chi_seconds(7) = m_walltime() - chi_started
1010 152 : unit_nr = 0
1011 : IF (fft_profile_enabled) unit_nr = cp_logger_get_default_io_unit()
1012 : IF (unit_nr > 0) THEN
1013 : WRITE (unit_nr, '(T2,A)') 'GW_CHI_PROFILE| logger-rank wall seconds, no MPI reduction'
1014 : WRITE (unit_nr, '(T2,A,4I12)') 'GW_CHI_PROFILE| time/spin/pairs/cells: ', &
1015 : i_t, ispin, n_pairs_local, n_cells
1016 : WRITE (unit_nr, '(T2,A,2I12)') 'GW_CHI_PROFILE| received q batch capacity / full local: ', &
1017 : MIN(q_batch, n_needed_local), n_needed_local
1018 : ! product+R2K includes allocations, input release and nested FFT profile output.
1019 : WRITE (unit_nr, '(T2,A,7F12.6)') &
1020 : 'GW_CHI_PROFILE| build+alloc/K2R/filter/product+R2K/transfer/projection/total: ', chi_seconds
1021 : END IF
1022 3864 : CALL count_time_loop_k_points(bs_env, INT(COUNT(needed), int_8))
1023 : END DO
1024 : ! The spin sum lives in the upper triangle. Complete each stored time slice once.
1025 1966 : DO i_needed = 1, n_needed_local
1026 22576 : DO i_aux = 1, n_aux
1027 152680 : chi_partial(i_needed)%values(:i_aux, i_aux, i_time_local) = chi_q(:i_aux, i_aux, i_needed)
1028 133856 : chi_partial(i_needed)%values(i_aux + 1:, i_aux, i_time_local) = CONJG(chi_q(i_aux, i_aux + 1:, i_needed))
1029 : END DO
1030 : END DO
1031 : END DO
1032 14 : CALL release_pair_fft(fft_stencil_work)
1033 14 : bs_env%gwkp_env%k_points_total = 0
1034 :
1035 14 : IF (ALLOCATED(psi_time)) DEALLOCATE (psi_time)
1036 14 : n_local = SIZE(gwkp_env%Z_lP_q, 3)
1037 105 : ALLOCATE (gwkp_env%chi_q_frequency(n_local))
1038 14 : iq_local = 0
1039 262 : DO iq = MODULO(chi_env%mepos - 1, i_size) + 1, bs_env%nkp_scf_desymm, i_size
1040 248 : IF (.NOT. needed(iq)) CYCLE
1041 126 : iq_local = iq_local + 1
1042 126 : i_root = MODULO(iq, n_world)/i_size
1043 126 : i_q_world = gwkp_env%q_slot(iq)
1044 126 : IF (column_env%mepos == i_root) THEN
1045 0 : ALLOCATE (gwkp_env%chi_q_frequency(i_q_world)%values(n_aux, n_aux, &
1046 315 : bs_env%num_time_freq_points))
1047 : END IF
1048 : ! Send each computed time slice once, point to point, to the world q owner, which alone
1049 : ! forms the complete frequency result; slices and frequency sets are never replicated.
1050 3726 : DO i_t = 1, bs_env%num_time_freq_points
1051 3600 : i_time_group = MODULO(i_t - 1, n_groups)
1052 3600 : i_time_local = (i_t - 1)/n_groups + 1
1053 3726 : IF (column_env%mepos == i_root) THEN
1054 1800 : IF (i_time_group == i_root) THEN
1055 : gwkp_env%chi_q_frequency(i_q_world)%values(:, :, i_t) = &
1056 132956 : chi_partial(iq_local)%values(:, :, i_time_local)
1057 : ELSE
1058 900 : i_source = i_time_group
1059 900 : i_tag = 0
1060 900 : CALL column_env%recv(gwkp_env%chi_q_frequency(i_q_world)%values(:, :, i_t), i_source, i_tag)
1061 : END IF
1062 1800 : ELSE IF (column_env%mepos == i_time_group) THEN
1063 900 : CALL column_env%send(chi_partial(iq_local)%values(:, :, i_time_local), i_root, 0)
1064 : END IF
1065 : END DO
1066 126 : IF (column_env%mepos == i_root) THEN
1067 : CALL cosine_transform_ri_q(gwkp_env%chi_q_frequency(i_q_world), &
1068 63 : bs_env%time_frequency_grid%cosine_time_to_frequency)
1069 : END IF
1070 262 : DEALLOCATE (chi_partial(iq_local)%values)
1071 : END DO
1072 140 : DEALLOCATE (chi_partial)
1073 : NULLIFY (Z_time)
1074 14 : IF (ALLOCATED(Z_group)) DEALLOCATE (Z_group)
1075 14 : CALL column_env%free()
1076 14 : CALL mp_para_env_release(chi_env)
1077 14 : DEALLOCATE (chi_q, full_cells, needed, needed_q, green_norms, cell_active)
1078 :
1079 14 : CALL timestop(handle)
1080 :
1081 42 : END SUBROUTINE compute_chi
1082 :
1083 : ! **************************************************************************************************
1084 : !> \brief Build K(q) = M(q)^-1 V(q)^1/2 from the Gaussian RI metric M and the Ewald Coulomb matrix V,
1085 : !> and the charge-neutralisation vectors e(q) = M(q) c/|c| and X(q,iω) = M(q) M(0)^-1 X0(iω).
1086 : !> The Gamma-cell head is corrected separately from these operators.
1087 : !> \param bs_env ...
1088 : !> \param qs_env ...
1089 : !> \param gwkp_env K(q) and the neutralisation vectors for the q points owned by this rank
1090 : ! **************************************************************************************************
1091 14 : SUBROUTINE compute_metric_coulomb_factor(bs_env, qs_env, gwkp_env)
1092 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1093 : TYPE(qs_environment_type), POINTER :: qs_env
1094 : TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
1095 :
1096 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_metric_coulomb_factor'
1097 :
1098 14 : COMPLEX(KIND=dp), ALLOCATABLE :: charge(:), charge_hat(:), M_inv(:, :), M_ref(:, :, :), &
1099 14 : M_zero(:, :), U_Pp(:, :), V_ref(:, :, :), V_sqrt(:, :), X0(:, :), X_coef(:, :)
1100 : INTEGER :: handle, ikp, ilocal, n_ref, n_RI, nkp, &
1101 : nkp_local
1102 14 : INTEGER, ALLOCATABLE :: metric_cells(:, :)
1103 : REAL(KIND=dp), ALLOCATABLE :: local_q(:, :)
1104 14 : TYPE(cp_fm_type), ALLOCATABLE :: fm_metric(:)
1105 :
1106 14 : CALL timeset(routineN, handle)
1107 :
1108 14 : n_RI = bs_env%n_RI
1109 14 : n_ref = n_RI
1110 30 : IF (bs_env%auto_ri%enabled) n_ref = SUM(bs_env%auto_ri%sizes_ref_RI)
1111 14 : nkp = bs_env%nkp_scf_desymm
1112 14 : CPASSERT(bs_env%nkp_chi_eps_W_orig == nkp)
1113 14 : nkp_local = SIZE(gwkp_env%Z_lP_q, 3)
1114 42 : ALLOCATE (local_q(3, nkp_local))
1115 262 : DO ikp = 1, nkp
1116 248 : IF (MODULO(ikp, bs_env%para_env%num_pe) /= bs_env%para_env%mepos) CYCLE
1117 124 : ilocal = gwkp_env%q_slot(ikp)
1118 327 : IF (ilocal > 0) local_q(:, ilocal) = bs_env%kpoints_chi_eps_W%xkp(:, ikp)
1119 : END DO
1120 112 : ALLOCATE (M_ref(n_ref, n_ref, nkp_local), V_ref(n_ref, n_ref, nkp_local), source=z_zero)
1121 175 : ALLOCATE (gwkp_env%metric_coulomb_factor(nkp_local), M_inv(n_RI, n_RI), V_sqrt(n_RI, n_RI))
1122 : CALL build_ri_2c_lattice(fm_metric, metric_cells, bs_env%ri_metric, &
1123 14 : 0.0_dp, bs_env, qs_env, reference_basis=bs_env%auto_ri%enabled)
1124 : CALL ri_2c_lattice_to_owned_q(fm_metric, metric_cells, &
1125 : bs_env%kpoints_chi_eps_W%xkp(1:3, 1:nkp), &
1126 : bs_env%kpoints_chi_eps_W%nkp_grid, gwkp_env%q_slot, M_ref, &
1127 14 : bs_env%kpoints_chi_eps_W%rskp_transform)
1128 : ! The erfc(omega r)/r metric is the real-space Ewald part of the Coulomb matrix. At q = 0 of an
1129 : ! odd mesh the reciprocal part drops the singular G = 0 term, which the Γ correction integrates.
1130 16871 : V_ref(:, :, :) = M_ref
1131 14 : CALL add_ewald_reciprocal(V_ref, qs_env, local_q, 'RI_AUX', bs_env%ri_metric%omega)
1132 112 : ALLOCATE (M_zero(n_ref, n_ref), charge(n_ref), charge_hat(n_RI))
1133 14 : CALL ri_2c_lattice_to_kp(fm_metric, metric_cells, [0.0_dp, 0.0_dp, 0.0_dp], M_zero)
1134 14 : CALL gamma_auxiliary_charges(qs_env, charge)
1135 14 : CALL cp_fm_release(fm_metric)
1136 14 : DEALLOCATE (metric_cells)
1137 14 : IF (bs_env%auto_ri%enabled) CALL assemble_AA_contraction(bs_env, U_Pp)
1138 14 : IF (bs_env%auto_ri%enabled) THEN
1139 68120 : M_inv(:, :) = MATMUL(CONJG(TRANSPOSE(U_Pp)), MATMUL(M_zero, U_Pp))
1140 812 : charge_hat(:) = MATMUL(CONJG(TRANSPOSE(U_Pp)), charge)
1141 : ELSE
1142 942 : M_inv(:, :) = M_zero
1143 78 : charge_hat(:) = charge
1144 : END IF
1145 14 : DEALLOCATE (M_zero, charge)
1146 222 : charge_hat(:) = charge_hat/SQRT(REAL(DOT_PRODUCT(charge_hat, charge_hat), dp))
1147 14 : CALL local_complex_power(M_inv, -1.0_dp, 0.0_dp, relative_eps=bs_env%regularization_RI)
1148 : ! The pair densities enter χ as Z^H ρ = M d. Neutralise the fitted coefficients d, which are
1149 : ! continuous in q: X0 = Σ (Z(0)^H ρ) m* becomes M(0)^-1 X0 = Σ d m*, and M(q) M(0)^-1 X0 at q.
1150 : ! Carrying X0 itself to q would let M(q)^-1 amplify its O(q) mismatch in ill-conditioned metrics.
1151 2322 : CALL compute_charge_leak_q0(bs_env, gwkp_env, MATMUL(M_inv, charge_hat), X0)
1152 56 : ALLOCATE (X_coef(n_RI, SIZE(X0, 2)))
1153 140318 : X_coef(:, :) = MATMUL(M_inv, X0)
1154 14 : DEALLOCATE (X0)
1155 112 : ALLOCATE (gwkp_env%leak_e(n_RI, nkp_local), gwkp_env%leak_x(n_RI, SIZE(X_coef, 2), nkp_local))
1156 77 : DO ilocal = 1, nkp_local
1157 63 : IF (bs_env%auto_ri%enabled) THEN
1158 124744 : M_inv(:, :) = MATMUL(CONJG(TRANSPOSE(U_Pp)), MATMUL(M_ref(:, :, ilocal), U_Pp))
1159 168664 : V_sqrt(:, :) = MATMUL(CONJG(TRANSPOSE(U_Pp)), MATMUL(V_ref(:, :, ilocal), U_Pp))
1160 : ELSE
1161 7850 : M_inv(:, :) = M_ref(:, :, ilocal)
1162 7850 : V_sqrt(:, :) = V_ref(:, :, ilocal)
1163 : END IF
1164 24287 : gwkp_env%leak_e(:, ilocal) = MATMUL(M_inv, charge_hat)
1165 1018863 : gwkp_env%leak_x(:, :, ilocal) = MATMUL(M_inv, X_coef)
1166 63 : CALL local_complex_power(M_inv, -1.0_dp, 0.0_dp, relative_eps=bs_env%regularization_RI)
1167 63 : CALL local_complex_power(V_sqrt, 0.5_dp, 0.0_dp)
1168 189 : ALLOCATE (gwkp_env%metric_coulomb_factor(ilocal)%values(n_RI, n_RI, 1))
1169 559497 : gwkp_env%metric_coulomb_factor(ilocal)%values(:, :, 1) = MATMUL(M_inv, V_sqrt)
1170 : END DO
1171 14 : DEALLOCATE (M_ref, V_ref, M_inv, V_sqrt, charge_hat, X_coef)
1172 14 : IF (ALLOCATED(U_Pp)) DEALLOCATE (U_Pp)
1173 :
1174 14 : CALL timestop(handle)
1175 :
1176 28 : END SUBROUTINE compute_metric_coulomb_factor
1177 :
1178 : ! **************************************************************************************************
1179 : !> \brief Spurious charge of the fitted q = 0 occupied-virtual pair densities.
1180 : !> The RI fit gives ρ_ia = ψ_ik* ψ_ak (exactly neutral) the charge m_ia(k) = Σ_l g_l* ψ_ik(r_l) ψ_ak(r_l)*
1181 : !> with g = Z(0) v0. With the time dependence and cosine transform of compute_chi,
1182 : !> L0(iω) = (s/N_k) Σ_k,ia |m_ia|² w_ia(iω) and X0(iω) = Z(0)^H T(iω), T_l = (s/N_k) Σ_k,ia ψ_i ψ_a* m_ia* w_ia.
1183 : !> \param bs_env ...
1184 : !> \param gwkp_env ...
1185 : !> \param v0 M(0)^-1 c/|c|
1186 : !> \param X0 X(iω) in the projection space Z(0)^H ρ
1187 : ! **************************************************************************************************
1188 14 : SUBROUTINE compute_charge_leak_q0(bs_env, gwkp_env, v0, X0)
1189 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1190 : TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
1191 : COMPLEX(KIND=dp), INTENT(IN) :: v0(:)
1192 : COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: X0(:, :)
1193 :
1194 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_charge_leak_q0'
1195 :
1196 14 : COMPLEX(KIND=dp), ALLOCATABLE :: cross(:, :), g0(:), m(:, :), psi(:, :), &
1197 14 : T_tau(:, :), work(:, :), X_tau(:, :), &
1198 14 : Y(:, :)
1199 : INTEGER :: a, handle, i, ikp, ispin, j_w, n_ao, &
1200 : n_grid, n_occ, n_t, n_vir, nkp, &
1201 : num_pe, t
1202 : REAL(KIND=dp) :: prefactor, x
1203 : REAL(KIND=dp), ALLOCATABLE :: L_tau(:)
1204 :
1205 14 : CALL timeset(routineN, handle)
1206 :
1207 14 : n_grid = bs_env%ri_rs%n_grid_points
1208 14 : n_ao = SIZE(gwkp_env%psi_lm_k, 2)
1209 14 : nkp = bs_env%nkp_scf_desymm
1210 14 : n_t = bs_env%num_time_freq_points
1211 14 : num_pe = bs_env%para_env%num_pe
1212 182 : ALLOCATE (g0(n_grid), psi(n_grid, n_ao), work(n_grid, n_ao), T_tau(n_grid, n_t), L_tau(n_t))
1213 3502 : g0(:) = MATMUL(gwkp_env%Z_lP_zero(:, 2:, 1), v0)
1214 14 : T_tau(:, :) = z_zero
1215 14 : L_tau(:) = 0.0_dp
1216 28 : DO ispin = 1, bs_env%n_spin
1217 14 : n_occ = bs_env%n_occ(ispin)
1218 14 : n_vir = n_ao - n_occ
1219 14 : IF (n_occ == 0 .OR. n_vir == 0) CYCLE
1220 126 : ALLOCATE (m(n_occ, n_vir), cross(n_occ, n_vir), Y(n_grid, n_occ))
1221 262 : DO ikp = 1, nkp
1222 248 : IF (MODULO(ikp - 1, num_pe) /= bs_env%para_env%mepos) CYCLE
1223 24116 : psi(:, :) = gwkp_env%psi_lm_k(:, :, (ikp - 1)/num_pe + 1, ispin)
1224 248 : DO i = 1, n_occ
1225 4202 : work(:, i) = CONJG(g0)*psi(:, i)
1226 : END DO
1227 20162 : m(:, :) = MATMUL(TRANSPOSE(work(:, 1:n_occ)), CONJG(psi(:, n_occ + 1:n_ao)))
1228 3698 : DO t = 1, n_t
1229 3560 : cross(:, :) = z_zero
1230 20816 : DO a = 1, n_vir
1231 38072 : DO i = 1, n_occ
1232 : x = bs_env%time_frequency_grid%imaginary_time(t)* &
1233 17256 : (gwkp_env%eigenvalues_G(n_occ + a, ikp, ispin) - gwkp_env%eigenvalues_G(i, ikp, ispin))
1234 34512 : IF (x < 700.0_dp) THEN
1235 12805 : cross(i, a) = CONJG(m(i, a))*EXP(-x)
1236 12805 : L_tau(t) = L_tau(t) + ABS(m(i, a))**2*EXP(-x)
1237 : END IF
1238 : END DO
1239 : END DO
1240 641512 : Y(:, :) = MATMUL(CONJG(psi(:, n_occ + 1:n_ao)), TRANSPOSE(cross))
1241 251968 : T_tau(:, t) = T_tau(:, t) + SUM(psi(:, 1:n_occ)*Y, DIM=2)
1242 : END DO
1243 : END DO
1244 28 : DEALLOCATE (m, cross, Y)
1245 : END DO
1246 14 : CALL bs_env%para_env%sum(T_tau)
1247 14 : CALL bs_env%para_env%sum(L_tau)
1248 14 : prefactor = bs_env%spin_degeneracy/REAL(nkp, dp)
1249 56 : ALLOCATE (X_tau(bs_env%n_RI, n_t))
1250 326182 : X_tau(:, :) = prefactor*MATMUL(CONJG(TRANSPOSE(gwkp_env%Z_lP_zero(:, 2:, 1))), T_tau)
1251 56 : ALLOCATE (X0(bs_env%n_RI, n_t), SOURCE=z_zero)
1252 332 : ALLOCATE (gwkp_env%leak_l0(n_t), SOURCE=0.0_dp)
1253 318 : DO t = 1, n_t
1254 8894 : DO j_w = 1, n_t
1255 103552 : X0(:, j_w) = X0(:, j_w) + bs_env%time_frequency_grid%cosine_time_to_frequency(j_w, t)*X_tau(:, t)
1256 : gwkp_env%leak_l0(j_w) = gwkp_env%leak_l0(j_w) + &
1257 8880 : bs_env%time_frequency_grid%cosine_time_to_frequency(j_w, t)*prefactor*L_tau(t)
1258 : END DO
1259 : END DO
1260 14 : DEALLOCATE (g0, psi, work, L_tau, T_tau, X_tau)
1261 :
1262 14 : CALL timestop(handle)
1263 :
1264 28 : END SUBROUTINE compute_charge_leak_q0
1265 :
1266 : ! **************************************************************************************************
1267 : !> \brief Contract the occupied density with the bare RI Coulomb operator.
1268 : !> \param bs_env Band-structure settings and physical results.
1269 : !> \param gwkp_env Periodic GW work data.
1270 : ! **************************************************************************************************
1271 14 : SUBROUTINE compute_Sigma_x_from_V(bs_env, gwkp_env)
1272 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1273 : TYPE(gwkp_type), INTENT(INOUT), TARGET :: gwkp_env
1274 :
1275 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_Sigma_x_from_V'
1276 :
1277 14 : COMPLEX(KIND=dp), ALLOCATABLE :: G_grid_k(:, :, :), head_cell_factor(:)
1278 : INTEGER :: first_pair, handle, ispin, n_grid, &
1279 : n_pairs_local
1280 14 : INTEGER, ALLOCATABLE :: full_cells(:, :)
1281 14 : LOGICAL, ALLOCATABLE :: cell_active(:)
1282 : REAL(KIND=dp) :: head_metric_zero
1283 14 : REAL(KIND=dp), ALLOCATABLE :: Sigma_grid_R(:, :, :), V_grid_R(:, :, :)
1284 308 : TYPE(gw_pair_fft_work_type) :: fft_stencil_work
1285 :
1286 14 : CALL timeset(routineN, handle)
1287 :
1288 70 : ALLOCATE (gwkp_env%sigma_x_bands(bs_env%n_ao, bs_env%nkp_bs_and_DOS, bs_env%n_spin))
1289 14 : n_grid = bs_env%ri_rs%n_grid_points
1290 : ! Same grid-pair distribution as Σc with q owners MOD(q,P).
1291 : CALL block_range(INT(INT(n_grid, int_8)*(n_grid + 1)/2), bs_env%para_env%mepos, &
1292 14 : bs_env%para_env%num_pe, first_pair, n_pairs_local)
1293 14 : IF (SIZE(gwkp_env%Z_lP_zero, 2) /= bs_env%n_RI + 1 .OR. SIZE(gwkp_env%Z_lP_zero, 3) /= 1) THEN
1294 0 : CPABORT("Periodic RI-RS GW requires the direct Gamma auxiliary fit")
1295 : END IF
1296 14 : CPASSERT(SIZE(gwkp_env%Z_lP_q, 2) == bs_env%n_RI)
1297 14 : CPASSERT(ALLOCATED(gwkp_env%metric_coulomb_factor))
1298 14 : CALL build_full_cell_grid(bs_env, full_cells)
1299 : CALL prepare_pair_fft(bs_env, full_cells, first_pair, n_pairs_local, bs_env%para_env%num_pe, &
1300 14 : fft_stencil_work, .FALSE., q_partner=gwkp_env%q_partner)
1301 56 : ALLOCATE (G_grid_k(n_pairs_local, 1, bs_env%nkp_scf_desymm))
1302 : CALL project_auxiliary_q_to_fft_pairs(gwkp_env%Z_lP_q, gwkp_env%metric_coulomb_factor, 1, &
1303 14 : bs_env%para_env, fft_stencil_work, G_grid_k, factor=.TRUE.)
1304 77 : DEALLOCATE (gwkp_env%metric_coulomb_factor)
1305 14 : CALL q_mesh_and_gamma_shell_average(bs_env, values_q=G_grid_k, q_order=fft_stencil_work%input_to_native)
1306 56 : ALLOCATE (V_grid_R(n_pairs_local, 1, SIZE(full_cells, 2)))
1307 14 : CALL k_to_grid_pairs(G_grid_k, fft_stencil_work, values_R=V_grid_R)
1308 14 : CALL singular_head_cell_factors(bs_env, full_cells, head_cell_factor)
1309 14 : head_metric_zero = ri_metric_head_zero(bs_env)
1310 304 : head_cell_factor = (MERGE(twopi, fourpi, ALL(bs_env%periodic == [1, 1, 0]))/head_metric_zero**2)*head_cell_factor
1311 : CALL add_gamma_corr_pairs(bs_env, full_cells, head_cell_factor, &
1312 14 : gwkp_env%Z_lP_zero(:, 1, 1), first_pair, V_grid_R)
1313 14 : DEALLOCATE (head_cell_factor)
1314 :
1315 70 : ALLOCATE (Sigma_grid_R, MOLD=V_grid_R)
1316 290 : ALLOCATE (cell_active(SIZE(full_cells, 2)), SOURCE=.TRUE.)
1317 28 : DO ispin = 1, bs_env%n_spin
1318 : CALL build_green_pairs(bs_env, gwkp_env%band_orbitals(:, :, :, ispin), bs_env%para_env, &
1319 14 : 0.0_dp, ispin, .TRUE., fft_stencil_work, G_grid_k)
1320 14 : CALL k_to_grid_pairs(G_grid_k, fft_stencil_work, values_R=Sigma_grid_R, multiply_R=V_grid_R, product_scale=-1.0_dp)
1321 : CALL execute_pair_fft(fft_stencil_work, G_grid_k, cell_active, &
1322 14 : 'GW self-energy (K2R extraction feeds R2K input)', Sigma_grid_R)
1323 :
1324 : CALL self_energy_pairs_to_Bloch(bs_env, G_grid_k, Sigma_grid_R, bs_env%para_env, &
1325 : fft_stencil_work, gwkp_env%sigma_projection, gwkp_env%band_orbitals, &
1326 28 : ispin, gwkp_env%sigma_x_bands(:, :, ispin), .FALSE.)
1327 : END DO
1328 14 : CALL release_pair_fft(fft_stencil_work)
1329 :
1330 14 : DEALLOCATE (full_cells, G_grid_k, Sigma_grid_R, V_grid_R, cell_active)
1331 :
1332 14 : CALL timestop(handle)
1333 :
1334 28 : END SUBROUTINE compute_Sigma_x_from_V
1335 :
1336 : ! **************************************************************************************************
1337 : !> \brief Form Σc with one owner per unordered (l,l') pair through both lattice FFTs.
1338 : !> \param bs_env Band-structure settings and physical results.
1339 : !> \param gwkp_env Periodic GW work data.
1340 : ! **************************************************************************************************
1341 14 : SUBROUTINE compute_Sigma_c_from_W_PQ(bs_env, gwkp_env)
1342 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1343 : TYPE(gwkp_type), INTENT(INOUT), TARGET :: gwkp_env
1344 :
1345 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_Sigma_c_from_W_PQ'
1346 :
1347 : COMPLEX(KIND=dp), ALLOCATABLE :: G_grid_k(:, :, :)
1348 : INTEGER :: first_pair, handle, i_component, i_t, &
1349 : i_time_local, ispin, n_grid, &
1350 : n_pairs_local, n_time_local
1351 14 : INTEGER, ALLOCATABLE :: full_cells(:, :)
1352 : LOGICAL, ALLOCATABLE :: cell_active(:)
1353 : REAL(KIND=dp) :: tau
1354 : REAL(KIND=dp), ALLOCATABLE :: green_norms(:), sigma_band(:, :), &
1355 : Sigma_grid_R(:, :, :), &
1356 14 : sigma_result_buffer(:), &
1357 14 : W_grid_R(:, :, :)
1358 266 : TYPE(gw_pair_fft_work_type) :: fft_stencil_work
1359 :
1360 14 : CALL timeset(routineN, handle)
1361 :
1362 14 : CPASSERT(ALLOCATED(gwkp_env%band_orbitals))
1363 14 : IF (ALLOCATED(gwkp_env%sigma_c_bands)) DEALLOCATE (gwkp_env%sigma_c_bands)
1364 14 : CALL build_full_cell_grid(bs_env, full_cells)
1365 14 : n_grid = bs_env%ri_rs%n_grid_points
1366 : CALL block_range(INT(INT(n_grid, int_8)*(n_grid + 1)/2), gwkp_env%group_env%mepos, gwkp_env%group_env%num_pe, &
1367 14 : first_pair, n_pairs_local)
1368 56 : ALLOCATE (G_grid_k(n_pairs_local, 1, bs_env%nkp_scf_desymm))
1369 56 : ALLOCATE (W_grid_R(n_pairs_local, 1, SIZE(full_cells, 2)))
1370 56 : ALLOCATE (Sigma_grid_R, MOLD=W_grid_R)
1371 70 : ALLOCATE (green_norms(SIZE(full_cells, 2)), cell_active(SIZE(full_cells, 2)))
1372 14 : CPASSERT(bs_env%n_atom > 0)
1373 : CALL prepare_pair_fft(bs_env, full_cells, first_pair, n_pairs_local, gwkp_env%group_env%num_pe, &
1374 14 : fft_stencil_work, .TRUE., q_partner=gwkp_env%q_partner)
1375 56 : ALLOCATE (sigma_band(bs_env%n_ao, bs_env%nkp_bs_and_DOS))
1376 0 : ALLOCATE (gwkp_env%sigma_c_bands(bs_env%n_ao, bs_env%nkp_bs_and_DOS, &
1377 98 : bs_env%num_time_freq_points, 2, bs_env%n_spin))
1378 109784 : gwkp_env%sigma_c_bands = 0.0_dp
1379 :
1380 14 : n_time_local = (bs_env%num_time_freq_points - gwkp_env%my_group + gwkp_env%ngroups - 1)/gwkp_env%ngroups
1381 : CALL start_time_loop_progress(bs_env, 'Σ_c(iτ)', 'k points of G and Σ_c', gwkp_env%ngroups, &
1382 : gwkp_env%group_env%num_pe, INT(2*n_time_local*bs_env%n_spin, int_8)* &
1383 14 : INT(bs_env%nkp_scf_desymm + bs_env%nkp_bs_and_DOS, int_8))
1384 166 : DO i_t = gwkp_env%my_group + 1, bs_env%num_time_freq_points, gwkp_env%ngroups
1385 152 : i_time_local = (i_t - gwkp_env%my_group - 1)/gwkp_env%ngroups + 1
1386 : CALL project_auxiliary_q_to_fft_pairs(gwkp_env%Z_lP_q, &
1387 : gwkp_env%W_PQ_tau, i_time_local, &
1388 152 : gwkp_env%group_env, fft_stencil_work, G_grid_k, factor=.FALSE.)
1389 152 : CALL q_mesh_and_gamma_shell_average(bs_env, values_q=G_grid_k, q_order=fft_stencil_work%input_to_native)
1390 152 : CALL k_to_grid_pairs(G_grid_k, fft_stencil_work, values_R=W_grid_R)
1391 : CALL add_gamma_corr_pairs(bs_env, full_cells, gwkp_env%gamma_correction%head_cell_tau(:, i_t), &
1392 152 : gwkp_env%Z_lP_zero(:, 1, 1), first_pair, W_grid_R, gwkp_env%gamma_correction, i_t)
1393 152 : tau = bs_env%time_frequency_grid%imaginary_time(i_t)
1394 318 : DO ispin = 1, bs_env%n_spin
1395 : ! Preserve the component order: occupied (2, -G W), then virtual (1, +G W).
1396 608 : DO i_component = 2, 1, -1
1397 : CALL build_green_pairs(bs_env, gwkp_env%band_orbitals(:, :, :, ispin), gwkp_env%group_env, &
1398 304 : tau, ispin, i_component == 2, fft_stencil_work, G_grid_k)
1399 : CALL k_to_grid_pairs(G_grid_k, fft_stencil_work, values_R=Sigma_grid_R, multiply_R=W_grid_R, &
1400 456 : product_scale=MERGE(-1.0_dp, 1.0_dp, i_component == 2), norm_squared=green_norms)
1401 304 : CALL gwkp_env%group_env%sum(green_norms)
1402 7424 : cell_active(:) = .NOT. (SQRT(green_norms)/REAL(bs_env%n_atom, dp)**2 < bs_env%eps_filter)
1403 : CALL execute_pair_fft(fft_stencil_work, G_grid_k, cell_active, &
1404 304 : 'GW self-energy (K2R extraction feeds R2K input)', Sigma_grid_R)
1405 : CALL self_energy_pairs_to_Bloch(bs_env, G_grid_k, Sigma_grid_R, gwkp_env%group_env, &
1406 : fft_stencil_work, gwkp_env%sigma_projection, gwkp_env%band_orbitals, &
1407 304 : ispin, sigma_band, .TRUE.)
1408 55016 : IF (gwkp_env%group_env%mepos == 0) gwkp_env%sigma_c_bands(:, :, i_t, i_component, ispin) = sigma_band
1409 : END DO
1410 : END DO
1411 : END DO
1412 14 : CALL release_pair_fft(fft_stencil_work)
1413 14 : gwkp_env%k_points_total = 0
1414 :
1415 112 : ALLOCATE (sigma_result_buffer(SIZE(gwkp_env%sigma_c_bands)))
1416 98 : sigma_result_buffer(:) = RESHAPE(gwkp_env%sigma_c_bands, [SIZE(gwkp_env%sigma_c_bands)])
1417 14 : CALL bs_env%para_env%sum(sigma_result_buffer)
1418 109854 : gwkp_env%sigma_c_bands(:, :, :, :, :) = RESHAPE(sigma_result_buffer, SHAPE(gwkp_env%sigma_c_bands))
1419 :
1420 0 : DEALLOCATE (full_cells, G_grid_k, &
1421 0 : sigma_band, sigma_result_buffer, Sigma_grid_R, &
1422 14 : W_grid_R, green_norms, cell_active)
1423 :
1424 14 : CALL timestop(handle)
1425 :
1426 28 : END SUBROUTINE compute_Sigma_c_from_W_PQ
1427 :
1428 : ! **************************************************************************************************
1429 : !> \brief Print how the imaginary-time points of χ or Σc are split into groups of MPI ranks and
1430 : !> start the progress count of the group of rank 0.
1431 : !> Each group computes every n_groups-th time point without communicating with the other groups.
1432 : !> Only rank 0 writes output, so the progress lines are those of the group of rank 0.
1433 : !> \param bs_env Band-structure settings.
1434 : !> \param quantity χ(iτ) or Σ_c(iτ).
1435 : !> \param counted_in What the progress counts.
1436 : !> \param n_groups Number of groups that compute time points in parallel.
1437 : !> \param group_size MPI ranks per group.
1438 : !> \param k_points_total Number of k and q points the group of rank 0 processes in the time loop.
1439 : ! **************************************************************************************************
1440 28 : SUBROUTINE start_time_loop_progress(bs_env, quantity, counted_in, n_groups, group_size, k_points_total)
1441 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1442 : CHARACTER(LEN=*), INTENT(IN) :: quantity, counted_in
1443 : INTEGER, INTENT(IN) :: n_groups, group_size
1444 : INTEGER(KIND=int_8), INTENT(IN) :: k_points_total
1445 :
1446 : INTEGER :: n_time
1447 :
1448 28 : bs_env%gwkp_env%k_points_done = 0
1449 28 : bs_env%gwkp_env%k_points_total = k_points_total
1450 28 : bs_env%gwkp_env%next_progress = 1
1451 28 : bs_env%gwkp_env%progress_start = m_walltime()
1452 28 : bs_env%gwkp_env%progress_label = quantity//' progress of group'
1453 28 : IF (bs_env%unit_nr <= 0) RETURN
1454 14 : n_time = bs_env%num_time_freq_points
1455 14 : IF (n_groups == 1) THEN
1456 0 : WRITE (bs_env%unit_nr, '(/,T2,3A,I0,A,I0,A)') 'GW ', quantity, ': all ', n_time, &
1457 0 : ' time points computed by one group of ', group_size, ' MPI ranks'
1458 : ELSE
1459 14 : WRITE (bs_env%unit_nr, '(/,T2,3A,I0,A,I0,A,I0,A)') 'GW ', quantity, ': ', n_time, &
1460 28 : ' time points split over ', n_groups, ' groups of ', group_size, ' MPI ranks'
1461 : END IF
1462 14 : WRITE (bs_env%unit_nr, '(T2,6A)') 'GW ', quantity, ': progress of one group, counted in ', counted_in
1463 14 : CALL m_flush(bs_env%unit_nr)
1464 :
1465 : END SUBROUTINE start_time_loop_progress
1466 :
1467 : ! **************************************************************************************************
1468 : !> \brief Count k points processed in the χ or Σc time loop and print the progress of the group of
1469 : !> rank 0 at 0.1, 0.3, 1, 3, 10, 30, 60, 90 and 100 %. Does nothing outside these loops.
1470 : !> \param bs_env Band-structure settings.
1471 : !> \param n_k_points Number of k points just processed by the group.
1472 : ! **************************************************************************************************
1473 1092 : SUBROUTINE count_time_loop_k_points(bs_env, n_k_points)
1474 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1475 : INTEGER(KIND=int_8), INTENT(IN) :: n_k_points
1476 :
1477 : TYPE(gwkp_type), POINTER :: gwkp_env
1478 :
1479 1092 : gwkp_env => bs_env%gwkp_env
1480 1092 : IF (gwkp_env%k_points_total == 0) RETURN
1481 1064 : gwkp_env%k_points_done = gwkp_env%k_points_done + n_k_points
1482 : CALL write_ri_rs_progress(bs_env%unit_nr, TRIM(gwkp_env%progress_label), gwkp_env%k_points_done, &
1483 1064 : gwkp_env%k_points_total, gwkp_env%next_progress, gwkp_env%progress_start)
1484 :
1485 : END SUBROUTINE count_time_loop_k_points
1486 :
1487 : ! **************************************************************************************************
1488 : !> \brief Form the unordered pairs of G(k,τ)=U(k,τ) U(k,τ)^H and exchange bounded k batches.
1489 : !> Each upper-triangle Gram tile writes directly to its destination's message. No full
1490 : !> grid matrix, conjugate lower triangle, or separate send-packing pass is formed.
1491 : !> The exchange sends each reusable inversion pair once; pair owners restore its partner.
1492 : !> \param bs_env ...
1493 : !> \param orbitals Band Bloch orbitals of one spin at the k points owned in env, MOD(k-1,P).
1494 : !> \param env Ranks that share the grid pairs and the band k points.
1495 : !> \param tau ...
1496 : !> \param ispin ...
1497 : !> \param occupied ...
1498 : !> \param work shared geometry and exchange layout
1499 : !> \param G_pairs unordered pairs in source-rank-major k order within each exchange
1500 : ! **************************************************************************************************
1501 622 : SUBROUTINE build_green_pairs(bs_env, orbitals, env, tau, ispin, occupied, work, G_pairs)
1502 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1503 : COMPLEX(KIND=dp), INTENT(IN) :: orbitals(:, :, :)
1504 : TYPE(mp_para_env_type), POINTER :: env
1505 : REAL(KIND=dp), INTENT(IN) :: tau
1506 : INTEGER, INTENT(IN) :: ispin
1507 : LOGICAL, INTENT(IN) :: occupied
1508 : TYPE(gw_pair_fft_work_type), INTENT(IN) :: work
1509 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(OUT), TARGET :: G_pairs(:, :, :)
1510 :
1511 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_green_pairs'
1512 :
1513 622 : COMPLEX(KIND=dp), ALLOCATABLE :: gram(:, :), scaled_coeff(:, :, :), &
1514 622 : sent(:)
1515 : INTEGER :: chunk_first, first_band, handle, i_band, i_batch, i_col, i_first, i_job, ikp, &
1516 : k_batch, n_active, n_bands, n_chunk, n_cols, n_grid, n_jobs, n_k, n_local, n_owned, &
1517 : n_pairs, n_rank, n_round, n_scheduled, unit_nr
1518 : INTEGER(KIND=int_8) :: active_bands, possible_bands
1519 622 : INTEGER, ALLOCATABLE :: active_columns(:), k_jobs(:), &
1520 622 : recv_counts(:), recv_displs(:)
1521 : REAL(KIND=dp) :: seconds(4), started, tau_E, total_started
1522 : TYPE(gwkp_type), POINTER :: gwkp_env
1523 :
1524 622 : CALL timeset(routineN, handle)
1525 : IF (fft_profile_enabled) total_started = m_walltime()
1526 : seconds = 0.0_dp
1527 622 : active_bands = 0_int_8
1528 622 : possible_bands = 0_int_8
1529 :
1530 622 : gwkp_env => bs_env%gwkp_env
1531 :
1532 622 : n_grid = bs_env%ri_rs%n_grid_points
1533 622 : n_k = bs_env%nkp_scf_desymm
1534 622 : n_local = SIZE(G_pairs, 1)
1535 622 : n_rank = env%num_pe
1536 622 : first_band = 1
1537 622 : n_bands = bs_env%n_occ(ispin)
1538 622 : IF (.NOT. occupied) THEN
1539 304 : first_band = n_bands + 1
1540 304 : n_bands = bs_env%n_ao - n_bands
1541 : END IF
1542 622 : CPASSERT(n_bands >= 0 .AND. n_bands <= bs_env%n_ao)
1543 622 : IF (n_bands == 0) THEN
1544 0 : G_pairs = z_zero
1545 0 : CALL timestop(handle)
1546 0 : RETURN
1547 : END IF
1548 622 : k_batch = work%k_batch
1549 1258 : n_pairs = SUM(work%pair_counts)
1550 622 : CPASSERT(n_local == work%pair_counts(env%mepos))
1551 622 : n_chunk = 1
1552 622 : !$ n_chunk = MIN(k_batch, omp_get_max_threads())
1553 4354 : ALLOCATE (scaled_coeff(n_grid, n_bands, n_chunk), active_columns(n_chunk))
1554 0 : ALLOCATE (sent(MAX(1, n_pairs*k_batch)), k_jobs(k_batch), &
1555 4976 : recv_counts(0:n_rank - 1), recv_displs(0:n_rank - 1))
1556 : IF (fft_profile_enabled) seconds(1) = m_walltime() - total_started
1557 : ! G_ll′(k,τ) = Σ_n [ψ_ln(k) exp(-|τ(ε_nk-ε_F)|/2)]
1558 : ! [ψ_l′n(k) exp(-|τ(ε_nk-ε_F)|/2)]*.
1559 1244 : DO i_first = 1, n_k, n_rank*k_batch
1560 622 : n_round = MIN(n_rank*k_batch, n_k - i_first + 1)
1561 : CALL pair_exchange_jobs(work, work%green_source, env, i_first, n_round, 0, &
1562 622 : k_jobs, n_owned, n_scheduled, recv_counts, recv_displs)
1563 622 : IF (n_scheduled == 0) THEN
1564 0 : CALL count_time_loop_k_points(bs_env, INT(n_round, int_8))
1565 0 : CYCLE
1566 : END IF
1567 : IF (fft_profile_enabled) started = m_walltime()
1568 : ! Scale at most one reciprocal point per worker, then distribute the Gram
1569 : ! column blocks independently so one owned k point can occupy the whole team.
1570 : !$OMP PARALLEL DEFAULT(NONE) IF(n_owned > 0) &
1571 : !$OMP SHARED(bs_env, first_band, gwkp_env, k_jobs, n_bands, n_chunk, n_grid, n_owned, n_rank) &
1572 : !$OMP SHARED(active_columns, orbitals, scaled_coeff, sent, tau, ispin, work) &
1573 : !$OMP PRIVATE(chunk_first, gram, i_band, i_batch, i_col, i_job, ikp, n_active, n_cols, n_jobs, tau_E) &
1574 622 : !$OMP REDUCTION(+:active_bands, possible_bands)
1575 : ALLOCATE (gram(n_grid, MIN(n_grid, column_block_cols)))
1576 : DO chunk_first = 1, n_owned, n_chunk
1577 : n_jobs = MIN(n_chunk, n_owned - chunk_first + 1)
1578 : !$OMP DO SCHEDULE(STATIC)
1579 : DO i_job = 1, n_jobs
1580 : ikp = k_jobs(chunk_first + i_job - 1)
1581 : n_active = 0
1582 : DO i_band = 1, n_bands
1583 : tau_E = ABS(0.5_dp*tau*(gwkp_env%eigenvalues_G(first_band + i_band - 1, ikp, ispin) - &
1584 : bs_env%e_fermi(ispin)))
1585 : IF (tau_E < bs_env%stabilize_exp) THEN
1586 : n_active = n_active + 1
1587 : scaled_coeff(:, n_active, i_job) = &
1588 : orbitals(:, first_band + i_band - 1, (ikp - 1)/n_rank + 1)*EXP(-tau_E)
1589 : END IF
1590 : END DO
1591 : active_columns(i_job) = n_active
1592 : IF (fft_profile_enabled) THEN
1593 : active_bands = active_bands + INT(n_active, int_8)
1594 : possible_bands = possible_bands + INT(n_bands, int_8)
1595 : END IF
1596 : END DO
1597 : !$OMP END DO
1598 : ! Successive column tiles grow in cost. Interleave them among workers.
1599 : !$OMP DO COLLAPSE(2) SCHEDULE(STATIC, 1)
1600 : DO i_job = 1, n_jobs
1601 : DO i_col = 1, n_grid, column_block_cols
1602 : i_batch = chunk_first + i_job - 1
1603 : n_cols = MIN(column_block_cols, n_grid - i_col + 1)
1604 : n_active = active_columns(i_job)
1605 : IF (n_active == 0) THEN
1606 : gram(:, :) = z_zero
1607 : ELSE
1608 : CALL zherk('U', 'N', n_cols, n_active, 1.0_dp, scaled_coeff(i_col, 1, i_job), n_grid, &
1609 : 0.0_dp, gram(i_col, 1), n_grid)
1610 : IF (i_col > 1) THEN
1611 : CALL zgemm('N', 'C', i_col - 1, n_cols, n_active, z_one, scaled_coeff(:, :, i_job), n_grid, &
1612 : scaled_coeff(i_col, 1, i_job), n_grid, z_zero, gram, n_grid)
1613 : END IF
1614 : END IF
1615 : CALL pack_grid_pair_panel(work, i_col, n_cols, gram, n_owned, i_batch, sent)
1616 : END DO
1617 : END DO
1618 : !$OMP END DO
1619 : END DO
1620 622 : DEALLOCATE (gram)
1621 : !$OMP END PARALLEL
1622 : IF (fft_profile_enabled) seconds(2) = seconds(2) + m_walltime() - started
1623 : IF (fft_profile_enabled) started = m_walltime()
1624 : CALL exchange_pair_round(work, work%green_source, env, i_first, n_round, 0, &
1625 622 : n_owned, sent, recv_counts, recv_displs, G_pairs)
1626 : IF (fft_profile_enabled) seconds(3) = seconds(3) + m_walltime() - started
1627 1244 : CALL count_time_loop_k_points(bs_env, INT(n_round, int_8))
1628 : END DO
1629 622 : IF (work%green_reuses > 0) THEN
1630 : IF (fft_profile_enabled) started = m_walltime()
1631 622 : CALL restore_pair_conjugates(work%green_source, G_pairs)
1632 : IF (fft_profile_enabled) seconds(3) = seconds(3) + m_walltime() - started
1633 : END IF
1634 : IF (fft_profile_enabled) seconds(4) = m_walltime() - total_started
1635 622 : unit_nr = 0
1636 : IF (fft_profile_enabled) unit_nr = cp_logger_get_default_io_unit()
1637 : IF (unit_nr > 0) THEN
1638 : WRITE (unit_nr, '(T2,A)') 'GW_GREEN_PROFILE| logger-rank wall seconds, no MPI reduction'
1639 : WRITE (unit_nr, '(T2,A,L2,2I12)') 'GW_GREEN_PROFILE| occupied / k points / local pairs: ', occupied, n_k, n_local
1640 : WRITE (unit_nr, '(T2,A,2I16)') 'GW_GREEN_PROFILE| active / possible local band columns: ', active_bands, possible_bands
1641 : WRITE (unit_nr, '(T2,A,I12)') 'GW_GREEN_PROFILE| k points per rank / exchange: ', k_batch
1642 : WRITE (unit_nr, '(T2,A,I12)') 'GW_GREEN_PROFILE| conjugate k points reused: ', work%green_reuses
1643 : WRITE (unit_nr, '(T2,A,4F12.6)') 'GW_GREEN_PROFILE| setup/build/transfer/total: ', seconds
1644 : END IF
1645 :
1646 622 : CALL timestop(handle)
1647 :
1648 1244 : END SUBROUTINE build_green_pairs
1649 :
1650 : ! **************************************************************************************************
1651 : !> \brief Schedule only the representatives in one rank-blocked reciprocal batch.
1652 : !> \param work FFT input order and pair ownership
1653 : !> \param source representative slot for every FFT input column
1654 : !> \param env pair and reciprocal point owners
1655 : !> \param first first FFT input column of the batch
1656 : !> \param n_round number of FFT input columns of the batch
1657 : !> \param owner_shift shift of cyclic source ownership (zero for k, one for q)
1658 : !> \param jobs native reciprocal indices to compute on this rank
1659 : !> \param n_owned number of local jobs
1660 : !> \param n_scheduled number of jobs over all ranks
1661 : !> \param recv_counts ...
1662 : !> \param recv_displs ...
1663 : ! **************************************************************************************************
1664 788 : SUBROUTINE pair_exchange_jobs(work, source, env, first, n_round, owner_shift, &
1665 788 : jobs, n_owned, n_scheduled, recv_counts, recv_displs)
1666 : TYPE(gw_pair_fft_work_type), INTENT(IN) :: work
1667 : INTEGER, INTENT(IN) :: source(:)
1668 : TYPE(mp_para_env_type), INTENT(IN) :: env
1669 : INTEGER, INTENT(IN) :: first, n_round, owner_shift
1670 : INTEGER, INTENT(OUT) :: jobs(:), n_owned, n_scheduled, &
1671 : recv_counts(0:), recv_displs(0:)
1672 :
1673 : INTEGER :: block_first, block_size, i_input, &
1674 : i_rank, n_local, n_rank, packed, sender
1675 :
1676 788 : n_rank = env%num_pe
1677 788 : n_local = work%pair_counts(env%mepos)
1678 788 : n_owned = 0
1679 788 : n_scheduled = 0
1680 788 : block_first = first
1681 1604 : DO i_rank = 0, n_rank - 1
1682 816 : sender = MODULO(i_rank + owner_shift, n_rank)
1683 816 : block_size = (n_round + n_rank - 1 - i_rank)/n_rank
1684 816 : packed = 0
1685 19112 : DO i_input = block_first, block_first + block_size - 1
1686 18296 : IF (source(i_input) /= i_input) CYCLE
1687 9252 : packed = packed + 1
1688 10068 : IF (sender == env%mepos) jobs(packed) = work%input_to_native(i_input)
1689 : END DO
1690 816 : recv_counts(sender) = n_local*packed
1691 816 : recv_displs(sender) = n_local*(block_first - first)
1692 816 : IF (sender == env%mepos) n_owned = packed
1693 816 : n_scheduled = n_scheduled + packed
1694 1604 : block_first = block_first + block_size
1695 : END DO
1696 788 : END SUBROUTINE pair_exchange_jobs
1697 :
1698 : ! **************************************************************************************************
1699 : !> \brief Receive one batch directly into its FFT input and expand the packed source blocks in place.
1700 : !> \param work FFT input order and pair ownership
1701 : !> \param source representative slot for every FFT input column
1702 : !> \param env pair and reciprocal point owners
1703 : !> \param first first FFT input column of the batch
1704 : !> \param n_round number of FFT input columns of the batch
1705 : !> \param owner_shift shift of cyclic source ownership (zero for k, one for q)
1706 : !> \param n_owned number of local jobs
1707 : !> \param sent grid pairs packed by destination rank
1708 : !> \param recv_counts ...
1709 : !> \param recv_displs ...
1710 : !> \param pairs partially filled inverse FFT input
1711 : ! **************************************************************************************************
1712 1576 : SUBROUTINE exchange_pair_round(work, source, env, first, n_round, owner_shift, &
1713 788 : n_owned, sent, recv_counts, recv_displs, pairs)
1714 : TYPE(gw_pair_fft_work_type), INTENT(IN) :: work
1715 : INTEGER, INTENT(IN) :: source(:)
1716 : TYPE(mp_para_env_type), INTENT(IN) :: env
1717 : INTEGER, INTENT(IN) :: first, n_round, owner_shift, n_owned
1718 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(IN) :: sent(:)
1719 : INTEGER, CONTIGUOUS, INTENT(IN) :: recv_counts(0:), recv_displs(0:)
1720 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(INOUT), &
1721 : TARGET :: pairs(:, :, :)
1722 :
1723 : COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: received(:)
1724 : COMPLEX(KIND=dp), TARGET :: unused_receive(1)
1725 : INTEGER :: block_first, block_size, i_input, i_rank, n_local, n_rank, packed, sender, &
1726 788 : send_displs(0:env%num_pe - 1), send_counts(0:env%num_pe - 1)
1727 :
1728 788 : n_rank = env%num_pe
1729 788 : n_local = SIZE(pairs, 1)
1730 1604 : send_counts(:) = work%pair_counts*n_owned
1731 1604 : send_displs(:) = work%pair_displs*n_owned
1732 788 : unused_receive = z_zero
1733 788 : received => unused_receive
1734 788 : IF (n_local > 0) received(1:n_local*n_round) => pairs(:, :, first:first + n_round - 1)
1735 788 : CALL env%alltoall(sent, send_counts, send_displs, received, recv_counts, recv_displs)
1736 788 : IF (n_local == 0) RETURN
1737 :
1738 : ! Expand from the last column to preserve data still to be read.
1739 1604 : DO i_rank = 0, n_rank - 1
1740 816 : sender = MODULO(i_rank + owner_shift, n_rank)
1741 816 : block_first = first + recv_displs(sender)/n_local
1742 816 : block_size = (n_round + n_rank - 1 - i_rank)/n_rank
1743 816 : packed = block_first + recv_counts(sender)/n_local - 1
1744 19900 : DO i_input = block_first + block_size - 1, block_first, -1
1745 18296 : IF (source(i_input) /= i_input) CYCLE
1746 9252 : IF (i_input /= packed) pairs(:, 1, i_input) = pairs(:, 1, packed)
1747 19112 : packed = packed - 1
1748 : END DO
1749 : END DO
1750 : END SUBROUTINE exchange_pair_round
1751 :
1752 : ! **************************************************************************************************
1753 : !> \brief Complete the inverse FFT input after all representatives have been exchanged.
1754 : !> \param source representative slot for every FFT input column
1755 : !> \param pairs partially filled inverse FFT input
1756 : ! **************************************************************************************************
1757 788 : SUBROUTINE restore_pair_conjugates(source, pairs)
1758 : INTEGER, INTENT(IN) :: source(:)
1759 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(INOUT) :: pairs(:, :, :)
1760 :
1761 : INTEGER :: i_input
1762 :
1763 788 : IF (SIZE(pairs, 1) == 0) RETURN
1764 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) IF(SIZE(pairs, 1) > 256) &
1765 788 : !$OMP SHARED(pairs, source) PRIVATE(i_input)
1766 : DO i_input = 1, SIZE(source)
1767 : IF (source(i_input) /= i_input) pairs(:, 1, i_input) = CONJG(pairs(:, 1, source(i_input)))
1768 : END DO
1769 : !$OMP END PARALLEL DO
1770 : END SUBROUTINE restore_pair_conjugates
1771 :
1772 : ! **************************************************************************************************
1773 : !> \brief Packed unordered grid-pair blocks of all ranks, as assigned by block_range.
1774 : !> Rank r owns the pairs displs(r)+1, ..., displs(r)+counts(r).
1775 : !> \param n_grid ...
1776 : !> \param n_rank ...
1777 : !> \param counts ...
1778 : !> \param displs ...
1779 : ! **************************************************************************************************
1780 194 : SUBROUTINE grid_pair_blocks(n_grid, n_rank, counts, displs)
1781 : INTEGER, INTENT(IN) :: n_grid, n_rank
1782 : INTEGER, ALLOCATABLE, INTENT(OUT) :: counts(:), displs(:)
1783 :
1784 : INTEGER :: i_rank
1785 :
1786 776 : ALLOCATE (counts(0:n_rank - 1), displs(0:n_rank - 1))
1787 402 : DO i_rank = 0, n_rank - 1
1788 402 : CALL block_range(INT(INT(n_grid, int_8)*(n_grid + 1)/2), i_rank, n_rank, displs(i_rank), counts(i_rank))
1789 : END DO
1790 402 : displs(:) = displs - 1
1791 :
1792 194 : END SUBROUTINE grid_pair_blocks
1793 :
1794 : ! **************************************************************************************************
1795 : !> \brief Bound one exchange by its send buffer and the MPI wrapper's byte counter.
1796 : !> \param n_grid ...
1797 : !> \param n_k ...
1798 : !> \param n_rank ...
1799 : !> \return source k points per rank
1800 : ! **************************************************************************************************
1801 402 : PURE INTEGER FUNCTION pair_exchange_width(n_grid, n_k, n_rank) RESULT(width)
1802 : INTEGER, INTENT(IN) :: n_grid, n_k, n_rank
1803 :
1804 : REAL(KIND=dp) :: pair_elements, recv_elements
1805 :
1806 402 : pair_elements = REAL(n_grid, dp)*REAL(n_grid + 1, dp)/2.0_dp
1807 402 : recv_elements = REAL(CEILING(pair_elements/REAL(n_rank, dp)), dp)*REAL(n_rank, dp)
1808 : width = MAX(1, MIN((n_k + n_rank - 1)/n_rank, INT(projection_round_bytes/(complex_bytes*pair_elements)), &
1809 402 : INT(REAL(HUGE(0), dp)/(complex_bytes*(pair_elements + recv_elements)))))
1810 402 : END FUNCTION pair_exchange_width
1811 :
1812 : ! **************************************************************************************************
1813 : !> \brief Write each upper-triangular column segment directly into its rank's message.
1814 : !> \param work ...
1815 : !> \param first_col ...
1816 : !> \param n_cols ...
1817 : !> \param panel ...
1818 : !> \param n_owned source points in this batch
1819 : !> \param i_batch point within the batch
1820 : !> \param sent destination-rank-major send buffer
1821 : ! **************************************************************************************************
1822 17874 : SUBROUTINE pack_grid_pair_panel(work, first_col, n_cols, panel, n_owned, i_batch, sent)
1823 : TYPE(gw_pair_fft_work_type), INTENT(IN) :: work
1824 : INTEGER, INTENT(IN) :: first_col, n_cols
1825 : COMPLEX(KIND=dp), INTENT(IN) :: panel(:, :)
1826 : INTEGER, INTENT(IN) :: n_owned, i_batch
1827 : COMPLEX(KIND=dp), INTENT(INOUT) :: sent(:)
1828 :
1829 : INTEGER :: col, i_rank, j_col, offset, row_first, &
1830 : row_last, segment
1831 :
1832 335070 : DO col = 1, n_cols
1833 317196 : j_col = first_col + col - 1
1834 652374 : DO segment = work%column_segments(j_col), work%column_segments(j_col + 1) - 1
1835 317304 : i_rank = work%segments(segment)%rank
1836 317304 : row_first = work%segments(segment)%first_row
1837 317304 : row_last = work%segments(segment)%last_row
1838 : offset = work%pair_displs(i_rank)*n_owned + (i_batch - 1)*work%pair_counts(i_rank) + &
1839 317304 : work%segments(segment)%displacement
1840 6468606 : sent(offset + 1:offset + row_last - row_first + 1) = panel(row_first:row_last, col)
1841 : END DO
1842 : END DO
1843 17874 : END SUBROUTINE pack_grid_pair_panel
1844 :
1845 : ! **************************************************************************************************
1846 : !> \brief Cache grid values of every Bloch molecular orbital without replicating all MO matrices.
1847 : !> \param bs_env ...
1848 : !> \param phi_lmu_k ...
1849 : !> \param psi_lm_k ...
1850 : ! **************************************************************************************************
1851 14 : SUBROUTINE build_all_psi_k(bs_env, phi_lmu_k, psi_lm_k)
1852 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1853 : COMPLEX(KIND=dp), DIMENSION(:, :, :), INTENT(IN) :: phi_lmu_k
1854 : COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: psi_lm_k(:, :, :, :)
1855 :
1856 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_all_psi_k'
1857 :
1858 14 : COMPLEX(KIND=dp), ALLOCATABLE :: mo_coeff(:, :, :, :)
1859 : INTEGER :: handle, ikp_local, ispin
1860 :
1861 14 : CALL timeset(routineN, handle)
1862 :
1863 14 : CALL owned_mo_coefficients(bs_env, bs_env%cfm_mo_coeff_kp(1:bs_env%nkp_scf_desymm, :), mo_coeff)
1864 0 : ALLOCATE (psi_lm_k(bs_env%ri_rs%n_grid_points, bs_env%n_ao, SIZE(phi_lmu_k, 3), &
1865 84 : bs_env%n_spin), SOURCE=z_zero)
1866 28 : DO ispin = 1, bs_env%n_spin
1867 152 : DO ikp_local = 1, SIZE(mo_coeff, 3)
1868 585170 : psi_lm_k(:, :, ikp_local, ispin) = MATMUL(phi_lmu_k(:, :, ikp_local), mo_coeff(:, :, ikp_local, ispin))
1869 : END DO
1870 : END DO
1871 14 : DEALLOCATE (mo_coeff)
1872 :
1873 14 : CALL timestop(handle)
1874 :
1875 14 : END SUBROUTINE build_all_psi_k
1876 :
1877 : ! **************************************************************************************************
1878 : !> \brief Cache Bloch sums of the RI-RS grid values for the k-points owned by this rank.
1879 : !> \param bs_env ...
1880 : !> \param phi_lmu_k ...
1881 : ! **************************************************************************************************
1882 14 : SUBROUTINE build_all_phi_k(bs_env, phi_lmu_k)
1883 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
1884 : COMPLEX(dp), ALLOCATABLE, INTENT(OUT) :: phi_lmu_k(:, :, :)
1885 :
1886 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_all_phi_k'
1887 :
1888 : INTEGER :: first, handle, i_R, ikp, image_width, &
1889 : last, n_k_local
1890 14 : INTEGER, ALLOCATABLE :: owned_k(:)
1891 14 : REAL(dp), ALLOCATABLE :: phi_R(:, :, :)
1892 :
1893 14 : CALL timeset(routineN, handle)
1894 :
1895 : n_k_local = (bs_env%nkp_scf_desymm + bs_env%para_env%num_pe - 1 - &
1896 14 : bs_env%para_env%mepos)/bs_env%para_env%num_pe
1897 70 : ALLOCATE (phi_lmu_k(bs_env%ri_rs%n_grid_points, bs_env%n_ao, n_k_local))
1898 14 : phi_lmu_k = z_zero
1899 14 : IF (n_k_local > 0) THEN
1900 42 : ALLOCATE (owned_k(n_k_local))
1901 276 : owned_k(:) = [(ikp, ikp=bs_env%para_env%mepos + 1, bs_env%nkp_scf_desymm, bs_env%para_env%num_pe)]
1902 : ! Retain a bounded panel of AO images; each image is sampled once.
1903 : image_width = MAX(1, MIN(SIZE(bs_env%index_to_cell_3c, 2), &
1904 : INT(projection_workspace/(real_bytes* &
1905 14 : REAL(bs_env%ri_rs%n_grid_points, dp)*REAL(bs_env%n_ao, dp)))))
1906 70 : ALLOCATE (phi_R(bs_env%ri_rs%n_grid_points, bs_env%n_ao, image_width))
1907 14 : DO first = 1, SIZE(bs_env%index_to_cell_3c, 2), image_width
1908 14 : last = MIN(first + image_width - 1, SIZE(bs_env%index_to_cell_3c, 2))
1909 4364 : DO i_R = first, last
1910 4364 : CALL evaluate_mu_rl_R_values(bs_env, bs_env%ri_rs%grid_points, i_R, phi_R(:, :, i_R - first + 1))
1911 : END DO
1912 : CALL cell_to_k_transform(phi_R(:, :, 1:last - first + 1), &
1913 : bs_env%index_to_cell_3c(:, first:last), &
1914 : bs_env%kpoints_scf_desymm%xkp(:, owned_k), &
1915 : bs_env%kpoints_scf_desymm%nkp_grid, phi_lmu_k, &
1916 510 : bs_env%kpoints_scf_desymm%rskp_transform, accumulate=.TRUE., profile_fft=fft_profile_enabled)
1917 : END DO
1918 : END IF
1919 :
1920 14 : CALL timestop(handle)
1921 :
1922 28 : END SUBROUTINE build_all_phi_k
1923 :
1924 : ! **************************************************************************************************
1925 : !> \brief Fourier transform distributed real RI lattice blocks to cyclic q owners in one exchange.
1926 : !>
1927 : !> I_PQ(q) = Σ_R exp(2πiq·R) I_PQ(R). Each sender transforms its own matrix block
1928 : !> for the retained q points with the selected RSKP backend and sends the blocks to their q owners, iq mod P.
1929 : !> \param fm_R ...
1930 : !> \param cells ...
1931 : !> \param qpoints ...
1932 : !> \param nkp_grid regular q mesh of qpoints
1933 : !> \param q_slot compact owner-local indices; zero omits a q point
1934 : !> \param matrices_q ...
1935 : !> \param backend GEMM or FFT
1936 : ! **************************************************************************************************
1937 14 : SUBROUTINE ri_2c_lattice_to_owned_q(fm_R, cells, qpoints, nkp_grid, q_slot, matrices_q, backend)
1938 : TYPE(cp_fm_type), INTENT(IN) :: fm_R(:)
1939 : INTEGER, INTENT(IN) :: cells(:, :)
1940 : REAL(KIND=dp), INTENT(IN) :: qpoints(:, :)
1941 : INTEGER, INTENT(IN) :: nkp_grid(3), q_slot(:)
1942 : COMPLEX(KIND=dp), INTENT(OUT) :: matrices_q(:, :, :)
1943 : INTEGER, INTENT(IN) :: backend
1944 :
1945 : CHARACTER(LEN=*), PARAMETER :: routineN = 'ri_2c_lattice_to_owned_q'
1946 :
1947 14 : COMPLEX(KIND=dp), ALLOCATABLE :: recv_buffer(:)
1948 14 : COMPLEX(KIND=dp), ALLOCATABLE, TARGET :: send_buffer(:)
1949 14 : COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: values_q(:, :, :)
1950 : INTEGER :: dest, handle, icell, icol, iq, iq_local, &
1951 : irow, n_q, n_q_local, n_RI, &
1952 : n_selected, ncols, nprocs, nrecv, &
1953 : nrows, nsend, pos, source
1954 14 : INTEGER, ALLOCATABLE :: block_metadata(:), q_order(:), recv_counts(:), recv_displs(:), &
1955 14 : send_counts(:), send_displs(:), source_cols(:, :), source_rows(:, :), source_sizes(:, :)
1956 14 : INTEGER, POINTER :: cols(:), rows(:)
1957 14 : REAL(KIND=dp), ALLOCATABLE :: values_R(:, :, :)
1958 : TYPE(mp_para_env_type), POINTER :: para_env
1959 :
1960 14 : CALL timeset(routineN, handle)
1961 :
1962 14 : CPASSERT(SIZE(fm_R) > 0)
1963 14 : CPASSERT(SIZE(cells, 1) == 3 .AND. SIZE(cells, 2) == SIZE(fm_R))
1964 14 : CPASSERT(SIZE(qpoints, 1) == 3)
1965 14 : n_q = SIZE(qpoints, 2)
1966 14 : n_RI = SIZE(matrices_q, 1)
1967 14 : CPASSERT(SIZE(matrices_q, 2) == n_RI)
1968 14 : para_env => fm_R(1)%matrix_struct%para_env
1969 14 : nprocs = para_env%num_pe
1970 138 : n_q_local = COUNT(q_slot(MODULO(para_env%mepos - 1, nprocs) + 1:n_q:nprocs) > 0)
1971 262 : n_selected = COUNT(q_slot > 0)
1972 14 : CPASSERT(SIZE(matrices_q, 3) == n_q_local)
1973 : CALL cp_fm_get_info(fm_R(1), nrow_local=nrows, ncol_local=ncols, &
1974 14 : row_indices=rows, col_indices=cols)
1975 :
1976 : ALLOCATE (block_metadata(2 + 2*n_RI), source_sizes(2, 0:nprocs - 1), &
1977 140 : source_rows(n_RI, 0:nprocs - 1), source_cols(n_RI, 0:nprocs - 1))
1978 42 : DO source = 0, nprocs - 1
1979 28 : block_metadata = 0
1980 28 : IF (para_env%mepos == source) THEN
1981 42 : block_metadata(1:2) = [nrows, ncols]
1982 152 : block_metadata(3:2 + nrows) = rows(1:nrows)
1983 290 : block_metadata(3 + n_RI:2 + n_RI + ncols) = cols(1:ncols)
1984 : END IF
1985 28 : CALL para_env%bcast(block_metadata, source)
1986 84 : source_sizes(:, source) = block_metadata(1:2)
1987 580 : source_rows(:, source) = block_metadata(3:2 + n_RI)
1988 594 : source_cols(:, source) = block_metadata(3 + n_RI:2 + 2*n_RI)
1989 : END DO
1990 :
1991 : ! The q points sorted by owner make the transformed blocks the send buffer itself.
1992 : ALLOCATE (q_order(n_selected), send_counts(0:nprocs - 1), recv_counts(0:nprocs - 1), &
1993 112 : send_displs(0:nprocs - 1), recv_displs(0:nprocs - 1))
1994 14 : pos = 0
1995 42 : DO dest = 0, nprocs - 1
1996 28 : send_displs(dest) = pos*nrows*ncols
1997 524 : DO iq = 1, n_q
1998 496 : IF (q_slot(iq) == 0 .OR. MODULO(iq, nprocs) /= dest) CYCLE
1999 126 : pos = pos + 1
2000 524 : q_order(pos) = iq
2001 : END DO
2002 42 : send_counts(dest) = pos*nrows*ncols - send_displs(dest)
2003 : END DO
2004 14 : nsend = n_selected*nrows*ncols
2005 14 : CALL para_env%alltoall(send_counts, recv_counts, 1)
2006 14 : recv_displs(0) = 0
2007 28 : DO source = 1, nprocs - 1
2008 28 : recv_displs(source) = recv_displs(source - 1) + recv_counts(source - 1)
2009 : END DO
2010 14 : nrecv = recv_displs(nprocs - 1) + recv_counts(nprocs - 1)
2011 70 : ALLOCATE (send_buffer(MAX(1, nsend)), recv_buffer(MAX(1, nrecv)))
2012 14 : IF (nsend > 0) THEN
2013 70 : ALLOCATE (values_R(nrows, ncols, SIZE(fm_R)))
2014 2588 : DO icell = 1, SIZE(fm_R)
2015 411260 : values_R(:, :, icell) = fm_R(icell)%local_data(1:nrows, 1:ncols)
2016 : END DO
2017 14 : values_q(1:nrows, 1:ncols, 1:n_selected) => send_buffer(1:nsend)
2018 : CALL cell_to_k_transform(values_R, cells, qpoints(:, q_order), nkp_grid, values_q, backend, &
2019 518 : profile_fft=fft_profile_enabled)
2020 14 : DEALLOCATE (values_R)
2021 : END IF
2022 : CALL para_env%alltoall(send_buffer, send_counts, send_displs, &
2023 14 : recv_buffer, recv_counts, recv_displs)
2024 16871 : matrices_q = z_zero
2025 42 : DO source = 0, nprocs - 1
2026 28 : nrows = source_sizes(1, source)
2027 28 : ncols = source_sizes(2, source)
2028 28 : CPASSERT(recv_counts(source) == nrows*ncols*n_q_local)
2029 28 : pos = recv_displs(source)
2030 168 : DO iq_local = 1, n_q_local
2031 1962 : DO icol = 1, ncols
2032 17838 : DO irow = 1, nrows
2033 : matrices_q(source_rows(irow, source), source_cols(icol, source), iq_local) = &
2034 17712 : recv_buffer(pos + (icol - 1)*nrows + irow)
2035 : END DO
2036 : END DO
2037 154 : pos = pos + nrows*ncols
2038 : END DO
2039 : END DO
2040 :
2041 14 : CALL timestop(handle)
2042 :
2043 42 : END SUBROUTINE ri_2c_lattice_to_owned_q
2044 :
2045 : ! **************************************************************************************************
2046 : !> \brief Evaluate the band Bloch orbitals of the k points owned by this rank, MOD(k-1,P), one AO
2047 : !> cell at a time. No rank stores the orbitals of other k points.
2048 : !> \param bs_env ...
2049 : !> \param orbitals orbitals(:, :, (k-1)/P+1, spin) on the RI-RS grid
2050 : ! **************************************************************************************************
2051 14 : SUBROUTINE collect_band_bloch_orbitals(bs_env, orbitals)
2052 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2053 : COMPLEX(dp), ALLOCATABLE, INTENT(OUT) :: orbitals(:, :, :, :)
2054 :
2055 : CHARACTER(LEN=*), PARAMETER :: routineN = 'collect_band_bloch_orbitals'
2056 :
2057 14 : COMPLEX(dp), ALLOCATABLE :: coefficients(:, :, :, :), phi_k(:, :)
2058 : INTEGER :: handle, i_kp, i_local, i_R, ispin, &
2059 : n_grid, n_own, n_partner
2060 : REAL(dp) :: available_GB, orbital_bytes
2061 14 : REAL(dp), ALLOCATABLE :: phi_R(:, :)
2062 : TYPE(mp_para_env_type), POINTER :: env
2063 :
2064 14 : CALL timeset(routineN, handle)
2065 :
2066 14 : env => bs_env%para_env
2067 14 : n_grid = bs_env%ri_rs%n_grid_points
2068 14 : CALL owned_mo_coefficients(bs_env, bs_env%cfm_mo_coeff_kp, coefficients)
2069 14 : n_own = SIZE(coefficients, 3)
2070 14 : n_partner = 0
2071 138 : DO i_kp = env%mepos + 1, bs_env%nkp_scf_desymm, env%num_pe
2072 138 : IF (bs_env%gwkp_env%q_partner(i_kp) > i_kp) n_partner = n_partner + 1
2073 : END DO
2074 : orbital_bytes = REAL(n_grid, dp)*REAL(bs_env%n_ao, dp)* &
2075 14 : (complex_bytes*REAL((n_own + n_partner)*bs_env%n_spin, dp) + complex_bytes + real_bytes)
2076 14 : CALL mp_mem_avail_per_rank_GB(env, available_GB)
2077 14 : CALL env%min(available_GB)
2078 14 : CALL env%max(orbital_bytes)
2079 14 : IF (orbital_bytes > stage_memory_share*MAX(0.0_dp, available_GB)*bytes_per_GB) THEN
2080 0 : CPABORT("Insufficient memory for GW band-path orbitals")
2081 : END IF
2082 56 : ALLOCATE (phi_R(n_grid, bs_env%n_ao))
2083 84 : ALLOCATE (orbitals(n_grid, bs_env%n_ao, n_own, bs_env%n_spin))
2084 14 : orbitals = z_zero
2085 4364 : DO i_R = 1, SIZE(bs_env%index_to_cell_3c, 2)
2086 4350 : IF (n_own == 0) EXIT
2087 4350 : CALL evaluate_mu_rl_R_values(bs_env, bs_env%ri_rs%grid_points, i_R, phi_R)
2088 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
2089 4364 : !$OMP SHARED(bs_env, env, i_R, n_own, orbitals, phi_R) PRIVATE(i_kp, i_local)
2090 : DO i_local = 1, n_own
2091 : i_kp = env%mepos + 1 + (i_local - 1)*env%num_pe
2092 : orbitals(:, :, i_local, 1) = orbitals(:, :, i_local, 1) + phi_R* &
2093 : EXP(gaussi*twopi*SUM(bs_env%kpoints_DOS%xkp(:, i_kp)* &
2094 : REAL(bs_env%index_to_cell_3c(:, i_R), dp)))
2095 : END DO
2096 : !$OMP END PARALLEL DO
2097 : END DO
2098 : !$OMP PARALLEL DEFAULT(NONE) SHARED(bs_env, coefficients, n_grid, n_own, orbitals) &
2099 14 : !$OMP PRIVATE(i_local, ispin, phi_k)
2100 : ALLOCATE (phi_k(n_grid, bs_env%n_ao))
2101 : !$OMP DO SCHEDULE(STATIC)
2102 : DO i_local = 1, n_own
2103 : phi_k(:, :) = orbitals(:, :, i_local, 1)
2104 : DO ispin = 1, bs_env%n_spin
2105 : CALL zgemm('N', 'N', n_grid, bs_env%n_ao, bs_env%n_ao, z_one, &
2106 : phi_k, n_grid, coefficients(:, :, i_local, ispin), &
2107 : bs_env%n_ao, z_zero, orbitals(:, :, i_local, ispin), n_grid)
2108 : END DO
2109 : END DO
2110 : !$OMP END DO
2111 : DEALLOCATE (phi_k)
2112 : !$OMP END PARALLEL
2113 :
2114 14 : CALL timestop(handle)
2115 :
2116 28 : END SUBROUTINE collect_band_bloch_orbitals
2117 :
2118 : ! **************************************************************************************************
2119 : !> \brief Move the band orbitals from their owners in all ranks, MOD(k-1,P), to their owners in
2120 : !> the time groups of Σc, MOD(k-1,g). As g divides P, both owners have the same rank within their
2121 : !> group, so every k point is broadcast once among the ranks with this group rank.
2122 : !> \param bs_env ...
2123 : !> \param gwkp_env ...
2124 : ! **************************************************************************************************
2125 14 : SUBROUTINE band_orbitals_to_groups(bs_env, gwkp_env)
2126 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2127 : TYPE(gwkp_type), INTENT(INOUT) :: gwkp_env
2128 :
2129 : CHARACTER(LEN=*), PARAMETER :: routineN = 'band_orbitals_to_groups'
2130 :
2131 14 : COMPLEX(dp), ALLOCATABLE :: group_orbitals(:, :, :, :)
2132 : INTEGER :: handle, i_kp, i_local, i_root, ispin, &
2133 : n_group, n_own, n_rank
2134 : TYPE(mp_comm_type) :: column_env
2135 :
2136 14 : CALL timeset(routineN, handle)
2137 :
2138 14 : n_rank = bs_env%para_env%num_pe
2139 14 : n_group = gwkp_env%group_env%num_pe
2140 14 : n_own = (bs_env%nkp_bs_and_DOS - gwkp_env%group_env%mepos + n_group - 1)/n_group
2141 : ALLOCATE (group_orbitals(SIZE(gwkp_env%band_orbitals, 1), SIZE(gwkp_env%band_orbitals, 2), &
2142 84 : n_own, bs_env%n_spin))
2143 14 : CALL column_env%from_split(bs_env%para_env, gwkp_env%group_env%mepos)
2144 304 : DO i_local = 1, n_own
2145 290 : i_kp = gwkp_env%group_env%mepos + 1 + (i_local - 1)*n_group
2146 290 : i_root = MODULO(i_kp - 1, n_rank)/n_group
2147 594 : DO ispin = 1, bs_env%n_spin
2148 290 : IF (column_env%mepos == i_root) THEN
2149 26927 : group_orbitals(:, :, i_local, ispin) = gwkp_env%band_orbitals(:, :, (i_kp - 1)/n_rank + 1, ispin)
2150 : END IF
2151 580 : CALL column_env%bcast(group_orbitals(:, :, i_local, ispin), i_root)
2152 : END DO
2153 : END DO
2154 14 : CALL column_env%free()
2155 14 : CALL MOVE_ALLOC(group_orbitals, gwkp_env%band_orbitals)
2156 :
2157 14 : CALL timestop(handle)
2158 :
2159 14 : END SUBROUTINE band_orbitals_to_groups
2160 :
2161 : ! **************************************************************************************************
2162 : !> \brief Return the SCF-mesh index of a band k point, or 0 if it lies off the mesh.
2163 : !> The first nkp_scf_desymm band points are the SCF mesh in the same order.
2164 : !> \param bs_env ...
2165 : !> \param i_kp_eval Band k point.
2166 : !> \return ...
2167 : ! **************************************************************************************************
2168 580 : FUNCTION native_q_index(bs_env, i_kp_eval) RESULT(native_iq)
2169 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2170 : INTEGER, INTENT(IN) :: i_kp_eval
2171 : INTEGER :: native_iq
2172 :
2173 : INTEGER :: iq
2174 : REAL(KIND=dp) :: delta_k(3)
2175 :
2176 580 : native_iq = 0
2177 11872 : DO iq = 1, bs_env%nkp_scf_desymm
2178 11800 : IF (i_kp_eval <= bs_env%nkp_scf_desymm .AND. iq /= i_kp_eval) CYCLE
2179 7664 : delta_k = bs_env%kpoints_DOS%xkp(:, i_kp_eval) - bs_env%kpoints_scf_desymm%xkp(:, iq)
2180 7736 : IF (MAXVAL(ABS(delta_k - ANINT(delta_k))) <= 64.0_dp*EPSILON(1.0_dp)) THEN
2181 580 : native_iq = iq
2182 : RETURN
2183 : END IF
2184 : END DO
2185 :
2186 : END FUNCTION native_q_index
2187 :
2188 : ! **************************************************************************************************
2189 : !> \brief Schedule one Σ matrix per native inversion pair and cache the actual partner orbitals.
2190 : !> \param bs_env ...
2191 : !> \param env ...
2192 : !> \param orbitals Orbitals with cyclic ownership, MOD(k-1,P).
2193 : !> \param plan Destination-rank job lists and conjugated partner orbitals.
2194 : ! **************************************************************************************************
2195 224 : SUBROUTINE prepare_sigma_projection(bs_env, env, orbitals, plan)
2196 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2197 : TYPE(mp_para_env_type), INTENT(IN) :: env
2198 : COMPLEX(KIND=dp), ASYNCHRONOUS, CONTIGUOUS, &
2199 : INTENT(IN) :: orbitals(:, :, :, :)
2200 : TYPE(sigma_projection_type), ASYNCHRONOUS, &
2201 : INTENT(OUT) :: plan
2202 :
2203 : CHARACTER(LEN=*), PARAMETER :: routineN = 'prepare_sigma_projection'
2204 :
2205 : COMPLEX(KIND=dp) :: phase
2206 : INTEGER :: c1, c2, c3, first_pair, handle, i_dim, i_image, i_kp, i_local, i_pair, i_partner, &
2207 : i_rank, i_slot, il, ilp, ispin, n_cells, n_grid, n_kp, n_mesh, n_off, n_pairs, n_partner, &
2208 : n_rank, n_requests, nmesh(3), partner_rank, tied
2209 : LOGICAL :: tie(3)
2210 : REAL(KIND=dp) :: angle, boundary(3), xk(3)
2211 28 : REAL(KIND=dp), ALLOCATABLE :: fractional_grid(:, :)
2212 28 : TYPE(mp_request_type), ALLOCATABLE :: requests(:)
2213 :
2214 28 : CALL timeset(routineN, handle)
2215 28 : n_kp = bs_env%nkp_bs_and_DOS
2216 28 : n_mesh = bs_env%nkp_scf_desymm
2217 28 : n_rank = env%num_pe
2218 196 : ALLOCATE (plan%counts(0:n_rank - 1), plan%mesh_index(n_kp), plan%partner(n_kp), plan%partner_slot(n_kp))
2219 112 : ALLOCATE (plan%jobs((n_kp + n_rank - 1)/n_rank, 0:n_rank - 1))
2220 70 : plan%counts(:) = 0
2221 608 : plan%partner(:) = 0
2222 608 : plan%partner_slot(:) = 0
2223 608 : DO i_kp = 1, n_kp
2224 608 : plan%mesh_index(i_kp) = native_q_index(bs_env, i_kp)
2225 : END DO
2226 28 : n_partner = 0
2227 608 : DO i_kp = 1, n_kp
2228 580 : IF (i_kp <= n_mesh) THEN
2229 496 : i_partner = bs_env%gwkp_env%q_partner(i_kp)
2230 : ! Σ comes from the real-output K2R and a real-space product. Its R2K
2231 : ! conjugacy also holds when evGW0 constructs both Green spectra separately.
2232 : ! Keep independent band-path targets even when they land on the mesh.
2233 496 : IF (i_kp /= i_partner .AND. plan%mesh_index(i_kp) == i_kp .AND. &
2234 : plan%mesh_index(i_partner) == i_partner) THEN
2235 488 : IF (i_partner < i_kp) CYCLE
2236 244 : plan%partner(i_kp) = i_partner
2237 : END IF
2238 : END IF
2239 336 : i_rank = MODULO(i_kp - 1, n_rank)
2240 336 : plan%counts(i_rank) = plan%counts(i_rank) + 1
2241 336 : plan%jobs(plan%counts(i_rank), i_rank) = i_kp
2242 336 : IF (i_rank /= env%mepos .OR. plan%partner(i_kp) == 0) CYCLE
2243 183 : n_partner = n_partner + 1
2244 608 : plan%partner_slot(i_kp) = n_partner
2245 : END DO
2246 168 : ALLOCATE (plan%partner_orbitals(SIZE(orbitals, 1), SIZE(orbitals, 2), n_partner, SIZE(orbitals, 4)))
2247 702 : ALLOCATE (requests((n_partner + SIZE(orbitals, 3))*SIZE(orbitals, 4)))
2248 28 : n_requests = 0
2249 : ! MPI preserves message order between each pair of ranks. Both peers visit
2250 : ! the representatives and spin channels in the same order.
2251 524 : DO i_kp = 1, n_mesh
2252 496 : i_partner = plan%partner(i_kp)
2253 496 : IF (i_partner == 0) CYCLE
2254 244 : i_rank = MODULO(i_kp - 1, n_rank)
2255 244 : partner_rank = MODULO(i_partner - 1, n_rank)
2256 244 : i_local = (i_partner - 1)/n_rank + 1
2257 244 : i_slot = plan%partner_slot(i_kp)
2258 768 : DO ispin = 1, SIZE(orbitals, 4)
2259 488 : IF (env%mepos == i_rank) THEN
2260 183 : IF (i_rank == partner_rank) THEN
2261 26837 : plan%partner_orbitals(:, :, i_slot, ispin) = orbitals(:, :, i_local, ispin)
2262 : ELSE
2263 44 : n_requests = n_requests + 1
2264 44 : CALL env%irecv(plan%partner_orbitals(:, :, i_slot, ispin), partner_rank, requests(n_requests), tag=0)
2265 : END IF
2266 61 : ELSE IF (env%mepos == partner_rank) THEN
2267 44 : n_requests = n_requests + 1
2268 44 : CALL env%isend(orbitals(:, :, i_local, ispin), i_rank, requests(n_requests), tag=0)
2269 : END IF
2270 : END DO
2271 : END DO
2272 28 : CALL mp_waitall(requests(:n_requests))
2273 : !$OMP PARALLEL DO DEFAULT(NONE) COLLAPSE(2) SCHEDULE(STATIC) IF(n_partner > 1) &
2274 28 : !$OMP SHARED(plan, n_partner, orbitals) PRIVATE(ispin, i_slot)
2275 : DO ispin = 1, SIZE(orbitals, 4)
2276 : DO i_slot = 1, n_partner
2277 : plan%partner_orbitals(:, :, i_slot, ispin) = CONJG(plan%partner_orbitals(:, :, i_slot, ispin))
2278 : END DO
2279 : END DO
2280 : !$OMP END PARALLEL DO
2281 : ! The image geometry and off-mesh phases are common to Σx and every Σc time slice.
2282 608 : n_off = COUNT(plan%mesh_index == 0)
2283 28 : n_grid = SIZE(orbitals, 1)
2284 28 : CALL block_range(INT(INT(n_grid, int_8)*(n_grid + 1)/2), env%mepos, n_rank, first_pair, n_pairs)
2285 28 : IF (n_off > 0 .AND. n_pairs > 0) THEN
2286 168 : ALLOCATE (plan%image_shift(3, n_pairs), plan%image_tie(3, n_pairs), plan%displacement(3, n_pairs))
2287 84 : ALLOCATE (fractional_grid(3, n_grid))
2288 25948 : fractional_grid(:, :) = MATMUL(bs_env%ri_rs%cell%h_inv, bs_env%ri_rs%grid_points)
2289 112 : DO i_dim = 1, 3
2290 184 : IF (bs_env%periodic(i_dim) == 0) fractional_grid(i_dim, :) = 0.0_dp
2291 : END DO
2292 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
2293 28 : !$OMP SHARED(bs_env, first_pair, fractional_grid, n_pairs, plan) PRIVATE(i_pair, il, ilp, tie)
2294 : DO i_pair = 1, n_pairs
2295 : CALL grid_pair_indices(first_pair + i_pair - 1, il, ilp)
2296 : CALL grid_pair_shift(il, ilp, fractional_grid, plan%image_shift(:, i_pair), tie)
2297 : plan%image_tie(:, i_pair) = MERGE(1_int_1, 0_int_1, tie)
2298 : plan%displacement(:, i_pair) = bs_env%ri_rs%grid_points(:, il) - bs_env%ri_rs%grid_points(:, ilp)
2299 : END DO
2300 : !$OMP END PARALLEL DO
2301 28 : DEALLOCATE (fractional_grid)
2302 :
2303 112 : nmesh = bs_env%kpoints_scf_desymm%nkp_grid
2304 112 : boundary = 0.25_dp*(1.0_dp + COS(twopi*bs_env%kpoints_scf_desymm%xkp(:, 1)*nmesh))
2305 : ! Omit endpoints with zero weight for both pair-center choices from the GEMM dimension.
2306 112 : plan%first_image = -nmesh/2
2307 112 : DO i_dim = 1, 3
2308 112 : IF (image_weight(plan%first_image(i_dim), nmesh(i_dim), .FALSE., boundary(i_dim)) == 0.0_dp) THEN
2309 52 : plan%first_image(i_dim) = plan%first_image(i_dim) + 1
2310 : END IF
2311 : END DO
2312 32836 : plan%last_image = -plan%first_image + MERGE(1, 0, ANY(plan%image_tie /= 0_int_1, DIM=2))
2313 112 : n_cells = PRODUCT(plan%last_image - plan%first_image + 1)
2314 196 : ALLOCATE (plan%image_weights(MAXVAL(plan%last_image - plan%first_image + 1), 3, 0:1))
2315 84 : DO tied = 0, 1
2316 252 : DO i_dim = 1, 3
2317 520 : DO i_image = plan%first_image(i_dim), plan%last_image(i_dim)
2318 : plan%image_weights(i_image - plan%first_image(i_dim) + 1, i_dim, tied) = &
2319 464 : image_weight(i_image, nmesh(i_dim), tied == 1, boundary(i_dim))
2320 : END DO
2321 : END DO
2322 : END DO
2323 196 : ALLOCATE (plan%offmesh_slot(n_kp), plan%phase_cos(n_cells, n_off), plan%phase_sin(n_cells, n_off))
2324 608 : plan%offmesh_slot(:) = 0
2325 28 : n_off = 0
2326 608 : DO i_kp = 1, n_kp
2327 580 : IF (plan%mesh_index(i_kp) /= 0) CYCLE
2328 72 : n_off = n_off + 1
2329 608 : plan%offmesh_slot(i_kp) = n_off
2330 : END DO
2331 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
2332 28 : !$OMP SHARED(bs_env, n_kp, plan) PRIVATE(angle, c1, c2, c3, i_image, i_kp, i_slot, phase, xk)
2333 : DO i_kp = 1, n_kp
2334 : i_slot = plan%offmesh_slot(i_kp)
2335 : IF (i_slot == 0) CYCLE
2336 : xk = bs_env%kpoints_DOS%xkp(:, i_kp)
2337 : i_image = 0
2338 : DO c3 = plan%first_image(3), plan%last_image(3)
2339 : DO c2 = plan%first_image(2), plan%last_image(2)
2340 : DO c1 = plan%first_image(1), plan%last_image(1)
2341 : i_image = i_image + 1
2342 : angle = twopi*(xk(1)*c1 + xk(2)*c2 + xk(3)*c3)
2343 : phase = EXP(CMPLX(0.0_dp, angle, KIND=dp))
2344 : plan%phase_cos(i_image, i_slot) = REAL(phase, KIND=dp)
2345 : plan%phase_sin(i_image, i_slot) = AIMAG(phase)
2346 : END DO
2347 : END DO
2348 : END DO
2349 : END DO
2350 : !$OMP END PARALLEL DO
2351 : END IF
2352 28 : CALL timestop(handle)
2353 56 : END SUBROUTINE prepare_sigma_projection
2354 :
2355 : ! **************************************************************************************************
2356 : !> \brief Exchange batched Σ matrices and contract the packed panels with the actual target orbitals.
2357 : !> \param bs_env ...
2358 : !> \param sigma_mesh Native Σ(k) for the local pairs.
2359 : !> \param sigma_R Real-space Σ for the local pairs.
2360 : !> \param env Pair and band owners.
2361 : !> \param fft FFT pair partition.
2362 : !> \param plan Projection jobs and conjugated partner orbitals.
2363 : !> \param orbitals Local band orbitals.
2364 : !> \param ispin Spin channel.
2365 : !> \param sigma_band Diagonals in the original band-point order.
2366 : !> \param apply_cutoff Use the W distance cutoff in off-mesh image sums.
2367 : ! **************************************************************************************************
2368 318 : SUBROUTINE self_energy_pairs_to_Bloch(bs_env, sigma_mesh, sigma_R, env, fft, plan, orbitals, &
2369 318 : ispin, sigma_band, apply_cutoff)
2370 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2371 : COMPLEX(KIND=dp), INTENT(IN) :: sigma_mesh(:, :, :)
2372 : REAL(KIND=dp), INTENT(IN) :: sigma_R(:, :, :)
2373 : TYPE(mp_para_env_type), INTENT(IN) :: env
2374 : TYPE(gw_pair_fft_work_type), INTENT(IN) :: fft
2375 : TYPE(sigma_projection_type), INTENT(IN), TARGET :: plan
2376 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(IN), TARGET :: orbitals(:, :, :, :)
2377 : INTEGER, INTENT(IN) :: ispin
2378 : REAL(KIND=dp), INTENT(OUT) :: sigma_band(:, :)
2379 : LOGICAL, INTENT(IN) :: apply_cutoff
2380 :
2381 : CHARACTER(LEN=*), PARAMETER :: routineN = 'self_energy_pairs_to_Bloch'
2382 :
2383 318 : COMPLEX(KIND=dp), ALLOCATABLE :: panel(:, :), received(:), work(:, :)
2384 318 : COMPLEX(KIND=dp), ALLOCATABLE, TARGET :: send_buffer(:, :)
2385 318 : COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: psi(:, :), sent(:)
2386 : COMPLEX(KIND=dp), TARGET :: unused_send(1)
2387 : INTEGER :: first_col, handle, i_band, i_batch, i_col, i_first, i_job, i_kp, i_local, &
2388 : i_orbital, i_panel, i_rank, i_slot, i_target, last_col, m, n_ao, n_batch, n_cols, n_done, &
2389 : n_grid, n_kp, n_local, n_recv, n_rhs, n_round, n_send, offset, row_first, row_last, &
2390 : segment
2391 318 : INTEGER, ALLOCATABLE :: jobs(:), recv_counts(:), recv_displs(:), &
2392 : send_counts(:), send_displs(:)
2393 :
2394 318 : CALL timeset(routineN, handle)
2395 :
2396 318 : n_grid = bs_env%ri_rs%n_grid_points
2397 318 : n_ao = bs_env%n_ao
2398 318 : n_kp = bs_env%nkp_bs_and_DOS
2399 318 : n_local = SIZE(sigma_mesh, 1)
2400 318 : n_batch = MIN(sigma_rhs_cols, n_ao)
2401 318 : m = projection_round_kpoints(n_grid, n_kp, env%num_pe)
2402 650 : n_round = SUM(MIN(m, plan%counts))
2403 318 : n_recv = MIN(m, plan%counts(env%mepos))
2404 0 : ALLOCATE (received(MAX(1, SUM(fft%pair_counts)*n_recv)), &
2405 2876 : send_buffer(n_local, n_round), jobs(n_round))
2406 0 : ALLOCATE (recv_counts(0:env%num_pe - 1), recv_displs(0:env%num_pe - 1), &
2407 1908 : send_counts(0:env%num_pe - 1), send_displs(0:env%num_pe - 1))
2408 318 : unused_send = z_zero
2409 56700 : sigma_band(:, :) = 0.0_dp
2410 :
2411 968 : DO i_first = 1, MAXVAL(plan%counts), m
2412 : ! The send buffer is destination-major, with consecutive k columns per destination.
2413 318 : n_round = 0
2414 650 : DO i_rank = 0, env%num_pe - 1
2415 332 : n_send = MAX(0, MIN(m, plan%counts(i_rank) - i_first + 1))
2416 332 : send_displs(i_rank) = n_local*n_round
2417 332 : send_counts(i_rank) = n_local*n_send
2418 332 : IF (n_send > 0) THEN
2419 5012 : jobs(n_round + 1:n_round + n_send) = plan%jobs(i_first:i_first + n_send - 1, i_rank)
2420 : END IF
2421 650 : n_round = n_round + n_send
2422 : END DO
2423 318 : sent => unused_send
2424 318 : IF (n_local > 0) THEN
2425 : CALL band_self_energy(bs_env, sigma_mesh, sigma_R, jobs(:n_round), plan, &
2426 318 : send_buffer(:, :n_round), apply_cutoff)
2427 318 : sent(1:n_local*n_round) => send_buffer(:, :n_round)
2428 : END IF
2429 318 : n_recv = MAX(0, MIN(m, plan%counts(env%mepos) - i_first + 1))
2430 650 : recv_counts(:) = fft%pair_counts*n_recv
2431 650 : recv_displs(:) = fft%pair_displs*n_recv
2432 318 : CALL env%alltoall(sent, send_counts, send_displs, received, recv_counts, recv_displs)
2433 :
2434 318 : IF (n_recv > 0) THEN
2435 : BLOCK
2436 636 : REAL(KIND=dp) :: diagonal(n_ao, 2, n_recv)
2437 :
2438 66810 : diagonal(:, :, :) = 0.0_dp
2439 : !$OMP PARALLEL DEFAULT(NONE) REDUCTION(+:diagonal) &
2440 : !$OMP SHARED(env, fft, i_first, ispin, n_ao, n_batch, n_grid, n_recv, orbitals, plan, received, recv_displs) &
2441 : !$OMP PRIVATE(first_col, i_band, i_batch, i_col, i_job, i_kp, i_local, i_orbital, i_panel, i_rank, i_slot, &
2442 318 : !$OMP i_target, last_col, n_cols, n_rhs, offset, panel, psi, row_first, row_last, segment, work)
2443 : ALLOCATE (panel(n_grid, MIN(n_grid, grid_pair_panel_cols)), &
2444 : work(n_batch, MIN(n_grid, grid_pair_panel_cols)))
2445 : !$OMP DO COLLAPSE(2) SCHEDULE(STATIC, 1)
2446 : DO i_job = 1, n_recv
2447 : DO first_col = 1, n_grid, grid_pair_panel_cols
2448 : last_col = MIN(first_col + grid_pair_panel_cols - 1, n_grid)
2449 : n_cols = last_col - first_col + 1
2450 : ! Each column may span multiple MPI sources. Read their intersections
2451 : ! directly from the receive buffer and form U with half the diagonal.
2452 : DO i_col = first_col, last_col
2453 : i_panel = i_col - first_col + 1
2454 : DO segment = fft%column_segments(i_col), fft%column_segments(i_col + 1) - 1
2455 : i_rank = fft%segments(segment)%rank
2456 : row_first = fft%segments(segment)%first_row
2457 : row_last = fft%segments(segment)%last_row
2458 : offset = recv_displs(i_rank) + (i_job - 1)*fft%pair_counts(i_rank) + &
2459 : fft%segments(segment)%displacement
2460 : panel(row_first:row_last, i_panel) = received(offset + 1:offset + row_last - row_first + 1)
2461 : END DO
2462 : panel(i_col, i_panel) = CMPLX(0.5_dp*REAL(panel(i_col, i_panel), dp), 0.0_dp, dp)
2463 : panel(i_col + 1:last_col, i_panel) = z_zero
2464 : END DO
2465 : i_kp = plan%jobs(i_first + i_job - 1, env%mepos)
2466 : i_local = (i_kp - 1)/env%num_pe + 1
2467 : i_slot = plan%partner_slot(i_kp)
2468 : ! Contract Psi^H U_panel first so the result has only one column per panel column.
2469 : DO i_target = 1, MERGE(2, 1, i_slot > 0)
2470 : IF (i_target == 1) THEN
2471 : psi => orbitals(:, :, i_local, ispin)
2472 : ELSE
2473 : psi => plan%partner_orbitals(:, :, i_slot, ispin)
2474 : END IF
2475 : DO i_batch = 1, n_ao, n_batch
2476 : n_rhs = MIN(n_batch, n_ao - i_batch + 1)
2477 : CALL zgemm('C', 'N', n_rhs, n_cols, last_col, z_one, psi(:, i_batch:i_batch + n_rhs - 1), &
2478 : n_grid, panel, n_grid, z_zero, work, n_batch)
2479 : DO i_band = 1, n_rhs
2480 : i_orbital = i_batch + i_band - 1
2481 : diagonal(i_orbital, i_target, i_job) = diagonal(i_orbital, i_target, i_job) + &
2482 : REAL(SUM(work(i_band, :n_cols)*psi(first_col:last_col, i_orbital)), dp)
2483 : END DO
2484 : END DO
2485 : END DO
2486 : END DO
2487 : END DO
2488 : !$OMP END DO NOWAIT
2489 : DEALLOCATE (panel, work)
2490 : !$OMP END PARALLEL
2491 4914 : DO i_job = 1, n_recv
2492 4596 : i_kp = plan%jobs(i_first + i_job - 1, env%mepos)
2493 30948 : sigma_band(:, i_kp) = 2.0_dp*diagonal(:, 1, i_job)
2494 25856 : IF (plan%partner(i_kp) > 0) sigma_band(:, plan%partner(i_kp)) = 2.0_dp*diagonal(:, 2, i_job)
2495 : END DO
2496 : END BLOCK
2497 : END IF
2498 4998 : n_done = n_round + COUNT(plan%partner(jobs(:n_round)) > 0)
2499 636 : CALL count_time_loop_k_points(bs_env, INT(n_done, int_8))
2500 : END DO
2501 113082 : CALL env%sum(sigma_band)
2502 :
2503 318 : CALL timestop(handle)
2504 :
2505 636 : END SUBROUTINE self_energy_pairs_to_Bloch
2506 :
2507 : ! **************************************************************************************************
2508 : !> \brief Bound the peak per-rank workspace for the Σ(k) exchange and projection.
2509 : !> \param n_grid ...
2510 : !> \param n_rank ...
2511 : !> \param n_kp Total band targets.
2512 : !> \param n_jobs Targets exchanged after pairing conjugate matrices.
2513 : !> \param n_offmesh Targets requiring real-space interpolation.
2514 : !> \param n_images Bound of the image cells summed per pair.
2515 : !> \param n_ao ...
2516 : !> \param n_threads ...
2517 : !> \return ...
2518 : ! **************************************************************************************************
2519 14 : FUNCTION projection_workspace_bytes(n_grid, n_rank, n_kp, n_jobs, n_offmesh, n_images, n_ao, n_threads) RESULT(bytes)
2520 : INTEGER, INTENT(IN) :: n_grid, n_rank, n_kp, n_jobs, n_offmesh, &
2521 : n_images, n_ao, n_threads
2522 : REAL(dp) :: bytes
2523 :
2524 : REAL(dp), PARAMETER :: integer_bytes = REAL(STORAGE_SIZE(0)/8, dp)
2525 :
2526 : REAL(dp) :: contraction_bytes, exchange_bytes, &
2527 : interpolation_bytes, m, n_c, n_off, &
2528 : n_pairs, n_round
2529 :
2530 14 : m = REAL(MIN(n_jobs, projection_round_kpoints(n_grid, n_kp, n_rank)), dp)
2531 14 : n_round = MIN(m*REAL(n_rank, dp), REAL(n_jobs, dp))
2532 14 : n_pairs = REAL(n_grid, dp)*REAL(n_grid + 1, dp)/2.0_dp
2533 : exchange_bytes = complex_bytes*(REAL(CEILING(n_pairs/REAL(n_rank, dp), KIND=int_8), dp)*n_round + n_pairs*m + 2.0_dp) + &
2534 14 : integer_bytes*(n_round + 4.0_dp*REAL(n_rank, dp))
2535 14 : n_off = MIN(n_round, REAL(n_offmesh, dp))
2536 14 : interpolation_bytes = integer_bytes*n_round
2537 14 : IF (n_off > 0.0_dp) THEN
2538 14 : n_c = REAL(n_images, dp)
2539 : interpolation_bytes = interpolation_bytes + real_bytes*((2.0_dp*n_c + 3.0_dp)*n_off + &
2540 14 : REAL(n_threads*pair_batch_size, dp)*(n_c + 2.0_dp*n_off))
2541 : END IF
2542 : contraction_bytes = REAL(n_threads, dp)*complex_bytes*REAL(MIN(n_grid, grid_pair_panel_cols), dp)* &
2543 : REAL(n_grid + MIN(sigma_rhs_cols, n_ao), dp) + &
2544 14 : 2.0_dp*real_bytes*REAL(n_ao, dp)*m*REAL(n_threads + 1, dp)
2545 : ! The interpolation temporaries are released before the panel contraction allocates its work arrays.
2546 14 : bytes = exchange_bytes + MAX(interpolation_bytes, contraction_bytes)
2547 14 : END FUNCTION projection_workspace_bytes
2548 :
2549 : ! **************************************************************************************************
2550 : !> \brief Band k points per rank and projection round: a round evaluates up to
2551 : !> band_kpoints_per_pass k points, with the exchange width bounded as for Green and W.
2552 : !> \param n_grid ...
2553 : !> \param n_kp ...
2554 : !> \param n_rank ...
2555 : !> \return ...
2556 : ! **************************************************************************************************
2557 332 : PURE INTEGER FUNCTION projection_round_kpoints(n_grid, n_kp, n_rank) RESULT(m)
2558 : INTEGER, INTENT(IN) :: n_grid, n_kp, n_rank
2559 :
2560 332 : m = MAX(1, MIN(band_kpoints_per_pass/n_rank, pair_exchange_width(n_grid, n_kp, n_rank)))
2561 332 : END FUNCTION projection_round_kpoints
2562 :
2563 : ! **************************************************************************************************
2564 : !> \brief Bytes left for a stage after the reserve for data that stays alive and for libraries.
2565 : !> \param available_GB ...
2566 : !> \return ...
2567 : ! **************************************************************************************************
2568 28 : PURE REAL(KIND=dp) FUNCTION budget_after_reserve(available_GB) RESULT(budget)
2569 : REAL(KIND=dp), INTENT(IN) :: available_GB
2570 :
2571 28 : budget = MAX(0.0_dp, available_GB - MAX(min_reserve_GB, reserve_share*available_GB))*bytes_per_GB
2572 28 : END FUNCTION budget_after_reserve
2573 :
2574 : ! **************************************************************************************************
2575 : !> \brief Σ_ll′(k) = Σ_R exp(2πik·R) Σ_ll′(R) at the band k points of one projection round.
2576 : !> Native k points are copied from the mesh transform. Elsewhere every pair sums its images
2577 : !> R = s+c around the lattice shift s nearest to H⁻¹(r_l-r_l′), weighted by image_weight in
2578 : !> each direction; on the native mesh this is the pair-centered lattice FFT. The two images on
2579 : !> the supercell boundary share (1+cos(2πk₀N))/2 for the mesh offset k₀: on a Γ-centered mesh
2580 : !> they are equivalent, on a half-shifted mesh G and W are antiperiodic there and the images
2581 : !> cancel. All pairs share the phases exp(2πik·c), so batches of pairs are summed by two real
2582 : !> GEMMs.
2583 : !> \param bs_env ...
2584 : !> \param sigma_mesh ...
2585 : !> \param sigma_R ...
2586 : !> \param k_targets band k points in send-buffer order
2587 : !> \param plan cached native indices, image geometry, and off-mesh phases
2588 : !> \param sigma_k ...
2589 : !> \param apply_cutoff use the W distance cutoff on the same centered images as the native FFT
2590 : ! **************************************************************************************************
2591 318 : SUBROUTINE band_self_energy(bs_env, sigma_mesh, sigma_R, k_targets, plan, sigma_k, apply_cutoff)
2592 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2593 : COMPLEX(KIND=dp), INTENT(IN) :: sigma_mesh(:, :, :)
2594 : REAL(KIND=dp), INTENT(IN) :: sigma_R(:, :, :)
2595 : INTEGER, INTENT(IN) :: k_targets(:)
2596 : TYPE(sigma_projection_type), INTENT(IN) :: plan
2597 : COMPLEX(KIND=dp), INTENT(OUT) :: sigma_k(:, :)
2598 : LOGICAL, INTENT(IN) :: apply_cutoff
2599 :
2600 : INTEGER :: c1, c2, c3, first_c(3), i_c, i_col, i_first, i_local, i_off, i_pair, i_slot, &
2601 : last_c(3), lower(3), n_c, n_off, n_pairs, native_iq, nmesh(3), shift(3), tied(3), &
2602 : wrapped(3)
2603 318 : INTEGER, ALLOCATABLE :: offmesh_col(:)
2604 : REAL(KIND=dp) :: angle, cutoff_squared, distance(3), &
2605 : weight
2606 318 : REAL(KIND=dp), ALLOCATABLE :: cos_c(:, :), images(:, :), sin_c(:, :), &
2607 318 : sum_im(:, :), sum_re(:, :), xk(:, :)
2608 :
2609 954 : ALLOCATE (offmesh_col(SIZE(sigma_k, 2)))
2610 318 : n_off = 0
2611 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
2612 318 : !$OMP SHARED(k_targets, plan, sigma_k, sigma_mesh) PRIVATE(i_col, native_iq)
2613 : DO i_col = 1, SIZE(sigma_k, 2)
2614 : native_iq = plan%mesh_index(k_targets(i_col))
2615 : IF (native_iq > 0) sigma_k(:, i_col) = sigma_mesh(:, 1, native_iq)
2616 : END DO
2617 : !$OMP END PARALLEL DO
2618 4998 : DO i_col = 1, SIZE(sigma_k, 2)
2619 4680 : IF (plan%mesh_index(k_targets(i_col)) > 0) CYCLE
2620 852 : n_off = n_off + 1
2621 4998 : offmesh_col(n_off) = i_col
2622 : END DO
2623 318 : IF (n_off == 0) RETURN
2624 :
2625 1272 : nmesh = bs_env%kpoints_scf_desymm%nkp_grid
2626 1272 : lower = -nmesh/2
2627 318 : cutoff_squared = -1.0_dp
2628 318 : IF (apply_cutoff .AND. bs_env%ri_rs%cutoff_radius_v_w > 0.0_dp) THEN
2629 0 : cutoff_squared = bs_env%ri_rs%cutoff_radius_v_w**2
2630 : END IF
2631 1272 : first_c = plan%first_image
2632 1272 : last_c = plan%last_image
2633 318 : n_c = SIZE(plan%phase_cos, 1)
2634 2544 : ALLOCATE (xk(3, n_off), cos_c(n_c, n_off), sin_c(n_c, n_off))
2635 1170 : DO i_off = 1, n_off
2636 852 : i_col = k_targets(offmesh_col(i_off))
2637 852 : i_slot = plan%offmesh_slot(i_col)
2638 3408 : xk(:, i_off) = bs_env%kpoints_DOS%xkp(:, i_col)
2639 10428 : cos_c(:, i_off) = plan%phase_cos(:, i_slot)
2640 10746 : sin_c(:, i_off) = plan%phase_sin(:, i_slot)
2641 : END DO
2642 :
2643 : !$OMP PARALLEL DEFAULT(NONE) &
2644 : !$OMP SHARED(bs_env, cutoff_squared, cos_c, first_c, last_c, lower, n_c, n_off, nmesh) &
2645 : !$OMP SHARED(offmesh_col, plan, sigma_k, sigma_R, sin_c, xk) &
2646 : !$OMP PRIVATE(angle, c1, c2, c3, distance, i_c, i_first, i_local, i_off, i_pair, images, n_pairs, shift, &
2647 318 : !$OMP sum_im, sum_re, tied, weight, wrapped)
2648 : ALLOCATE (images(n_c, pair_batch_size), sum_re(pair_batch_size, n_off), sum_im(pair_batch_size, n_off))
2649 : !$OMP DO SCHEDULE(DYNAMIC)
2650 : DO i_first = 1, SIZE(sigma_R, 1), pair_batch_size
2651 : n_pairs = MIN(pair_batch_size, SIZE(sigma_R, 1) - i_first + 1)
2652 : DO i_pair = 1, n_pairs
2653 : i_local = i_first + i_pair - 1
2654 : shift = plan%image_shift(:, i_local)
2655 : tied = INT(plan%image_tie(:, i_local))
2656 : i_c = 0
2657 : DO c3 = first_c(3), last_c(3)
2658 : DO c2 = first_c(2), last_c(2)
2659 : DO c1 = first_c(1), last_c(1)
2660 : i_c = i_c + 1
2661 : images(i_c, i_pair) = 0.0_dp
2662 : weight = plan%image_weights(c1 - first_c(1) + 1, 1, tied(1))* &
2663 : plan%image_weights(c2 - first_c(2) + 1, 2, tied(2))* &
2664 : plan%image_weights(c3 - first_c(3) + 1, 3, tied(3))
2665 : IF (weight == 0.0_dp) CYCLE
2666 : IF (cutoff_squared > 0.0_dp) THEN
2667 : distance = plan%displacement(:, i_local) - MATMUL(bs_env%hmat, REAL(shift + [c1, c2, c3], dp))
2668 : IF (SUM(distance**2) > cutoff_squared) CYCLE
2669 : END IF
2670 : wrapped = MODULO(shift + [c1, c2, c3] - lower, nmesh) + lower
2671 : images(i_c, i_pair) = weight*sigma_R(i_local, 1, 1 + wrapped(3) - lower(3) + &
2672 : nmesh(3)*(wrapped(2) - lower(2) + nmesh(2)*(wrapped(1) - lower(1))))
2673 : END DO
2674 : END DO
2675 : END DO
2676 : END DO
2677 : CALL dgemm('T', 'N', n_pairs, n_off, n_c, 1.0_dp, images, n_c, cos_c, n_c, 0.0_dp, sum_re, pair_batch_size)
2678 : CALL dgemm('T', 'N', n_pairs, n_off, n_c, 1.0_dp, images, n_c, sin_c, n_c, 0.0_dp, sum_im, pair_batch_size)
2679 : DO i_off = 1, n_off
2680 : DO i_pair = 1, n_pairs
2681 : angle = twopi*DOT_PRODUCT(xk(:, i_off), REAL(plan%image_shift(:, i_first + i_pair - 1), dp))
2682 : sigma_k(i_first + i_pair - 1, offmesh_col(i_off)) = &
2683 : EXP(CMPLX(0.0_dp, angle, KIND=dp))*CMPLX(sum_re(i_pair, i_off), sum_im(i_pair, i_off), KIND=dp)
2684 : END DO
2685 : END DO
2686 : END DO
2687 : !$OMP END DO
2688 : DEALLOCATE (images, sum_re, sum_im)
2689 : !$OMP END PARALLEL
2690 318 : DEALLOCATE (cos_c, offmesh_col, sin_c, xk)
2691 318 : END SUBROUTINE band_self_energy
2692 :
2693 : ! **************************************************************************************************
2694 : !> \brief Prepare the common unordered-pair geometry and persistent two-real R2K input panels.
2695 : !> The first chi K2R stores its real fields in these panels; the last K2R replaces them with
2696 : !> the weighted product. Cell filtering is applied after the global Green norms are known.
2697 : !> \param bs_env ...
2698 : !> \param cells canonical physical cells
2699 : !> \param first_pair first owned unordered pair
2700 : !> \param n_pairs local unordered pairs
2701 : !> \param n_rank ranks sharing the k points and grid pairs
2702 : !> \param plan cached geometry, coefficients, and next-FFT input
2703 : !> \param apply_cutoff apply the configured W distance cutoff to the centered product images
2704 : !> \param q_indices requested output q points
2705 : !> \param q_partner inversion partners in the native W mesh
2706 : ! **************************************************************************************************
2707 42 : SUBROUTINE prepare_pair_fft(bs_env, cells, first_pair, n_pairs, n_rank, plan, apply_cutoff, q_indices, q_partner)
2708 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
2709 : INTEGER, INTENT(IN) :: cells(:, :), first_pair, n_pairs, n_rank
2710 : TYPE(gw_pair_fft_work_type), INTENT(INOUT), TARGET :: plan
2711 : LOGICAL, INTENT(IN) :: apply_cutoff
2712 : INTEGER, INTENT(IN), OPTIONAL :: q_indices(:), q_partner(:)
2713 :
2714 : INTEGER :: count, fft_count, first, half_shift(3), i, i_col, i_extract, i_first, i_rank, &
2715 : i_time, ibatch, icell, idir, ik, il, ilp, iq, isource, lower(3), n_extract, n_k, n_occ, &
2716 : n_round, n_segments, ngrid, nmesh(3), pair_first, pair_last, partner, partner_native, &
2717 : partner_output, rank_end, reflected(3), row_first, row_last, shift(3), width
2718 : INTEGER(KIND=int_8) :: nfft_grid, spectrum_columns
2719 42 : INTEGER, ALLOCATABLE :: extract_partner(:), extract_q(:), grid_linear(:), &
2720 42 : input_to_native(:), native_to_input(:), partner_linear(:), q_position(:)
2721 : LOGICAL :: tie(3)
2722 : REAL(KIND=dp) :: cutoff_squared, displacement(3), &
2723 : energy_tolerance, half_tau, started, &
2724 : total_started
2725 42 : REAL(KIND=dp), ALLOCATABLE :: fractional_grid(:, :)
2726 : TYPE(gw_pair_fft_batch_type), POINTER :: batch
2727 :
2728 : IF (fft_profile_enabled) total_started = m_walltime()
2729 42 : CPASSERT(.NOT. ALLOCATED(plan%batches))
2730 42 : ngrid = bs_env%ri_rs%n_grid_points
2731 168 : nmesh = bs_env%kpoints_scf_desymm%nkp_grid
2732 168 : lower = -nmesh/2
2733 : ! GW uses the same native reciprocal mesh for Green functions, chi and W.
2734 3018 : IF (.NOT. ALL(bs_env%kpoints_chi_eps_W%xkp(:, 1:bs_env%nkp_scf_desymm) == bs_env%kpoints_scf_desymm%xkp)) THEN
2735 0 : CPABORT('GW lattice transforms require one native reciprocal mesh')
2736 : END IF
2737 42 : n_k = bs_env%nkp_scf_desymm
2738 :
2739 42 : plan%k_batch = pair_exchange_width(ngrid, n_k, n_rank)
2740 42 : CALL grid_pair_blocks(ngrid, n_rank, plan%pair_counts, plan%pair_displs)
2741 : ! Columns and destination blocks partition the same packed triangle. Only a column
2742 : ! or rank boundary can begin a new segment, so the map is linear in ngrid + n_rank.
2743 1238 : ALLOCATE (plan%column_segments(ngrid + 1), plan%segments(ngrid + n_rank))
2744 : i_rank = 0
2745 : n_segments = 0
2746 : pair_first = 1
2747 1014 : DO i_col = 1, ngrid
2748 972 : plan%column_segments(i_col) = n_segments + 1
2749 972 : pair_last = pair_first + i_col - 1
2750 1028 : DO WHILE (i_rank < n_rank)
2751 986 : rank_end = plan%pair_displs(i_rank) + plan%pair_counts(i_rank)
2752 986 : row_first = MAX(pair_first, plan%pair_displs(i_rank) + 1) - pair_first + 1
2753 986 : row_last = MIN(pair_last, rank_end) - pair_first + 1
2754 986 : IF (row_first <= row_last) THEN
2755 980 : n_segments = n_segments + 1
2756 : plan%segments(n_segments) = pair_segment_type(i_rank, row_first, row_last, &
2757 980 : pair_first + (row_first - 1) - plan%pair_displs(i_rank) - 1)
2758 : END IF
2759 986 : IF (rank_end > pair_last) EXIT
2760 986 : i_rank = i_rank + 1
2761 : END DO
2762 1014 : IF (i_col < ngrid) pair_first = pair_last + 1
2763 : END DO
2764 42 : plan%column_segments(ngrid + 1) = n_segments + 1
2765 210 : ALLOCATE (input_to_native(n_k), native_to_input(n_k), plan%green_source(n_k))
2766 42 : i_extract = 0
2767 : ! Receive contiguous source-rank blocks; the inverse FFT owns the resulting k permutation.
2768 84 : DO i_first = 1, n_k, n_rank*plan%k_batch
2769 42 : n_round = MIN(n_rank*plan%k_batch, n_k - i_first + 1)
2770 140 : DO i_rank = 0, n_rank - 1
2771 98 : DO ik = i_first + i_rank, i_first + n_round - 1, n_rank
2772 744 : i_extract = i_extract + 1
2773 744 : input_to_native(i_extract) = ik
2774 744 : native_to_input(ik) = i_extract
2775 744 : plan%green_source(i_extract) = i_extract
2776 : END DO
2777 : END DO
2778 : END DO
2779 42 : CPASSERT(i_extract == n_k)
2780 126 : ALLOCATE (plan%product_phase(SIZE(cells, 2)))
2781 : CALL k_grid_to_cell_prepare(plan%inverse, bs_env%kpoints_scf_desymm%xkp(:, input_to_native), nmesh, cells, &
2782 3018 : cell_phase_forward=plan%product_phase)
2783 168 : CPASSERT(ALL(plan%inverse%nfft == nmesh([3, 2, 1])))
2784 42 : CPASSERT(ALLOCATED(plan%inverse%k_partner))
2785 42 : IF (PRESENT(q_partner)) THEN
2786 56 : ALLOCATE (plan%q_source(n_k))
2787 28 : CALL compact_q_slots(q_partner, n_rank, plan%q_slot)
2788 524 : DO isource = 1, n_k
2789 496 : ik = input_to_native(isource)
2790 524 : plan%q_source(isource) = native_to_input(MIN(ik, q_partner(ik)))
2791 : END DO
2792 : END IF
2793 : ! For the scalar KS problem H(-k)=H(k)* and S(-k)=S(k)*. Its spectral
2794 : ! projectors give G(-k,τ)=G(k,τ)* when the weights and selected subspaces
2795 : ! agree. Eigenvalue updates in evGW0 may give the two points different weights.
2796 42 : IF (bs_env%gw_flavour == G0W0 .AND. bs_env%n_spin == 1 .AND. .NOT. bs_env%do_soc) THEN
2797 42 : n_occ = bs_env%n_occ(1)
2798 : ASSOCIATE (energies => bs_env%gwkp_env%eigenvalues_G(:, :, 1))
2799 786 : DO isource = 1, n_k
2800 744 : partner = plan%inverse%k_partner(isource)
2801 744 : IF (partner <= isource) CYCLE
2802 366 : ik = input_to_native(isource)
2803 366 : partner_native = input_to_native(partner)
2804 : energy_tolerance = 1024.0_dp*EPSILON(1.0_dp)* &
2805 4374 : MAX(1.0_dp, MAXVAL(ABS(energies(:, ik))), MAXVAL(ABS(energies(:, partner_native))))
2806 2370 : IF (MAXVAL(ABS(energies(:, ik) - energies(:, partner_native))) > energy_tolerance) CYCLE
2807 366 : IF (n_occ > 0 .AND. n_occ < SIZE(energies, 1)) THEN
2808 : ! A projector cannot select part of a degenerate eigenspace.
2809 366 : IF (energies(n_occ + 1, ik) - energies(n_occ, ik) <= 2.0_dp*energy_tolerance) CYCLE
2810 : END IF
2811 10926 : DO i_time = 1, bs_env%num_time_freq_points
2812 10560 : half_tau = 0.5_dp*ABS(bs_env%time_frequency_grid%imaginary_time(i_time))
2813 72384 : IF (ANY(ABS(ABS(half_tau*(energies(:, ik) - bs_env%e_fermi(1))) - bs_env%stabilize_exp) <= &
2814 366 : half_tau*energy_tolerance)) EXIT
2815 : END DO
2816 366 : IF (i_time <= bs_env%num_time_freq_points) CYCLE
2817 366 : plan%green_source(partner) = isource
2818 786 : plan%green_reuses = plan%green_reuses + 1
2819 : END DO
2820 : END ASSOCIATE
2821 : END IF
2822 : ! The packed forward transform keeps its factor of 1/2 for the two-real unpacking.
2823 786 : plan%product_phase(:) = plan%product_phase/(2.0_dp*grid_pair_stencil_denominator)
2824 168 : half_shift = MODULO(NINT(2.0_dp*REAL(nmesh, dp)*bs_env%kpoints_scf_desymm%xkp(:, 1)), 2)
2825 : ! Forward FFT extraction keeps native q order, independently of the inverse input layout.
2826 42 : n_extract = n_k
2827 42 : IF (PRESENT(q_indices)) THEN
2828 140 : IF (ANY(q_indices < 1 .OR. q_indices > n_k)) THEN
2829 0 : CPABORT('Invalid GW FFT output q indices')
2830 : END IF
2831 14 : n_extract = SIZE(q_indices)
2832 : END IF
2833 : ! GNU Fortran can give nested vector subscripts a zero lower bound in ALLOCATE(SOURCE=...).
2834 168 : ALLOCATE (grid_linear(n_extract), partner_linear(n_extract))
2835 664 : DO iq = 1, n_extract
2836 622 : ik = iq
2837 622 : IF (PRESENT(q_indices)) ik = q_indices(iq)
2838 622 : isource = native_to_input(ik)
2839 622 : grid_linear(iq) = plan%inverse%k_slot(isource)
2840 664 : partner_linear(iq) = plan%inverse%k_slot(plan%inverse%k_partner(isource))
2841 : END DO
2842 168 : CPASSERT(SIZE(cells, 2, KIND=int_8) == PRODUCT(INT(nmesh, int_8)))
2843 210 : ALLOCATE (plan%mirror_cell(SIZE(cells, 2)), plan%diagonal(n_pairs))
2844 786 : DO icell = 1, SIZE(cells, 2)
2845 : isource = 1 + cells(3, icell) - lower(3) + &
2846 744 : nmesh(3)*(cells(2, icell) - lower(2) + nmesh(2)*(cells(1, icell) - lower(1)))
2847 744 : IF (isource /= icell) CPABORT('GW product needs canonical cells')
2848 2976 : reflected = MODULO(-cells(:, icell) - lower, nmesh) + lower
2849 : plan%mirror_cell(icell) = 1 + reflected(3) - lower(3) + &
2850 786 : nmesh(3)*(reflected(2) - lower(2) + nmesh(2)*(reflected(1) - lower(1)))
2851 : END DO
2852 126 : ALLOCATE (fractional_grid(3, ngrid))
2853 38922 : fractional_grid(:, :) = MATMUL(bs_env%ri_rs%cell%h_inv, bs_env%ri_rs%grid_points)
2854 168 : DO idir = 1, 3
2855 276 : IF (bs_env%periodic(idir) == 0) fractional_grid(idir, :) = 0.0_dp
2856 : END DO
2857 168 : nfft_grid = PRODUCT(INT(plan%inverse%nfft, int_8))
2858 42 : width = MAX(1, k_grid_to_cell_panel_width(plan%inverse, INT(n_pairs, int_8), fft_panel_bytes, real_output=.TRUE.))
2859 42 : CPASSERT(nfft_grid*width <= HUGE(0))
2860 126 : ALLOCATE (extract_q(n_extract), extract_partner(n_extract), SOURCE=0)
2861 42 : spectrum_columns = 0_int_8
2862 : ! When both q and -q are requested, one unpack produces both output columns.
2863 126 : ALLOCATE (q_position(INT(nfft_grid)), SOURCE=0)
2864 664 : DO ik = 1, SIZE(grid_linear)
2865 622 : CPASSERT(q_position(grid_linear(ik)) == 0)
2866 664 : q_position(grid_linear(ik)) = ik
2867 : END DO
2868 : n_extract = 0
2869 664 : DO ik = 1, SIZE(grid_linear)
2870 622 : partner_output = q_position(partner_linear(ik))
2871 622 : IF (partner_output > 0 .AND. partner_output < ik) CYCLE
2872 378 : n_extract = n_extract + 1
2873 378 : extract_q(n_extract) = ik
2874 420 : IF (partner_output /= ik) extract_partner(n_extract) = partner_output
2875 : END DO
2876 42 : DEALLOCATE (q_position)
2877 : IF (fft_profile_enabled) THEN
2878 : DO i_extract = 1, n_extract
2879 : ik = extract_q(i_extract)
2880 : spectrum_columns = spectrum_columns + MERGE(1_int_8, 2_int_8, grid_linear(ik) == partner_linear(ik))
2881 : END DO
2882 : END IF
2883 :
2884 42 : plan%n_pairs = n_pairs
2885 42 : plan%width = width
2886 168 : plan%nmesh = nmesh
2887 42 : plan%nfft_grid = nfft_grid
2888 42 : plan%spectrum_columns = spectrum_columns
2889 42 : cutoff_squared = -1.0_dp
2890 42 : IF (apply_cutoff .AND. bs_env%ri_rs%cutoff_radius_v_w > 0.0_dp) THEN
2891 0 : cutoff_squared = bs_env%ri_rs%cutoff_radius_v_w**2
2892 : END IF
2893 191 : ALLOCATE (plan%batches((n_pairs + width - 1)/width))
2894 42 : DO first = 1, n_pairs, width
2895 65 : count = MIN(width, n_pairs - first + 1)
2896 65 : fft_count = (count + 1)/2
2897 65 : ibatch = (first - 1)/width + 1
2898 65 : batch => plan%batches(ibatch)
2899 : IF (fft_profile_enabled) started = m_walltime()
2900 130 : CALL fft_allocate(batch%input, [INT(nfft_grid)*fft_count])
2901 : IF (fft_profile_enabled) plan%profile%seconds(3) = plan%profile%seconds(3) + m_walltime() - started
2902 : IF (fft_profile_enabled) started = m_walltime()
2903 260 : ALLOCATE (batch%weight(count, SIZE(cells, 2)))
2904 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
2905 : !$OMP SHARED(first, count, first_pair, fractional_grid, batch, bs_env, cells, nmesh, half_shift, cutoff_squared, plan) &
2906 65 : !$OMP PRIVATE(i, il, ilp, shift, tie, displacement, icell)
2907 : DO i = 1, count
2908 : CALL grid_pair_indices(first_pair + first + i - 2, il, ilp)
2909 : plan%diagonal(first + i - 1) = il == ilp
2910 : CALL grid_pair_shift(il, ilp, fractional_grid, shift, tie)
2911 : displacement = bs_env%ri_rs%grid_points(:, il) - bs_env%ri_rs%grid_points(:, ilp)
2912 : DO icell = 1, SIZE(cells, 2)
2913 : batch%weight(i, icell) = INT(grid_pair_stencil_weight(cells(:, icell), shift, tie, nmesh, half_shift, &
2914 : displacement, bs_env%hmat, cutoff_squared), int_1)
2915 : END DO
2916 : END DO
2917 : !$OMP END PARALLEL DO
2918 42 : IF (fft_profile_enabled) plan%setup_seconds = plan%setup_seconds + m_walltime() - started
2919 : END DO
2920 42 : CALL MOVE_ALLOC(grid_linear, plan%grid_linear)
2921 42 : CALL MOVE_ALLOC(partner_linear, plan%partner_linear)
2922 42 : CALL MOVE_ALLOC(input_to_native, plan%input_to_native)
2923 504 : ALLOCATE (plan%extract_q, SOURCE=extract_q(:n_extract))
2924 462 : ALLOCATE (plan%extract_partner, SOURCE=extract_partner(:n_extract))
2925 : IF (fft_profile_enabled) plan%profile%seconds(2) = m_walltime() - total_started - plan%profile%seconds(3)
2926 42 : END SUBROUTINE prepare_pair_fft
2927 :
2928 : ! **************************************************************************************************
2929 : !> \brief Execute prepared pair-product panels after the global physical-cell filtering decision.
2930 : !> No real-space gather or packing pass is needed; active input panels are already in FFT order.
2931 : !> \param plan prepared geometry and product input
2932 : !> \param values_k compact unordered-pair output
2933 : !> \param cell_active full-cell mask from the global original-Green norms
2934 : !> \param profile_label chi or self-energy
2935 : !> \param values_R physical product retained for off-mesh self-energy projection
2936 : ! **************************************************************************************************
2937 470 : SUBROUTINE execute_pair_fft(plan, values_k, cell_active, profile_label, values_R)
2938 : TYPE(gw_pair_fft_work_type), INTENT(INOUT), TARGET :: plan
2939 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(OUT) :: values_k(:, :, :)
2940 : LOGICAL, INTENT(IN) :: cell_active(:)
2941 : CHARACTER(LEN=*), INTENT(IN) :: profile_label
2942 : REAL(KIND=dp), INTENT(INOUT), OPTIONAL :: values_R(:, :, :)
2943 :
2944 : COMPLEX(KIND=dp) :: result_a, result_b, spectrum_a, &
2945 : spectrum_b
2946 470 : COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: fft_in(:), fft_out(:), input(:, :), &
2947 470 : output(:, :), scratch(:)
2948 : INTEGER :: count, fft_count, first, i, i_extract, &
2949 : ibatch, icell, ik, n_extract, nfft(3), &
2950 : nmesh(3), partner_output, unit_nr, &
2951 : width
2952 : INTEGER(KIND=int_8) :: extract_reads, fft_transforms, &
2953 : nfft_grid, spectrum_columns
2954 470 : INTEGER, POINTER :: extract_partner(:), extract_q(:), &
2955 470 : grid_linear(:), partner_linear(:)
2956 : REAL(KIND=dp) :: elapsed, other_seconds, &
2957 : prepared_seconds, started, &
2958 : total_started
2959 470 : TYPE(lattice_fft_profile_type), ALLOCATABLE :: profile
2960 :
2961 : IF (fft_profile_enabled) total_started = m_walltime()
2962 0 : CPASSERT(ALLOCATED(plan%batches))
2963 470 : CPASSERT(SIZE(cell_active) == SIZE(plan%inverse%cell_slot))
2964 470 : CPASSERT(SIZE(values_k, 1) == plan%n_pairs)
2965 470 : CPASSERT(SIZE(values_k, 2) == 1)
2966 470 : CPASSERT(SIZE(values_k, 3) == SIZE(plan%grid_linear))
2967 : IF (fft_profile_enabled) THEN
2968 : ALLOCATE (profile, SOURCE=plan%profile)
2969 : prepared_seconds = SUM(profile%seconds(1:3))
2970 : END IF
2971 1880 : nfft = plan%inverse%nfft
2972 1880 : nmesh = plan%nmesh
2973 470 : nfft_grid = plan%nfft_grid
2974 470 : width = plan%width
2975 470 : n_extract = SIZE(plan%extract_q)
2976 470 : spectrum_columns = plan%spectrum_columns
2977 470 : grid_linear => plan%grid_linear
2978 470 : partner_linear => plan%partner_linear
2979 470 : extract_q => plan%extract_q
2980 470 : extract_partner => plan%extract_partner
2981 470 : fft_transforms = 0_int_8
2982 470 : extract_reads = 0_int_8
2983 : IF (fft_profile_enabled) started = m_walltime()
2984 940 : CALL fft_allocate(scratch, [INT(nfft_grid)*((width + 1)/2)])
2985 : IF (fft_profile_enabled) profile%seconds(3) = profile%seconds(3) + m_walltime() - started
2986 470 : DO first = 1, plan%n_pairs, width
2987 553 : count = MIN(width, plan%n_pairs - first + 1)
2988 553 : fft_count = (count + 1)/2
2989 : IF (fft_profile_enabled) fft_transforms = fft_transforms + INT(fft_count, int_8)
2990 553 : ibatch = (first - 1)/width + 1
2991 : IF (fft_profile_enabled) THEN
2992 : profile%elements = profile%elements + INT(count, int_8)
2993 : profile%batches = profile%batches + 1_int_8
2994 : profile%tile_min = MIN(profile%tile_min, count)
2995 : profile%tile_max = MAX(profile%tile_max, count)
2996 : END IF
2997 553 : fft_in => plan%batches(ibatch)%input
2998 553 : fft_out => scratch
2999 553 : input(1:fft_count, 1:INT(nfft_grid)) => fft_in
3000 : IF (fft_profile_enabled) started = m_walltime()
3001 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
3002 553 : !$OMP SHARED(cell_active, input, plan, first, count, values_R) PRIVATE(icell)
3003 : DO icell = 1, SIZE(cell_active)
3004 : IF (cell_active(icell)) CYCLE
3005 : input(:, plan%inverse%cell_slot(icell)) = z_zero
3006 : IF (PRESENT(values_R)) values_R(first:first + count - 1, 1, icell) = 0.0_dp
3007 : END DO
3008 : !$OMP END PARALLEL DO
3009 : IF (fft_profile_enabled) THEN
3010 : profile%seconds(4) = profile%seconds(4) + m_walltime() - started
3011 : profile%pair_major = .TRUE.
3012 : END IF
3013 553 : CALL lattice_fft_execute_pair_major(BWFFT, nfft, fft_count, fft_in, fft_out, profile)
3014 553 : output(1:fft_count, 1:INT(nfft_grid)) => fft_in
3015 : IF (fft_profile_enabled) started = m_walltime()
3016 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
3017 : !$OMP SHARED(values_k, output, grid_linear, partner_linear, first, count, fft_count) &
3018 : !$OMP SHARED(n_extract, extract_q, extract_partner) &
3019 553 : !$OMP PRIVATE(ik, partner_output, i, spectrum_a, spectrum_b, result_a, result_b)
3020 : DO i_extract = 1, n_extract
3021 : ik = extract_q(i_extract)
3022 : partner_output = extract_partner(i_extract)
3023 : ! The cached stencil packs Z=(F_a+i*F_b)/2. Extract with real
3024 : ! additions/subtractions only; partner_linear includes the MP shift.
3025 : IF (grid_linear(ik) == partner_linear(ik)) THEN
3026 : !$OMP SIMD PRIVATE(spectrum_a)
3027 : DO i = 1, count/2
3028 : spectrum_a = output(i, grid_linear(ik))
3029 : values_k(first + 2*i - 2, 1, ik) = CMPLX(2.0_dp*REAL(spectrum_a, dp), 0.0_dp, dp)
3030 : values_k(first + 2*i - 1, 1, ik) = CMPLX(2.0_dp*AIMAG(spectrum_a), 0.0_dp, dp)
3031 : END DO
3032 : ELSE IF (partner_output > 0) THEN
3033 : !$OMP SIMD PRIVATE(spectrum_a, spectrum_b, result_a, result_b)
3034 : DO i = 1, count/2
3035 : spectrum_a = output(i, grid_linear(ik))
3036 : spectrum_b = output(i, partner_linear(ik))
3037 : result_a = CMPLX(REAL(spectrum_a, dp) + REAL(spectrum_b, dp), &
3038 : AIMAG(spectrum_a) - AIMAG(spectrum_b), dp)
3039 : result_b = CMPLX(AIMAG(spectrum_a) + AIMAG(spectrum_b), &
3040 : REAL(spectrum_b, dp) - REAL(spectrum_a, dp), dp)
3041 : values_k(first + 2*i - 2, 1, ik) = result_a
3042 : values_k(first + 2*i - 1, 1, ik) = result_b
3043 : values_k(first + 2*i - 2, 1, partner_output) = CONJG(result_a)
3044 : values_k(first + 2*i - 1, 1, partner_output) = CONJG(result_b)
3045 : END DO
3046 : ELSE
3047 : !$OMP SIMD PRIVATE(spectrum_a, spectrum_b)
3048 : DO i = 1, count/2
3049 : spectrum_a = output(i, grid_linear(ik))
3050 : spectrum_b = output(i, partner_linear(ik))
3051 : values_k(first + 2*i - 2, 1, ik) = &
3052 : CMPLX(REAL(spectrum_a, dp) + REAL(spectrum_b, dp), AIMAG(spectrum_a) - AIMAG(spectrum_b), dp)
3053 : values_k(first + 2*i - 1, 1, ik) = &
3054 : CMPLX(AIMAG(spectrum_a) + AIMAG(spectrum_b), REAL(spectrum_b, dp) - REAL(spectrum_a, dp), dp)
3055 : END DO
3056 : END IF
3057 : IF (MODULO(count, 2) /= 0) THEN
3058 : ! The lone transform is also half-scaled; preserve its complex roundoff.
3059 : values_k(first + count - 1, 1, ik) = 2.0_dp*output(fft_count, grid_linear(ik))
3060 : IF (partner_output > 0) THEN
3061 : values_k(first + count - 1, 1, partner_output) = CONJG(values_k(first + count - 1, 1, ik))
3062 : END IF
3063 : END IF
3064 : END DO
3065 : !$OMP END PARALLEL DO
3066 470 : IF (fft_profile_enabled) THEN
3067 : profile%seconds(8) = profile%seconds(8) + m_walltime() - started
3068 : extract_reads = extract_reads + INT(count/2, int_8)*spectrum_columns + &
3069 : INT(MODULO(count, 2), int_8)*INT(n_extract, int_8)
3070 : END IF
3071 : END DO
3072 :
3073 : IF (fft_profile_enabled) started = m_walltime()
3074 470 : CALL fft_deallocate(scratch)
3075 470 : unit_nr = 0
3076 : IF (fft_profile_enabled) THEN
3077 : profile%seconds(9) = m_walltime() - started
3078 : elapsed = m_walltime() - total_started + prepared_seconds
3079 : other_seconds = MAX(0.0_dp, elapsed - profile%seconds(4) - SUM(profile%seconds(5:8)))
3080 : unit_nr = cp_logger_get_default_io_unit()
3081 : END IF
3082 : IF (unit_nr > 0) THEN
3083 : WRITE (unit_nr, '(T2,A)') 'R2K_FFT_PROFILE| GW unordered-pair panels; logger-rank wall seconds'
3084 : WRITE (unit_nr, '(T2,A,A)') 'R2K_FFT_PROFILE| operation: ', profile_label
3085 : WRITE (unit_nr, '(T2,A,L2)') 'R2K_FFT_PROFILE| input packed by K2R extraction: ', .TRUE.
3086 : WRITE (unit_nr, '(T2,A,2I10)') 'R2K_FFT_PROFILE| active / full input cells: ', &
3087 : SUM(MERGE(1, 0, cell_active)), SIZE(cell_active)
3088 : WRITE (unit_nr, '(T2,A,3I10)') 'R2K_FFT_PROFILE| source shape: ', plan%n_pairs, 1, SIZE(cell_active)
3089 : WRITE (unit_nr, '(T2,A,2I10)') 'R2K_FFT_PROFILE| extracted / full q points: ', &
3090 : SIZE(values_k, 3), SIZE(plan%inverse%k_slot)
3091 : WRITE (unit_nr, '(T2,A,2I10)') 'R2K_FFT_PROFILE| unpack q jobs / conjugate reuses: ', &
3092 : n_extract, SIZE(values_k, 3) - n_extract
3093 : WRITE (unit_nr, '(T2,A,2F14.6)') 'R2K_FFT_PROFILE| extract read/write [GB, logical]: ', &
3094 : complex_bytes*REAL(extract_reads, dp)/bytes_per_GB, &
3095 : complex_bytes*REAL(profile%elements, dp)*REAL(SIZE(values_k, 3), dp)/bytes_per_GB
3096 : WRITE (unit_nr, '(T2,A)') 'R2K_FFT_PROFILE| pair-major FFT-X/plan includes the full 3D batched FFT'
3097 : WRITE (unit_nr, '(T2,A,6I8)') 'R2K_FFT_PROFILE| grid / padded: ', nmesh, nfft
3098 : WRITE (unit_nr, '(T2,A,L2)') 'R2K_FFT_PROFILE| two real pairs per complex FFT: ', .TRUE.
3099 : WRITE (unit_nr, '(T2,A,2I16)') 'R2K_FFT_PROFILE| FFT transforms / real pairs: ', &
3100 : fft_transforms, profile%elements
3101 : WRITE (unit_nr, '(T2,A,2I16)') 'R2K_FFT_PROFILE| complex FFT elements / full: ', &
3102 : nfft_grid*fft_transforms, nfft_grid*profile%elements
3103 : WRITE (unit_nr, '(T2,A,2I16,2I8)') 'R2K_FFT_PROFILE| elements/batches/tile min/max: ', &
3104 : profile%elements, profile%batches, profile%tile_min, profile%tile_max
3105 : WRITE (unit_nr, '(T2,A,9F12.6)') 'R2K_FFT_PROFILE| init/geometry/buffers/fill/X/Y/Z/extract/release: ', profile%seconds
3106 : WRITE (unit_nr, '(T2,A,F14.6)') 'R2K_FFT_PROFILE| stencil-setup (included in geometry): ', plan%setup_seconds
3107 : WRITE (unit_nr, '(T2,A,I16)') 'R2K_FFT_PROFILE| cached real stencil [bytes]: ', &
3108 : INT(int_1_size, int_8)*INT(plan%n_pairs, int_8)*SIZE(cell_active, KIND=int_8)
3109 : WRITE (unit_nr, '(T2,A,L2)') 'R2K_FFT_PROFILE| reused stencil cache: ', plan%cache_hit
3110 : WRITE (unit_nr, '(T2,A,5F12.6)') 'R2K_FFT_PROFILE| grid-fill/FFT+plan/extract/other/total: ', &
3111 : profile%seconds(4), SUM(profile%seconds(5:7)), profile%seconds(8), other_seconds, elapsed
3112 : WRITE (unit_nr, '(T2,A,F14.6)') 'R2K_FFT_PROFILE| total ', elapsed
3113 : END IF
3114 6580 : plan%profile = lattice_fft_profile_type()
3115 470 : plan%setup_seconds = 0.0_dp
3116 470 : plan%cache_hit = .TRUE.
3117 470 : END SUBROUTINE execute_pair_fft
3118 :
3119 : ! **************************************************************************************************
3120 : !> \brief Cosine transform of one q block between imaginary time and frequency,
3121 : !> X_PQ(q,j) = Σ_i T_ji Y_PQ(q,i), as one GEMM over all (P,Q).
3122 : !> \param block Y on input, X on output
3123 : !> \param transform T, the real minimax cosine transform in either direction
3124 : ! **************************************************************************************************
3125 126 : SUBROUTINE cosine_transform_ri_q(block, transform)
3126 : TYPE(ri_q_matrix_block_type), INTENT(INOUT) :: block
3127 : REAL(KIND=dp), INTENT(IN) :: transform(:, :)
3128 :
3129 : CHARACTER(LEN=*), PARAMETER :: routineN = 'cosine_transform_ri_q'
3130 :
3131 126 : COMPLEX(KIND=dp), ALLOCATABLE :: values(:, :, :)
3132 : INTEGER :: handle, n_elements
3133 :
3134 126 : CALL timeset(routineN, handle)
3135 :
3136 126 : CPASSERT(SIZE(transform, 2) == SIZE(block%values, 3))
3137 126 : n_elements = SIZE(block%values, 1)*SIZE(block%values, 2)
3138 630 : ALLOCATE (values(SIZE(block%values, 1), SIZE(block%values, 2), SIZE(transform, 1)))
3139 : CALL zgemm('N', 'T', n_elements, SIZE(transform, 1), SIZE(transform, 2), z_one, block%values, &
3140 115470 : n_elements, CMPLX(transform, KIND=dp), SIZE(transform, 1), z_zero, values, n_elements)
3141 126 : CALL MOVE_ALLOC(values, block%values)
3142 :
3143 126 : CALL timestop(handle)
3144 :
3145 126 : END SUBROUTINE cosine_transform_ri_q
3146 :
3147 : ! **************************************************************************************************
3148 : !> \brief Count live complex elements across independently allocated q blocks.
3149 : !> \param blocks ...
3150 : !> \return ...
3151 : ! **************************************************************************************************
3152 14 : FUNCTION ri_q_storage_elements(blocks) RESULT(nelements)
3153 : TYPE(ri_q_matrix_block_type), INTENT(IN) :: blocks(:)
3154 : INTEGER(KIND=int_8) :: nelements
3155 :
3156 : INTEGER :: iq
3157 :
3158 14 : nelements = 0_int_8
3159 77 : DO iq = 1, SIZE(blocks)
3160 77 : IF (ALLOCATED(blocks(iq)%values)) THEN
3161 252 : nelements = nelements + SIZE(blocks(iq)%values, KIND=int_8)
3162 : END IF
3163 : END DO
3164 14 : END FUNCTION ri_q_storage_elements
3165 :
3166 : ! **************************************************************************************************
3167 : !> \brief Transfer unordered grid pairs into bounded q batches and consume the Hermitian
3168 : !> panels as Z(q)^H chi(q) Z(q), without expanding a full grid matrix.
3169 : !> \param fft_pairs compact needed-q columns, ordered by ascending original q index
3170 : !> \param para_env ...
3171 : !> \param needed q points to transfer
3172 : !> \param Z_q interpolation matrices at the q points owned by this rank, MOD(q,P)
3173 : !> \param chi_q accumulate projected matrices in compact local q order
3174 : !> \param batch_rounds common maximum number of communication rounds per projection batch
3175 : !> \param projection_seconds logger-rank projection wall time, excluding transfers
3176 : ! **************************************************************************************************
3177 152 : SUBROUTINE project_fft_pairs_to_chi(fft_pairs, para_env, needed, Z_q, chi_q, batch_rounds, projection_seconds)
3178 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(IN), TARGET :: fft_pairs(:, :, :)
3179 : TYPE(mp_para_env_type), POINTER :: para_env
3180 : LOGICAL, INTENT(IN) :: needed(:)
3181 : COMPLEX(KIND=dp), INTENT(IN) :: Z_q(:, :, :)
3182 : COMPLEX(KIND=dp), INTENT(INOUT) :: chi_q(:, :, :)
3183 : INTEGER, INTENT(IN) :: batch_rounds
3184 : REAL(KIND=dp), INTENT(OUT) :: projection_seconds
3185 :
3186 : CHARACTER(LEN=*), PARAMETER :: routineN = 'project_fft_pairs_to_chi'
3187 :
3188 152 : COMPLEX(KIND=dp), ALLOCATABLE :: panel(:, :, :), work(:, :, :)
3189 152 : COMPLEX(KIND=dp), ALLOCATABLE, TARGET :: q_pairs(:, :)
3190 152 : COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: received(:), sent(:)
3191 : COMPLEX(KIND=dp), TARGET :: unused_receive(1), unused_send(1)
3192 : INTEGER :: capacity, first_round, handle, i_buffer, i_column, i_first, i_local, i_needed, &
3193 : i_rank, i_round, i_thread, iq, n_aux, n_buffer, n_grid, n_local, n_needed_local, n_q, &
3194 : n_rank, n_round, n_selected, n_workers
3195 152 : INTEGER, ALLOCATABLE :: pair_counts(:), pair_displs(:), &
3196 : recv_counts(:), send_counts(:), &
3197 : send_displs(:)
3198 : LOGICAL :: receiving
3199 : REAL(KIND=dp) :: started
3200 :
3201 152 : CALL timeset(routineN, handle)
3202 :
3203 152 : n_rank = para_env%num_pe
3204 152 : n_grid = SIZE(Z_q, 1)
3205 152 : n_aux = SIZE(Z_q, 2)
3206 152 : n_local = SIZE(fft_pairs, 1)
3207 152 : n_q = SIZE(needed)
3208 152 : CPASSERT(SIZE(fft_pairs, 2) == 1)
3209 3712 : CPASSERT(SIZE(fft_pairs, 3) == COUNT(needed))
3210 152 : CPASSERT(batch_rounds > 0)
3211 7424 : n_needed_local = COUNT([(needed(iq) .AND. MODULO(iq, n_rank) == para_env%mepos, iq=1, n_q)])
3212 608 : CPASSERT(ALL(SHAPE(chi_q) == [n_aux, n_aux, n_needed_local]))
3213 152 : capacity = MIN(batch_rounds, n_needed_local)
3214 152 : n_workers = 1
3215 152 : !$ n_workers = omp_get_max_threads()
3216 152 : n_workers = MAX(1, MIN(n_workers, capacity))
3217 152 : CALL grid_pair_blocks(n_grid, n_rank, pair_counts, pair_displs)
3218 : ALLOCATE (recv_counts(0:n_rank - 1), &
3219 : send_counts(0:n_rank - 1), send_displs(0:n_rank - 1), &
3220 0 : q_pairs(INT(INT(n_grid, int_8)*(n_grid + 1)/2), capacity), work(n_grid, n_aux, n_workers), &
3221 2432 : panel(n_grid, MIN(n_grid, grid_pair_panel_cols), n_workers))
3222 152 : unused_receive = z_zero
3223 152 : unused_send = z_zero
3224 152 : i_column = 0
3225 152 : i_local = 0
3226 152 : projection_seconds = 0.0_dp
3227 1056 : DO first_round = 1, (n_q + n_rank - 1)/n_rank, batch_rounds
3228 904 : n_buffer = 0
3229 4464 : DO i_round = first_round, MIN(first_round + batch_rounds - 1, (n_q + n_rank - 1)/n_rank)
3230 3560 : i_first = (i_round - 1)*n_rank
3231 3560 : n_round = MIN(n_rank, n_q - i_first)
3232 3560 : send_counts(:) = 0
3233 3560 : send_displs(:) = 0
3234 3560 : n_selected = 0
3235 7120 : DO iq = i_first + 1, i_first + n_round
3236 3560 : IF (.NOT. needed(iq)) CYCLE
3237 1800 : i_rank = MODULO(iq, n_rank)
3238 1800 : send_counts(i_rank) = n_local
3239 1800 : send_displs(i_rank) = n_selected*n_local
3240 7120 : n_selected = n_selected + 1
3241 : END DO
3242 3560 : IF (n_selected == 0) CYCLE
3243 : ! Compact q columns are contiguous even when their original q indices have gaps.
3244 1800 : sent => unused_send
3245 1800 : IF (n_local > 0) THEN
3246 1800 : sent(1:n_local*n_selected) => fft_pairs(:, :, i_column + 1:i_column + n_selected)
3247 : END IF
3248 1800 : i_column = i_column + n_selected
3249 1800 : iq = i_first + MODULO(para_env%mepos - 1, n_rank) + 1
3250 1800 : receiving = iq <= n_q
3251 1800 : IF (receiving) receiving = needed(iq)
3252 1800 : recv_counts(:) = 0
3253 1800 : received => unused_receive
3254 1800 : IF (receiving) THEN
3255 3600 : recv_counts(:) = pair_counts
3256 1800 : i_local = i_local + 1
3257 1800 : n_buffer = n_buffer + 1
3258 1800 : CPASSERT(n_buffer <= capacity)
3259 1800 : received => q_pairs(:, n_buffer)
3260 : END IF
3261 4464 : CALL para_env%alltoall(sent, send_counts, send_displs, received, recv_counts, pair_displs)
3262 : END DO
3263 904 : IF (n_buffer == 0) CYCLE
3264 : IF (fft_profile_enabled) started = m_walltime()
3265 : ! Distinct q matrices remain independent; each thread reuses its GEMM workspace.
3266 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) IF(n_buffer > 1) NUM_THREADS(n_workers) &
3267 : !$OMP SHARED(n_buffer, n_grid, n_aux, q_pairs, Z_q, chi_q, panel, work, i_local) &
3268 952 : !$OMP PRIVATE(i_buffer, i_thread, i_needed)
3269 : DO i_buffer = 1, n_buffer
3270 : i_thread = 1
3271 : !$ i_thread = omp_get_thread_num() + 1
3272 : i_needed = i_local - n_buffer + i_buffer
3273 : CALL grid_pair_upper_multiply(n_grid, n_aux, q_pairs(:, i_buffer), Z_q(:, :, i_needed), &
3274 : work(:, :, i_thread), panel(:, :, i_thread))
3275 : ! Z^H H Z = Z^H (U Z) + (U Z)^H Z; accumulate only the Hermitian upper triangle.
3276 : CALL zher2k('U', 'C', n_aux, n_grid, z_one, Z_q(:, :, i_needed), n_grid, &
3277 : work(:, :, i_thread), n_grid, 1.0_dp, chi_q(:, :, i_needed), n_aux)
3278 : END DO
3279 : !$OMP END PARALLEL DO
3280 576 : IF (fft_profile_enabled) projection_seconds = projection_seconds + m_walltime() - started
3281 : END DO
3282 152 : CPASSERT(i_column == SIZE(fft_pairs, 3))
3283 152 : CPASSERT(i_local == n_needed_local)
3284 :
3285 152 : CALL timestop(handle)
3286 :
3287 304 : END SUBROUTINE project_fft_pairs_to_chi
3288 :
3289 : ! **************************************************************************************************
3290 : !> \brief Project W as Z W Z^H or the bare interaction as (ZK)(ZK)^H, then exchange its panels.
3291 : !> D(q) and d(q) come from real-space real coefficients, so the RI fit obeys Z(-q)=Z(q)*.
3292 : !> Apply the same conjugation to the projected W after exchanging it with the grid-pair owners.
3293 : !> \param Z_q ...
3294 : !> \param interaction_q Compact local W(q,iτ) matrices or one K(q) factor per q
3295 : !> \param itime ...
3296 : !> \param para_env ...
3297 : !> \param work shared geometry and exchange layout
3298 : !> \param fft_pairs ...
3299 : !> \param factor whether interaction_q contains Coulomb factors
3300 : ! **************************************************************************************************
3301 166 : SUBROUTINE project_auxiliary_q_to_fft_pairs(Z_q, interaction_q, itime, para_env, work, fft_pairs, factor)
3302 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(IN), TARGET :: Z_q(:, :, :)
3303 : TYPE(ri_q_matrix_block_type), INTENT(IN) :: interaction_q(:)
3304 : INTEGER, INTENT(IN) :: itime
3305 : TYPE(mp_para_env_type), POINTER :: para_env
3306 : TYPE(gw_pair_fft_work_type), INTENT(IN) :: work
3307 : COMPLEX(KIND=dp), CONTIGUOUS, INTENT(OUT), TARGET :: fft_pairs(:, :, :)
3308 : LOGICAL, INTENT(IN) :: factor
3309 :
3310 : CHARACTER(LEN=*), PARAMETER :: routineN = 'project_auxiliary_q_to_fft_pairs'
3311 :
3312 166 : COMPLEX(KIND=dp), ALLOCATABLE :: panel(:, :), sent(:)
3313 166 : COMPLEX(KIND=dp), ALLOCATABLE, TARGET :: ZW(:, :, :)
3314 166 : COMPLEX(KIND=dp), CONTIGUOUS, POINTER :: right(:, :)
3315 : INTEGER :: first_col, handle, i_first, i_q, i_z, n_aux_cols, n_cols, n_grid, n_owned, &
3316 : n_pairs, n_q, n_q_local, n_rank, n_RI, n_round, n_scheduled, n_threads, ri_block
3317 166 : INTEGER, ALLOCATABLE :: jobs(:), recv_counts(:), recv_displs(:)
3318 :
3319 166 : CALL timeset(routineN, handle)
3320 :
3321 166 : n_grid = SIZE(Z_q, 1)
3322 166 : n_RI = SIZE(Z_q, 2)
3323 166 : n_q_local = SIZE(Z_q, 3)
3324 166 : n_q = SIZE(fft_pairs, 3)
3325 166 : n_rank = para_env%num_pe
3326 346 : n_pairs = SUM(work%pair_counts)
3327 166 : CPASSERT(SIZE(fft_pairs, 1) == work%pair_counts(para_env%mepos))
3328 166 : CPASSERT(ALLOCATED(work%q_source))
3329 166 : CPASSERT(SIZE(interaction_q) == n_q_local)
3330 0 : ALLOCATE (sent(MAX(1, n_pairs*MIN(n_q_local, work%k_batch))), ZW(n_grid, n_RI, MIN(n_q_local, work%k_batch)), &
3331 1992 : jobs(work%k_batch), recv_counts(0:n_rank - 1), recv_displs(0:n_rank - 1))
3332 166 : n_threads = 1
3333 166 : !$ n_threads = omp_get_max_threads()
3334 166 : ri_block = MAX(64, (n_RI + n_threads - 1)/n_threads)
3335 :
3336 332 : DO i_first = 1, n_q, n_rank*work%k_batch
3337 166 : n_round = MIN(n_rank*work%k_batch, n_q - i_first + 1)
3338 : CALL pair_exchange_jobs(work, work%q_source, para_env, i_first, n_round, 1, &
3339 166 : jobs, n_owned, n_scheduled, recv_counts, recv_displs)
3340 166 : IF (n_scheduled == 0) CYCLE
3341 :
3342 : !$OMP PARALLEL DEFAULT(NONE) IF(n_owned > 0) &
3343 : !$OMP SHARED(Z_q, ZW, interaction_q, itime, work, sent, jobs, n_owned, n_grid, n_RI, ri_block, factor) &
3344 166 : !$OMP PRIVATE(first_col, i_q, i_z, n_aux_cols, n_cols, panel, right)
3345 : ALLOCATE (panel(n_grid, MIN(n_grid, grid_pair_panel_cols)))
3346 : !$OMP DO COLLAPSE(2) SCHEDULE(STATIC)
3347 : DO i_q = 1, n_owned
3348 : DO first_col = 1, n_RI, ri_block
3349 : i_z = work%q_slot(jobs(i_q))
3350 : n_aux_cols = MIN(ri_block, n_RI - first_col + 1)
3351 : CALL zgemm('N', 'N', n_grid, n_aux_cols, n_RI, z_one, Z_q(:, :, i_z), n_grid, &
3352 : interaction_q(i_z)%values(1, first_col, itime), n_RI, z_zero, ZW(1, first_col, i_q), n_grid)
3353 : END DO
3354 : END DO
3355 : !$OMP END DO
3356 : !$OMP DO COLLAPSE(2) SCHEDULE(STATIC, 1)
3357 : DO i_q = 1, n_owned
3358 : DO first_col = 1, n_grid, grid_pair_panel_cols
3359 : i_z = work%q_slot(jobs(i_q))
3360 : n_cols = MIN(grid_pair_panel_cols, n_grid - first_col + 1)
3361 : right => Z_q(:, :, i_z)
3362 : IF (factor) right => ZW(:, :, i_q)
3363 : CALL grid_pair_gemm_panel(n_grid, n_RI, first_col, n_cols, ZW(:, :, i_q), right, panel)
3364 : CALL pack_grid_pair_panel(work, first_col, n_cols, panel, n_owned, i_q, sent)
3365 : END DO
3366 : END DO
3367 : !$OMP END DO
3368 166 : DEALLOCATE (panel)
3369 : !$OMP END PARALLEL
3370 :
3371 : CALL exchange_pair_round(work, work%q_source, para_env, i_first, n_round, 1, &
3372 332 : n_owned, sent, recv_counts, recv_displs, fft_pairs)
3373 : END DO
3374 166 : CALL restore_pair_conjugates(work%q_source, fft_pairs)
3375 :
3376 166 : CALL timestop(handle)
3377 :
3378 332 : END SUBROUTINE project_auxiliary_q_to_fft_pairs
3379 :
3380 : ! **************************************************************************************************
3381 : !> \brief ...
3382 : !> \param distributed ...
3383 : !> \param owner_matrix ...
3384 : !> \param group_env ...
3385 : !> \param root ...
3386 : !> \param n ...
3387 : ! **************************************************************************************************
3388 1863 : SUBROUTINE set_owner_matrix(distributed, owner_matrix, group_env, root, n)
3389 : TYPE(cp_cfm_type), INTENT(IN) :: distributed
3390 : COMPLEX(KIND=dp), INTENT(IN) :: owner_matrix(:, :)
3391 : TYPE(mp_para_env_type), INTENT(INOUT) :: group_env
3392 : INTEGER, INTENT(IN) :: root, n
3393 :
3394 1863 : COMPLEX(KIND=dp), ALLOCATABLE :: column_block(:, :)
3395 : INTEGER :: first, width
3396 :
3397 1863 : IF (group_env%mepos == root) THEN
3398 5589 : CPASSERT(ALL(SHAPE(owner_matrix) == [n, n]))
3399 : END IF
3400 5589 : ALLOCATE (column_block(n, column_block_cols))
3401 3726 : DO first = 1, n, column_block_cols
3402 1863 : width = MIN(column_block_cols, n - first + 1)
3403 274269 : IF (group_env%mepos == root) column_block(:, 1:width) = owner_matrix(:, first:first + width - 1)
3404 1863 : CALL group_env%bcast(column_block(:, 1:width), root)
3405 : CALL cp_cfm_set_submatrix(distributed, column_block(:, 1:width), &
3406 3726 : start_row=1, start_col=first)
3407 : END DO
3408 1863 : DEALLOCATE (column_block)
3409 1863 : END SUBROUTINE set_owner_matrix
3410 :
3411 : ! **************************************************************************************************
3412 : !> \brief ...
3413 : !> \param distributed ...
3414 : !> \param owner_matrix ...
3415 : !> \param group_env ...
3416 : !> \param root ...
3417 : !> \param n ...
3418 : ! **************************************************************************************************
3419 1800 : SUBROUTINE get_owner_matrix(distributed, owner_matrix, group_env, root, n)
3420 : TYPE(cp_cfm_type), INTENT(IN) :: distributed
3421 : COMPLEX(KIND=dp), INTENT(OUT) :: owner_matrix(:, :)
3422 : TYPE(mp_para_env_type), INTENT(INOUT) :: group_env
3423 : INTEGER, INTENT(IN) :: root, n
3424 :
3425 1800 : COMPLEX(KIND=dp), ALLOCATABLE :: column_block(:, :)
3426 : INTEGER :: first, width
3427 :
3428 1800 : IF (group_env%mepos == root) THEN
3429 5400 : CPASSERT(ALL(SHAPE(owner_matrix) == [n, n]))
3430 : END IF
3431 5400 : ALLOCATE (column_block(n, column_block_cols))
3432 3600 : DO first = 1, n, column_block_cols
3433 1800 : width = MIN(column_block_cols, n - first + 1)
3434 : CALL cp_cfm_get_submatrix(distributed, column_block(:, 1:width), &
3435 1800 : start_row=1, start_col=first)
3436 267712 : IF (group_env%mepos == root) owner_matrix(:, first:first + width - 1) = column_block(:, 1:width)
3437 : END DO
3438 1800 : DEALLOCATE (column_block)
3439 1800 : END SUBROUTINE get_owner_matrix
3440 :
3441 : ! **************************************************************************************************
3442 : !> \brief Number representative q points in native order within each cyclic owner.
3443 : !> \param q_partner ...
3444 : !> \param n_rank ...
3445 : !> \param q_slot local slot for each q, with zero for its omitted partner
3446 : ! **************************************************************************************************
3447 42 : SUBROUTINE compact_q_slots(q_partner, n_rank, q_slot)
3448 : INTEGER, INTENT(IN) :: q_partner(:), n_rank
3449 : INTEGER, ALLOCATABLE, INTENT(OUT) :: q_slot(:)
3450 :
3451 84 : INTEGER :: iq, owner, counts(0:n_rank - 1)
3452 :
3453 126 : ALLOCATE (q_slot(SIZE(q_partner)), SOURCE=0)
3454 112 : counts(:) = 0
3455 786 : DO iq = 1, SIZE(q_partner)
3456 744 : IF (q_partner(iq) < iq) CYCLE
3457 378 : owner = MODULO(iq, n_rank)
3458 378 : counts(owner) = counts(owner) + 1
3459 786 : q_slot(iq) = counts(owner)
3460 : END DO
3461 42 : END SUBROUTINE compact_q_slots
3462 :
3463 : ! **************************************************************************************************
3464 : !> \brief Find -q in the same mesh for every q; q is its own partner where -q ≡ q.
3465 : !> \param xkp q points in fractional coordinates
3466 : !> \param q_partner index of -q for every q
3467 : ! **************************************************************************************************
3468 14 : SUBROUTINE minus_q_partners(xkp, q_partner)
3469 : REAL(KIND=dp), INTENT(IN) :: xkp(:, :)
3470 : INTEGER, ALLOCATABLE, INTENT(OUT) :: q_partner(:)
3471 :
3472 : INTEGER :: ikp, jkp
3473 : REAL(KIND=dp) :: q_sum(3)
3474 :
3475 42 : ALLOCATE (q_partner(SIZE(xkp, 2)), SOURCE=0)
3476 262 : DO ikp = 1, SIZE(xkp, 2)
3477 5190 : DO jkp = 1, SIZE(xkp, 2)
3478 20760 : q_sum(:) = xkp(:, ikp) + xkp(:, jkp)
3479 7446 : IF (ALL(ABS(q_sum - ANINT(q_sum)) < eps_kpoint)) THEN
3480 248 : q_partner(ikp) = jkp
3481 248 : EXIT
3482 : END IF
3483 : END DO
3484 262 : CPASSERT(q_partner(ikp) > 0)
3485 : END DO
3486 14 : END SUBROUTINE minus_q_partners
3487 :
3488 : ! **************************************************************************************************
3489 : !> \brief Partition MPI ranks into balanced contiguous q groups.
3490 : !> \param para_env ...
3491 : !> \param n_qgroups ...
3492 : !> \param my_qgroup ...
3493 : !> \param q_roots ...
3494 : ! **************************************************************************************************
3495 14 : SUBROUTINE grouped_w_partition(para_env, n_qgroups, my_qgroup, q_roots)
3496 : TYPE(mp_para_env_type), POINTER :: para_env
3497 : INTEGER, INTENT(IN) :: n_qgroups
3498 : INTEGER, INTENT(OUT) :: my_qgroup
3499 : INTEGER, ALLOCATABLE, INTENT(OUT) :: q_roots(:)
3500 :
3501 : INTEGER :: i_first, i_group, i_last
3502 :
3503 14 : CPASSERT(n_qgroups > 0 .AND. n_qgroups <= para_env%num_pe)
3504 42 : ALLOCATE (q_roots(n_qgroups))
3505 42 : DO i_group = 0, n_qgroups - 1
3506 28 : i_first = (para_env%num_pe*i_group)/n_qgroups
3507 28 : i_last = (para_env%num_pe*(i_group + 1))/n_qgroups - 1
3508 28 : q_roots(i_group + 1) = i_first
3509 42 : IF (para_env%mepos >= i_first .AND. para_env%mepos <= i_last) my_qgroup = i_group
3510 : END DO
3511 :
3512 14 : END SUBROUTINE grouped_w_partition
3513 :
3514 : ! **************************************************************************************************
3515 : !> \brief ...
3516 : !> \param matrices ...
3517 : !> \param blacs ...
3518 : !> \param matrix_struct ...
3519 : !> \param group_env ...
3520 : !> \param n ...
3521 : !> Construct block-cyclic matrices on the MPI ranks assigned to a q group.
3522 : ! **************************************************************************************************
3523 14 : SUBROUTINE grouped_w_create(matrices, blacs, matrix_struct, group_env, n)
3524 : TYPE(cp_cfm_type), INTENT(INOUT) :: matrices(n_w_matrices)
3525 : TYPE(cp_blacs_env_type), POINTER :: blacs
3526 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct
3527 : TYPE(mp_para_env_type), INTENT(INOUT), TARGET :: group_env
3528 : INTEGER, INTENT(IN) :: n
3529 :
3530 14 : CPASSERT(n > 0)
3531 14 : CALL cp_blacs_env_create(blacs, group_env)
3532 : CALL cp_fm_struct_create(matrix_struct, context=blacs, &
3533 14 : nrow_global=n, ncol_global=n)
3534 14 : CALL cp_cfm_create(matrices(w_chi), matrix_struct)
3535 14 : CALL cp_cfm_create(matrices(w_metric), matrix_struct)
3536 14 : CALL cp_cfm_create(matrices(w_tmp), matrix_struct)
3537 14 : CALL cp_cfm_create(matrices(w_dielectric), matrix_struct)
3538 14 : CALL cp_cfm_create(matrices(w_output), matrix_struct)
3539 14 : END SUBROUTINE grouped_w_create
3540 :
3541 : ! **************************************************************************************************
3542 : !> \brief ...
3543 : !> \param world ...
3544 : !> \param n ...
3545 : !> \param source_rank ...
3546 : !> \param destination_rank ...
3547 : !> \param source ...
3548 : !> \param destination ...
3549 : !> Transfer a matrix between world owners without a full-size communication buffer.
3550 : ! **************************************************************************************************
3551 7326 : SUBROUTINE grouped_w_copy_matrix(world, n, source_rank, destination_rank, source, destination)
3552 : TYPE(mp_para_env_type), INTENT(INOUT) :: world
3553 : INTEGER, INTENT(IN) :: n, source_rank, destination_rank
3554 : COMPLEX(KIND=dp), INTENT(IN) :: source(:, :)
3555 : COMPLEX(KIND=dp), INTENT(INOUT) :: destination(:, :)
3556 :
3557 : INTEGER :: first, last, sender, tag
3558 :
3559 7326 : IF (source_rank == destination_rank) THEN
3560 543844 : IF (world%mepos == source_rank) destination = source
3561 7326 : RETURN
3562 : END IF
3563 0 : DO first = 1, n, column_block_cols
3564 0 : last = MIN(first + column_block_cols - 1, n)
3565 0 : IF (world%mepos == source_rank) THEN
3566 0 : CALL world%send(source(:, first:last), destination_rank, 0)
3567 0 : ELSE IF (world%mepos == destination_rank) THEN
3568 0 : sender = source_rank
3569 0 : tag = 0
3570 0 : CALL world%recv(destination(:, first:last), sender, tag)
3571 : END IF
3572 : END DO
3573 : END SUBROUTINE grouped_w_copy_matrix
3574 :
3575 : ! **************************************************************************************************
3576 : !> \brief Evaluate screened W with a distributed positive-definite dielectric inverse.
3577 : !> For ε=I+BᴴχB after the unchanged positive χ filter, Cholesky gives ε^-1.
3578 : !> Reuse K=M^-1 V^1/2 to form W=K(ε^-1-I)Kᴴ.
3579 : !> \param matrices ...
3580 : !> \param group_env ...
3581 : !> \param n ...
3582 : !> \param root ...
3583 : !> \param chi_owner ...
3584 : !> \param cutoff ...
3585 : !> \param output_owner ...
3586 : ! **************************************************************************************************
3587 1800 : SUBROUTINE grouped_w_solve(matrices, group_env, n, root, chi_owner, cutoff, output_owner)
3588 : TYPE(cp_cfm_type), INTENT(INOUT) :: matrices(n_w_matrices)
3589 : TYPE(mp_para_env_type), INTENT(INOUT) :: group_env
3590 : INTEGER, INTENT(IN) :: n, root
3591 : COMPLEX(KIND=dp), INTENT(IN) :: chi_owner(:, :)
3592 : REAL(KIND=dp), INTENT(IN) :: cutoff
3593 : COMPLEX(KIND=dp), INTENT(OUT) :: output_owner(:, :)
3594 :
3595 : INTEGER :: info
3596 :
3597 1800 : CPASSERT(root >= 0 .AND. root < group_env%num_pe)
3598 1800 : CALL set_owner_matrix(matrices(w_chi), chi_owner, group_env, root, n)
3599 :
3600 : ! Match the dense reference: symmetrize before each Hermitian eigensolve.
3601 1800 : CALL cp_cfm_transpose(matrices(w_chi), 'C', matrices(w_output))
3602 : CALL cp_cfm_scale_and_add(CMPLX(0.5_dp, 0.0_dp, KIND=dp), matrices(w_chi), &
3603 1800 : CMPLX(0.5_dp, 0.0_dp, KIND=dp), matrices(w_output))
3604 1800 : CALL cp_cfm_power(matrices(w_chi), exponent=1.0_dp, threshold=cutoff)
3605 : CALL parallel_gemm('C', 'N', n, n, n, z_one, matrices(w_metric), &
3606 1800 : matrices(w_chi), z_zero, matrices(w_tmp))
3607 : CALL parallel_gemm('N', 'N', n, n, n, z_one, matrices(w_tmp), &
3608 1800 : matrices(w_metric), z_zero, matrices(w_dielectric))
3609 1800 : CALL cp_cfm_add_on_diag(matrices(w_dielectric), z_one)
3610 1800 : CALL cp_cfm_transpose(matrices(w_dielectric), 'C', matrices(w_output))
3611 : CALL cp_cfm_scale_and_add(CMPLX(0.5_dp, 0.0_dp, KIND=dp), matrices(w_dielectric), &
3612 1800 : CMPLX(0.5_dp, 0.0_dp, KIND=dp), matrices(w_output))
3613 : ! χ is positive semidefinite after filtering: ε=I+BᴴχB has λ_min>=1.
3614 1800 : CALL cp_cfm_cholesky_decompose(matrices(w_dielectric), info_out=info)
3615 1800 : IF (info /= 0) THEN
3616 0 : CPABORT("Screened dielectric Cholesky factorization failed")
3617 : END IF
3618 1800 : CALL cp_cfm_cholesky_invert(matrices(w_dielectric), info_out=info)
3619 1800 : IF (info /= 0) CPABORT("Screened dielectric Cholesky inversion failed")
3620 1800 : CALL cp_cfm_uplo_to_full(matrices(w_dielectric), matrices(w_output), uplo='U')
3621 1800 : CALL cp_cfm_add_on_diag(matrices(w_dielectric), -z_one)
3622 : ! With K=M^-1 V^1/2 and Hermitian M^-1, W=K(ε^-1-I)Kᴴ.
3623 : ! The existing factor removes two matrix products at every later frequency.
3624 : CALL parallel_gemm('N', 'N', n, n, n, z_one, matrices(w_metric), &
3625 1800 : matrices(w_dielectric), z_zero, matrices(w_tmp))
3626 : CALL parallel_gemm('N', 'C', n, n, n, z_one, matrices(w_tmp), &
3627 1800 : matrices(w_metric), z_zero, matrices(w_output))
3628 1800 : CALL get_owner_matrix(matrices(w_output), output_owner, group_env, root, n)
3629 1800 : END SUBROUTINE grouped_w_solve
3630 :
3631 : ! **************************************************************************************************
3632 : !> \brief Release the distributed dielectric workspaces for a q-point group.
3633 : !> \param matrices Distributed matrix storage.
3634 : !> \param matrix_struct ...
3635 : !> \param blacs ...
3636 : ! **************************************************************************************************
3637 14 : SUBROUTINE grouped_w_release(matrices, matrix_struct, blacs)
3638 : TYPE(cp_cfm_type), INTENT(INOUT) :: matrices(n_w_matrices)
3639 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct
3640 : TYPE(cp_blacs_env_type), POINTER :: blacs
3641 :
3642 14 : CALL cp_cfm_release(matrices(w_chi))
3643 14 : CALL cp_cfm_release(matrices(w_metric))
3644 14 : CALL cp_cfm_release(matrices(w_tmp))
3645 14 : CALL cp_cfm_release(matrices(w_dielectric))
3646 14 : CALL cp_cfm_release(matrices(w_output))
3647 14 : CALL cp_fm_struct_release(matrix_struct)
3648 14 : CALL cp_blacs_env_release(blacs)
3649 14 : END SUBROUTINE grouped_w_release
3650 :
3651 : ! **************************************************************************************************
3652 : !> \brief Release all periodic RI-RS work data after the last quasiparticle iteration.
3653 : !> \param bs_env Band-structure environment owning the work data.
3654 : ! **************************************************************************************************
3655 14 : SUBROUTINE release_gw_work(bs_env)
3656 : TYPE(post_scf_bandstructure_type), POINTER :: bs_env
3657 :
3658 : CHARACTER(LEN=*), PARAMETER :: routineN = 'release_gw_work'
3659 :
3660 : INTEGER :: handle
3661 : TYPE(gwkp_type), POINTER :: gwkp_env
3662 :
3663 14 : CALL timeset(routineN, handle)
3664 :
3665 14 : gwkp_env => bs_env%gwkp_env
3666 14 : CALL mp_para_env_release(gwkp_env%group_env)
3667 14 : IF (ALLOCATED(gwkp_env%eigenvalues_G)) DEALLOCATE (gwkp_env%eigenvalues_G)
3668 : ! AO image pointers are released immediately after the Gamma contraction.
3669 14 : CPASSERT(.NOT. ASSOCIATED(gwkp_env%gamma_corr_d))
3670 14 : CPASSERT(.NOT. ASSOCIATED(gwkp_env%gamma_corr_h))
3671 14 : CPASSERT(.NOT. ASSOCIATED(gwkp_env%gamma_corr_s))
3672 14 : CPASSERT(.NOT. ASSOCIATED(gwkp_env%gamma_corr_index_to_cell))
3673 14 : CPASSERT(.NOT. ASSOCIATED(gwkp_env%gamma_corr_cell_to_index))
3674 140 : DEALLOCATE (bs_env%gwkp_env)
3675 14 : NULLIFY (gwkp_env)
3676 :
3677 14 : CALL timestop(handle)
3678 :
3679 14 : END SUBROUTINE release_gw_work
3680 :
3681 48 : END MODULE gw_ri_rs_small_cell_full_kp_utils
|