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 DBCSR collocation, layouts and panel contractions for real-space resolution of the identity.
10 : ! **************************************************************************************************
11 : MODULE ri_rs_matrices
12 : USE basis_set_types, ONLY: gto_basis_set_p_type,&
13 : gto_basis_set_type
14 : USE cp_dbcsr_api, ONLY: &
15 : dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_distribution_get, dbcsr_distribution_new, &
16 : dbcsr_distribution_release, dbcsr_distribution_type, dbcsr_finalize, dbcsr_get_data_size, &
17 : dbcsr_get_info, dbcsr_get_occupation, dbcsr_iterator_blocks_left, &
18 : dbcsr_iterator_next_block, dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, &
19 : dbcsr_multiply, dbcsr_p_type, dbcsr_put_block, dbcsr_release, dbcsr_set, dbcsr_type, &
20 : dbcsr_type_no_symmetry
21 : USE cp_dbcsr_operations, ONLY: copy_fm_to_dbcsr,&
22 : max_elements_per_block
23 : USE cp_fm_types, ONLY: cp_fm_create,&
24 : cp_fm_release,&
25 : cp_fm_to_fm,&
26 : cp_fm_type
27 : USE gw_auto_ri_types, ONLY: auto_ri_type
28 : USE gw_utils_dbcsr, ONLY: hadamard_product_inplace
29 : USE gw_utils_fm, ONLY: fm_contract_ABA
30 : USE integral_library_types, ONLY: coulomb_operator_type
31 : USE kinds, ONLY: dp,&
32 : int_8,&
33 : max_line_length
34 : USE machine, ONLY: m_flush
35 : USE message_passing, ONLY: mp_mem_avail_per_rank_GB,&
36 : mp_para_env_type
37 : USE mp2_ri_2c, ONLY: RI_2c_integral_mat
38 : USE qs_environment_types, ONLY: qs_environment_type
39 : USE ri_rs_types, ONLY: ri_rs_env
40 : USE ri_rs_utils, ONLY: evaluate_ao_basis_on_points
41 : #include "./base/base_uses.f90"
42 :
43 : IMPLICIT NONE
44 : PRIVATE
45 : PUBLIC :: atomic_basis_at_grid_point, setup_square_topology, &
46 : release_dbcsr_topology_and_matrices, reserve_blocks_within_radius, &
47 : print_matrix_occupation, &
48 : contract_grid_panels, mask_grid_blocks_near_panel, &
49 : panel_template_elems, panel_mem_estimate_GB, resolve_grid_panels, &
50 : build_ri_rs_bare_aux_interaction
51 :
52 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_matrices'
53 :
54 : CONTAINS
55 :
56 : ! **************************************************************************************************
57 : !> \brief Builds the grid-by-AO collocation matrix with bounded dense evaluation panels.
58 : !> The molecular path uses atom-aligned row blocks and minimum-image AO values.
59 : !> The Gamma path uses uniform row blocks and sums neighbouring periodic images.
60 : !> \param ri_rs grid, borrowed geometry references and output collocation matrix
61 : !> \param basis_set_AO AO basis sets, one per atomic kind
62 : !> \param matrix_template template for the process grid and AO column block sizes
63 : !> \param para_env parallel environment
64 : !> \param eps_filter block screening threshold
65 : !> \param msg_elem_limit maximum number of elements per DBCSR message
66 : !> \param unit_nr output unit
67 : !> \param periodic_images select neighbouring-image summation for Gamma
68 : ! **************************************************************************************************
69 50 : SUBROUTINE atomic_basis_at_grid_point(ri_rs, basis_set_AO, matrix_template, &
70 : para_env, eps_filter, msg_elem_limit, unit_nr, periodic_images)
71 : TYPE(ri_rs_env), INTENT(INOUT) :: ri_rs
72 : TYPE(gto_basis_set_p_type), INTENT(IN) :: basis_set_AO(:)
73 : TYPE(dbcsr_type), INTENT(IN) :: matrix_template
74 : TYPE(mp_para_env_type), POINTER :: para_env
75 : REAL(KIND=dp), INTENT(IN) :: eps_filter
76 : INTEGER(KIND=int_8), INTENT(IN) :: msg_elem_limit
77 : INTEGER, INTENT(IN) :: unit_nr
78 : LOGICAL, INTENT(IN) :: periodic_images
79 :
80 : CHARACTER(LEN=*), PARAMETER :: routineN = 'atomic_basis_at_grid_point'
81 : INTEGER, PARAMETER :: grid_panel_size = 4096
82 :
83 : INTEGER :: blocks_per_panel, bs_eff, c_size, first_block, handle, i, i_blk, ia, iatom, &
84 : ikind, last_block, n_grid, natom, npcol, nprow, num_grid_chunks, panel_size, panel_start, &
85 : r_end, r_start, remaining, row_first, row_last, run, safe_max
86 50 : INTEGER, ALLOCATABLE :: blk_row_start(:)
87 50 : INTEGER, CONTIGUOUS, POINTER :: c_blk_sizes(:), col_dist(:), &
88 50 : r_blk_sizes(:), row_dist(:)
89 : REAL(KIND=dp) :: cutoff_squared
90 50 : REAL(KIND=dp), ALLOCATABLE :: atom_col_buffer(:, :)
91 : TYPE(dbcsr_distribution_type) :: dist, template_dist
92 : TYPE(gto_basis_set_type), POINTER :: ao_basis
93 :
94 50 : CALL timeset(routineN, handle)
95 50 : CPASSERT(ASSOCIATED(ri_rs%cell))
96 50 : CPASSERT(ASSOCIATED(para_env))
97 50 : CPASSERT(ASSOCIATED(ri_rs%particle_set))
98 50 : CALL dbcsr_get_info(matrix_template, distribution=template_dist, col_blk_size=c_blk_sizes)
99 50 : natom = SIZE(c_blk_sizes)
100 50 : n_grid = SIZE(ri_rs%grid_points, 2)
101 50 : CPASSERT(SIZE(ri_rs%particle_set) == natom)
102 50 : CPASSERT(n_grid == ri_rs%n_grid_points)
103 50 : CALL dbcsr_distribution_get(template_dist, nprows=nprow, npcols=npcol)
104 :
105 50 : bs_eff = max_elements_per_block
106 50 : IF (.NOT. periodic_images) THEN
107 50 : safe_max = INT(msg_elem_limit*MAX(MIN(nprow, npcol), 1)/n_grid/2)
108 50 : bs_eff = MAX(1, MIN(bs_eff, MAX(1, safe_max)))
109 : END IF
110 50 : IF (periodic_images) THEN
111 0 : num_grid_chunks = n_grid/bs_eff
112 0 : IF (MOD(n_grid, bs_eff) /= 0) num_grid_chunks = num_grid_chunks + 1
113 : ELSE
114 50 : num_grid_chunks = 0
115 174 : DO ia = 1, natom
116 124 : run = ri_rs%grid_atom_boundaries(ia + 1) - ri_rs%grid_atom_boundaries(ia)
117 174 : IF (run > 0) num_grid_chunks = num_grid_chunks + (run + bs_eff - 1)/bs_eff
118 : END DO
119 : END IF
120 200 : ALLOCATE (r_blk_sizes(num_grid_chunks), blk_row_start(num_grid_chunks))
121 50 : i_blk = 0
122 50 : r_start = 1
123 224 : DO ia = 1, MERGE(1, natom, periodic_images)
124 124 : IF (periodic_images) THEN
125 : remaining = n_grid
126 : ELSE
127 124 : remaining = ri_rs%grid_atom_boundaries(ia + 1) - ri_rs%grid_atom_boundaries(ia)
128 : END IF
129 830 : DO WHILE (remaining > 0)
130 656 : i_blk = i_blk + 1
131 656 : r_blk_sizes(i_blk) = MIN(bs_eff, remaining)
132 656 : blk_row_start(i_blk) = r_start
133 656 : r_start = r_start + r_blk_sizes(i_blk)
134 656 : remaining = remaining - r_blk_sizes(i_blk)
135 : END DO
136 : END DO
137 :
138 50 : IF (.NOT. periodic_images) THEN
139 50 : IF (unit_nr > 0) THEN
140 25 : WRITE (unit_nr, '(T2,A,T71,I12)') 'RI-RS grid row-blocks of ϕ_μ(r_l)', num_grid_chunks
141 25 : WRITE (unit_nr, '(T2,A,T69,I12)') 'RI-RS grid points per block (max)', bs_eff
142 : END IF
143 50 : IF (ri_rs%cutoff_radius_v_w > 0.0_dp .OR. ri_rs%cutoff_radius_w0 > 0.0_dp) THEN
144 8 : IF (ALLOCATED(ri_rs%chunk_centroids)) DEALLOCATE (ri_rs%chunk_centroids)
145 24 : ALLOCATE (ri_rs%chunk_centroids(3, num_grid_chunks))
146 110 : DO i_blk = 1, num_grid_chunks
147 102 : r_start = blk_row_start(i_blk)
148 102 : r_end = r_start + r_blk_sizes(i_blk) - 1
149 458 : DO i = 1, 3
150 : ri_rs%chunk_centroids(i, i_blk) = &
151 8952 : SUM(ri_rs%grid_points(i, r_start:r_end))/REAL(r_blk_sizes(i_blk), dp)
152 : END DO
153 : END DO
154 : END IF
155 : END IF
156 :
157 200 : ALLOCATE (row_dist(num_grid_chunks), col_dist(natom))
158 706 : DO i = 1, num_grid_chunks
159 706 : row_dist(i) = MOD(i - 1, nprow)
160 : END DO
161 174 : DO i = 1, natom
162 174 : col_dist(i) = MOD(i - 1, npcol)
163 : END DO
164 50 : CALL dbcsr_distribution_new(dist, template=template_dist, row_dist=row_dist, col_dist=col_dist)
165 : CALL dbcsr_create(ri_rs%mat_phi_mu_l, name='phi_val_sparse', dist=dist, &
166 50 : matrix_type=dbcsr_type_no_symmetry, row_blk_size=r_blk_sizes, col_blk_size=c_blk_sizes)
167 :
168 706 : blocks_per_panel = MAX(1, grid_panel_size/MAXVAL(r_blk_sizes))
169 50 : DO iatom = para_env%mepos + 1, natom, para_env%num_pe
170 62 : c_size = c_blk_sizes(iatom)
171 1074 : ALLOCATE (atom_col_buffer(MIN(n_grid, blocks_per_panel*MAXVAL(r_blk_sizes)), c_size))
172 62 : cutoff_squared = ri_rs%radius_ao_per_atom(iatom)**2
173 62 : IF (.NOT. periodic_images .AND. ri_rs%cutoff_radius_ri_ao > 0.0_dp) THEN
174 3 : cutoff_squared = ri_rs%cutoff_radius_ri_ao**2
175 : END IF
176 62 : ikind = ri_rs%particle_set(iatom)%atomic_kind%kind_number
177 62 : ao_basis => basis_set_AO(ikind)%gto_basis_set
178 124 : DO first_block = 1, num_grid_chunks, blocks_per_panel
179 62 : last_block = MIN(first_block + blocks_per_panel - 1, num_grid_chunks)
180 62 : panel_start = blk_row_start(first_block)
181 62 : panel_size = blk_row_start(last_block) + r_blk_sizes(last_block) - panel_start
182 83589 : atom_col_buffer(:panel_size, :) = 0.0_dp
183 : CALL evaluate_ao_basis_on_points(atom_col_buffer(:panel_size, :), &
184 : ri_rs%grid_points(:, panel_start:panel_start + panel_size - 1), &
185 : ao_basis, ri_rs%particle_set(iatom)%r, ri_rs%cell, &
186 62 : cutoff_squared=cutoff_squared, periodic_images=periodic_images)
187 950 : DO i_blk = first_block, last_block
188 826 : row_first = blk_row_start(i_blk) - panel_start + 1
189 826 : row_last = row_first + r_blk_sizes(i_blk) - 1
190 87177 : IF (MAXVAL(ABS(atom_col_buffer(row_first:row_last, :))) > eps_filter) THEN
191 : CALL dbcsr_put_block(ri_rs%mat_phi_mu_l, row=i_blk, col=iatom, &
192 814 : block=atom_col_buffer(row_first:row_last, :))
193 : END IF
194 : END DO
195 : END DO
196 62 : DEALLOCATE (atom_col_buffer)
197 : END DO
198 50 : CALL dbcsr_finalize(ri_rs%mat_phi_mu_l)
199 50 : DEALLOCATE (r_blk_sizes, row_dist, col_dist)
200 50 : CALL dbcsr_distribution_release(dist)
201 50 : CALL timestop(handle)
202 250 : END SUBROUTINE atomic_basis_at_grid_point
203 :
204 : ! **************************************************************************************************
205 : !> \brief Creates a square topology from a selected row or column layout.
206 : !> \param matrix_template source matrix
207 : !> \param dim_type ROW or COL
208 : !> \param square_dist output distribution
209 : !> \param blk_sizes borrowed block sizes
210 : !> \param mapped_dist owned mapping in the other process-grid direction
211 : ! **************************************************************************************************
212 37914 : SUBROUTINE setup_square_topology(matrix_template, dim_type, square_dist, blk_sizes, mapped_dist)
213 :
214 : TYPE(dbcsr_type), INTENT(IN) :: matrix_template
215 : CHARACTER(LEN=*), INTENT(IN) :: dim_type
216 : TYPE(dbcsr_distribution_type), INTENT(OUT) :: square_dist
217 : INTEGER, CONTIGUOUS, DIMENSION(:), INTENT(OUT), &
218 : POINTER :: blk_sizes, mapped_dist
219 :
220 : CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_square_topology'
221 :
222 : INTEGER :: handle, i, np, npcols, nprows
223 12638 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk, col_dist, row_blk, row_dist
224 : TYPE(dbcsr_distribution_type) :: dist_template
225 :
226 12638 : CALL timeset(routineN, handle)
227 :
228 : CALL dbcsr_get_info(matrix_template, distribution=dist_template, &
229 12638 : row_blk_size=row_blk, col_blk_size=col_blk)
230 : CALL dbcsr_distribution_get(dist_template, row_dist=row_dist, col_dist=col_dist, &
231 12638 : nprows=nprows, npcols=npcols)
232 :
233 12638 : IF (TRIM(dim_type) == 'ROW') THEN
234 : ! Creates ROW x ROW (e.g., Grid x Grid from mat_phi_mu_l)
235 600 : blk_sizes => row_blk
236 600 : np = npcols
237 1800 : ALLOCATE (mapped_dist(SIZE(blk_sizes)))
238 6960 : DO i = 1, SIZE(blk_sizes)
239 6960 : mapped_dist(i) = MOD(i - 1, np)
240 : END DO
241 : CALL dbcsr_distribution_new(square_dist, template=dist_template, &
242 600 : row_dist=row_dist, col_dist=mapped_dist)
243 :
244 12038 : ELSE IF (TRIM(dim_type) == 'COL') THEN
245 : ! Creates COL x COL (e.g., Aux x Aux from mat_Z_lP)
246 12038 : blk_sizes => col_blk
247 12038 : np = nprows
248 36114 : ALLOCATE (mapped_dist(SIZE(blk_sizes)))
249 37394 : DO i = 1, SIZE(blk_sizes)
250 37394 : mapped_dist(i) = MOD(i - 1, np)
251 : END DO
252 : CALL dbcsr_distribution_new(square_dist, template=dist_template, &
253 12038 : row_dist=mapped_dist, col_dist=col_dist)
254 : END IF
255 :
256 12638 : CALL timestop(handle)
257 :
258 12638 : END SUBROUTINE setup_square_topology
259 :
260 : ! **************************************************************************************************
261 : !> \brief Releases RI-RS topology mappings and temporary DBCSR matrices.
262 : !> \param dist distribution to release
263 : !> \param mapped_dist owned mapping to deallocate
264 : !> \param m1 optional matrix
265 : !> \param m2 optional matrix
266 : !> \param m3 optional matrix
267 : !> \param m4 optional matrix
268 : ! **************************************************************************************************
269 17238 : SUBROUTINE release_dbcsr_topology_and_matrices(dist, mapped_dist, m1, m2, m3, m4)
270 :
271 : TYPE(dbcsr_distribution_type), INTENT(INOUT), &
272 : OPTIONAL :: dist
273 : INTEGER, DIMENSION(:), INTENT(INOUT), OPTIONAL, &
274 : POINTER :: mapped_dist
275 : TYPE(dbcsr_type), INTENT(INOUT), OPTIONAL :: m1, m2, m3, m4
276 :
277 : CHARACTER(LEN=*), PARAMETER :: routineN = 'release_dbcsr_topology_and_matrices'
278 :
279 : INTEGER :: handle
280 :
281 17238 : CALL timeset(routineN, handle)
282 :
283 17238 : IF (PRESENT(dist)) CALL dbcsr_distribution_release(dist)
284 17238 : IF (PRESENT(mapped_dist)) THEN
285 12638 : IF (ASSOCIATED(mapped_dist)) THEN
286 12638 : DEALLOCATE (mapped_dist)
287 : NULLIFY (mapped_dist)
288 : END IF
289 : END IF
290 17238 : IF (PRESENT(m1)) CALL dbcsr_release(m1)
291 17238 : IF (PRESENT(m2)) CALL dbcsr_release(m2)
292 17238 : IF (PRESENT(m3)) CALL dbcsr_release(m3)
293 17238 : IF (PRESENT(m4)) CALL dbcsr_release(m4)
294 :
295 17238 : CALL timestop(handle)
296 :
297 17238 : END SUBROUTINE release_dbcsr_topology_and_matrices
298 :
299 : ! **************************************************************************************************
300 : !> \brief Reserves grid blocks with block-centre distances within a fixed radius.
301 : !> \param matrix target matrix
302 : !> \param centers block-centre coordinates
303 : !> \param radius truncation radius
304 : ! **************************************************************************************************
305 6 : SUBROUTINE reserve_blocks_within_radius(matrix, centers, radius)
306 :
307 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix
308 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: centers
309 : REAL(KIND=dp), INTENT(IN) :: radius
310 :
311 : CHARACTER(LEN=*), PARAMETER :: routineN = 'reserve_blocks_within_radius'
312 :
313 : INTEGER :: handle, i, j, my_pcol, my_prow, &
314 : nblkcols, nblkrows
315 6 : INTEGER, DIMENSION(:), POINTER :: col_blk, col_dist, row_blk, row_dist
316 : REAL(KIND=dp) :: radius2
317 6 : REAL(KIND=dp), ALLOCATABLE :: zero_blk(:, :)
318 : TYPE(dbcsr_distribution_type) :: dist
319 :
320 6 : CALL timeset(routineN, handle)
321 :
322 : CALL dbcsr_get_info(matrix, nblkrows_total=nblkrows, nblkcols_total=nblkcols, &
323 6 : row_blk_size=row_blk, col_blk_size=col_blk, distribution=dist)
324 : CALL dbcsr_distribution_get(dist, row_dist=row_dist, col_dist=col_dist, &
325 6 : myprow=my_prow, mypcol=my_pcol)
326 6 : CPASSERT(nblkrows == SIZE(centers, 2))
327 6 : CPASSERT(nblkcols == SIZE(centers, 2))
328 :
329 6 : radius2 = radius**2
330 144 : ALLOCATE (zero_blk(MAXVAL(row_blk(1:nblkrows)), MAXVAL(col_blk(1:nblkcols))))
331 6 : zero_blk(:, :) = 0.0_dp
332 :
333 66 : DO i = 1, nblkrows
334 60 : IF (row_dist(i) /= my_prow) CYCLE
335 336 : DO j = 1, nblkcols
336 300 : IF (col_dist(j) /= my_pcol) CYCLE
337 1260 : IF (DOT_PRODUCT(centers(:, i) - centers(:, j), centers(:, i) - centers(:, j)) <= radius2) THEN
338 168 : CALL dbcsr_put_block(matrix, i, j, zero_blk(1:row_blk(i), 1:col_blk(j)))
339 : END IF
340 : END DO
341 : END DO
342 6 : CALL dbcsr_finalize(matrix)
343 :
344 6 : DEALLOCATE (zero_blk)
345 6 : CALL timestop(handle)
346 :
347 6 : END SUBROUTINE reserve_blocks_within_radius
348 :
349 : ! **************************************************************************************************
350 : !> \brief Prints the percentage of non-zero elements in a distributed RI-RS matrix.
351 : !> \param matrix Distributed matrix whose occupation is reported
352 : !> \param label Mathematical matrix label used in the output
353 : !> \param para_env communicator shared by all matrix ranks
354 : !> \param unit_nr output unit; nonpositive values suppress printing
355 : !> \param msg_elem_limit maximum number of elements per DBCSR message
356 : !> \param suffix Optional text appended to the matrix label
357 : ! **************************************************************************************************
358 318 : SUBROUTINE print_matrix_occupation(matrix, label, para_env, unit_nr, msg_elem_limit, suffix)
359 :
360 : TYPE(dbcsr_type), INTENT(IN) :: matrix
361 : CHARACTER(LEN=*), INTENT(IN) :: label
362 : TYPE(mp_para_env_type), INTENT(IN) :: para_env
363 : INTEGER, INTENT(IN) :: unit_nr
364 : INTEGER(KIND=int_8), INTENT(IN) :: msg_elem_limit
365 : CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: suffix
366 :
367 : CHARACTER(LEN=*), PARAMETER :: routineN = 'print_matrix_occupation'
368 :
369 : CHARACTER(LEN=32) :: output_format
370 : CHARACTER(LEN=max_line_length) :: msg, output_label
371 : INTEGER :: handle, i, unicode_shift
372 : REAL(KIND=dp) :: frac_2p31, max_loc, occ
373 :
374 318 : CALL timeset(routineN, handle)
375 :
376 318 : occ = dbcsr_get_occupation(matrix)
377 318 : max_loc = REAL(dbcsr_get_data_size(matrix), dp)
378 318 : CALL para_env%max(max_loc)
379 :
380 318 : IF (unit_nr > 0) THEN
381 159 : frac_2p31 = max_loc/REAL(msg_elem_limit, dp)
382 159 : output_label = 'Percentage of non-zero matrix elements in '//TRIM(label)
383 159 : IF (PRESENT(suffix)) output_label = TRIM(output_label)//TRIM(suffix)
384 : ! Fortran counts UTF-8 bytes, whereas the terminal displays each Greek letter in one
385 : ! column. Shift the absolute output tab once for every continuation byte.
386 159 : unicode_shift = 0
387 10215 : DO i = 1, LEN_TRIM(output_label)
388 10215 : IF (IAND(IACHAR(output_label(i:i)), 192) == 128) unicode_shift = unicode_shift + 1
389 : END DO
390 159 : WRITE (output_format, '(A,I0,A)') '(T2,A,T', 72 + unicode_shift, ',F7.2,A)'
391 159 : WRITE (unit_nr, output_format) TRIM(output_label), occ*100.0_dp, ' %'
392 159 : IF (frac_2p31 > 0.5_dp) THEN
393 : WRITE (msg, '(3A,F0.2,A)') &
394 0 : "The largest per-rank message of ", TRIM(label), " reaches ", frac_2p31, &
395 : " of the 32-bit limit that DBCSR uses for its message length. Beyond it the "// &
396 : "length overflows and multiply_cannon fails. Reduce the per-rank block size, "// &
397 0 : "for instance with more MPI ranks."
398 0 : CPWARN(TRIM(msg))
399 : END IF
400 159 : CALL m_flush(unit_nr)
401 : END IF
402 :
403 318 : CALL timestop(handle)
404 :
405 318 : END SUBROUTINE print_matrix_occupation
406 :
407 : ! **************************************************************************************************
408 : !> \brief Marks the grid blocks whose centroid lies within cutoff of the bounding box of the
409 : !> panel [blk0, blk1]'s chunk centroids.
410 : !> \param centroids ...
411 : !> \param blk0 ...
412 : !> \param blk1 ...
413 : !> \param cutoff ...
414 : !> \param used ...
415 : ! **************************************************************************************************
416 46 : SUBROUTINE mask_grid_blocks_near_panel(centroids, blk0, blk1, cutoff, used)
417 :
418 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: centroids
419 : INTEGER, INTENT(IN) :: blk0, blk1
420 : REAL(KIND=dp), INTENT(IN) :: cutoff
421 : LOGICAL, ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: used
422 :
423 : CHARACTER(LEN=*), PARAMETER :: routineN = 'mask_grid_blocks_near_panel'
424 :
425 : INTEGER :: c, handle, k
426 : REAL(KIND=dp) :: cutoff2, d2, dx
427 : REAL(KIND=dp), DIMENSION(3) :: hi, lo
428 :
429 46 : CALL timeset(routineN, handle)
430 :
431 46 : cutoff2 = cutoff**2
432 46 : lo(:) = MINVAL(centroids(:, blk0:blk1), DIM=2)
433 46 : hi(:) = MAXVAL(centroids(:, blk0:blk1), DIM=2)
434 :
435 138 : ALLOCATE (used(SIZE(centroids, 2)))
436 1012 : DO c = 1, SIZE(centroids, 2)
437 : d2 = 0.0_dp
438 3864 : DO k = 1, 3
439 2898 : dx = MAX(0.0_dp, lo(k) - centroids(k, c), centroids(k, c) - hi(k))
440 3864 : d2 = d2 + dx*dx
441 : END DO
442 1012 : used(c) = (d2 <= cutoff2)
443 : END DO
444 :
445 46 : CALL timestop(handle)
446 :
447 46 : END SUBROUTINE mask_grid_blocks_near_panel
448 :
449 : ! **************************************************************************************************
450 : !> \brief Exact allocated-element count of the geo template of panel [blk0, blk1]: the very same
451 : !> per-block-pair centroid test as build_geo_template_panel, so this is the true DBCSR
452 : !> data size of A_pan/B_pan (DBCSR stores whole blocks).
453 : !> \param r_blk_sizes ...
454 : !> \param centroids ...
455 : !> \param used ...
456 : !> \param blk0 ...
457 : !> \param blk1 ...
458 : !> \param cutoff ...
459 : !> \param nze_tmpl ...
460 : ! **************************************************************************************************
461 4 : SUBROUTINE panel_template_elems(r_blk_sizes, centroids, used, blk0, blk1, cutoff, nze_tmpl)
462 :
463 : INTEGER, DIMENSION(:), INTENT(IN) :: r_blk_sizes
464 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: centroids
465 : LOGICAL, DIMENSION(:), INTENT(IN) :: used
466 : INTEGER, INTENT(IN) :: blk0, blk1
467 : REAL(KIND=dp), INTENT(IN) :: cutoff
468 : INTEGER(KIND=int_8), INTENT(OUT) :: nze_tmpl
469 :
470 : CHARACTER(LEN=*), PARAMETER :: routineN = 'panel_template_elems'
471 :
472 : INTEGER :: c, handle, ib, n_used
473 4 : INTEGER, ALLOCATABLE, DIMENSION(:) :: used_idx
474 : REAL(KIND=dp) :: cutoff2
475 :
476 4 : CALL timeset(routineN, handle)
477 :
478 : ! Compress the near mask once so the pair loop only visits candidate columns.
479 88 : n_used = COUNT(used)
480 12 : ALLOCATE (used_idx(n_used))
481 4 : n_used = 0
482 88 : DO c = 1, SIZE(used)
483 88 : IF (used(c)) THEN
484 84 : n_used = n_used + 1
485 84 : used_idx(n_used) = c
486 : END IF
487 : END DO
488 :
489 4 : cutoff2 = cutoff**2
490 4 : nze_tmpl = 0_int_8
491 : !$OMP PARALLEL DO DEFAULT(NONE) SHARED(blk0, blk1, n_used, used_idx, centroids, cutoff2, &
492 4 : !$OMP r_blk_sizes) PRIVATE(ib, c) REDUCTION(+:nze_tmpl)
493 : DO ib = blk0, blk1
494 : DO c = 1, n_used
495 : IF (SUM((centroids(:, ib) - centroids(:, used_idx(c)))**2) <= cutoff2) THEN
496 : nze_tmpl = nze_tmpl + INT(r_blk_sizes(ib), int_8)*INT(r_blk_sizes(used_idx(c)), int_8)
497 : END IF
498 : END DO
499 : END DO
500 : !$OMP END PARALLEL DO
501 :
502 4 : CALL timestop(handle)
503 :
504 8 : END SUBROUTINE panel_template_elems
505 :
506 : ! **************************************************************************************************
507 : !> \brief Per-rank peak memory (GB) of one panel step of the neighborhood-restricted
508 : !> contractions: up to three grid x grid panels (two A operands and the shared B operand)
509 : !> plus the grid x RI intermediates (tmp2 and the accumulation operand) and the
510 : !> grid x AO intermediate (tmpA), whose column support is the panel's geometric
511 : !> neighborhood fraction f_near = width/n_grid. Used by planning and memory reporting.
512 : !> \param nze_tmpl ...
513 : !> \param pan_rows ...
514 : !> \param width ...
515 : !> \param n_grid_total ...
516 : !> \param n_RI ...
517 : !> \param n_ao ...
518 : !> \param n_procs ...
519 : !> \param mem_GB ...
520 : ! **************************************************************************************************
521 44 : SUBROUTINE panel_mem_estimate_GB(nze_tmpl, pan_rows, width, n_grid_total, n_RI, n_ao, &
522 : n_procs, mem_GB)
523 :
524 : INTEGER(KIND=int_8), INTENT(IN) :: nze_tmpl
525 : INTEGER, INTENT(IN) :: pan_rows, width, n_grid_total, n_RI, &
526 : n_ao, n_procs
527 : REAL(KIND=dp), INTENT(OUT) :: mem_GB
528 :
529 : REAL(KIND=dp) :: f_near
530 :
531 44 : f_near = REAL(width, dp)/REAL(MAX(n_grid_total, 1), dp)
532 : mem_GB = (3.0_dp*REAL(nze_tmpl, dp) + &
533 : REAL(pan_rows, dp)*f_near*(2.0_dp*REAL(n_RI, dp) + REAL(n_ao, dp)))* &
534 44 : 8.0_dp/REAL(MAX(n_procs, 1), dp)*1.0E-9_dp
535 :
536 44 : END SUBROUTINE panel_mem_estimate_GB
537 :
538 : ! **************************************************************************************************
539 : !> \brief Plans whole-grid-block panels using a message-size target without a cutoff.
540 : !> A positive cutoff additionally checks estimated message and memory costs.
541 : !> A single indivisible cutoff block exceeding an estimated limit is reported
542 : !> and accepted, since the panel cannot be split further.
543 : !> \param msg_elem_limit maximum number of elements per DBCSR message
544 : !> \param r_blk_sizes grid row-block sizes
545 : !> \param min_dim smaller dimension of the DBCSR process grid
546 : !> \param pan_first first grid block of each panel
547 : !> \param pan_last last grid block of each panel
548 : !> \param n_RI auxiliary basis dimension
549 : !> \param n_ao AO basis dimension
550 : !> \param n_procs number of MPI processes
551 : !> \param mem_budget_GB per-rank panel memory budget; nonpositive disables the memory check
552 : !> \param centroids optional grid-block centres
553 : !> \param cutoff optional geometric truncation radius
554 : ! **************************************************************************************************
555 42 : SUBROUTINE plan_grid_panels(msg_elem_limit, r_blk_sizes, min_dim, pan_first, pan_last, &
556 42 : n_RI, n_ao, n_procs, mem_budget_GB, centroids, cutoff)
557 :
558 : INTEGER(KIND=int_8), INTENT(IN) :: msg_elem_limit
559 : INTEGER, DIMENSION(:), INTENT(IN) :: r_blk_sizes
560 : INTEGER, INTENT(IN) :: min_dim
561 : INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: pan_first, pan_last
562 : INTEGER, INTENT(IN) :: n_RI, n_ao, n_procs
563 : REAL(KIND=dp), INTENT(IN) :: mem_budget_GB
564 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN), &
565 : OPTIONAL :: centroids
566 : REAL(KIND=dp), INTENT(IN), OPTIONAL :: cutoff
567 :
568 : CHARACTER(LEN=*), PARAMETER :: routineN = 'plan_grid_panels'
569 :
570 : INTEGER :: blk0, blk1, handle, ib, n_grid_blocks, &
571 : n_grid_total, n_panels, panel_size, &
572 : rows_acc, TARGET, width
573 : INTEGER(KIND=int_8) :: msg, msg_limit, nze_tmpl, side
574 42 : INTEGER, ALLOCATABLE :: tmp_first(:), tmp_last(:)
575 : LOGICAL :: fits, use_cutoff, warned_memory, &
576 : warned_message
577 42 : LOGICAL, ALLOCATABLE :: used(:)
578 : REAL(KIND=dp) :: f_near, mem_GB
579 :
580 42 : CALL timeset(routineN, handle)
581 :
582 42 : use_cutoff = PRESENT(centroids) .AND. PRESENT(cutoff)
583 42 : IF (use_cutoff) use_cutoff = cutoff > 0.0_dp
584 42 : msg_limit = msg_elem_limit/4
585 42 : warned_memory = .FALSE.
586 42 : warned_message = .FALSE.
587 42 : n_grid_blocks = SIZE(r_blk_sizes)
588 614 : n_grid_total = SUM(r_blk_sizes)
589 42 : panel_size = n_grid_total
590 42 : IF (.NOT. use_cutoff) THEN
591 : ! Preserve the original message-derived target and whole-block growth.
592 40 : panel_size = INT(0.5_dp*REAL(msg_elem_limit, dp)*REAL(min_dim, dp)/REAL(n_grid_total, dp))
593 40 : panel_size = MAX(1, MIN(panel_size, n_grid_total))
594 : END IF
595 168 : ALLOCATE (tmp_first(n_grid_blocks), tmp_last(n_grid_blocks))
596 :
597 42 : n_panels = 0
598 42 : blk0 = 1
599 84 : DO WHILE (blk0 <= n_grid_blocks)
600 : TARGET = panel_size
601 2 : DO
602 42 : rows_acc = 0
603 572 : DO ib = blk0, n_grid_blocks
604 572 : rows_acc = rows_acc + r_blk_sizes(ib)
605 572 : blk1 = ib
606 572 : IF (rows_acc >= TARGET) EXIT
607 : END DO
608 42 : IF (.NOT. use_cutoff) EXIT
609 2 : CALL mask_grid_blocks_near_panel(centroids, blk0, blk1, cutoff, used)
610 44 : width = SUM(r_blk_sizes, MASK=used)
611 2 : CALL panel_template_elems(r_blk_sizes, centroids, used, blk0, blk1, cutoff, nze_tmpl)
612 2 : f_near = REAL(width, dp)/REAL(MAX(n_grid_total, 1), dp)
613 2 : side = INT(REAL(rows_acc, dp)*f_near*REAL(MAX(n_RI, n_ao), dp), int_8)
614 2 : msg = MAX(nze_tmpl, side)/INT(MAX(min_dim, 1), int_8)
615 : CALL panel_mem_estimate_GB(nze_tmpl, rows_acc, width, n_grid_total, &
616 2 : n_RI, n_ao, n_procs, mem_GB)
617 2 : fits = msg <= msg_limit
618 2 : IF (mem_budget_GB > 0.0_dp) fits = fits .AND. mem_GB <= mem_budget_GB
619 0 : IF (fits) EXIT
620 0 : IF (blk1 == blk0) THEN
621 0 : IF (msg > msg_limit .AND. .NOT. warned_message) THEN
622 0 : CPWARN("A single RI-RS grid block exceeds the estimated panel message limit.")
623 0 : warned_message = .TRUE.
624 : END IF
625 0 : IF (mem_budget_GB > 0.0_dp .AND. mem_GB > mem_budget_GB .AND. .NOT. warned_memory) THEN
626 0 : CPWARN("A single RI-RS grid block exceeds the estimated panel memory budget.")
627 0 : warned_memory = .TRUE.
628 : END IF
629 : EXIT
630 : END IF
631 : ! Preserve whole blocks and prefer large panels to reduce the number of multiplies.
632 40 : TARGET = MAX(1, MIN(TARGET, rows_acc)/2)
633 : END DO
634 42 : n_panels = n_panels + 1
635 42 : tmp_first(n_panels) = blk0
636 42 : tmp_last(n_panels) = blk1
637 42 : blk0 = blk1 + 1
638 : END DO
639 :
640 168 : ALLOCATE (pan_first, SOURCE=tmp_first(:n_panels))
641 126 : ALLOCATE (pan_last, SOURCE=tmp_last(:n_panels))
642 42 : CALL timestop(handle)
643 :
644 84 : END SUBROUTINE plan_grid_panels
645 :
646 : ! **************************************************************************************************
647 : !> \brief Creates an empty (panel_chunks x neighborhood_chunks) DBCSR matrix with zero blocks
648 : !> pre-allocated only where |centroid(panel_row r) - centroid(column c)| <= cutoff.
649 : !> Used with retain_sparsity=.TRUE. in the subsequent dbcsr_multiply so distant blocks
650 : !> of the grid-basis panels (ϕ G ϕ^T, Z W Z^T, ...) are never computed at all.
651 : !> \param L_pan ...
652 : !> \param L_full ...
653 : !> \param centroids ...
654 : !> \param cutoff ...
655 : !> \param blk0 ...
656 : !> \param A_template ...
657 : !> \param col_map ...
658 : ! **************************************************************************************************
659 62 : SUBROUTINE build_geo_template_panel(L_pan, L_full, centroids, cutoff, blk0, A_template, col_map)
660 : TYPE(dbcsr_type), INTENT(IN) :: L_pan, L_full
661 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: centroids
662 : REAL(KIND=dp), INTENT(IN) :: cutoff
663 : INTEGER, INTENT(IN) :: blk0
664 : TYPE(dbcsr_type), INTENT(OUT) :: A_template
665 : INTEGER, DIMENSION(:), INTENT(IN) :: col_map
666 :
667 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_geo_template_panel'
668 :
669 : INTEGER :: c, cg, cs, handle, my_pcol, my_prow, &
670 : n_grid_blks, n_pan_blks, npcols, &
671 : nprows, r, rs
672 62 : INTEGER, DIMENSION(:), POINTER :: grid_blk_sizes, pan_blk_sizes
673 : REAL(KIND=dp) :: cutoff2
674 62 : REAL(KIND=dp), ALLOCATABLE :: zero_blk(:, :)
675 : TYPE(dbcsr_distribution_type) :: dist
676 :
677 62 : CALL timeset(routineN, handle)
678 :
679 62 : cutoff2 = cutoff**2
680 62 : CALL dbcsr_get_info(L_pan, nblkrows_total=n_pan_blks, row_blk_size=pan_blk_sizes)
681 62 : CALL dbcsr_get_info(L_full, nblkrows_total=n_grid_blks, row_blk_size=grid_blk_sizes)
682 :
683 : ! create_product_matrix assigns row r to process MOD(r-1,nprows) and
684 : ! col c to MOD(c-1,npcols), so we can determine local ownership analytically.
685 62 : CALL create_product_matrix(L_pan, L_full, 'T', A_template)
686 62 : CALL dbcsr_get_info(A_template, distribution=dist)
687 : CALL dbcsr_distribution_get(dist, nprows=nprows, npcols=npcols, &
688 62 : myprow=my_prow, mypcol=my_pcol)
689 :
690 : ALLOCATE (zero_blk(MAXVAL(pan_blk_sizes(1:n_pan_blks)), &
691 2852 : MAXVAL(grid_blk_sizes(1:n_grid_blks))))
692 62 : zero_blk(:, :) = 0.0_dp
693 :
694 1364 : DO r = 1, n_pan_blks
695 1302 : IF (MOD(r - 1, nprows) /= my_prow) CYCLE
696 651 : rs = pan_blk_sizes(r)
697 14384 : DO c = 1, n_grid_blks
698 13671 : IF (MOD(c - 1, npcols) /= my_pcol) CYCLE
699 13671 : cg = col_map(c)
700 13671 : IF ((centroids(1, blk0 + r - 1) - centroids(1, cg))**2 + &
701 : (centroids(2, blk0 + r - 1) - centroids(2, cg))**2 + &
702 1302 : (centroids(3, blk0 + r - 1) - centroids(3, cg))**2 <= cutoff2) THEN
703 12741 : cs = grid_blk_sizes(c)
704 12741 : CALL dbcsr_put_block(A_template, r, c, zero_blk(1:rs, 1:cs))
705 : END IF
706 : END DO
707 : END DO
708 62 : CALL dbcsr_finalize(A_template)
709 :
710 62 : DEALLOCATE (zero_blk)
711 62 : CALL timestop(handle)
712 :
713 62 : END SUBROUTINE build_geo_template_panel
714 :
715 : ! **************************************************************************************************
716 : !> \brief Slices a contiguous range of grid row-blocks [blk0, blk1] out of a (grid x n) DBCSR
717 : !> matrix into a new (P x n) panel matrix: iterate the source's local blocks, put the
718 : !> in-range ones into the panel with a remapped row-block index, then finalize. Row-block
719 : !> index i of the panel corresponds to source row-block blk0+i-1.
720 : !> \param mat_full ...
721 : !> \param blk0 ...
722 : !> \param blk1 ...
723 : !> \param mat_panel ...
724 : ! **************************************************************************************************
725 3616 : SUBROUTINE extract_grid_panel(mat_full, blk0, blk1, mat_panel)
726 :
727 : TYPE(dbcsr_type), INTENT(INOUT) :: mat_full
728 : INTEGER, INTENT(IN) :: blk0, blk1
729 : TYPE(dbcsr_type), INTENT(OUT) :: mat_panel
730 :
731 : CHARACTER(LEN=*), PARAMETER :: routineN = 'extract_grid_panel'
732 :
733 : INTEGER :: handle, ib, jb, npb
734 3616 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_full, col_dist_full, &
735 3616 : row_blk_full, row_blk_pan, &
736 3616 : row_dist_full, row_dist_pan
737 3616 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: blk
738 : TYPE(dbcsr_distribution_type) :: dist_full, dist_pan
739 : TYPE(dbcsr_iterator_type) :: iter
740 :
741 3616 : CALL timeset(routineN, handle)
742 :
743 : CALL dbcsr_get_info(mat_full, distribution=dist_full, &
744 3616 : row_blk_size=row_blk_full, col_blk_size=col_blk_full)
745 3616 : CALL dbcsr_distribution_get(dist_full, row_dist=row_dist_full, col_dist=col_dist_full)
746 :
747 3616 : npb = blk1 - blk0 + 1
748 14464 : ALLOCATE (row_dist_pan(npb), row_blk_pan(npb))
749 119888 : row_dist_pan(:) = row_dist_full(blk0:blk1)
750 119888 : row_blk_pan(:) = row_blk_full(blk0:blk1)
751 :
752 : CALL dbcsr_distribution_new(dist_pan, template=dist_full, &
753 3616 : row_dist=row_dist_pan, col_dist=col_dist_full)
754 : CALL dbcsr_create(mat_panel, name="grid_panel", dist=dist_pan, &
755 : matrix_type=dbcsr_type_no_symmetry, &
756 3616 : row_blk_size=row_blk_pan, col_blk_size=col_blk_full)
757 :
758 3616 : CALL dbcsr_iterator_start(iter, mat_full)
759 69163 : DO WHILE (dbcsr_iterator_blocks_left(iter))
760 65547 : CALL dbcsr_iterator_next_block(iter, ib, jb, blk)
761 65547 : IF (ib < blk0 .OR. ib > blk1) CYCLE
762 69163 : CALL dbcsr_put_block(mat_panel, ib - blk0 + 1, jb, blk)
763 : END DO
764 3616 : CALL dbcsr_iterator_stop(iter)
765 3616 : CALL dbcsr_finalize(mat_panel)
766 :
767 3616 : CALL dbcsr_distribution_release(dist_pan)
768 3616 : DEALLOCATE (row_dist_pan, row_blk_pan)
769 :
770 3616 : CALL timestop(handle)
771 :
772 10848 : END SUBROUTINE extract_grid_panel
773 :
774 : ! **************************************************************************************************
775 : !> \brief Marks which column blocks of a DBCSR matrix carry at least one non-zero block anywhere
776 : !> (global union). Used to restrict the inner index of the panel multiplies to the
777 : !> AO/RI atoms that actually touch the panel (exact: dropped rows only meet zeros).
778 : !> \param matrix ...
779 : !> \param para_env ...
780 : !> \param used ...
781 : ! **************************************************************************************************
782 2956 : SUBROUTINE collect_used_col_blocks(matrix, para_env, used)
783 :
784 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix
785 : TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
786 : LOGICAL, ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: used
787 :
788 : CHARACTER(LEN=*), PARAMETER :: routineN = 'collect_used_col_blocks'
789 :
790 : INTEGER :: handle, ib, jb, nblkcols
791 : INTEGER, ALLOCATABLE, DIMENSION(:) :: iused
792 2956 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: blk
793 : TYPE(dbcsr_iterator_type) :: iter
794 :
795 2956 : CALL timeset(routineN, handle)
796 :
797 2956 : CALL dbcsr_get_info(matrix, nblkcols_total=nblkcols)
798 8868 : ALLOCATE (iused(nblkcols))
799 2956 : iused(:) = 0
800 :
801 2956 : CALL dbcsr_iterator_start(iter, matrix)
802 57743 : DO WHILE (dbcsr_iterator_blocks_left(iter))
803 54787 : CALL dbcsr_iterator_next_block(iter, ib, jb, blk)
804 54787 : iused(jb) = 1
805 : END DO
806 2956 : CALL dbcsr_iterator_stop(iter)
807 :
808 2956 : CALL para_env%sum(iused)
809 :
810 5912 : ALLOCATE (used(nblkcols))
811 9856 : used(:) = (iused(:) > 0)
812 2956 : DEALLOCATE (iused)
813 :
814 2956 : CALL timestop(handle)
815 :
816 5912 : END SUBROUTINE collect_used_col_blocks
817 :
818 : ! **************************************************************************************************
819 : !> \brief Copies the flagged block rows (compress_rows=.TRUE.) or block columns (.FALSE.) of a
820 : !> DBCSR matrix into a compressed matrix. The subset keeps the parent's process assignment
821 : !> along the compressed dimension, so every block stays on its owning rank: the extraction
822 : !> is purely local (zero communication), like extract_grid_panel.
823 : !> \param mat_full ...
824 : !> \param used ...
825 : !> \param mat_out ...
826 : !> \param compress_rows ...
827 : !> \param blk_map ...
828 : ! **************************************************************************************************
829 7756 : SUBROUTINE extract_masked_blocks(mat_full, used, mat_out, compress_rows, blk_map)
830 :
831 : TYPE(dbcsr_type), INTENT(INOUT) :: mat_full
832 : LOGICAL, DIMENSION(:), INTENT(IN) :: used
833 : TYPE(dbcsr_type), INTENT(OUT) :: mat_out
834 : LOGICAL, INTENT(IN) :: compress_rows
835 : INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT), &
836 : OPTIONAL :: blk_map
837 :
838 : CHARACTER(LEN=*), PARAMETER :: routineN = 'extract_masked_blocks'
839 :
840 : INTEGER :: handle, ib, jb, n_blk, n_sub, r
841 7756 : INTEGER, ALLOCATABLE, DIMENSION(:) :: inv_map
842 7756 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: blk_full, blk_sub, col_blk_full, &
843 7756 : col_dist_full, dist_full_1d, &
844 7756 : dist_sub_1d, row_blk_full, &
845 7756 : row_dist_full
846 7756 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: blk
847 : TYPE(dbcsr_distribution_type) :: dist_full, dist_sub
848 : TYPE(dbcsr_iterator_type) :: iter
849 :
850 7756 : CALL timeset(routineN, handle)
851 :
852 : CALL dbcsr_get_info(mat_full, distribution=dist_full, &
853 7756 : row_blk_size=row_blk_full, col_blk_size=col_blk_full)
854 7756 : CALL dbcsr_distribution_get(dist_full, row_dist=row_dist_full, col_dist=col_dist_full)
855 :
856 7756 : IF (compress_rows) THEN
857 4800 : blk_full => row_blk_full
858 4800 : dist_full_1d => row_dist_full
859 : ELSE
860 2956 : blk_full => col_blk_full
861 2956 : dist_full_1d => col_dist_full
862 : END IF
863 7756 : n_blk = SIZE(blk_full)
864 :
865 27440 : n_sub = COUNT(used)
866 7756 : CPASSERT(n_sub > 0)
867 46536 : ALLOCATE (inv_map(n_blk), blk_sub(n_sub), dist_sub_1d(n_sub))
868 7798 : IF (PRESENT(blk_map)) ALLOCATE (blk_map(n_sub))
869 7756 : inv_map(:) = 0
870 7756 : r = 0
871 27440 : DO ib = 1, n_blk
872 27440 : IF (used(ib)) THEN
873 19684 : r = r + 1
874 19684 : inv_map(ib) = r
875 19684 : blk_sub(r) = blk_full(ib)
876 19684 : dist_sub_1d(r) = dist_full_1d(ib)
877 19684 : IF (PRESENT(blk_map)) blk_map(r) = ib
878 : END IF
879 : END DO
880 :
881 7756 : IF (compress_rows) THEN
882 : CALL dbcsr_distribution_new(dist_sub, template=dist_full, &
883 4800 : row_dist=dist_sub_1d, col_dist=col_dist_full)
884 : CALL dbcsr_create(mat_out, name="row_subset", dist=dist_sub, &
885 : matrix_type=dbcsr_type_no_symmetry, &
886 4800 : row_blk_size=blk_sub, col_blk_size=col_blk_full)
887 : ELSE
888 : CALL dbcsr_distribution_new(dist_sub, template=dist_full, &
889 2956 : row_dist=row_dist_full, col_dist=dist_sub_1d)
890 : CALL dbcsr_create(mat_out, name="col_subset", dist=dist_sub, &
891 : matrix_type=dbcsr_type_no_symmetry, &
892 2956 : row_blk_size=row_blk_full, col_blk_size=blk_sub)
893 : END IF
894 :
895 7756 : CALL dbcsr_iterator_start(iter, mat_full)
896 78150 : DO WHILE (dbcsr_iterator_blocks_left(iter))
897 70394 : CALL dbcsr_iterator_next_block(iter, ib, jb, blk)
898 78150 : IF (compress_rows) THEN
899 15607 : IF (inv_map(ib) > 0) CALL dbcsr_put_block(mat_out, inv_map(ib), jb, blk)
900 : ELSE
901 54787 : IF (inv_map(jb) > 0) CALL dbcsr_put_block(mat_out, ib, inv_map(jb), blk)
902 : END IF
903 : END DO
904 7756 : CALL dbcsr_iterator_stop(iter)
905 7756 : CALL dbcsr_finalize(mat_out)
906 :
907 7756 : CALL dbcsr_distribution_release(dist_sub)
908 7756 : DEALLOCATE (inv_map, blk_sub, dist_sub_1d)
909 :
910 7756 : CALL timestop(handle)
911 :
912 15512 : END SUBROUTINE extract_masked_blocks
913 :
914 : ! **************************************************************************************************
915 : !> \brief Creates the (empty) result matrix of op(mat_left) * op(mat_right) with the correct block
916 : !> structure and a distribution on the shared process grid, ready to be filled by
917 : !> dbcsr_multiply. Row structure comes from op(left), column structure from op(right).
918 : !> \param mat_left ...
919 : !> \param mat_right ...
920 : !> \param transb 'N' or 'T' applied to mat_right
921 : !> \param mat_out ...
922 : ! **************************************************************************************************
923 10532 : SUBROUTINE create_product_matrix(mat_left, mat_right, transb, mat_out)
924 :
925 : TYPE(dbcsr_type), INTENT(IN) :: mat_left, mat_right
926 : CHARACTER(LEN=1), INTENT(IN) :: transb
927 : TYPE(dbcsr_type), INTENT(OUT) :: mat_out
928 :
929 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_product_matrix'
930 :
931 : INTEGER :: handle, i, npcols, nprows
932 10532 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_r, out_col_blk, out_col_dist, &
933 10532 : out_row_blk, out_row_dist, row_blk_l, &
934 10532 : row_blk_r
935 : TYPE(dbcsr_distribution_type) :: dist_l, dist_out
936 :
937 10532 : CALL timeset(routineN, handle)
938 :
939 10532 : CALL dbcsr_get_info(mat_left, distribution=dist_l, row_blk_size=row_blk_l)
940 10532 : CALL dbcsr_get_info(mat_right, row_blk_size=row_blk_r, col_blk_size=col_blk_r)
941 10532 : CALL dbcsr_distribution_get(dist_l, nprows=nprows, npcols=npcols)
942 :
943 : ! block SIZES follow op(left)/op(right); DISTRIBUTIONS are freshly round-robined onto the
944 : ! shared process grid (a transposed operand's row-dist is NOT a valid col-dist on a
945 : ! non-square grid). dbcsr_multiply redistributes internally, so any valid mapping works.
946 10532 : out_row_blk => row_blk_l
947 10532 : IF (transb == 'N') THEN
948 7624 : out_col_blk => col_blk_r
949 : ELSE
950 2908 : out_col_blk => row_blk_r
951 : END IF
952 :
953 52660 : ALLOCATE (out_row_dist(SIZE(out_row_blk)), out_col_dist(SIZE(out_col_blk)))
954 178808 : DO i = 1, SIZE(out_row_blk)
955 178808 : out_row_dist(i) = MOD(i - 1, nprows)
956 : END DO
957 75036 : DO i = 1, SIZE(out_col_blk)
958 75036 : out_col_dist(i) = MOD(i - 1, npcols)
959 : END DO
960 :
961 : CALL dbcsr_distribution_new(dist_out, template=dist_l, &
962 10532 : row_dist=out_row_dist, col_dist=out_col_dist)
963 : CALL dbcsr_create(mat_out, name="panel_product", dist=dist_out, &
964 : matrix_type=dbcsr_type_no_symmetry, &
965 10532 : row_blk_size=out_row_blk, col_blk_size=out_col_blk)
966 10532 : CALL dbcsr_distribution_release(dist_out)
967 10532 : DEALLOCATE (out_row_dist, out_col_dist)
968 :
969 10532 : CALL timestop(handle)
970 :
971 31596 : END SUBROUTINE create_product_matrix
972 :
973 : ! **************************************************************************************************
974 : !> \brief Projects two operators onto grid panels, takes their Hadamard product, and
975 : !> back-projects it: out += scale * L_out^T (A_grid ∘ B_grid) L_out,
976 : !> where A_grid = L_A M_A L_A^T and B_grid = L_B M_B L_B^T.
977 : !> Each L has shape (grid, basis), and its M has shape (basis, basis).
978 : !> The three L matrices must share the ordered grid rows, row blocking and process grid.
979 : !> Only panel x grid slabs are formed; contributions are accumulated over all panels.
980 : !> The sequence of EPS_FILTER screening and floating-point reductions may differ from
981 : !> a full-grid evaluation.
982 : !>
983 : !> The physical contractions and their signs/spin factors are specified in gw_ri_rs_kernels:
984 : !> χ: L_A=L_B=ϕ, M_A=G_occ, M_B=G_vir, L_out=Z -> RI x RI
985 : !> Σx: L_A=ϕ, M_A=D, L_B=Z, M_B=M^-1 V^tr M^-1, L_out=ϕ -> AO x AO
986 : !> Σc: L_A=ϕ, M_A=[G_occ,G_vir], L_B=Z, M_B=W, L_out=ϕ -> two AO x AO outputs.
987 : !> The second Σc output reuses the same W grid panel; its projection and back-projection
988 : !> otherwise follow the same operations as the first output.
989 : !> \param L_A grid-by-basis collocation or fitting matrix for the first operand
990 : !> \param M_A one or two square operators, with row/column blocking matching the columns of L_A
991 : !> \param L_B grid-by-basis collocation or fitting matrix for the second operand
992 : !> \param M_B second square operator, with row/column blocking matching the columns of L_B
993 : !> \param L_out grid-by-output-basis matrix for back-projection
994 : !> \param mat_out borrowed pointers to pre-created outputs, one per M_A, with the column blocking of L_out
995 : !> \param scale prefactor of the Hadamard product, including the caller's sign/spin convention
996 : !> \param eps DBCSR multiplication screening threshold
997 : !> \param para_env communicator containing all matrix ranks
998 : !> \param pan_first first global grid row-block of each ordered, nonoverlapping panel
999 : !> \param pan_last last global grid row-block of each panel; panels together cover the grid
1000 : !> \param lb_eq_la L_B equals L_A in values and layout, allowing their grid/AO slices to be shared
1001 : !> \param lout_eq_la L_out equals L_A in values and layout, allowing back-projection slices to be shared
1002 : !> \param zero_out clear the output matrices before accumulating, otherwise add to their existing values
1003 : !> \param centroids optional global grid-block centres, paired with a positive cutoff for truncation
1004 : !> \param cutoff retain grid-block pairs within this centroid distance; nonpositive disables truncation
1005 : !> \param grid_occupation allocated-element fraction of the first grid operand before the Hadamard product
1006 : ! **************************************************************************************************
1007 1808 : SUBROUTINE contract_grid_panels(L_A, M_A, L_B, M_B, L_out, mat_out, scale, eps, para_env, &
1008 1808 : pan_first, pan_last, lb_eq_la, lout_eq_la, zero_out, &
1009 1808 : centroids, cutoff, grid_occupation)
1010 :
1011 : TYPE(dbcsr_type), INTENT(INOUT), TARGET :: L_A
1012 : TYPE(dbcsr_type), DIMENSION(:), INTENT(INOUT) :: M_A
1013 : TYPE(dbcsr_type), INTENT(INOUT), TARGET :: L_B
1014 : TYPE(dbcsr_type), INTENT(INOUT) :: M_B
1015 : TYPE(dbcsr_type), INTENT(INOUT), TARGET :: L_out
1016 : TYPE(dbcsr_p_type), DIMENSION(:), INTENT(IN) :: mat_out
1017 : REAL(KIND=dp), INTENT(IN) :: scale, eps
1018 : TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
1019 : INTEGER, DIMENSION(:), INTENT(IN) :: pan_first, pan_last
1020 : LOGICAL, INTENT(IN) :: lb_eq_la, lout_eq_la, zero_out
1021 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN), &
1022 : OPTIONAL :: centroids
1023 : REAL(KIND=dp), INTENT(IN), OPTIONAL :: cutoff
1024 : REAL(KIND=dp), INTENT(OUT), OPTIONAL :: grid_occupation
1025 :
1026 : CHARACTER(LEN=*), PARAMETER :: routineN = 'contract_grid_panels'
1027 :
1028 : INTEGER :: blk0, blk1, handle, i_A, ipan, &
1029 : n_grid_total, ncols_pan, nrows_pan
1030 1808 : INTEGER, ALLOCATABLE, DIMENSION(:) :: gmap
1031 : LOGICAL :: use_cutoff
1032 1808 : LOGICAL, ALLOCATABLE, DIMENSION(:) :: grid_used, usedA, usedB
1033 5424 : TYPE(dbcsr_type) :: A_pan(2), B_pan, LB_pan, LB_panC, &
1034 : MA_sub, MB_sub, tmp2, tmpA, tmpB
1035 : TYPE(dbcsr_type), POINTER :: panel_out, RB_A, RB_B, RB_out
1036 : TYPE(dbcsr_type), TARGET :: LA_near, LA_pan, LA_panC, LB_near, &
1037 : Lout_near, Lout_pan
1038 :
1039 1808 : CALL timeset(routineN, handle)
1040 :
1041 1808 : CPASSERT(SIZE(M_A) >= 1 .AND. SIZE(M_A) <= 2)
1042 1808 : CPASSERT(SIZE(mat_out) == SIZE(M_A))
1043 :
1044 1808 : use_cutoff = PRESENT(centroids) .AND. PRESENT(cutoff)
1045 1808 : IF (use_cutoff) use_cutoff = cutoff > 0.0_dp
1046 1808 : IF (PRESENT(grid_occupation)) grid_occupation = 0.0_dp
1047 :
1048 1808 : CALL dbcsr_get_info(L_A, nfullrows_total=n_grid_total)
1049 :
1050 1808 : IF (zero_out) THEN
1051 4556 : DO i_A = 1, SIZE(mat_out)
1052 4556 : CALL dbcsr_set(mat_out(i_A)%matrix, 0.0_dp)
1053 : END DO
1054 : END IF
1055 :
1056 3616 : DO ipan = 1, SIZE(pan_first)
1057 1808 : blk0 = pan_first(ipan)
1058 1808 : blk1 = pan_last(ipan)
1059 :
1060 : ! Slice only the current grid rows; compact away AO/RI columns that are exactly absent.
1061 1808 : CALL extract_grid_panel(L_A, blk0, blk1, LA_pan)
1062 1808 : CALL collect_used_col_blocks(LA_pan, para_env, usedA)
1063 1808 : IF (.NOT. lb_eq_la) THEN
1064 1148 : CALL extract_grid_panel(L_B, blk0, blk1, LB_pan)
1065 1148 : CALL collect_used_col_blocks(LB_pan, para_env, usedB)
1066 : ELSE
1067 660 : IF (ALLOCATED(usedB)) DEALLOCATE (usedB)
1068 3560 : ALLOCATE (usedB, SOURCE=usedA)
1069 : END IF
1070 1808 : IF (.NOT. ANY(usedA) .OR. .NOT. ANY(usedB)) THEN
1071 0 : CALL dbcsr_release(LA_pan)
1072 0 : IF (.NOT. lb_eq_la) CALL dbcsr_release(LB_pan)
1073 : CYCLE
1074 : END IF
1075 1808 : IF (lout_eq_la) THEN
1076 : panel_out => LA_pan
1077 : ELSE
1078 660 : CALL extract_grid_panel(L_out, blk0, blk1, Lout_pan)
1079 660 : panel_out => Lout_pan
1080 : END IF
1081 1808 : CALL extract_masked_blocks(LA_pan, usedA, LA_panC, compress_rows=.FALSE.)
1082 :
1083 : ! Grid rows within reach of the panel: with the CUTOFF_RADIUS_RL_W truncation only
1084 : ! they can appear as columns of the panel products / inner rows of the L_out multiply,
1085 : ! so the system-wide phi/Z right operands are cut down to this neighborhood slice
1086 : ! (local extraction, zero communication; exact w.r.t. the geo template).
1087 1808 : IF (use_cutoff) THEN
1088 42 : CALL mask_grid_blocks_near_panel(centroids, blk0, blk1, cutoff, grid_used)
1089 42 : CALL extract_masked_blocks(L_A, grid_used, LA_near, compress_rows=.TRUE., blk_map=gmap)
1090 42 : IF (.NOT. lb_eq_la) CALL extract_masked_blocks(L_B, grid_used, LB_near, compress_rows=.TRUE.)
1091 42 : IF (.NOT. lout_eq_la) CALL extract_masked_blocks(L_out, grid_used, Lout_near, compress_rows=.TRUE.)
1092 : RB_A => LA_near
1093 : ELSE
1094 : RB_A => L_A
1095 : END IF
1096 1808 : IF (lb_eq_la) THEN
1097 : RB_B => RB_A
1098 1148 : ELSE IF (use_cutoff) THEN
1099 : RB_B => LB_near
1100 : ELSE
1101 1126 : RB_B => L_B
1102 : END IF
1103 1808 : IF (lout_eq_la) THEN
1104 : RB_out => RB_A
1105 660 : ELSE IF (use_cutoff) THEN
1106 : RB_out => Lout_near
1107 : ELSE
1108 640 : RB_out => L_out
1109 : END IF
1110 :
1111 4716 : DO i_A = 1, SIZE(M_A)
1112 : ! A_pan(i_A) = LA_pan * M_A(i_A) * L_A^T (P x grid_near).
1113 : ! When cutoff is active, A_pan(i_A) is pre-seeded with only nearby blocks via
1114 : ! build_geo_template_panel, and the multiply uses retain_sparsity to skip
1115 : ! computing distant blocks entirely within the chosen grid cutoff.
1116 2908 : CALL extract_masked_blocks(M_A(i_A), usedA, MA_sub, compress_rows=.TRUE.)
1117 2908 : CALL create_product_matrix(LA_panC, MA_sub, 'N', tmpA)
1118 2908 : CALL dbcsr_multiply('N', 'N', 1.0_dp, LA_panC, MA_sub, 0.0_dp, tmpA, filter_eps=eps)
1119 2908 : CALL dbcsr_release(MA_sub)
1120 2908 : IF (use_cutoff) THEN
1121 : CALL build_geo_template_panel(LA_pan, LA_near, centroids, cutoff, blk0, A_pan(i_A), &
1122 62 : col_map=gmap)
1123 : ELSE
1124 2846 : CALL create_product_matrix(tmpA, RB_A, 'T', A_pan(i_A))
1125 : END IF
1126 : CALL dbcsr_multiply('N', 'T', 1.0_dp, tmpA, RB_A, 0.0_dp, A_pan(i_A), &
1127 2908 : filter_eps=eps, retain_sparsity=use_cutoff)
1128 2908 : CALL dbcsr_release(tmpA)
1129 :
1130 : ! Grid-basis occupation of A_pan(i_A) = ϕ G ϕ^T, accumulated over ALL panels into the
1131 : ! occupation of the (never formed) full grid x grid object:
1132 : ! sum_panels nnz(A_pan(i_A)) / n_grid^2, with nnz = occ * pan_rows * pan_cols.
1133 : ! Panel-independent by construction -- a single-panel sample would instead report the
1134 : ! local neighbor count of whichever region happens to land in that panel.
1135 7624 : IF (PRESENT(grid_occupation) .AND. i_A == 1) THEN
1136 48 : CALL dbcsr_get_info(A_pan(i_A), nfullrows_total=nrows_pan, nfullcols_total=ncols_pan)
1137 : grid_occupation = grid_occupation + dbcsr_get_occupation(A_pan(i_A))* &
1138 : REAL(ncols_pan, dp)*REAL(nrows_pan, dp)/ &
1139 48 : (REAL(n_grid_total, dp)*REAL(n_grid_total, dp))
1140 : END IF
1141 : END DO
1142 :
1143 : ! B_pan = LB_pan * M_B * L_B^T (P x grid_near); reuse the L_A slices when L_B == L_A.
1144 : ! B_pan retains the union of the A panels' block structures so that
1145 : ! retain_sparsity forces the final multiply to fill only those blocks (exact for ∘).
1146 1808 : IF (lb_eq_la) THEN
1147 660 : CALL extract_masked_blocks(M_B, usedA, MB_sub, compress_rows=.TRUE.)
1148 660 : CALL create_product_matrix(LA_panC, MB_sub, 'N', tmpB)
1149 660 : CALL dbcsr_multiply('N', 'N', 1.0_dp, LA_panC, MB_sub, 0.0_dp, tmpB, filter_eps=eps)
1150 : ELSE
1151 1148 : CALL extract_masked_blocks(LB_pan, usedB, LB_panC, compress_rows=.FALSE.)
1152 1148 : CALL extract_masked_blocks(M_B, usedB, MB_sub, compress_rows=.TRUE.)
1153 1148 : CALL create_product_matrix(LB_panC, MB_sub, 'N', tmpB)
1154 1148 : CALL dbcsr_multiply('N', 'N', 1.0_dp, LB_panC, MB_sub, 0.0_dp, tmpB, filter_eps=eps)
1155 1148 : CALL dbcsr_release(LB_panC)
1156 : END IF
1157 1808 : CALL dbcsr_release(MB_sub)
1158 1808 : CALL dbcsr_create(B_pan, template=A_pan(1))
1159 1808 : CALL dbcsr_copy(B_pan, A_pan(1))
1160 1808 : IF (SIZE(M_A) == 2) CALL dbcsr_add(B_pan, A_pan(2), 1.0_dp, 1.0_dp)
1161 1808 : CALL dbcsr_set(B_pan, 0.0_dp)
1162 : ! The output pattern is already fixed. Omitting redundant on-the-fly filtering also
1163 : ! avoids overflowing DBCSR's single-precision screening norms for conditioned Z fits.
1164 : CALL dbcsr_multiply('N', 'T', 1.0_dp, tmpB, RB_B, 0.0_dp, B_pan, &
1165 1808 : retain_sparsity=.TRUE.)
1166 1808 : CALL dbcsr_release(tmpB)
1167 1808 : CALL dbcsr_release(LA_panC)
1168 :
1169 4716 : DO i_A = 1, SIZE(M_A)
1170 : ! Reuse A_pan storage for its Hadamard product with the shared B panel.
1171 2908 : CALL hadamard_product_inplace(A_pan(i_A), B_pan, scale)
1172 2908 : CALL create_product_matrix(A_pan(i_A), RB_out, 'N', tmp2)
1173 2908 : CALL dbcsr_multiply('N', 'N', 1.0_dp, A_pan(i_A), RB_out, 0.0_dp, tmp2, filter_eps=eps)
1174 2908 : CALL dbcsr_release(A_pan(i_A))
1175 :
1176 : ! mat_out += L_out_pan^T * tmp2.
1177 2908 : CALL dbcsr_multiply('T', 'N', 1.0_dp, panel_out, tmp2, 1.0_dp, mat_out(i_A)%matrix, filter_eps=eps)
1178 7624 : CALL dbcsr_release(tmp2)
1179 : END DO
1180 1808 : CALL dbcsr_release(B_pan)
1181 1808 : IF (.NOT. lout_eq_la) CALL dbcsr_release(Lout_pan)
1182 1808 : IF (.NOT. lb_eq_la) CALL dbcsr_release(LB_pan)
1183 1808 : CALL dbcsr_release(LA_pan)
1184 5424 : IF (use_cutoff) THEN
1185 42 : CALL dbcsr_release(LA_near)
1186 42 : IF (.NOT. lb_eq_la) CALL dbcsr_release(LB_near)
1187 42 : IF (.NOT. lout_eq_la) CALL dbcsr_release(Lout_near)
1188 : END IF
1189 :
1190 : END DO
1191 :
1192 1808 : CALL timestop(handle)
1193 :
1194 5424 : END SUBROUTINE contract_grid_panels
1195 :
1196 : ! **************************************************************************************************
1197 : !> \brief Build the unfiltered auxiliary interaction M^-1 V^tr M^-1 in the Z-column layout.
1198 : !> AUTO_RI uses its already transformed Coulomb matrix in the optimized auxiliary basis.
1199 : !> Filtering and cache lifetime are owned by the caller.
1200 : !> \param qs_env electronic-structure environment for ordinary RI integrals
1201 : !> \param metric_inverse inverse auxiliary metric
1202 : !> \param trunc_coulomb Coulomb operator for ordinary RI integrals
1203 : !> \param auto_ri optimized auxiliary basis and its Coulomb matrix
1204 : !> \param mat_Z_lP template defining the auxiliary blocking and process grid
1205 : !> \param matrix_V_aux output bare interaction in the auxiliary basis
1206 : ! **************************************************************************************************
1207 42 : SUBROUTINE build_ri_rs_bare_aux_interaction(qs_env, metric_inverse, trunc_coulomb, auto_ri, &
1208 : mat_Z_lP, matrix_V_aux)
1209 : TYPE(qs_environment_type), POINTER :: qs_env
1210 : TYPE(cp_fm_type), INTENT(IN) :: metric_inverse
1211 : TYPE(coulomb_operator_type), INTENT(IN) :: trunc_coulomb
1212 : TYPE(auto_ri_type), INTENT(IN) :: auto_ri
1213 : TYPE(dbcsr_type), INTENT(IN) :: mat_Z_lP
1214 : TYPE(dbcsr_type), INTENT(OUT) :: matrix_V_aux
1215 :
1216 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_ri_rs_bare_aux_interaction'
1217 :
1218 : INTEGER :: handle
1219 42 : INTEGER, CONTIGUOUS, POINTER :: blk_aux(:), dist_row_aux(:)
1220 42 : TYPE(cp_fm_type), ALLOCATABLE :: fm_Vtr_Gamma(:, :)
1221 : TYPE(dbcsr_distribution_type) :: dist_aux_aux
1222 :
1223 42 : CALL timeset(routineN, handle)
1224 42 : IF (auto_ri%enabled) THEN
1225 4 : CPASSERT(auto_ri%ready)
1226 12 : ALLOCATE (fm_Vtr_Gamma(1, 1))
1227 4 : CALL cp_fm_create(fm_Vtr_Gamma(1, 1), metric_inverse%matrix_struct)
1228 4 : CALL cp_fm_to_fm(auto_ri%V_pq, fm_Vtr_Gamma(1, 1))
1229 : ELSE
1230 : CALL RI_2c_integral_mat(qs_env, fm_Vtr_Gamma, metric_inverse%matrix_struct, &
1231 38 : metric_inverse%matrix_struct%nrow_global, trunc_coulomb)
1232 : END IF
1233 42 : CALL fm_contract_ABA(metric_inverse, fm_Vtr_Gamma(:, 1))
1234 42 : CALL setup_square_topology(mat_Z_lP, 'COL', dist_aux_aux, blk_aux, dist_row_aux)
1235 42 : CALL dbcsr_create(matrix_V_aux, "V_aux", dist_aux_aux, dbcsr_type_no_symmetry, blk_aux, blk_aux)
1236 42 : CALL copy_fm_to_dbcsr(fm_Vtr_Gamma(1, 1), matrix_V_aux, keep_sparsity=.FALSE.)
1237 42 : CALL cp_fm_release(fm_Vtr_Gamma)
1238 42 : CALL release_dbcsr_topology_and_matrices(dist=dist_aux_aux, mapped_dist=dist_row_aux)
1239 42 : CALL timestop(handle)
1240 84 : END SUBROUTINE build_ri_rs_bare_aux_interaction
1241 :
1242 : ! **************************************************************************************************
1243 : !> \brief Resolve and cache grid-panel boundaries for RI-RS streaming contractions.
1244 : !> \param ri_rs collocation and fitting matrices, grid geometry and owned panel cache
1245 : !> \param para_env parallel environment
1246 : !> \param msg_elem_limit maximum number of elements per DBCSR message
1247 : !> \param pan_first ...
1248 : !> \param pan_last ...
1249 : !> \param cutoff grid-centroid distance cutoff; zero disables geometric truncation
1250 : ! **************************************************************************************************
1251 194 : SUBROUTINE resolve_grid_panels(ri_rs, para_env, msg_elem_limit, pan_first, pan_last, cutoff)
1252 :
1253 : TYPE(ri_rs_env), INTENT(INOUT) :: ri_rs
1254 : TYPE(mp_para_env_type), POINTER :: para_env
1255 : INTEGER(KIND=int_8), INTENT(IN) :: msg_elem_limit
1256 : INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: pan_first, pan_last
1257 : REAL(KIND=dp), INTENT(IN) :: cutoff
1258 :
1259 : CHARACTER(LEN=*), PARAMETER :: routineN = 'resolve_grid_panels'
1260 :
1261 : INTEGER :: handle, min_dim, n_ao, n_grid_total, &
1262 : n_RI, npcols, nprows
1263 194 : INTEGER, DIMENSION(:), POINTER :: r_blk_sizes
1264 : LOGICAL :: use_cutoff
1265 : REAL(KIND=dp) :: effective_cutoff, mem_avail_GB, &
1266 : mem_budget_GB
1267 : TYPE(dbcsr_distribution_type) :: dist
1268 :
1269 194 : CALL timeset(routineN, handle)
1270 :
1271 : use_cutoff = cutoff > 0.0_dp .AND. &
1272 194 : ALLOCATED(ri_rs%chunk_centroids)
1273 186 : effective_cutoff = 0.0_dp
1274 8 : IF (use_cutoff) effective_cutoff = cutoff
1275 194 : IF (ALLOCATED(ri_rs%pan_first)) THEN
1276 : ! This is an exact cache key, not a tolerance-based geometric comparison.
1277 152 : IF (ri_rs%panel_plan_cutoff == effective_cutoff) THEN
1278 608 : ALLOCATE (pan_first, SOURCE=ri_rs%pan_first)
1279 608 : ALLOCATE (pan_last, SOURCE=ri_rs%pan_last)
1280 152 : CALL timestop(handle)
1281 : RETURN
1282 : END IF
1283 0 : DEALLOCATE (ri_rs%pan_first, ri_rs%pan_last)
1284 : END IF
1285 :
1286 : ! MIN(nprows, npcols) is the divisor that bounds the worst-rank Cannon message: a
1287 : ! P x n_grid panel is replicated into block row strips (P/nprows x n_grid) or column
1288 : ! strips (P x n_grid/npcols) during multiply_cannon, so the largest single-rank
1289 : ! message is ~ P*n_grid / MIN(nprows,npcols) elements.
1290 : CALL dbcsr_get_info(ri_rs%mat_phi_mu_l, nfullrows_total=n_grid_total, nfullcols_total=n_ao, &
1291 42 : row_blk_size=r_blk_sizes, distribution=dist)
1292 42 : CALL dbcsr_distribution_get(dist, nprows=nprows, npcols=npcols)
1293 42 : min_dim = MAX(MIN(nprows, npcols), 1)
1294 :
1295 42 : CALL dbcsr_get_info(ri_rs%mat_Z_lP, nfullcols_total=n_RI)
1296 : ! Only the cutoff path uses the measured free memory to limit panel sizes.
1297 42 : mem_budget_GB = 0.0_dp
1298 42 : IF (use_cutoff) THEN
1299 2 : CALL mp_mem_avail_per_rank_GB(para_env, mem_avail_GB)
1300 2 : mem_budget_GB = 0.5_dp*mem_avail_GB
1301 : END IF
1302 : CALL plan_grid_panels(msg_elem_limit, r_blk_sizes, min_dim, pan_first, pan_last, &
1303 : n_RI, n_ao, para_env%num_pe, mem_budget_GB, &
1304 42 : centroids=ri_rs%chunk_centroids, cutoff=effective_cutoff)
1305 :
1306 168 : ALLOCATE (ri_rs%pan_first, SOURCE=pan_first)
1307 168 : ALLOCATE (ri_rs%pan_last, SOURCE=pan_last)
1308 42 : ri_rs%panel_plan_cutoff = effective_cutoff
1309 :
1310 42 : CALL timestop(handle)
1311 :
1312 236 : END SUBROUTINE resolve_grid_panels
1313 :
1314 : END MODULE ri_rs_matrices
|