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 Experimental CP2K-native GPW real-space-grid path for SKALA TorchScript models.
10 : ! **************************************************************************************************
11 : MODULE skala_gpw_functional
12 : USE OMP_LIB, ONLY: omp_get_max_threads,&
13 : omp_set_num_threads
14 : USE cell_types, ONLY: cell_type,&
15 : pbc
16 : USE cp_array_utils, ONLY: cp_3d_r_cp_type
17 : USE cp_log_handling, ONLY: cp_logger_get_default_io_unit
18 : USE input_constants, ONLY: do_gpu_auto,&
19 : do_gpu_device,&
20 : do_gpu_host
21 : USE input_section_types, ONLY: section_get_ival,&
22 : section_get_rval,&
23 : section_vals_get_subs_vals,&
24 : section_vals_get_subs_vals2,&
25 : section_vals_type,&
26 : section_vals_val_get
27 : USE kinds, ONLY: default_path_length,&
28 : dp,&
29 : int_8
30 : USE message_passing, ONLY: mp_comm_type
31 : USE offload_api, ONLY: offload_get_chosen_device,&
32 : offload_get_device_count
33 : USE particle_types, ONLY: particle_type
34 : USE pw_grid_types, ONLY: pw_grid_type
35 : USE pw_methods, ONLY: pw_scale,&
36 : pw_zero
37 : USE pw_pool_types, ONLY: pw_pool_type
38 : USE pw_types, ONLY: pw_c1d_gs_type,&
39 : pw_r3d_rs_type
40 : USE qs_grid_atom, ONLY: grid_atom_type
41 : USE skala_gpw_features, ONLY: &
42 : skala_gpw_atom_partition_hard, skala_gpw_atom_partition_smooth, &
43 : skala_gpw_atom_subchunk_count, skala_gpw_atom_subchunk_layout, skala_gpw_feature_build, &
44 : skala_gpw_feature_build_atom_subchunk_bounds, skala_gpw_feature_release, &
45 : skala_gpw_feature_type, skala_gpw_features_release_cache, &
46 : skala_gpw_smooth_partition_derivatives, smooth_partition_atomic_weight_scale_derivative
47 : USE skala_torch_api, ONLY: skala_torch_model_get_exc,&
48 : skala_torch_model_load,&
49 : skala_torch_model_release,&
50 : skala_torch_model_type
51 : USE string_utilities, ONLY: uppercase
52 : USE torch_api, ONLY: &
53 : torch_cuda_device_count, torch_cuda_is_available, torch_dict_create, torch_dict_insert, &
54 : torch_dict_release, torch_dict_type, torch_get_use_cuda, torch_tensor_backward_scalar, &
55 : torch_tensor_data_ptr, torch_tensor_from_array, torch_tensor_grad, &
56 : torch_tensor_grad_batch3, torch_tensor_hessian_vector, torch_tensor_narrow, &
57 : torch_tensor_release, torch_tensor_reset_from_array, torch_tensor_to_device_leaf, &
58 : torch_tensor_type, torch_use_cuda
59 : USE xc_input_constants, ONLY: skala_gapw_atom_composite_grid,&
60 : skala_gapw_common_grid,&
61 : skala_gapw_cp2k_default,&
62 : skala_gapw_direct_valence,&
63 : skala_gapw_paw_one_center,&
64 : skala_gapw_paw_one_center_split
65 : USE xc_rho_cflags_types, ONLY: xc_rho_cflags_type
66 : USE xc_rho_set_types, ONLY: xc_rho_set_create,&
67 : xc_rho_set_get,&
68 : xc_rho_set_release,&
69 : xc_rho_set_type,&
70 : xc_rho_set_update
71 : USE xc_util, ONLY: xc_pw_divergence,&
72 : xc_requires_tmp_g
73 : #include "../base/base_uses.f90"
74 :
75 : IMPLICIT NONE
76 :
77 : PRIVATE
78 :
79 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'skala_gpw_functional'
80 : INTEGER, PARAMETER, PRIVATE :: atom_chunk_auto_max_rows = 400000, &
81 : atom_chunk_auto_min_rows = 100000, &
82 : atom_chunk_auto_row_quantum = 100000, &
83 : ncollapsed_grad_per_point = 5, ngrad_per_point = 10
84 : INTEGER, PARAMETER, PUBLIC :: skala_gapw_density_partition_hard_minus_soft = 1, &
85 : skala_gapw_density_partition_hard_only = 2, &
86 : skala_gapw_density_partition_soft_only = 3, &
87 : skala_gapw_density_partition_none = 4
88 :
89 : TYPE skala_gapw_atom_cuda_tensor_cache_type
90 : TYPE(torch_tensor_type) :: atomic_coords_t, atomic_grid_weights_t, &
91 : density_t, grad_t, grid_coords_t, &
92 : grid_weights_t, kin_t
93 : END TYPE skala_gapw_atom_cuda_tensor_cache_type
94 :
95 : PUBLIC :: build_vxc_from_feature_grads, ensure_native_skala_grid_scope, get_gauxc_section, &
96 : get_skala_native_section, &
97 : skala_gapw_atom_composite_energy, skala_gapw_atom_vxc_of_r, &
98 : native_skala_gapw_atom_composite_requested, &
99 : native_skala_uses_atom_composite_grid, &
100 : native_skala_gapw_composite_direct_ao, native_skala_gapw_composite_reference, &
101 : native_skala_gapw_density_partition, skala_gpw_eval, skala_gpw_weight_derivative, &
102 : skala_execution_use_cuda, skala_gpw_release_caches, skala_gapw_representation, &
103 : skala_gpw_fxc, skala_gpw_fxc_prep, &
104 : xc_section_uses_native_skala_evaluator, &
105 : xc_section_uses_native_skala_grid, xc_section_uses_gauxc_model, &
106 : xc_section_uses_skala_model
107 :
108 : TYPE(skala_torch_model_type), SAVE :: cached_model
109 : TYPE(skala_gapw_atom_cuda_tensor_cache_type), SAVE, TARGET :: cached_atom_cuda_tensors
110 : LOGICAL, SAVE :: cached_atom_cuda_tensors_active = .FALSE.
111 : CHARACTER(len=default_path_length), SAVE :: cached_model_path = ""
112 : LOGICAL, SAVE :: cached_model_loaded = .FALSE.
113 : LOGICAL, SAVE :: cached_model_fp64 = .FALSE.
114 : LOGICAL, SAVE :: cached_model_higher_derivatives = .FALSE.
115 : INTEGER, SAVE :: cached_model_cuda_device = -3
116 : INTEGER, SAVE :: logged_cuda_device = -3, &
117 : logged_cuda_device_count = -1, &
118 : logged_cuda_nproc = -1
119 : LOGICAL, SAVE :: entry_torch_use_cuda = .TRUE.
120 :
121 : CONTAINS
122 :
123 : ! **************************************************************************************************
124 : !> \brief Return true if the GAUXC subsection requests the CP2K-native GPW grid path.
125 : !> \param xc_section ...
126 : !> \return ...
127 : ! **************************************************************************************************
128 402628 : FUNCTION xc_section_uses_native_skala_grid(xc_section) RESULT(uses_native_grid)
129 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
130 : LOGICAL :: uses_native_grid
131 :
132 : TYPE(section_vals_type), POINTER :: native_section
133 :
134 402628 : native_section => get_skala_native_section(xc_section)
135 402628 : uses_native_grid = ASSOCIATED(native_section)
136 :
137 402628 : END FUNCTION xc_section_uses_native_skala_grid
138 :
139 : ! **************************************************************************************************
140 : !> \brief Return the pseudopotential GAPW representation selected for an active model.
141 : !> \param xc_section ...
142 : !> \return ...
143 : ! **************************************************************************************************
144 32128 : FUNCTION skala_gapw_representation(xc_section) RESULT(representation)
145 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
146 : INTEGER :: representation
147 :
148 : TYPE(section_vals_type), POINTER :: gauxc_section, native_section
149 :
150 : representation = skala_gapw_cp2k_default
151 32128 : native_section => get_skala_native_section(xc_section)
152 32128 : IF (ASSOCIATED(native_section)) THEN
153 : CALL section_vals_val_get(native_section, "PSEUDOPOTENTIAL_GAPW_REPRESENTATION", &
154 712 : i_val=representation)
155 : ELSE
156 31416 : IF (.NOT. xc_section_uses_gauxc_model(xc_section)) RETURN
157 36 : gauxc_section => get_gauxc_section(xc_section)
158 36 : IF (.NOT. ASSOCIATED(gauxc_section)) RETURN
159 : CALL section_vals_val_get(gauxc_section, "PSEUDOPOTENTIAL_GAPW_REPRESENTATION", &
160 36 : i_val=representation)
161 : END IF
162 :
163 : SELECT CASE (representation)
164 : CASE (skala_gapw_direct_valence, skala_gapw_paw_one_center, &
165 : skala_gapw_cp2k_default, skala_gapw_paw_one_center_split)
166 0 : CONTINUE
167 : CASE DEFAULT
168 748 : CALL cp_abort(__LOCATION__, "Unknown pseudopotential GAPW representation.")
169 : END SELECT
170 :
171 : END FUNCTION skala_gapw_representation
172 :
173 : ! **************************************************************************************************
174 : !> \brief Return true when SKALA must be evaluated by the CP2K-native grid machinery.
175 : !> \param xc_section ...
176 : !> \return ...
177 : ! **************************************************************************************************
178 173987 : FUNCTION xc_section_uses_native_skala_evaluator(xc_section) RESULT(uses_native_evaluator)
179 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
180 : LOGICAL :: uses_native_evaluator
181 :
182 173987 : uses_native_evaluator = xc_section_uses_native_skala_grid(xc_section)
183 :
184 173987 : END FUNCTION xc_section_uses_native_skala_evaluator
185 :
186 : ! **************************************************************************************************
187 : !> \brief Return true if native SKALA should use the full GAPW ORB density on one common grid.
188 : !> \param xc_section ...
189 : !> \return ...
190 : ! **************************************************************************************************
191 161741 : FUNCTION native_skala_gapw_composite_reference(xc_section) RESULT(use_composite_reference)
192 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
193 : LOGICAL :: use_composite_reference
194 :
195 : TYPE(section_vals_type), POINTER :: native_section
196 :
197 161741 : use_composite_reference = .FALSE.
198 161741 : native_section => get_skala_native_section(xc_section)
199 161741 : IF (ASSOCIATED(native_section)) THEN
200 : CALL section_vals_val_get(native_section, "GAPW_COMPOSITE_REFERENCE", &
201 1264 : l_val=use_composite_reference)
202 : END IF
203 :
204 161741 : END FUNCTION native_skala_gapw_composite_reference
205 :
206 : ! **************************************************************************************************
207 : !> \brief Return true when the explicit atom-centered composite reference is requested.
208 : !> \param xc_section ...
209 : !> \return ...
210 : ! **************************************************************************************************
211 2050 : FUNCTION native_skala_gapw_atom_composite_requested(xc_section) RESULT(use_atom_composite)
212 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
213 : LOGICAL :: use_atom_composite
214 :
215 : TYPE(section_vals_type), POINTER :: native_section
216 :
217 2050 : use_atom_composite = .FALSE.
218 2050 : native_section => get_skala_native_section(xc_section)
219 2050 : IF (ASSOCIATED(native_section)) THEN
220 : CALL section_vals_val_get(native_section, &
221 : "GAPW_ATOM_COMPOSITE_REFERENCE", &
222 408 : l_val=use_atom_composite)
223 : END IF
224 :
225 2050 : END FUNCTION native_skala_gapw_atom_composite_requested
226 :
227 : ! **************************************************************************************************
228 : !> \brief Return true when native Skala uses atom-centered grids.
229 : !> \param xc_section ...
230 : !> \return ...
231 : ! **************************************************************************************************
232 738 : FUNCTION native_skala_uses_atom_composite_grid(xc_section) RESULT(use_atom_composite)
233 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
234 : LOGICAL :: use_atom_composite
235 :
236 : INTEGER :: composite_grid
237 : TYPE(section_vals_type), POINTER :: native_section
238 :
239 738 : use_atom_composite = .FALSE.
240 738 : composite_grid = skala_gapw_atom_composite_grid
241 738 : native_section => get_skala_native_section(xc_section)
242 738 : IF (ASSOCIATED(native_section)) THEN
243 : CALL section_vals_val_get(native_section, "LAYOUT", &
244 738 : i_val=composite_grid)
245 : END IF
246 : SELECT CASE (composite_grid)
247 : CASE (skala_gapw_common_grid)
248 564 : CONTINUE
249 : CASE (skala_gapw_atom_composite_grid)
250 564 : use_atom_composite = .TRUE.
251 : CASE DEFAULT
252 738 : CALL cp_abort(__LOCATION__, "Unknown native-grid layout.")
253 : END SELECT
254 :
255 738 : END FUNCTION native_skala_uses_atom_composite_grid
256 :
257 : ! **************************************************************************************************
258 : !> \brief Return true if the GAPW composite reference uses direct full-ORB collocation.
259 : !> \param xc_section ...
260 : !> \return ...
261 : ! **************************************************************************************************
262 8 : FUNCTION native_skala_gapw_composite_direct_ao(xc_section) RESULT(use_direct_ao)
263 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
264 : LOGICAL :: use_direct_ao
265 :
266 : TYPE(section_vals_type), POINTER :: native_section
267 :
268 8 : use_direct_ao = .FALSE.
269 8 : native_section => get_skala_native_section(xc_section)
270 8 : IF (ASSOCIATED(native_section)) THEN
271 : CALL section_vals_val_get(native_section, "GAPW_COMPOSITE_DIRECT_AO", &
272 8 : l_val=use_direct_ao)
273 : END IF
274 8 : use_direct_ao = native_skala_gapw_composite_reference(xc_section) .AND. use_direct_ao
275 :
276 8 : END FUNCTION native_skala_gapw_composite_direct_ao
277 :
278 : ! **************************************************************************************************
279 : !> \brief Return true if the GAUXC subsection requests a model evaluation.
280 : !> \param xc_section ...
281 : !> \return ...
282 : ! **************************************************************************************************
283 66876 : FUNCTION xc_section_uses_gauxc_model(xc_section) RESULT(uses_gauxc_model)
284 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
285 : LOGICAL :: uses_gauxc_model
286 :
287 : CHARACTER(len=default_path_length) :: model_key, model_name, xc_key, xc_name
288 : TYPE(section_vals_type), POINTER :: gauxc_section, native_section
289 :
290 66876 : uses_gauxc_model = .FALSE.
291 66876 : native_section => get_skala_native_section(xc_section)
292 66876 : IF (ASSOCIATED(native_section)) THEN
293 66876 : uses_gauxc_model = .TRUE.
294 : RETURN
295 : END IF
296 66606 : gauxc_section => get_gauxc_section(xc_section)
297 66606 : IF (ASSOCIATED(gauxc_section)) THEN
298 76 : CALL section_vals_val_get(gauxc_section, "MODEL", c_val=model_name)
299 76 : CALL section_vals_val_get(gauxc_section, "FUNCTIONAL", c_val=xc_name)
300 76 : model_key = ADJUSTL(model_name)
301 76 : xc_key = ADJUSTL(xc_name)
302 76 : CALL uppercase(model_key)
303 76 : CALL uppercase(xc_key)
304 : uses_gauxc_model = (TRIM(model_key) /= "" .AND. TRIM(model_key) /= "NONE" .AND. &
305 76 : TRIM(model_key) /= TRIM(xc_key))
306 : END IF
307 :
308 : END FUNCTION xc_section_uses_gauxc_model
309 :
310 : ! **************************************************************************************************
311 : !> \brief Return true if the XC section requests a SKALA model (GauXC or native/FTorch).
312 : !> \param xc_section ...
313 : !> \return ...
314 : ! **************************************************************************************************
315 6560 : FUNCTION xc_section_uses_skala_model(xc_section) RESULT(uses_skala_model)
316 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
317 : LOGICAL :: uses_skala_model
318 :
319 : CHARACTER(len=default_path_length) :: model_key, model_name
320 : TYPE(section_vals_type), POINTER :: gauxc_section
321 :
322 6560 : uses_skala_model = .FALSE.
323 6560 : gauxc_section => get_gauxc_section(xc_section)
324 6560 : IF (.NOT. ASSOCIATED(gauxc_section)) RETURN
325 :
326 48 : CALL section_vals_val_get(gauxc_section, "MODEL", c_val=model_name)
327 48 : model_key = ADJUSTL(model_name)
328 48 : CALL uppercase(model_key)
329 48 : uses_skala_model = INDEX(TRIM(model_key), "SKALA") > 0
330 :
331 48 : END FUNCTION xc_section_uses_skala_model
332 :
333 : ! **************************************************************************************************
334 : !> \brief Return the hard/soft GAPW one-center density partition for native SKALA.
335 : !> \param xc_section ...
336 : !> \return ...
337 : ! **************************************************************************************************
338 272 : FUNCTION native_skala_gapw_density_partition(xc_section) RESULT(partition)
339 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
340 : INTEGER :: partition
341 :
342 : TYPE(section_vals_type), POINTER :: native_section
343 :
344 272 : partition = skala_gapw_density_partition_hard_minus_soft
345 272 : native_section => get_skala_native_section(xc_section)
346 272 : IF (ASSOCIATED(native_section)) THEN
347 : CALL section_vals_val_get(native_section, "GAPW_DENSITY_PARTITION", &
348 270 : i_val=partition)
349 : END IF
350 :
351 : SELECT CASE (partition)
352 : CASE (skala_gapw_density_partition_hard_minus_soft, &
353 : skala_gapw_density_partition_hard_only, &
354 : skala_gapw_density_partition_soft_only, &
355 : skala_gapw_density_partition_none)
356 0 : CONTINUE
357 : CASE DEFAULT
358 : CALL cp_abort(__LOCATION__, &
359 272 : "Unknown SKALA_NATIVE%GAPW_DENSITY_PARTITION value.")
360 : END SELECT
361 :
362 272 : END FUNCTION native_skala_gapw_density_partition
363 :
364 : ! **************************************************************************************************
365 : !> \brief Enforce the currently implemented native SKALA GPW input scope.
366 : !> \param xc_section ...
367 : ! **************************************************************************************************
368 356 : SUBROUTINE ensure_native_skala_grid_scope(xc_section)
369 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
370 :
371 : CHARACTER(len=default_path_length) :: model_key, model_name
372 : INTEGER :: ifun, nfun
373 : TYPE(section_vals_type), POINTER :: functionals, native_section, xc_fun
374 :
375 356 : NULLIFY (native_section)
376 356 : IF (.NOT. ASSOCIATED(xc_section)) THEN
377 0 : CPABORT("Native SKALA GPW requires an XC section")
378 : END IF
379 :
380 356 : functionals => section_vals_get_subs_vals(xc_section, "XC_FUNCTIONAL")
381 356 : IF (.NOT. ASSOCIATED(functionals)) THEN
382 0 : CPABORT("Native SKALA GPW requires an XC_FUNCTIONAL section")
383 : END IF
384 :
385 356 : nfun = 0
386 356 : ifun = 0
387 : DO
388 712 : ifun = ifun + 1
389 712 : xc_fun => section_vals_get_subs_vals2(functionals, i_section=ifun)
390 712 : IF (.NOT. ASSOCIATED(xc_fun)) EXIT
391 356 : nfun = nfun + 1
392 712 : IF (xc_fun%section%name == "SKALA_NATIVE") native_section => xc_fun
393 : END DO
394 :
395 356 : IF (.NOT. ASSOCIATED(native_section)) THEN
396 0 : CPABORT("Native SKALA GPW requires an XC_FUNCTIONAL%SKALA_NATIVE section")
397 : END IF
398 356 : IF (nfun /= 1) THEN
399 0 : CPABORT("SKALA_NATIVE must be the only XC functional")
400 : END IF
401 :
402 356 : CALL section_vals_val_get(native_section, "MODEL", c_val=model_name)
403 356 : model_key = ADJUSTL(model_name)
404 356 : CALL uppercase(model_key)
405 356 : IF (TRIM(model_key) == "NONE" .OR. TRIM(model_key) == "") THEN
406 0 : CPABORT("SKALA_NATIVE requires MODEL SKALA or a TorchScript model path")
407 : END IF
408 :
409 356 : END SUBROUTINE ensure_native_skala_grid_scope
410 :
411 : ! **************************************************************************************************
412 : !> \brief Evaluate SKALA energy and first derivatives on a CP2K GPW grid.
413 : !> \param vxc_rho ...
414 : !> \param vxc_tau ...
415 : !> \param exc ...
416 : !> \param rho_r ...
417 : !> \param rho_g ...
418 : !> \param tau ...
419 : !> \param xc_section ...
420 : !> \param weights ...
421 : !> \param pw_pool ...
422 : !> \param particle_set ...
423 : !> \param cell ...
424 : !> \param compute_virial ...
425 : !> \param virial_xc ...
426 : !> \param just_energy ...
427 : !> \param atom_force ...
428 : ! **************************************************************************************************
429 102 : SUBROUTINE skala_gpw_eval(vxc_rho, vxc_tau, exc, rho_r, rho_g, tau, xc_section, &
430 : weights, pw_pool, particle_set, cell, compute_virial, virial_xc, &
431 102 : just_energy, atom_force)
432 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: vxc_rho, vxc_tau
433 : REAL(KIND=dp), INTENT(OUT) :: exc
434 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
435 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
436 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: tau
437 : TYPE(section_vals_type), POINTER :: xc_section
438 : TYPE(pw_r3d_rs_type), POINTER :: weights
439 : TYPE(pw_pool_type), POINTER :: pw_pool
440 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
441 : TYPE(cell_type), POINTER :: cell
442 : LOGICAL, INTENT(IN) :: compute_virial
443 : REAL(KIND=dp), DIMENSION(3, 3), INTENT(OUT) :: virial_xc
444 : LOGICAL, INTENT(IN), OPTIONAL :: just_energy
445 : REAL(KIND=dp), DIMENSION(:, :), INTENT(OUT), &
446 : OPTIONAL :: atom_force
447 :
448 : CHARACTER(len=default_path_length) :: model_path
449 : INTEGER :: i, ipt, ispin, iw, j, k, native_grid_atom_chunk_max_rows, &
450 : native_grid_atom_partition, native_grid_atom_subchunks, nspins, omp_max_threads_restore, &
451 : phase_handle, selected_cuda_device, xc_deriv_method_id, xc_rho_smooth_id
452 : INTEGER, DIMENSION(2, 3) :: bo
453 : LOGICAL :: has_atom_chunk_work, have_atom_coord_grad, lsd, my_just_energy, &
454 : native_grid_atom_chunk_routing, native_grid_atom_chunks, native_grid_diagnostics, &
455 : native_grid_use_cuda, needs_atom_force, use_atom_subchunks
456 : REAL(KIND=dp) :: density_contraction, tau_contraction
457 102 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: density_grad, kin_grad
458 102 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: grad_grad
459 : REAL(KIND=dp), DIMENSION(3, 3) :: virial_before
460 : TYPE(section_vals_type), POINTER :: gauxc_section
461 102 : TYPE(skala_gpw_feature_type) :: features
462 : TYPE(torch_tensor_type) :: atom_coord_grad_t, &
463 : atomic_grid_weight_grad_t, exc_tensor, &
464 : grid_coord_grad_t, grid_weight_grad_t
465 : TYPE(xc_rho_cflags_type) :: needs
466 : TYPE(xc_rho_set_type) :: rho_set
467 :
468 102 : virial_xc = 0.0_dp
469 102 : exc = 0.0_dp
470 102 : my_just_energy = .FALSE.
471 102 : IF (PRESENT(just_energy)) my_just_energy = just_energy
472 102 : needs_atom_force = PRESENT(atom_force)
473 166 : IF (needs_atom_force) atom_force = 0.0_dp
474 102 : have_atom_coord_grad = .FALSE.
475 102 : omp_max_threads_restore = omp_get_max_threads()
476 :
477 102 : IF (compute_virial .AND. my_just_energy) THEN
478 : CALL cp_abort(__LOCATION__, &
479 0 : "Native SKALA GPW stress/virial requires feature gradients.")
480 : END IF
481 102 : IF (.NOT. ASSOCIATED(rho_g)) THEN
482 : CALL cp_abort(__LOCATION__, &
483 0 : "Native SKALA GPW requires the reciprocal-space density to form density gradients.")
484 : END IF
485 102 : IF (.NOT. ASSOCIATED(tau)) THEN
486 : CALL cp_abort(__LOCATION__, &
487 0 : "Native SKALA GPW requires the kinetic-energy density.")
488 : END IF
489 :
490 102 : nspins = SIZE(rho_r)
491 102 : lsd = (nspins /= 1)
492 102 : CALL get_skala_model_path(xc_section, model_path)
493 102 : native_grid_use_cuda = skala_execution_use_cuda(xc_section)
494 102 : native_grid_atom_chunks = .TRUE.
495 102 : native_grid_atom_chunk_max_rows = -1
496 102 : native_grid_atom_partition = skala_gpw_atom_partition_smooth
497 102 : gauxc_section => get_skala_native_section(xc_section)
498 102 : IF (ASSOCIATED(gauxc_section)) THEN
499 : CALL section_vals_val_get(gauxc_section, "ATOM_CHUNKS", &
500 102 : l_val=native_grid_atom_chunks)
501 : CALL section_vals_val_get(gauxc_section, "ATOM_CHUNK_MAX_ROWS", &
502 102 : i_val=native_grid_atom_chunk_max_rows)
503 : CALL section_vals_val_get(gauxc_section, "ATOM_PARTITION", &
504 102 : i_val=native_grid_atom_partition)
505 : END IF
506 102 : native_grid_atom_chunk_routing = native_grid_atom_chunks
507 76 : SELECT CASE (native_grid_atom_partition)
508 : CASE (1)
509 76 : native_grid_atom_partition = skala_gpw_atom_partition_hard
510 : CASE (2)
511 26 : native_grid_atom_partition = skala_gpw_atom_partition_smooth
512 : CASE DEFAULT
513 : CALL cp_abort(__LOCATION__, &
514 102 : "Unknown SKALA_NATIVE%ATOM_PARTITION value.")
515 : END SELECT
516 102 : native_grid_atom_chunks = native_grid_atom_chunks .OR. native_grid_atom_chunk_routing
517 102 : IF (native_grid_atom_chunk_max_rows < -1) THEN
518 : CALL cp_abort(__LOCATION__, &
519 0 : "SKALA_NATIVE%ATOM_CHUNK_MAX_ROWS must be -1, zero, or positive.")
520 : END IF
521 102 : IF (needs_atom_force .OR. compute_virial) THEN
522 8 : IF (native_grid_atom_partition == skala_gpw_atom_partition_hard) THEN
523 0 : native_grid_atom_partition = skala_gpw_atom_partition_smooth
524 : END IF
525 8 : native_grid_atom_chunk_routing = .FALSE.
526 8 : native_grid_atom_chunks = .FALSE.
527 : END IF
528 : ! The portable SKALA export used by the regtests builds ragged-index tensors on CPU.
529 102 : entry_torch_use_cuda = torch_get_use_cuda()
530 102 : CALL torch_use_cuda(native_grid_use_cuda)
531 : selected_cuda_device = configure_native_grid_cuda( &
532 102 : native_grid_use_cuda, rho_r(1)%pw_grid%para%group)
533 102 : CALL ensure_model_loaded(model_path, selected_cuda_device, xc_section)
534 :
535 102 : IF (lsd) THEN
536 6 : needs%rho_spin = .TRUE.
537 6 : needs%drho_spin = .TRUE.
538 6 : needs%tau_spin = .TRUE.
539 : ELSE
540 96 : needs%rho = .TRUE.
541 96 : needs%drho = .TRUE.
542 96 : needs%tau = .TRUE.
543 : END IF
544 :
545 102 : CALL section_vals_val_get(xc_section, "XC_GRID%XC_DERIV", i_val=xc_deriv_method_id)
546 102 : CALL section_vals_val_get(xc_section, "XC_GRID%XC_SMOOTH_RHO", i_val=xc_rho_smooth_id)
547 :
548 : CALL xc_rho_set_create(rho_set, &
549 : rho_r(1)%pw_grid%bounds_local, &
550 : rho_cutoff=section_get_rval(xc_section, "density_cutoff"), &
551 : drho_cutoff=section_get_rval(xc_section, "gradient_cutoff"), &
552 102 : tau_cutoff=section_get_rval(xc_section, "tau_cutoff"))
553 : CALL xc_rho_set_update(rho_set, rho_r, rho_g, tau, needs, &
554 102 : xc_deriv_method_id, xc_rho_smooth_id, pw_pool)
555 :
556 : CALL skala_gpw_feature_build(features, rho_set, rho_r, particle_set, cell, &
557 : requires_grad=(.NOT. my_just_energy), weights=weights, &
558 : requires_coordinate_grad=(needs_atom_force .OR. compute_virial), &
559 : requires_stress_grad=compute_virial, &
560 : use_atom_chunks=native_grid_atom_chunks, &
561 : route_atom_chunks=native_grid_atom_chunk_routing, &
562 : atom_partition=native_grid_atom_partition, &
563 196 : cuda_device=selected_cuda_device)
564 102 : CALL section_vals_val_get(gauxc_section, "DIAGNOSTICS", l_val=native_grid_diagnostics)
565 102 : IF (native_grid_diagnostics) THEN
566 24 : CALL print_native_grid_diagnostics(features, rho_r(1)%pw_grid%para%group%mepos == 0)
567 : END IF
568 :
569 102 : IF (features%uses_atom_chunks .AND. native_grid_atom_chunk_max_rows == -1) THEN
570 : native_grid_atom_chunk_max_rows = auto_atom_chunk_max_rows(features, &
571 90 : rho_r(1)%pw_grid%para%group)
572 : END IF
573 102 : IF (native_grid_diagnostics .AND. features%uses_atom_chunks .AND. &
574 : rho_r(1)%pw_grid%para%group%mepos == 0) THEN
575 12 : iw = cp_logger_get_default_io_unit()
576 12 : IF (iw > 0) THEN
577 : WRITE (UNIT=iw, FMT="(T2,A,1X,I0)") &
578 12 : "SKALA_GPW| Native grid atom chunk max rows", native_grid_atom_chunk_max_rows
579 : END IF
580 : END IF
581 102 : native_grid_atom_subchunks = 1
582 102 : IF (features%uses_atom_chunks .AND. native_grid_atom_chunk_max_rows > 0) THEN
583 94 : native_grid_atom_subchunks = skala_gpw_atom_subchunk_count(native_grid_atom_chunk_max_rows)
584 94 : CALL rho_r(1)%pw_grid%para%group%max(native_grid_atom_subchunks)
585 : END IF
586 102 : use_atom_subchunks = features%uses_atom_chunks .AND. native_grid_atom_subchunks > 1
587 102 : has_atom_chunk_work = .NOT. features%uses_atom_chunks .OR. features%chunk_feature_count > 0
588 102 : exc = 0.0_dp
589 102 : IF (use_atom_subchunks) THEN
590 : CALL evaluate_atom_subchunks(features, rho_r(1)%pw_grid%para%group, &
591 : native_grid_atom_chunk_max_rows, &
592 : compute_grads=(.NOT. my_just_energy), exc=exc, &
593 : density_grad=density_grad, grad_grad=grad_grad, &
594 2 : kin_grad=kin_grad, collapse_spin_grads=(nspins == 1))
595 100 : ELSE IF (has_atom_chunk_work) THEN
596 : CALL skala_torch_model_get_exc(cached_model, features%inputs, &
597 100 : features%grid_weights_t, exc_tensor, exc)
598 : END IF
599 102 : IF (features%uses_atom_chunks) CALL rho_r(1)%pw_grid%para%group%sum(exc)
600 :
601 102 : IF (.NOT. my_just_energy) THEN
602 102 : IF (.NOT. use_atom_subchunks) THEN
603 100 : IF (has_atom_chunk_work) THEN
604 100 : CALL timeset("skala_gpw_backward", phase_handle)
605 100 : CALL torch_tensor_backward_scalar(exc_tensor)
606 100 : CALL timestop(phase_handle)
607 :
608 100 : IF (compute_virial) THEN
609 6 : IF (native_grid_diagnostics) virial_before = virial_xc
610 : CALL build_weight_virial(virial_xc, features, exc, grid_weight_grad_t, &
611 : atomic_grid_weight_grad_t, &
612 : rho_r(1)%pw_grid%para%group%mepos == 0, &
613 6 : native_grid_diagnostics)
614 6 : IF (native_grid_diagnostics) THEN
615 : CALL print_virial_delta("weight-residual", virial_xc - virial_before, &
616 0 : rho_r(1)%pw_grid%para%group%mepos == 0)
617 : END IF
618 : END IF
619 : END IF
620 :
621 100 : CALL timeset("skala_gpw_grad_fetch", phase_handle)
622 100 : IF (features%uses_atom_chunks) THEN
623 : CALL fetch_and_gather_atom_chunk_grads(features, rho_r(1)%pw_grid%para%group, &
624 92 : density_grad, grad_grad, kin_grad)
625 : ELSE
626 8 : CALL fetch_local_feature_grads(features, density_grad, grad_grad, kin_grad)
627 : END IF
628 100 : CALL timestop(phase_handle)
629 : END IF
630 102 : IF (needs_atom_force) THEN
631 : CALL add_explicit_coordinate_force(atom_force, features, atom_coord_grad_t, &
632 8 : rho_r(1)%pw_grid%para%group%mepos == 0)
633 8 : IF (features%atom_partition == skala_gpw_atom_partition_smooth) THEN
634 : CALL add_smooth_partition_force(atom_force, features, particle_set, cell, rho_r, &
635 8 : grid_weight_grad_t, atomic_grid_weight_grad_t)
636 : END IF
637 : have_atom_coord_grad = .TRUE.
638 : END IF
639 :
640 102 : CALL timeset("skala_gpw_vxc_unpack", phase_handle)
641 102 : IF (native_grid_diagnostics) THEN
642 240 : bo = rho_r(1)%pw_grid%bounds_local
643 24 : density_contraction = 0.0_dp
644 24 : tau_contraction = 0.0_dp
645 24 : ipt = 0
646 1004 : DO k = bo(1, 3), bo(2, 3)
647 41848 : DO j = bo(1, 2), bo(2, 2)
648 905054 : DO i = bo(1, 1), bo(2, 1)
649 863230 : ipt = ipt + 1
650 904074 : IF (nspins == 1) THEN
651 : density_contraction = density_contraction + rho_r(1)%array(i, j, k)* &
652 589855 : 0.5_dp*(density_grad(ipt, 1) + density_grad(ipt, 2))
653 : tau_contraction = tau_contraction + tau(1)%array(i, j, k)* &
654 589855 : 0.5_dp*(kin_grad(ipt, 1) + kin_grad(ipt, 2))
655 : ELSE
656 820125 : DO ispin = 1, nspins
657 : density_contraction = density_contraction + &
658 546750 : rho_r(ispin)%array(i, j, k)*density_grad(ipt, ispin)
659 : tau_contraction = tau_contraction + &
660 820125 : tau(ispin)%array(i, j, k)*kin_grad(ipt, ispin)
661 : END DO
662 : END IF
663 : END DO
664 : END DO
665 : END DO
666 24 : CALL rho_r(1)%pw_grid%para%group%sum(density_contraction)
667 24 : CALL rho_r(1)%pw_grid%para%group%sum(tau_contraction)
668 24 : IF (rho_r(1)%pw_grid%para%group%mepos == 0) THEN
669 12 : iw = cp_logger_get_default_io_unit()
670 12 : IF (iw > 0) THEN
671 : WRITE (iw, "(T2,A,1X,ES20.10)") &
672 12 : "SKALA_GPW| XC density-gradient contraction", density_contraction
673 : WRITE (iw, "(T2,A,1X,ES20.10)") &
674 12 : "SKALA_GPW| XC kinetic-gradient contraction", tau_contraction
675 : END IF
676 : END IF
677 : END IF
678 102 : IF (compute_virial) THEN
679 6 : IF (native_grid_diagnostics) virial_before = virial_xc
680 6 : CALL build_virial_from_feature_grads(virial_xc, rho_set, rho_r, grad_grad)
681 6 : IF (native_grid_diagnostics) THEN
682 : CALL print_virial_delta("feature-gradient", virial_xc - virial_before, &
683 0 : rho_r(1)%pw_grid%para%group%mepos == 0)
684 0 : virial_before = virial_xc
685 : END IF
686 6 : IF (.NOT. have_atom_coord_grad) THEN
687 0 : CALL torch_tensor_grad(features%coarse_0_atomic_coords_t, atom_coord_grad_t)
688 0 : have_atom_coord_grad = .TRUE.
689 : END IF
690 : CALL build_static_coordinate_virial(virial_xc, features, atom_coord_grad_t, &
691 : grid_coord_grad_t, &
692 : rho_r(1)%pw_grid%para%group%mepos == 0, &
693 6 : native_grid_diagnostics)
694 6 : IF (native_grid_diagnostics) THEN
695 : CALL print_virial_delta("static-coordinates", virial_xc - virial_before, &
696 0 : rho_r(1)%pw_grid%para%group%mepos == 0)
697 0 : virial_before = virial_xc
698 : END IF
699 6 : IF (features%atom_partition == skala_gpw_atom_partition_smooth) THEN
700 : CALL build_smooth_partition_virial(virial_xc, features, particle_set, cell, rho_r, &
701 6 : grid_weight_grad_t, atomic_grid_weight_grad_t)
702 6 : IF (native_grid_diagnostics) THEN
703 : CALL print_virial_delta("smooth-partition", virial_xc - virial_before, &
704 0 : rho_r(1)%pw_grid%para%group%mepos == 0)
705 : virial_before = virial_xc
706 : END IF
707 : END IF
708 : END IF
709 102 : CALL omp_set_num_threads(omp_max_threads_restore)
710 : CALL build_vxc_from_feature_grads(vxc_rho, vxc_tau, rho_r, pw_pool, &
711 : density_grad, grad_grad, kin_grad, &
712 102 : xc_deriv_method_id)
713 102 : CALL timestop(phase_handle)
714 :
715 102 : CALL timeset("skala_gpw_grad_release", phase_handle)
716 102 : DEALLOCATE (density_grad, grad_grad, kin_grad)
717 102 : IF (have_atom_coord_grad) CALL torch_tensor_release(atom_coord_grad_t)
718 102 : CALL timestop(phase_handle)
719 : END IF
720 :
721 102 : CALL timeset("skala_gpw_cleanup", phase_handle)
722 102 : IF (.NOT. use_atom_subchunks .AND. has_atom_chunk_work) CALL torch_tensor_release(exc_tensor)
723 102 : CALL skala_gpw_feature_release(features)
724 102 : CALL xc_rho_set_release(rho_set, pw_pool=pw_pool)
725 102 : CALL torch_use_cuda(entry_torch_use_cuda)
726 102 : CALL omp_set_num_threads(omp_max_threads_restore)
727 102 : CALL timestop(phase_handle)
728 :
729 2040 : END SUBROUTINE skala_gpw_eval
730 :
731 : ! **************************************************************************************************
732 : !> \brief Cache the unmodified reference fields for the nonlocal Skala response.
733 : !> \param rho_set Reference density, gradient and kinetic-energy density.
734 : !> \param rho_r Real-space reference density.
735 : !> \param tau_r Reference kinetic-energy density.
736 : !> \param xc_section XC settings.
737 : !> \param pw_pool Grid and Fourier transforms.
738 : ! **************************************************************************************************
739 88 : SUBROUTINE skala_gpw_fxc_prep(rho_set, rho_r, tau_r, xc_section, pw_pool)
740 : TYPE(xc_rho_set_type), INTENT(OUT) :: rho_set
741 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r, tau_r
742 : TYPE(section_vals_type), POINTER :: xc_section
743 : TYPE(pw_pool_type), POINTER :: pw_pool
744 :
745 : INTEGER :: ispin
746 : REAL(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
747 4 : POINTER :: tau_copy
748 4 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
749 : TYPE(xc_rho_cflags_type) :: needs
750 :
751 4 : IF (.NOT. ASSOCIATED(tau_r)) THEN
752 0 : CALL cp_abort(__LOCATION__, "Native Skala response requires kinetic-energy density.")
753 : END IF
754 4 : IF (SIZE(rho_r) == 1) THEN
755 4 : needs%rho = .TRUE.
756 4 : needs%drho = .TRUE.
757 4 : needs%tau = .TRUE.
758 : ELSE
759 0 : needs%rho_spin = .TRUE.
760 0 : needs%drho_spin = .TRUE.
761 0 : needs%tau_spin = .TRUE.
762 : END IF
763 : CALL xc_rho_set_create(rho_set, rho_r(1)%pw_grid%bounds_local, &
764 : rho_cutoff=section_get_rval(xc_section, "DENSITY_CUTOFF"), &
765 : drho_cutoff=section_get_rval(xc_section, "GRADIENT_CUTOFF"), &
766 4 : tau_cutoff=section_get_rval(xc_section, "TAU_CUTOFF"))
767 4 : NULLIFY (rho_g)
768 : CALL xc_rho_set_update(rho_set, rho_r, rho_g, tau_r, needs, &
769 : section_get_ival(xc_section, "XC_GRID%XC_DERIV"), &
770 4 : section_get_ival(xc_section, "XC_GRID%XC_SMOOTH_RHO"), pw_pool)
771 : ! Unlike rho and its gradient, xc_rho_set_update borrows tau storage.
772 : ! A cached response reference must survive release of a transferred grid.
773 8 : DO ispin = 1, SIZE(tau_r)
774 4 : NULLIFY (tau_copy)
775 4 : CALL pw_pool%create_cr3d(tau_copy)
776 4084 : tau_copy = tau_r(ispin)%array
777 8 : IF (SIZE(tau_r) == 1) THEN
778 4 : rho_set%tau => tau_copy
779 4 : rho_set%owns%tau = .TRUE.
780 0 : ELSE IF (ispin == 1) THEN
781 0 : rho_set%tau_a => tau_copy
782 : ELSE
783 0 : rho_set%tau_b => tau_copy
784 0 : rho_set%owns%tau_spin = .TRUE.
785 : END IF
786 : END DO
787 4 : END SUBROUTINE skala_gpw_fxc_prep
788 :
789 : ! **************************************************************************************************
790 : !> \brief Apply the nonlocal Skala Hessian to a density and kinetic-density response.
791 : !> \param rho0_set Reference fields.
792 : !> \param rho1_r Density response.
793 : !> \param tau1_r Kinetic-energy-density response.
794 : !> \param xc_section XC settings.
795 : !> \param weights Integration weights.
796 : !> \param pw_pool Grid and Fourier transforms.
797 : !> \param particle_set Fixed nuclei.
798 : !> \param cell Fixed simulation cell.
799 : !> \param fxc_rho XC potential response.
800 : !> \param fxc_tau Kinetic-energy-density potential response.
801 : ! **************************************************************************************************
802 2 : SUBROUTINE skala_gpw_fxc(rho0_set, rho1_r, tau1_r, xc_section, weights, pw_pool, &
803 : particle_set, cell, fxc_rho, fxc_tau)
804 : TYPE(xc_rho_set_type), INTENT(INOUT) :: rho0_set
805 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho1_r, tau1_r
806 : TYPE(section_vals_type), POINTER :: xc_section
807 : TYPE(pw_r3d_rs_type), POINTER :: weights
808 : TYPE(pw_pool_type), POINTER :: pw_pool
809 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
810 : TYPE(cell_type), POINTER :: cell
811 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: fxc_rho, fxc_tau
812 :
813 : CHARACTER(len=default_path_length) :: model_path
814 : INTEGER :: i, partition, selected_device
815 : LOGICAL :: fp64, use_cuda
816 : REAL(KIND=dp) :: exc
817 4 : REAL(KIND=dp), ALLOCATABLE :: density_grad(:, :), grad_grad(:, :, :), &
818 2 : kin_grad(:, :)
819 : TYPE(section_vals_type), POINTER :: native_section
820 2 : TYPE(skala_gpw_feature_type) :: direction, reference
821 16 : TYPE(torch_tensor_type) :: directions(3), exc_tensor, responses(3)
822 : TYPE(xc_rho_set_type) :: rho1_set
823 :
824 2 : fp64 = .TRUE.
825 2 : partition = skala_gpw_atom_partition_smooth
826 4 : native_section => get_skala_native_section(xc_section)
827 2 : IF (ASSOCIATED(native_section)) THEN
828 2 : CALL section_vals_val_get(native_section, "MODEL_FP64", l_val=fp64)
829 2 : CALL section_vals_val_get(native_section, "ATOM_PARTITION", i_val=partition)
830 : END IF
831 2 : IF (.NOT. fp64) THEN
832 0 : CALL cp_abort(__LOCATION__, "Native Skala response requires MODEL_FP64.")
833 : END IF
834 2 : SELECT CASE (partition)
835 : CASE (1)
836 2 : partition = skala_gpw_atom_partition_hard
837 : CASE (2)
838 0 : partition = skala_gpw_atom_partition_smooth
839 : CASE DEFAULT
840 : CALL cp_abort(__LOCATION__, &
841 2 : "Unknown SKALA_NATIVE%ATOM_PARTITION value.")
842 : END SELECT
843 2 : use_cuda = skala_execution_use_cuda(xc_section)
844 2 : CALL torch_use_cuda(use_cuda)
845 2 : selected_device = configure_native_grid_cuda(use_cuda, rho1_r(1)%pw_grid%para%group)
846 2 : CALL get_skala_model_path(xc_section, model_path)
847 2 : CALL ensure_model_loaded(model_path, selected_device, xc_section, higher_derivatives=.TRUE.)
848 :
849 2 : CALL skala_gpw_fxc_prep(rho1_set, rho1_r, tau1_r, xc_section, pw_pool)
850 : ! Both maps use the fixed SCF grid. The second map packs a signed direction,
851 : ! not a new physical density, without clipping or density normalization.
852 : CALL skala_gpw_feature_build(direction, rho1_set, rho1_r, particle_set, cell, &
853 : requires_grad=.FALSE., weights=weights, atom_partition=partition, &
854 2 : cuda_device=selected_device)
855 : ! The feature builder reuses dynamic tensors. Keep independent direction
856 : ! tensors before packing the reference into that cache.
857 2 : CALL torch_tensor_from_array(directions(1), direction%density)
858 2 : CALL torch_tensor_from_array(directions(2), direction%grad)
859 2 : CALL torch_tensor_from_array(directions(3), direction%kin)
860 8 : DO i = 1, SIZE(directions)
861 8 : CALL torch_tensor_to_device_leaf(directions(i), .FALSE.)
862 : END DO
863 : CALL skala_gpw_feature_build(reference, rho0_set, rho1_r, particle_set, cell, &
864 : requires_grad=.TRUE., weights=weights, atom_partition=partition, &
865 2 : cuda_device=selected_device)
866 2 : CALL skala_torch_model_get_exc(cached_model, reference%inputs, reference%grid_weights_t, exc_tensor, exc)
867 : CALL torch_tensor_hessian_vector(exc_tensor, &
868 : [reference%density_t, reference%grad_t, reference%kin_t], &
869 8 : directions, responses)
870 2 : CALL fetch_local_feature_grads(reference, density_grad, grad_grad, kin_grad, responses)
871 : CALL build_vxc_from_feature_grads(fxc_rho, fxc_tau, rho1_r, pw_pool, &
872 : density_grad, grad_grad, kin_grad, &
873 2 : section_get_ival(xc_section, "XC_GRID%XC_DERIV"))
874 8 : DO i = 1, SIZE(responses)
875 6 : CALL torch_tensor_release(responses(i))
876 8 : CALL torch_tensor_release(directions(i))
877 : END DO
878 2 : CALL torch_tensor_release(exc_tensor)
879 2 : CALL skala_gpw_feature_release(direction)
880 2 : CALL skala_gpw_feature_release(reference)
881 2 : CALL xc_rho_set_release(rho1_set)
882 2 : CALL torch_use_cuda(.TRUE.)
883 48 : END SUBROUTINE skala_gpw_fxc
884 :
885 : ! **************************************************************************************************
886 : !> \brief Evaluate the derivative of native SKALA XC energy with respect to CP2K's external
887 : !> real-space integration-weight multiplier.
888 : !> \param weight_deriv_r ...
889 : !> \param rho_r ...
890 : !> \param rho_g ...
891 : !> \param tau ...
892 : !> \param xc_section ...
893 : !> \param weights ...
894 : !> \param pw_pool ...
895 : !> \param particle_set ...
896 : !> \param cell ...
897 : ! **************************************************************************************************
898 0 : SUBROUTINE skala_gpw_weight_derivative(weight_deriv_r, rho_r, rho_g, tau, xc_section, &
899 : weights, pw_pool, particle_set, cell)
900 : TYPE(pw_r3d_rs_type), INTENT(INOUT) :: weight_deriv_r
901 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
902 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
903 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: tau
904 : TYPE(section_vals_type), POINTER :: xc_section
905 : TYPE(pw_r3d_rs_type), POINTER :: weights
906 : TYPE(pw_pool_type), POINTER :: pw_pool
907 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
908 : TYPE(cell_type), POINTER :: cell
909 :
910 : CHARACTER(len=default_path_length) :: model_path
911 : INTEGER :: feature_begin, feature_end, feature_pos, i, iatom, j, k, local_row, &
912 : native_grid_atom_partition, natom, nspins, omp_max_threads_restore, row, &
913 : selected_cuda_device, xc_deriv_method_id, xc_rho_smooth_id
914 : INTEGER, DIMENSION(2, 3) :: bo
915 : LOGICAL :: lsd, native_grid_use_cuda
916 0 : LOGICAL, ALLOCATABLE, DIMENSION(:) :: included
917 : REAL(KIND=dp) :: exc, local_derivative
918 0 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: partition_weights
919 0 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: atom_coords_pbc
920 0 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: dweights_datom, dweights_dstrain
921 : REAL(KIND=dp), DIMENSION(3) :: grid_point
922 0 : REAL(KIND=dp), DIMENSION(:), POINTER :: grid_weight_grad
923 : TYPE(section_vals_type), POINTER :: gauxc_section
924 0 : TYPE(skala_gpw_feature_type) :: features
925 : TYPE(torch_tensor_type) :: exc_tensor, grid_weight_grad_t
926 : TYPE(xc_rho_cflags_type) :: needs
927 : TYPE(xc_rho_set_type) :: rho_set
928 :
929 0 : CPASSERT(ASSOCIATED(rho_r))
930 0 : CPASSERT(ASSOCIATED(rho_g))
931 0 : CPASSERT(ASSOCIATED(tau))
932 0 : CALL pw_zero(weight_deriv_r)
933 0 : omp_max_threads_restore = omp_get_max_threads()
934 :
935 0 : nspins = SIZE(rho_r)
936 0 : lsd = (nspins /= 1)
937 0 : CALL get_skala_model_path(xc_section, model_path)
938 0 : native_grid_use_cuda = skala_execution_use_cuda(xc_section)
939 0 : native_grid_atom_partition = skala_gpw_atom_partition_smooth
940 0 : gauxc_section => get_skala_native_section(xc_section)
941 0 : IF (ASSOCIATED(gauxc_section)) THEN
942 : CALL section_vals_val_get(gauxc_section, "ATOM_PARTITION", &
943 0 : i_val=native_grid_atom_partition)
944 : END IF
945 0 : SELECT CASE (native_grid_atom_partition)
946 : CASE (1)
947 0 : native_grid_atom_partition = skala_gpw_atom_partition_hard
948 : CASE (2)
949 0 : native_grid_atom_partition = skala_gpw_atom_partition_smooth
950 : CASE DEFAULT
951 : CALL cp_abort(__LOCATION__, &
952 0 : "Unknown SKALA_NATIVE%ATOM_PARTITION value.")
953 : END SELECT
954 0 : IF (native_grid_atom_partition == skala_gpw_atom_partition_hard) THEN
955 0 : native_grid_atom_partition = skala_gpw_atom_partition_smooth
956 : END IF
957 :
958 0 : entry_torch_use_cuda = torch_get_use_cuda()
959 0 : CALL torch_use_cuda(native_grid_use_cuda)
960 : selected_cuda_device = configure_native_grid_cuda( &
961 0 : native_grid_use_cuda, rho_r(1)%pw_grid%para%group)
962 0 : CALL ensure_model_loaded(model_path, selected_cuda_device, xc_section)
963 :
964 0 : IF (lsd) THEN
965 0 : needs%rho_spin = .TRUE.
966 0 : needs%drho_spin = .TRUE.
967 0 : needs%tau_spin = .TRUE.
968 : ELSE
969 0 : needs%rho = .TRUE.
970 0 : needs%drho = .TRUE.
971 0 : needs%tau = .TRUE.
972 : END IF
973 :
974 0 : CALL section_vals_val_get(xc_section, "XC_GRID%XC_DERIV", i_val=xc_deriv_method_id)
975 0 : CALL section_vals_val_get(xc_section, "XC_GRID%XC_SMOOTH_RHO", i_val=xc_rho_smooth_id)
976 :
977 : CALL xc_rho_set_create(rho_set, &
978 : rho_r(1)%pw_grid%bounds_local, &
979 : rho_cutoff=section_get_rval(xc_section, "density_cutoff"), &
980 : drho_cutoff=section_get_rval(xc_section, "gradient_cutoff"), &
981 0 : tau_cutoff=section_get_rval(xc_section, "tau_cutoff"))
982 : CALL xc_rho_set_update(rho_set, rho_r, rho_g, tau, needs, &
983 0 : xc_deriv_method_id, xc_rho_smooth_id, pw_pool)
984 :
985 : CALL skala_gpw_feature_build(features, rho_set, rho_r, particle_set, cell, &
986 : requires_grad=.FALSE., weights=weights, &
987 : requires_coordinate_grad=.FALSE., &
988 : requires_stress_grad=.TRUE., &
989 : use_atom_chunks=.FALSE., route_atom_chunks=.FALSE., &
990 : atom_partition=native_grid_atom_partition, &
991 0 : cuda_device=selected_cuda_device)
992 : CALL skala_torch_model_get_exc(cached_model, features%inputs, features%grid_weights_t, &
993 0 : exc_tensor, exc)
994 0 : CALL torch_tensor_backward_scalar(exc_tensor)
995 0 : NULLIFY (grid_weight_grad)
996 0 : CALL torch_tensor_grad(features%grid_weights_t, grid_weight_grad_t)
997 0 : CALL torch_tensor_data_ptr(grid_weight_grad_t, grid_weight_grad)
998 :
999 0 : natom = SIZE(particle_set)
1000 0 : IF (native_grid_atom_partition == skala_gpw_atom_partition_smooth) THEN
1001 : ALLOCATE (atom_coords_pbc(3, natom), included(natom), partition_weights(natom), &
1002 0 : dweights_datom(3, natom, natom), dweights_dstrain(3, 3, natom))
1003 0 : DO iatom = 1, natom
1004 0 : atom_coords_pbc(:, iatom) = pbc(particle_set(iatom)%r, cell, positive_range=.TRUE.)
1005 : END DO
1006 : END IF
1007 :
1008 0 : bo = rho_r(1)%pw_grid%bounds_local
1009 0 : local_row = 0
1010 0 : DO k = bo(1, 3), bo(2, 3)
1011 0 : DO j = bo(1, 2), bo(2, 2)
1012 0 : DO i = bo(1, 1), bo(2, 1)
1013 0 : local_row = local_row + 1
1014 0 : feature_begin = features%local_feature_offsets(local_row)
1015 0 : feature_end = features%local_feature_offsets(local_row + 1) - 1
1016 0 : local_derivative = 0.0_dp
1017 0 : IF (native_grid_atom_partition == skala_gpw_atom_partition_hard) THEN
1018 0 : CPASSERT(feature_end == feature_begin)
1019 0 : row = features%local_feature_rows(feature_begin)
1020 0 : local_derivative = grid_weight_grad(row)
1021 : ELSE
1022 0 : grid_point = native_grid_coordinate(rho_r(1)%pw_grid, [i, j, k])
1023 : CALL skala_gpw_smooth_partition_derivatives( &
1024 : grid_point, atom_coords_pbc, cell, partition_weights, included, &
1025 0 : dweights_datom, dweights_dstrain)
1026 0 : CPASSERT(feature_end - feature_begin + 1 == COUNT(included))
1027 0 : feature_pos = feature_begin
1028 0 : DO iatom = 1, natom
1029 0 : IF (.NOT. included(iatom)) CYCLE
1030 0 : row = features%local_feature_rows(feature_pos)
1031 : local_derivative = local_derivative + &
1032 0 : partition_weights(iatom)*grid_weight_grad(row)
1033 0 : feature_pos = feature_pos + 1
1034 : END DO
1035 0 : CPASSERT(feature_pos == feature_end + 1)
1036 : END IF
1037 0 : weight_deriv_r%array(i, j, k) = local_derivative
1038 : END DO
1039 : END DO
1040 : END DO
1041 0 : CPASSERT(local_row == features%nflat_local)
1042 :
1043 0 : IF (ALLOCATED(atom_coords_pbc)) THEN
1044 0 : DEALLOCATE (atom_coords_pbc, dweights_datom, dweights_dstrain, included, &
1045 0 : partition_weights)
1046 : END IF
1047 0 : CALL torch_tensor_release(grid_weight_grad_t)
1048 0 : CALL torch_tensor_release(exc_tensor)
1049 0 : CALL skala_gpw_feature_release(features)
1050 0 : CALL xc_rho_set_release(rho_set, pw_pool=pw_pool)
1051 0 : CALL torch_use_cuda(entry_torch_use_cuda)
1052 0 : CALL omp_set_num_threads(omp_max_threads_restore)
1053 :
1054 0 : END SUBROUTINE skala_gpw_weight_derivative
1055 :
1056 : ! **************************************************************************************************
1057 : !> \brief Evaluate SKALA on a GAPW one-center atomic grid.
1058 : !> \param xc_section ...
1059 : !> \param grid_atom ...
1060 : !> \param group ...
1061 : !> \param atom_coord ...
1062 : !> \param rho ...
1063 : !> \param drho ...
1064 : !> \param tau ...
1065 : !> \param weights ...
1066 : !> \param lsd ...
1067 : !> \param nspins ...
1068 : !> \param na ...
1069 : !> \param nr ...
1070 : !> \param exc ...
1071 : !> \param vxc ...
1072 : !> \param vxg ...
1073 : !> \param vtau ...
1074 : !> \param energy_only ...
1075 : !> \param atom_force ...
1076 : !> \param atom_virial ...
1077 : ! **************************************************************************************************
1078 4 : SUBROUTINE skala_gapw_atom_vxc_of_r(xc_section, grid_atom, group, atom_coord, &
1079 4 : rho, drho, tau, weights, lsd, nspins, na, nr, &
1080 : exc, vxc, vxg, vtau, energy_only, atom_force, atom_virial)
1081 : TYPE(section_vals_type), POINTER :: xc_section
1082 : TYPE(grid_atom_type), POINTER :: grid_atom
1083 :
1084 : CLASS(mp_comm_type), INTENT(IN) :: group
1085 : REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: atom_coord
1086 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: rho, tau, vxc, vtau
1087 : REAL(KIND=dp), DIMENSION(:, :, :, :), POINTER :: drho, vxg
1088 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: weights
1089 : LOGICAL, INTENT(IN) :: lsd
1090 : INTEGER, INTENT(IN) :: nspins, na, nr
1091 : REAL(KIND=dp), INTENT(OUT) :: exc
1092 : LOGICAL, INTENT(IN), OPTIONAL :: energy_only
1093 : REAL(KIND=dp), DIMENSION(3), INTENT(OUT), &
1094 : OPTIONAL :: atom_force
1095 : REAL(KIND=dp), DIMENSION(3, 3), INTENT(OUT), &
1096 : OPTIONAL :: atom_virial
1097 :
1098 : CHARACTER(len=default_path_length) :: model_path
1099 : INTEGER :: ia, idir, ir, &
1100 : jdir, nflat, omp_max_threads_restore, row, &
1101 : selected_cuda_device
1102 4 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:) :: atomic_grid_sizes
1103 4 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:, :) :: atomic_grid_size_bound_shape
1104 : LOGICAL :: need_coord_grad, my_energy_only, native_grid_use_cuda
1105 : REAL(KIND=dp) :: tmp
1106 4 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: atomic_grid_weights, grid_weights
1107 4 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: coarse_0_atomic_coords, density, &
1108 4 : grid_coords, kin
1109 4 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: grad
1110 4 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: atom_coord_grad, density_grad, &
1111 4 : grid_coord_grad, kin_grad
1112 4 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: grad_grad
1113 : TYPE(torch_dict_type) :: inputs
1114 : TYPE(torch_tensor_type) :: atomic_grid_size_bound_shape_t, &
1115 : atomic_grid_sizes_t, &
1116 : atomic_grid_weights_t, &
1117 : atom_coord_grad_t, &
1118 : coarse_0_atomic_coords_t, density_t, &
1119 : density_grad_t, exc_tensor, grad_t, &
1120 : grad_grad_t, grid_coord_grad_t, &
1121 : grid_coords_t, grid_weights_t, kin_t, &
1122 : kin_grad_t
1123 :
1124 0 : CPASSERT(ASSOCIATED(xc_section))
1125 4 : CPASSERT(ASSOCIATED(grid_atom))
1126 4 : CPASSERT(ASSOCIATED(rho))
1127 4 : CPASSERT(ASSOCIATED(drho))
1128 4 : CPASSERT(ASSOCIATED(tau))
1129 4 : omp_max_threads_restore = omp_get_max_threads()
1130 :
1131 4 : my_energy_only = .FALSE.
1132 4 : IF (PRESENT(energy_only)) my_energy_only = energy_only
1133 4 : need_coord_grad = PRESENT(atom_force) .OR. PRESENT(atom_virial)
1134 4 : exc = 0.0_dp
1135 4 : IF (PRESENT(atom_force)) atom_force = 0.0_dp
1136 4 : IF (PRESENT(atom_virial)) atom_virial = 0.0_dp
1137 4 : IF (.NOT. my_energy_only) THEN
1138 10208 : vxc = 0.0_dp
1139 40208 : vxg = 0.0_dp
1140 10208 : vtau = 0.0_dp
1141 : END IF
1142 :
1143 4 : CALL get_skala_model_path(xc_section, model_path)
1144 4 : native_grid_use_cuda = skala_execution_use_cuda(xc_section)
1145 4 : entry_torch_use_cuda = torch_get_use_cuda()
1146 4 : CALL torch_use_cuda(native_grid_use_cuda)
1147 : selected_cuda_device = configure_native_grid_cuda( &
1148 4 : native_grid_use_cuda, group)
1149 4 : CALL ensure_model_loaded(model_path, selected_cuda_device, xc_section)
1150 :
1151 4 : nflat = na*nr
1152 : ALLOCATE (density(nflat, 2), grad(nflat, 3, 2), kin(nflat, 2), &
1153 : grid_coords(3, nflat), grid_weights(nflat), &
1154 : atomic_grid_weights(nflat), atomic_grid_sizes(1), &
1155 52 : coarse_0_atomic_coords(3, 1), atomic_grid_size_bound_shape(0, nflat))
1156 4 : density = 0.0_dp
1157 4 : grad = 0.0_dp
1158 4 : kin = 0.0_dp
1159 4 : grid_coords = 0.0_dp
1160 4 : grid_weights = 0.0_dp
1161 4 : atomic_grid_weights = 0.0_dp
1162 4 : atomic_grid_sizes(1) = INT(nflat, KIND=int_8)
1163 : atomic_grid_size_bound_shape = 0_int_8
1164 16 : coarse_0_atomic_coords(:, 1) = atom_coord
1165 :
1166 : row = 0
1167 204 : DO ir = 1, nr
1168 10204 : DO ia = 1, na
1169 10000 : row = row + 1
1170 : grid_coords(1, row) = atom_coord(1) + grid_atom%rad(ir)* &
1171 10000 : grid_atom%sin_pol(ia)*grid_atom%cos_azi(ia)
1172 : grid_coords(2, row) = atom_coord(2) + grid_atom%rad(ir)* &
1173 10000 : grid_atom%sin_pol(ia)*grid_atom%sin_azi(ia)
1174 10000 : grid_coords(3, row) = atom_coord(3) + grid_atom%rad(ir)*grid_atom%cos_pol(ia)
1175 10000 : grid_weights(row) = weights(ia, ir)
1176 10000 : atomic_grid_weights(row) = weights(ia, ir)
1177 10200 : IF (nspins == 1) THEN
1178 30000 : density(row, :) = 0.5_dp*rho(ia, ir, 1)
1179 40000 : DO idir = 1, 3
1180 100000 : grad(row, idir, :) = 0.5_dp*drho(idir, ia, ir, 1)
1181 : END DO
1182 30000 : kin(row, :) = 0.5_dp*tau(ia, ir, 1)
1183 : ELSE
1184 0 : density(row, :) = rho(ia, ir, 1:2)
1185 0 : DO idir = 1, 3
1186 0 : grad(row, idir, :) = drho(idir, ia, ir, 1:2)
1187 : END DO
1188 0 : kin(row, :) = tau(ia, ir, 1:2)
1189 : END IF
1190 : END DO
1191 : END DO
1192 :
1193 4 : CALL torch_tensor_from_array(grid_coords_t, grid_coords)
1194 4 : CALL torch_tensor_to_device_leaf(grid_coords_t, need_coord_grad)
1195 4 : CALL torch_tensor_from_array(grid_weights_t, grid_weights)
1196 4 : CALL torch_tensor_to_device_leaf(grid_weights_t, .FALSE.)
1197 4 : CALL torch_tensor_from_array(atomic_grid_weights_t, atomic_grid_weights)
1198 4 : CALL torch_tensor_to_device_leaf(atomic_grid_weights_t, .FALSE.)
1199 4 : CALL torch_tensor_from_array(atomic_grid_sizes_t, atomic_grid_sizes)
1200 4 : CALL torch_tensor_to_device_leaf(atomic_grid_sizes_t, .FALSE.)
1201 : CALL torch_tensor_from_array(atomic_grid_size_bound_shape_t, &
1202 4 : atomic_grid_size_bound_shape)
1203 4 : CALL torch_tensor_to_device_leaf(atomic_grid_size_bound_shape_t, .FALSE.)
1204 4 : CALL torch_tensor_from_array(coarse_0_atomic_coords_t, coarse_0_atomic_coords)
1205 4 : CALL torch_tensor_to_device_leaf(coarse_0_atomic_coords_t, need_coord_grad)
1206 4 : CALL torch_tensor_from_array(density_t, density)
1207 4 : CALL torch_tensor_to_device_leaf(density_t,.NOT. my_energy_only)
1208 4 : CALL torch_tensor_from_array(grad_t, grad)
1209 4 : CALL torch_tensor_to_device_leaf(grad_t,.NOT. my_energy_only)
1210 4 : CALL torch_tensor_from_array(kin_t, kin)
1211 4 : CALL torch_tensor_to_device_leaf(kin_t,.NOT. my_energy_only)
1212 :
1213 4 : CALL torch_dict_create(inputs)
1214 4 : CALL torch_dict_insert(inputs, "grid_coords", grid_coords_t)
1215 4 : CALL torch_dict_insert(inputs, "grid_weights", grid_weights_t)
1216 4 : CALL torch_dict_insert(inputs, "atomic_grid_weights", atomic_grid_weights_t)
1217 4 : CALL torch_dict_insert(inputs, "atomic_grid_sizes", atomic_grid_sizes_t)
1218 : CALL torch_dict_insert(inputs, "atomic_grid_size_bound_shape", &
1219 4 : atomic_grid_size_bound_shape_t)
1220 4 : CALL torch_dict_insert(inputs, "density", density_t)
1221 4 : CALL torch_dict_insert(inputs, "grad", grad_t)
1222 4 : CALL torch_dict_insert(inputs, "kin", kin_t)
1223 4 : CALL torch_dict_insert(inputs, "coarse_0_atomic_coords", coarse_0_atomic_coords_t)
1224 :
1225 4 : CALL skala_torch_model_get_exc(cached_model, inputs, grid_weights_t, exc_tensor, exc)
1226 :
1227 4 : IF (.NOT. my_energy_only) THEN
1228 4 : NULLIFY (atom_coord_grad, density_grad, grad_grad, grid_coord_grad, kin_grad)
1229 4 : CALL torch_tensor_backward_scalar(exc_tensor)
1230 4 : IF (need_coord_grad) THEN
1231 4 : CALL torch_tensor_grad(grid_coords_t, grid_coord_grad_t)
1232 4 : CALL torch_tensor_grad(coarse_0_atomic_coords_t, atom_coord_grad_t)
1233 4 : CALL torch_tensor_data_ptr(grid_coord_grad_t, grid_coord_grad)
1234 4 : CALL torch_tensor_data_ptr(atom_coord_grad_t, atom_coord_grad)
1235 4 : IF (PRESENT(atom_force)) THEN
1236 16 : atom_force(:) = atom_coord_grad(:, 1)
1237 10004 : DO row = 1, nflat
1238 40004 : atom_force(:) = atom_force(:) + grid_coord_grad(:, row)
1239 : END DO
1240 : END IF
1241 4 : IF (PRESENT(atom_virial)) THEN
1242 10004 : DO row = 1, nflat
1243 40004 : DO idir = 1, 3
1244 130000 : DO jdir = 1, 3
1245 90000 : tmp = grid_coord_grad(idir, row)*coarse_0_atomic_coords(jdir, 1)
1246 120000 : atom_virial(idir, jdir) = atom_virial(idir, jdir) + tmp
1247 : END DO
1248 : END DO
1249 : END DO
1250 16 : DO idir = 1, 3
1251 52 : DO jdir = 1, 3
1252 36 : tmp = atom_coord_grad(idir, 1)*coarse_0_atomic_coords(jdir, 1)
1253 48 : atom_virial(idir, jdir) = atom_virial(idir, jdir) + tmp
1254 : END DO
1255 : END DO
1256 : END IF
1257 : END IF
1258 : CALL torch_tensor_grad_batch3(density_t, grad_t, kin_t, density_grad_t, &
1259 4 : grad_grad_t, kin_grad_t)
1260 4 : CALL torch_tensor_data_ptr(density_grad_t, density_grad)
1261 4 : CALL torch_tensor_data_ptr(grad_grad_t, grad_grad)
1262 4 : CALL torch_tensor_data_ptr(kin_grad_t, kin_grad)
1263 :
1264 4 : row = 0
1265 204 : DO ir = 1, nr
1266 10204 : DO ia = 1, na
1267 10000 : row = row + 1
1268 10200 : IF (lsd) THEN
1269 0 : vxc(ia, ir, 1:2) = density_grad(row, 1:2)
1270 0 : DO idir = 1, 3
1271 0 : vxg(idir, ia, ir, 1:2) = grad_grad(row, idir, 1:2)
1272 : END DO
1273 0 : vtau(ia, ir, 1:2) = kin_grad(row, 1:2)
1274 : ELSE
1275 10000 : vxc(ia, ir, 1) = 0.5_dp*(density_grad(row, 1) + density_grad(row, 2))
1276 40000 : DO idir = 1, 3
1277 : vxg(idir, ia, ir, 1) = &
1278 40000 : 0.5_dp*(grad_grad(row, idir, 1) + grad_grad(row, idir, 2))
1279 : END DO
1280 10000 : vtau(ia, ir, 1) = 0.5_dp*(kin_grad(row, 1) + kin_grad(row, 2))
1281 : END IF
1282 : END DO
1283 : END DO
1284 :
1285 4 : CALL torch_tensor_release(density_grad_t)
1286 4 : CALL torch_tensor_release(grad_grad_t)
1287 4 : CALL torch_tensor_release(kin_grad_t)
1288 4 : IF (need_coord_grad) THEN
1289 4 : CALL torch_tensor_release(grid_coord_grad_t)
1290 4 : CALL torch_tensor_release(atom_coord_grad_t)
1291 : END IF
1292 : END IF
1293 :
1294 4 : CALL torch_tensor_release(exc_tensor)
1295 4 : CALL torch_tensor_release(density_t)
1296 4 : CALL torch_tensor_release(grad_t)
1297 4 : CALL torch_tensor_release(kin_t)
1298 4 : CALL torch_tensor_release(grid_coords_t)
1299 4 : CALL torch_tensor_release(grid_weights_t)
1300 4 : CALL torch_tensor_release(atomic_grid_weights_t)
1301 4 : CALL torch_tensor_release(atomic_grid_sizes_t)
1302 4 : CALL torch_tensor_release(atomic_grid_size_bound_shape_t)
1303 4 : CALL torch_tensor_release(coarse_0_atomic_coords_t)
1304 4 : CALL torch_dict_release(inputs)
1305 0 : DEALLOCATE (atomic_grid_size_bound_shape, atomic_grid_sizes, atomic_grid_weights, &
1306 4 : coarse_0_atomic_coords, density, grad, grid_coords, grid_weights, kin)
1307 4 : CALL torch_use_cuda(entry_torch_use_cuda)
1308 4 : CALL omp_set_num_threads(omp_max_threads_restore)
1309 :
1310 8 : END SUBROUTINE skala_gapw_atom_vxc_of_r
1311 :
1312 : ! **************************************************************************************************
1313 : !> \brief Evaluate a rank-local set of complete atom blocks and sum their SKALA energies.
1314 : !> \param xc_section ...
1315 : !> \param group ...
1316 : !> \param density ...
1317 : !> \param grad ...
1318 : !> \param kin ...
1319 : !> \param grid_coords ...
1320 : !> \param grid_weights ...
1321 : !> \param atomic_grid_weights ...
1322 : !> \param atomic_grid_sizes ...
1323 : !> \param atomic_coords ...
1324 : !> \param exc ...
1325 : !> \param density_grad_out ...
1326 : !> \param grad_grad_out ...
1327 : !> \param kin_grad_out ...
1328 : !> \param grid_coord_grad_out ...
1329 : !> \param grid_weight_grad_out ...
1330 : !> \param atomic_grid_weight_grad_out ...
1331 : !> \param atom_coord_grad_out ...
1332 : ! **************************************************************************************************
1333 254 : SUBROUTINE skala_gapw_atom_composite_energy(xc_section, group, density, grad, kin, &
1334 : grid_coords, grid_weights, atomic_grid_weights, &
1335 : atomic_grid_sizes, atomic_coords, exc, &
1336 : density_grad_out, grad_grad_out, kin_grad_out, &
1337 : grid_coord_grad_out, grid_weight_grad_out, &
1338 : atomic_grid_weight_grad_out, atom_coord_grad_out)
1339 : TYPE(section_vals_type), POINTER :: xc_section
1340 :
1341 : CLASS(mp_comm_type), INTENT(IN) :: group
1342 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), INTENT(IN) :: density, grid_coords, &
1343 : atomic_coords, kin
1344 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), INTENT(IN) :: grad
1345 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), INTENT(IN) :: grid_weights, atomic_grid_weights
1346 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:), INTENT(IN) :: atomic_grid_sizes
1347 : REAL(KIND=dp), INTENT(OUT) :: exc
1348 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), INTENT(OUT), OPTIONAL :: density_grad_out, &
1349 : kin_grad_out, &
1350 : grid_coord_grad_out, &
1351 : atom_coord_grad_out
1352 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), INTENT(OUT), OPTIONAL :: grad_grad_out
1353 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), INTENT(OUT), OPTIONAL :: grid_weight_grad_out, &
1354 : atomic_grid_weight_grad_out
1355 :
1356 : CHARACTER(len=default_path_length) :: model_path
1357 : INTEGER :: atom_begin, atom_count, chunk_max_rows, chunk_row_count, chunk_row_start, &
1358 : ichunk, local_natom, local_nrow, max_grid_size, &
1359 : nchunks, omp_max_threads_restore, phase_handle, selected_cuda_device
1360 254 : INTEGER, ALLOCATABLE, DIMENSION(:) :: chunk_atom_begin, chunk_atom_count, &
1361 254 : chunk_row_begin, chunk_row_counts
1362 254 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:, :) :: atomic_grid_size_bound_shape
1363 : LOGICAL :: active_rank, native_grid_atom_chunks, native_grid_use_cuda, &
1364 : need_coordinate_derivatives, need_derivatives
1365 : REAL(KIND=dp) :: chunk_exc
1366 254 : REAL(KIND=dp), DIMENSION(:), POINTER :: atomic_grid_weight_grad, grid_weight_grad
1367 254 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: atom_coord_grad, density_grad, &
1368 254 : grid_coord_grad, kin_grad
1369 254 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: grad_grad
1370 : TYPE(section_vals_type), POINTER :: gauxc_section
1371 : TYPE(torch_dict_type) :: inputs
1372 : TYPE(torch_tensor_type) :: atomic_grid_size_bound_shape_t, &
1373 : atomic_grid_sizes_t, &
1374 : atomic_grid_sizes_chunk_t, &
1375 : atomic_grid_weight_grad_t, &
1376 : atomic_grid_weights_chunk_t, &
1377 : atom_coord_grad_t, atomic_coords_chunk_t, &
1378 : density_chunk_t, density_grad_t, &
1379 : exc_tensor, grad_chunk_t, grad_grad_t, &
1380 : grid_coord_grad_t, grid_coords_chunk_t, &
1381 : grid_weight_grad_t, grid_weights_chunk_t, &
1382 : kin_chunk_t, kin_grad_t
1383 : TYPE(torch_tensor_type), TARGET :: atomic_coords_local_t, &
1384 : atomic_grid_weights_local_t, &
1385 : density_local_t, grad_local_t, &
1386 : grid_coords_local_t, grid_weights_local_t, &
1387 : kin_local_t
1388 : TYPE(torch_tensor_type), POINTER :: atomic_coords_t, atomic_grid_weights_t, &
1389 : density_t, grad_t, grid_coords_t, &
1390 : grid_weights_t, kin_t
1391 :
1392 0 : CPASSERT(ASSOCIATED(xc_section))
1393 254 : CPASSERT(SIZE(density, 1) == SIZE(grid_weights))
1394 254 : CPASSERT(SIZE(density, 1) == SIZE(atomic_grid_weights))
1395 254 : CPASSERT(SIZE(density, 1) == SIZE(grid_coords, 2))
1396 254 : CPASSERT(SIZE(density, 1) == SIZE(grad, 1))
1397 254 : CPASSERT(SIZE(density, 1) == SIZE(kin, 1))
1398 254 : CPASSERT(SIZE(density, 2) == 2)
1399 254 : CPASSERT(SIZE(grad, 2) == 3)
1400 254 : CPASSERT(SIZE(grad, 3) == 2)
1401 254 : CPASSERT(SIZE(kin, 2) == 2)
1402 254 : CPASSERT(SIZE(atomic_grid_sizes) == SIZE(atomic_coords, 2))
1403 509 : CPASSERT(SUM(atomic_grid_sizes) == INT(SIZE(density, 1), KIND=int_8))
1404 254 : need_derivatives = PRESENT(density_grad_out)
1405 254 : CPASSERT(PRESENT(grad_grad_out) .EQV. need_derivatives)
1406 254 : CPASSERT(PRESENT(kin_grad_out) .EQV. need_derivatives)
1407 254 : need_coordinate_derivatives = PRESENT(grid_coord_grad_out)
1408 254 : CPASSERT(PRESENT(grid_weight_grad_out) .EQV. need_coordinate_derivatives)
1409 254 : CPASSERT(PRESENT(atomic_grid_weight_grad_out) .EQV. need_coordinate_derivatives)
1410 254 : CPASSERT(PRESENT(atom_coord_grad_out) .EQV. need_coordinate_derivatives)
1411 254 : CPASSERT((.NOT. need_coordinate_derivatives) .OR. need_derivatives)
1412 254 : omp_max_threads_restore = omp_get_max_threads()
1413 :
1414 254 : local_nrow = SIZE(density, 1)
1415 254 : local_natom = SIZE(atomic_grid_sizes)
1416 254 : active_rank = local_natom > 0
1417 254 : CPASSERT(active_rank .EQV. (local_nrow > 0))
1418 :
1419 254 : CALL get_skala_model_path(xc_section, model_path)
1420 254 : native_grid_use_cuda = skala_execution_use_cuda(xc_section)
1421 254 : native_grid_atom_chunks = .TRUE.
1422 254 : chunk_max_rows = -1
1423 254 : gauxc_section => get_skala_native_section(xc_section)
1424 254 : IF (ASSOCIATED(gauxc_section)) THEN
1425 : CALL section_vals_val_get(gauxc_section, "ATOM_CHUNKS", &
1426 254 : l_val=native_grid_atom_chunks)
1427 : CALL section_vals_val_get(gauxc_section, "ATOM_CHUNK_MAX_ROWS", &
1428 254 : i_val=chunk_max_rows)
1429 : END IF
1430 254 : IF (chunk_max_rows < -1) THEN
1431 : CALL cp_abort(__LOCATION__, &
1432 0 : "SKALA_NATIVE%ATOM_CHUNK_MAX_ROWS must be -1, zero, or positive.")
1433 : END IF
1434 254 : IF (.NOT. native_grid_atom_chunks) chunk_max_rows = 0
1435 254 : IF (chunk_max_rows == -1) THEN
1436 244 : chunk_max_rows = auto_atom_composite_chunk_max_rows(atomic_grid_sizes, group)
1437 : END IF
1438 254 : entry_torch_use_cuda = torch_get_use_cuda()
1439 254 : CALL torch_use_cuda(native_grid_use_cuda)
1440 : selected_cuda_device = configure_native_grid_cuda( &
1441 254 : native_grid_use_cuda, group)
1442 254 : exc = 0.0_dp
1443 254 : IF (active_rank) THEN
1444 232 : CALL timeset("skala_atom_tensor_setup", phase_handle)
1445 232 : CALL ensure_model_loaded(model_path, selected_cuda_device, xc_section)
1446 : CALL atom_composite_chunk_layout(atomic_grid_sizes, chunk_max_rows, &
1447 : chunk_atom_begin, chunk_atom_count, &
1448 232 : chunk_row_begin, chunk_row_counts)
1449 232 : nchunks = SIZE(chunk_atom_begin)
1450 232 : CPASSERT(nchunks > 0)
1451 :
1452 232 : IF (native_grid_use_cuda) THEN
1453 0 : grid_coords_t => cached_atom_cuda_tensors%grid_coords_t
1454 0 : grid_weights_t => cached_atom_cuda_tensors%grid_weights_t
1455 0 : atomic_grid_weights_t => cached_atom_cuda_tensors%atomic_grid_weights_t
1456 0 : atomic_coords_t => cached_atom_cuda_tensors%atomic_coords_t
1457 0 : density_t => cached_atom_cuda_tensors%density_t
1458 0 : grad_t => cached_atom_cuda_tensors%grad_t
1459 0 : kin_t => cached_atom_cuda_tensors%kin_t
1460 : CALL torch_tensor_reset_from_array( &
1461 0 : grid_coords_t, grid_coords, need_coordinate_derivatives)
1462 : CALL torch_tensor_reset_from_array( &
1463 0 : grid_weights_t, grid_weights, need_coordinate_derivatives)
1464 : CALL torch_tensor_reset_from_array( &
1465 0 : atomic_grid_weights_t, atomic_grid_weights, need_coordinate_derivatives)
1466 : CALL torch_tensor_reset_from_array( &
1467 0 : atomic_coords_t, atomic_coords, need_coordinate_derivatives)
1468 0 : CALL torch_tensor_reset_from_array(density_t, density, need_derivatives)
1469 0 : CALL torch_tensor_reset_from_array(grad_t, grad, need_derivatives)
1470 0 : CALL torch_tensor_reset_from_array(kin_t, kin, need_derivatives)
1471 0 : cached_atom_cuda_tensors_active = .TRUE.
1472 : ELSE
1473 232 : grid_coords_t => grid_coords_local_t
1474 232 : grid_weights_t => grid_weights_local_t
1475 232 : atomic_grid_weights_t => atomic_grid_weights_local_t
1476 232 : atomic_coords_t => atomic_coords_local_t
1477 232 : density_t => density_local_t
1478 232 : grad_t => grad_local_t
1479 232 : kin_t => kin_local_t
1480 232 : CALL torch_tensor_from_array(grid_coords_t, grid_coords)
1481 232 : CALL torch_tensor_to_device_leaf(grid_coords_t, need_coordinate_derivatives)
1482 232 : CALL torch_tensor_from_array(grid_weights_t, grid_weights)
1483 232 : CALL torch_tensor_to_device_leaf(grid_weights_t, need_coordinate_derivatives)
1484 232 : CALL torch_tensor_from_array(atomic_grid_weights_t, atomic_grid_weights)
1485 232 : CALL torch_tensor_to_device_leaf(atomic_grid_weights_t, need_coordinate_derivatives)
1486 232 : CALL torch_tensor_from_array(atomic_coords_t, atomic_coords)
1487 232 : CALL torch_tensor_to_device_leaf(atomic_coords_t, need_coordinate_derivatives)
1488 232 : CALL torch_tensor_from_array(density_t, density)
1489 232 : CALL torch_tensor_to_device_leaf(density_t, need_derivatives)
1490 232 : CALL torch_tensor_from_array(grad_t, grad)
1491 232 : CALL torch_tensor_to_device_leaf(grad_t, need_derivatives)
1492 232 : CALL torch_tensor_from_array(kin_t, kin)
1493 232 : CALL torch_tensor_to_device_leaf(kin_t, need_derivatives)
1494 : END IF
1495 232 : CALL torch_tensor_from_array(atomic_grid_sizes_t, atomic_grid_sizes)
1496 232 : CALL torch_tensor_to_device_leaf(atomic_grid_sizes_t, .FALSE.)
1497 232 : CALL timestop(phase_handle)
1498 :
1499 464 : DO ichunk = 1, nchunks
1500 232 : atom_begin = chunk_atom_begin(ichunk)
1501 232 : atom_count = chunk_atom_count(ichunk)
1502 232 : chunk_row_start = chunk_row_begin(ichunk)
1503 232 : chunk_row_count = chunk_row_counts(ichunk)
1504 : max_grid_size = INT(MAXVAL(atomic_grid_sizes( &
1505 487 : atom_begin:atom_begin + atom_count - 1)))
1506 464 : ALLOCATE (atomic_grid_size_bound_shape(0, max_grid_size))
1507 : atomic_grid_size_bound_shape = 0_int_8
1508 :
1509 : CALL torch_tensor_narrow(grid_coords_t, 0, chunk_row_start - 1, &
1510 232 : chunk_row_count, grid_coords_chunk_t)
1511 : CALL torch_tensor_narrow(grid_weights_t, 0, chunk_row_start - 1, &
1512 232 : chunk_row_count, grid_weights_chunk_t)
1513 : CALL torch_tensor_narrow(atomic_grid_weights_t, 0, chunk_row_start - 1, &
1514 232 : chunk_row_count, atomic_grid_weights_chunk_t)
1515 : CALL torch_tensor_narrow(atomic_grid_sizes_t, 0, atom_begin - 1, &
1516 232 : atom_count, atomic_grid_sizes_chunk_t)
1517 : CALL torch_tensor_narrow(atomic_coords_t, 0, atom_begin - 1, &
1518 232 : atom_count, atomic_coords_chunk_t)
1519 : CALL torch_tensor_narrow(density_t, 1, chunk_row_start - 1, &
1520 232 : chunk_row_count, density_chunk_t)
1521 : CALL torch_tensor_narrow(grad_t, 2, chunk_row_start - 1, &
1522 232 : chunk_row_count, grad_chunk_t)
1523 : CALL torch_tensor_narrow(kin_t, 1, chunk_row_start - 1, &
1524 232 : chunk_row_count, kin_chunk_t)
1525 : CALL torch_tensor_from_array(atomic_grid_size_bound_shape_t, &
1526 232 : atomic_grid_size_bound_shape)
1527 232 : CALL torch_tensor_to_device_leaf(atomic_grid_size_bound_shape_t, .FALSE.)
1528 :
1529 232 : CALL torch_dict_create(inputs)
1530 232 : CALL torch_dict_insert(inputs, "grid_coords", grid_coords_chunk_t)
1531 232 : CALL torch_dict_insert(inputs, "grid_weights", grid_weights_chunk_t)
1532 232 : CALL torch_dict_insert(inputs, "atomic_grid_weights", atomic_grid_weights_chunk_t)
1533 232 : CALL torch_dict_insert(inputs, "atomic_grid_sizes", atomic_grid_sizes_chunk_t)
1534 : CALL torch_dict_insert(inputs, "atomic_grid_size_bound_shape", &
1535 232 : atomic_grid_size_bound_shape_t)
1536 232 : CALL torch_dict_insert(inputs, "density", density_chunk_t)
1537 232 : CALL torch_dict_insert(inputs, "grad", grad_chunk_t)
1538 232 : CALL torch_dict_insert(inputs, "kin", kin_chunk_t)
1539 232 : CALL torch_dict_insert(inputs, "coarse_0_atomic_coords", atomic_coords_chunk_t)
1540 :
1541 232 : CALL timeset("skala_atom_torch_forward", phase_handle)
1542 : CALL skala_torch_model_get_exc(cached_model, inputs, grid_weights_chunk_t, &
1543 232 : exc_tensor, chunk_exc)
1544 232 : CALL timestop(phase_handle)
1545 232 : exc = exc + chunk_exc
1546 232 : IF (need_derivatives) THEN
1547 232 : CALL timeset("skala_atom_torch_backward", phase_handle)
1548 232 : CALL torch_tensor_backward_scalar(exc_tensor)
1549 232 : CALL timestop(phase_handle)
1550 : END IF
1551 :
1552 232 : CALL torch_tensor_release(exc_tensor)
1553 232 : CALL torch_dict_release(inputs)
1554 232 : CALL torch_tensor_release(grid_coords_chunk_t)
1555 232 : CALL torch_tensor_release(grid_weights_chunk_t)
1556 232 : CALL torch_tensor_release(atomic_grid_weights_chunk_t)
1557 232 : CALL torch_tensor_release(atomic_grid_sizes_chunk_t)
1558 232 : CALL torch_tensor_release(atomic_coords_chunk_t)
1559 232 : CALL torch_tensor_release(density_chunk_t)
1560 232 : CALL torch_tensor_release(grad_chunk_t)
1561 232 : CALL torch_tensor_release(kin_chunk_t)
1562 232 : CALL torch_tensor_release(atomic_grid_size_bound_shape_t)
1563 696 : DEALLOCATE (atomic_grid_size_bound_shape)
1564 : END DO
1565 : END IF
1566 254 : CALL group%sum(exc)
1567 :
1568 254 : IF (need_derivatives) THEN
1569 0 : ALLOCATE (density_grad_out(local_nrow, 2), grad_grad_out(local_nrow, 3, 2), &
1570 1712 : kin_grad_out(local_nrow, 2))
1571 254 : density_grad_out = 0.0_dp
1572 254 : grad_grad_out = 0.0_dp
1573 254 : kin_grad_out = 0.0_dp
1574 254 : IF (active_rank) THEN
1575 232 : NULLIFY (density_grad, grad_grad, kin_grad)
1576 232 : CALL timeset("skala_atom_grad_fetch", phase_handle)
1577 : CALL torch_tensor_grad_batch3(density_t, grad_t, kin_t, density_grad_t, &
1578 232 : grad_grad_t, kin_grad_t)
1579 232 : CALL torch_tensor_data_ptr(density_grad_t, density_grad)
1580 232 : CALL torch_tensor_data_ptr(grad_grad_t, grad_grad)
1581 232 : CALL torch_tensor_data_ptr(kin_grad_t, kin_grad)
1582 552048 : density_grad_out(:, :) = density_grad
1583 1656144 : grad_grad_out(:, :, :) = grad_grad
1584 552048 : kin_grad_out(:, :) = kin_grad
1585 232 : CALL timestop(phase_handle)
1586 : END IF
1587 :
1588 254 : IF (need_coordinate_derivatives) THEN
1589 58 : IF (active_rank) CALL timeset("skala_atom_coord_grad_fetch", phase_handle)
1590 0 : ALLOCATE (grid_coord_grad_out(3, local_nrow), grid_weight_grad_out(local_nrow), &
1591 420 : atomic_grid_weight_grad_out(local_nrow), atom_coord_grad_out(3, local_natom))
1592 58 : grid_coord_grad_out = 0.0_dp
1593 58 : grid_weight_grad_out = 0.0_dp
1594 58 : atomic_grid_weight_grad_out = 0.0_dp
1595 58 : atom_coord_grad_out = 0.0_dp
1596 58 : IF (active_rank) THEN
1597 47 : NULLIFY (atomic_grid_weight_grad, atom_coord_grad, grid_coord_grad, grid_weight_grad)
1598 47 : CALL torch_tensor_grad(grid_coords_t, grid_coord_grad_t)
1599 47 : CALL torch_tensor_grad(grid_weights_t, grid_weight_grad_t)
1600 47 : CALL torch_tensor_grad(atomic_grid_weights_t, atomic_grid_weight_grad_t)
1601 47 : CALL torch_tensor_grad(atomic_coords_t, atom_coord_grad_t)
1602 47 : CALL torch_tensor_data_ptr(grid_coord_grad_t, grid_coord_grad)
1603 47 : CALL torch_tensor_data_ptr(grid_weight_grad_t, grid_weight_grad)
1604 47 : CALL torch_tensor_data_ptr(atomic_grid_weight_grad_t, atomic_grid_weight_grad)
1605 47 : CALL torch_tensor_data_ptr(atom_coord_grad_t, atom_coord_grad)
1606 390191 : grid_coord_grad_out(:, :) = grid_coord_grad
1607 97583 : grid_weight_grad_out(:) = grid_weight_grad
1608 97583 : atomic_grid_weight_grad_out(:) = atomic_grid_weight_grad
1609 279 : atom_coord_grad_out(:, :) = atom_coord_grad
1610 47 : CALL torch_tensor_release(grid_coord_grad_t)
1611 47 : CALL torch_tensor_release(grid_weight_grad_t)
1612 47 : CALL torch_tensor_release(atomic_grid_weight_grad_t)
1613 47 : CALL torch_tensor_release(atom_coord_grad_t)
1614 47 : CALL timestop(phase_handle)
1615 : END IF
1616 : END IF
1617 243 : IF (active_rank) THEN
1618 232 : CALL torch_tensor_release(density_grad_t)
1619 232 : CALL torch_tensor_release(grad_grad_t)
1620 232 : CALL torch_tensor_release(kin_grad_t)
1621 : END IF
1622 : END IF
1623 :
1624 243 : IF (active_rank) THEN
1625 232 : CALL timeset("skala_atom_tensor_release", phase_handle)
1626 232 : IF (.NOT. native_grid_use_cuda) THEN
1627 232 : CALL torch_tensor_release(density_t)
1628 232 : CALL torch_tensor_release(grad_t)
1629 232 : CALL torch_tensor_release(kin_t)
1630 232 : CALL torch_tensor_release(grid_coords_t)
1631 232 : CALL torch_tensor_release(grid_weights_t)
1632 232 : CALL torch_tensor_release(atomic_grid_weights_t)
1633 232 : CALL torch_tensor_release(atomic_coords_t)
1634 : END IF
1635 232 : CALL torch_tensor_release(atomic_grid_sizes_t)
1636 232 : DEALLOCATE (chunk_atom_begin, chunk_atom_count, chunk_row_begin, chunk_row_counts)
1637 232 : CALL timestop(phase_handle)
1638 : END IF
1639 254 : CALL torch_use_cuda(entry_torch_use_cuda)
1640 254 : CALL omp_set_num_threads(omp_max_threads_restore)
1641 :
1642 508 : END SUBROUTINE skala_gapw_atom_composite_energy
1643 :
1644 : ! **************************************************************************************************
1645 : !> \brief Select an automatic row cap for atom-composite Torch calls.
1646 : !> \param atomic_grid_sizes ...
1647 : !> \param group ...
1648 : !> \return ...
1649 : ! **************************************************************************************************
1650 244 : FUNCTION auto_atom_composite_chunk_max_rows(atomic_grid_sizes, group) RESULT(max_rows)
1651 : INTEGER(KIND=int_8), DIMENSION(:), INTENT(IN) :: atomic_grid_sizes
1652 :
1653 : CLASS(mp_comm_type), INTENT(IN) :: group
1654 :
1655 : INTEGER :: local_max_atom_rows, local_natom, &
1656 : local_padded_rows, max_rows
1657 :
1658 244 : local_padded_rows = 0
1659 244 : local_natom = SIZE(atomic_grid_sizes)
1660 244 : IF (local_natom > 0) THEN
1661 467 : local_max_atom_rows = INT(MAXVAL(atomic_grid_sizes))
1662 222 : IF (local_natom > atom_chunk_auto_max_rows .OR. &
1663 : local_max_atom_rows > atom_chunk_auto_max_rows/local_natom) THEN
1664 0 : local_padded_rows = atom_chunk_auto_max_rows + 1
1665 : ELSE
1666 222 : local_padded_rows = local_natom*local_max_atom_rows
1667 : END IF
1668 : END IF
1669 244 : CALL group%max(local_padded_rows)
1670 244 : IF (local_padded_rows <= atom_chunk_auto_max_rows) THEN
1671 244 : max_rows = 0
1672 244 : RETURN
1673 : END IF
1674 244 : max_rows = atom_chunk_auto_max_rows
1675 :
1676 : END FUNCTION auto_atom_composite_chunk_max_rows
1677 :
1678 : ! **************************************************************************************************
1679 : !> \brief Split contiguous atom-composite rows without dividing an atomic block.
1680 : !> \param atomic_grid_sizes ...
1681 : !> \param max_rows ...
1682 : !> \param atom_begin ...
1683 : !> \param atom_count ...
1684 : !> \param row_begin ...
1685 : !> \param row_count ...
1686 : ! **************************************************************************************************
1687 232 : SUBROUTINE atom_composite_chunk_layout(atomic_grid_sizes, max_rows, atom_begin, atom_count, &
1688 : row_begin, row_count)
1689 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:), &
1690 : INTENT(IN) :: atomic_grid_sizes
1691 : INTEGER, INTENT(IN) :: max_rows
1692 : INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: atom_begin, atom_count, row_begin, &
1693 : row_count
1694 :
1695 : INTEGER :: atom_begin_tmp, atom_count_tmp, atom_rows, chunk_atoms, chunk_max_atom_rows, &
1696 : iatom, insert_at, natom, nchunks, padded_rows_tmp, row_begin_tmp, row_count_tmp, rows, &
1697 : subchunk
1698 232 : INTEGER, ALLOCATABLE, DIMENSION(:) :: padded_rows
1699 :
1700 232 : natom = SIZE(atomic_grid_sizes)
1701 232 : CPASSERT(natom > 0)
1702 487 : CPASSERT(ALL(atomic_grid_sizes > 0_int_8))
1703 232 : IF (max_rows <= 0) THEN
1704 222 : ALLOCATE (atom_begin(1), atom_count(1), row_begin(1), row_count(1))
1705 222 : atom_begin(1) = 1
1706 222 : atom_count(1) = natom
1707 222 : row_begin(1) = 1
1708 467 : row_count(1) = INT(SUM(atomic_grid_sizes))
1709 : RETURN
1710 : END IF
1711 :
1712 : nchunks = 1
1713 : chunk_atoms = 0
1714 : chunk_max_atom_rows = 0
1715 20 : DO iatom = 1, natom
1716 10 : atom_rows = INT(atomic_grid_sizes(iatom))
1717 10 : IF (chunk_atoms > 0 .AND. &
1718 : MAX(chunk_max_atom_rows, atom_rows) > max_rows/(chunk_atoms + 1)) THEN
1719 0 : nchunks = nchunks + 1
1720 0 : chunk_atoms = 0
1721 0 : chunk_max_atom_rows = 0
1722 : END IF
1723 10 : chunk_atoms = chunk_atoms + 1
1724 20 : chunk_max_atom_rows = MAX(chunk_max_atom_rows, atom_rows)
1725 : END DO
1726 60 : ALLOCATE (atom_begin(nchunks), atom_count(nchunks), row_begin(nchunks), row_count(nchunks))
1727 :
1728 10 : subchunk = 1
1729 10 : atom_begin(subchunk) = 1
1730 10 : row_begin(subchunk) = 1
1731 10 : chunk_atoms = 0
1732 10 : chunk_max_atom_rows = 0
1733 10 : rows = 0
1734 20 : DO iatom = 1, natom
1735 10 : atom_rows = INT(atomic_grid_sizes(iatom))
1736 10 : IF (chunk_atoms > 0 .AND. &
1737 : MAX(chunk_max_atom_rows, atom_rows) > max_rows/(chunk_atoms + 1)) THEN
1738 0 : atom_count(subchunk) = iatom - atom_begin(subchunk)
1739 0 : row_count(subchunk) = rows
1740 0 : subchunk = subchunk + 1
1741 0 : atom_begin(subchunk) = iatom
1742 0 : row_begin(subchunk) = row_begin(subchunk - 1) + row_count(subchunk - 1)
1743 0 : chunk_atoms = 0
1744 0 : chunk_max_atom_rows = 0
1745 0 : rows = 0
1746 : END IF
1747 10 : chunk_atoms = chunk_atoms + 1
1748 10 : chunk_max_atom_rows = MAX(chunk_max_atom_rows, atom_rows)
1749 20 : rows = rows + atom_rows
1750 : END DO
1751 10 : atom_count(subchunk) = natom - atom_begin(subchunk) + 1
1752 10 : row_count(subchunk) = rows
1753 10 : CPASSERT(subchunk == nchunks)
1754 :
1755 20 : ALLOCATE (padded_rows(nchunks))
1756 20 : DO subchunk = 1, nchunks
1757 10 : iatom = atom_begin(subchunk) + atom_count(subchunk) - 1
1758 : padded_rows(subchunk) = atom_count(subchunk)* &
1759 30 : INT(MAXVAL(atomic_grid_sizes(atom_begin(subchunk):iatom)))
1760 : END DO
1761 :
1762 : ! Largest blocks run first so CUDA allocations can be reused by all following calls.
1763 10 : DO subchunk = 2, nchunks
1764 0 : padded_rows_tmp = padded_rows(subchunk)
1765 0 : atom_begin_tmp = atom_begin(subchunk)
1766 0 : atom_count_tmp = atom_count(subchunk)
1767 0 : row_begin_tmp = row_begin(subchunk)
1768 0 : row_count_tmp = row_count(subchunk)
1769 0 : insert_at = subchunk
1770 0 : DO WHILE (insert_at > 1 .AND. padded_rows(insert_at - 1) < padded_rows_tmp)
1771 0 : padded_rows(insert_at) = padded_rows(insert_at - 1)
1772 0 : atom_begin(insert_at) = atom_begin(insert_at - 1)
1773 0 : atom_count(insert_at) = atom_count(insert_at - 1)
1774 0 : row_begin(insert_at) = row_begin(insert_at - 1)
1775 0 : row_count(insert_at) = row_count(insert_at - 1)
1776 0 : insert_at = insert_at - 1
1777 : END DO
1778 0 : padded_rows(insert_at) = padded_rows_tmp
1779 0 : atom_begin(insert_at) = atom_begin_tmp
1780 0 : atom_count(insert_at) = atom_count_tmp
1781 0 : row_begin(insert_at) = row_begin_tmp
1782 10 : row_count(insert_at) = row_count_tmp
1783 : END DO
1784 10 : DEALLOCATE (padded_rows)
1785 :
1786 : END SUBROUTINE atom_composite_chunk_layout
1787 :
1788 : ! **************************************************************************************************
1789 : !> \brief Add the explicit SKALA derivative with respect to atom-center coordinates.
1790 : !> \param atom_force ...
1791 : !> \param features ...
1792 : !> \param atom_coord_grad_t ...
1793 : !> \param root_rank ...
1794 : ! **************************************************************************************************
1795 8 : SUBROUTINE add_explicit_coordinate_force(atom_force, features, atom_coord_grad_t, root_rank)
1796 : REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: atom_force
1797 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
1798 : TYPE(torch_tensor_type), INTENT(INOUT) :: atom_coord_grad_t
1799 : LOGICAL, INTENT(IN) :: root_rank
1800 :
1801 8 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: atom_coord_grad
1802 :
1803 8 : NULLIFY (atom_coord_grad)
1804 8 : CALL torch_tensor_grad(features%coarse_0_atomic_coords_t, atom_coord_grad_t)
1805 8 : IF (root_rank) THEN
1806 4 : CALL torch_tensor_data_ptr(atom_coord_grad_t, atom_coord_grad)
1807 4 : CPASSERT(SIZE(atom_force, 1) == SIZE(atom_coord_grad, 1))
1808 4 : CPASSERT(SIZE(atom_force, 2) == SIZE(atom_coord_grad, 2))
1809 36 : atom_force(:, :) = atom_force(:, :) + atom_coord_grad(:, :)
1810 : END IF
1811 :
1812 8 : END SUBROUTINE add_explicit_coordinate_force
1813 :
1814 : ! **************************************************************************************************
1815 : !> \brief Add the force from SMOOTH native-grid atom partition weights.
1816 : !> \param atom_force ...
1817 : !> \param features ...
1818 : !> \param particle_set ...
1819 : !> \param cell ...
1820 : !> \param rho_r ...
1821 : !> \param grid_weight_grad_t ...
1822 : !> \param atomic_grid_weight_grad_t ...
1823 : ! **************************************************************************************************
1824 8 : SUBROUTINE add_smooth_partition_force(atom_force, features, particle_set, cell, rho_r, &
1825 : grid_weight_grad_t, atomic_grid_weight_grad_t)
1826 : REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: atom_force
1827 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
1828 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1829 : TYPE(cell_type), POINTER :: cell
1830 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
1831 : TYPE(torch_tensor_type), INTENT(INOUT) :: grid_weight_grad_t, &
1832 : atomic_grid_weight_grad_t
1833 :
1834 : INTEGER :: feature_begin, feature_end, feature_pos, &
1835 : i, iatom, j, jatom, k, local_row, &
1836 : natom, row
1837 : INTEGER, DIMENSION(2, 3) :: bo
1838 : LOGICAL, ALLOCATABLE, DIMENSION(:) :: included
1839 : REAL(KIND=dp) :: grid_base_weight, weight_grad
1840 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: weights
1841 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: atom_coords_pbc
1842 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: dweights_datom, dweights_dstrain
1843 : REAL(KIND=dp), DIMENSION(3) :: grid_point
1844 8 : REAL(KIND=dp), DIMENSION(:), POINTER :: atomic_grid_weight_grad, grid_weight_grad
1845 :
1846 8 : NULLIFY (atomic_grid_weight_grad, grid_weight_grad)
1847 8 : CALL torch_tensor_grad(features%grid_weights_t, grid_weight_grad_t)
1848 8 : CALL torch_tensor_grad(features%atomic_grid_weights_t, atomic_grid_weight_grad_t)
1849 8 : CALL torch_tensor_data_ptr(grid_weight_grad_t, grid_weight_grad)
1850 8 : CALL torch_tensor_data_ptr(atomic_grid_weight_grad_t, atomic_grid_weight_grad)
1851 :
1852 8 : natom = SIZE(particle_set)
1853 8 : CPASSERT(SIZE(atom_force, 1) == 3)
1854 8 : CPASSERT(SIZE(atom_force, 2) == natom)
1855 : ALLOCATE (atom_coords_pbc(3, natom), included(natom), weights(natom), &
1856 96 : dweights_datom(3, natom, natom), dweights_dstrain(3, 3, natom))
1857 24 : DO iatom = 1, natom
1858 24 : atom_coords_pbc(:, iatom) = pbc(particle_set(iatom)%r, cell, positive_range=.TRUE.)
1859 : END DO
1860 :
1861 80 : bo = rho_r(1)%pw_grid%bounds_local
1862 8 : local_row = 0
1863 182 : DO k = bo(1, 3), bo(2, 3)
1864 4088 : DO j = bo(1, 2), bo(2, 2)
1865 48927 : DO i = bo(1, 1), bo(2, 1)
1866 44847 : local_row = local_row + 1
1867 179388 : grid_point = native_grid_coordinate(rho_r(1)%pw_grid, [i, j, k])
1868 : CALL skala_gpw_smooth_partition_derivatives(grid_point, atom_coords_pbc, cell, &
1869 : weights, included, dweights_datom, &
1870 44847 : dweights_dstrain)
1871 44847 : feature_begin = features%local_feature_offsets(local_row)
1872 44847 : feature_end = features%local_feature_offsets(local_row + 1) - 1
1873 134541 : CPASSERT(feature_end - feature_begin + 1 == COUNT(included))
1874 44847 : grid_base_weight = 0.0_dp
1875 134287 : DO feature_pos = feature_begin, feature_end
1876 89440 : row = features%local_feature_rows(feature_pos)
1877 134287 : grid_base_weight = grid_base_weight + features%grid_weights(row)
1878 : END DO
1879 : feature_pos = feature_begin
1880 134541 : DO iatom = 1, natom
1881 89694 : IF (.NOT. included(iatom)) CYCLE
1882 89440 : row = features%local_feature_rows(feature_pos)
1883 : weight_grad = grid_base_weight*grid_weight_grad(row) + &
1884 : rho_r(1)%pw_grid%dvol*atomic_grid_weight_grad(row)* &
1885 89440 : smooth_partition_atomic_weight_scale_derivative(weights(iatom))
1886 268320 : DO jatom = 1, natom
1887 : atom_force(:, jatom) = atom_force(:, jatom) + &
1888 804960 : weight_grad*dweights_datom(:, jatom, iatom)
1889 : END DO
1890 134541 : feature_pos = feature_pos + 1
1891 : END DO
1892 48753 : CPASSERT(feature_pos == feature_end + 1)
1893 : END DO
1894 : END DO
1895 : END DO
1896 8 : CPASSERT(local_row == features%nflat_local)
1897 :
1898 8 : DEALLOCATE (atom_coords_pbc, dweights_datom, dweights_dstrain, included, weights)
1899 8 : CALL torch_tensor_release(grid_weight_grad_t)
1900 8 : CALL torch_tensor_release(atomic_grid_weight_grad_t)
1901 :
1902 8 : END SUBROUTINE add_smooth_partition_force
1903 :
1904 : ! **************************************************************************************************
1905 : !> \brief Add the virial from SMOOTH native-grid atom partition weights.
1906 : !> \param virial_xc ...
1907 : !> \param features ...
1908 : !> \param particle_set ...
1909 : !> \param cell ...
1910 : !> \param rho_r ...
1911 : !> \param grid_weight_grad_t ...
1912 : !> \param atomic_grid_weight_grad_t ...
1913 : ! **************************************************************************************************
1914 6 : SUBROUTINE build_smooth_partition_virial(virial_xc, features, particle_set, cell, rho_r, &
1915 : grid_weight_grad_t, atomic_grid_weight_grad_t)
1916 : REAL(KIND=dp), DIMENSION(3, 3), INTENT(INOUT) :: virial_xc
1917 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
1918 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1919 : TYPE(cell_type), POINTER :: cell
1920 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
1921 : TYPE(torch_tensor_type), INTENT(INOUT) :: grid_weight_grad_t, &
1922 : atomic_grid_weight_grad_t
1923 :
1924 : INTEGER :: feature_begin, feature_end, feature_pos, &
1925 : i, iatom, idir, j, jdir, k, local_row, &
1926 : natom, row
1927 : INTEGER, DIMENSION(2, 3) :: bo
1928 : LOGICAL, ALLOCATABLE, DIMENSION(:) :: included
1929 : REAL(KIND=dp) :: grid_base_weight, tmp, weight_grad
1930 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: weights
1931 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: atom_coords_pbc
1932 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: dweights_datom, dweights_dstrain
1933 : REAL(KIND=dp), DIMENSION(3) :: grid_point
1934 6 : REAL(KIND=dp), DIMENSION(:), POINTER :: atomic_grid_weight_grad, grid_weight_grad
1935 :
1936 6 : NULLIFY (atomic_grid_weight_grad, grid_weight_grad)
1937 6 : CALL torch_tensor_grad(features%grid_weights_t, grid_weight_grad_t)
1938 6 : CALL torch_tensor_grad(features%atomic_grid_weights_t, atomic_grid_weight_grad_t)
1939 6 : CALL torch_tensor_data_ptr(grid_weight_grad_t, grid_weight_grad)
1940 6 : CALL torch_tensor_data_ptr(atomic_grid_weight_grad_t, atomic_grid_weight_grad)
1941 :
1942 6 : natom = SIZE(particle_set)
1943 : ALLOCATE (atom_coords_pbc(3, natom), included(natom), weights(natom), &
1944 72 : dweights_datom(3, natom, natom), dweights_dstrain(3, 3, natom))
1945 18 : DO iatom = 1, natom
1946 18 : atom_coords_pbc(:, iatom) = pbc(particle_set(iatom)%r, cell, positive_range=.TRUE.)
1947 : END DO
1948 :
1949 60 : bo = rho_r(1)%pw_grid%bounds_local
1950 6 : local_row = 0
1951 150 : DO k = bo(1, 3), bo(2, 3)
1952 3606 : DO j = bo(1, 2), bo(2, 2)
1953 45072 : DO i = bo(1, 1), bo(2, 1)
1954 41472 : local_row = local_row + 1
1955 165888 : grid_point = native_grid_coordinate(rho_r(1)%pw_grid, [i, j, k])
1956 : CALL skala_gpw_smooth_partition_derivatives(grid_point, atom_coords_pbc, cell, &
1957 : weights, included, dweights_datom, &
1958 41472 : dweights_dstrain)
1959 41472 : feature_begin = features%local_feature_offsets(local_row)
1960 41472 : feature_end = features%local_feature_offsets(local_row + 1) - 1
1961 124416 : CPASSERT(feature_end - feature_begin + 1 == COUNT(included))
1962 41472 : grid_base_weight = 0.0_dp
1963 124206 : DO feature_pos = feature_begin, feature_end
1964 82734 : row = features%local_feature_rows(feature_pos)
1965 124206 : grid_base_weight = grid_base_weight + features%grid_weights(row)
1966 : END DO
1967 : feature_pos = feature_begin
1968 124416 : DO iatom = 1, natom
1969 82944 : IF (.NOT. included(iatom)) CYCLE
1970 82734 : row = features%local_feature_rows(feature_pos)
1971 : weight_grad = grid_base_weight*grid_weight_grad(row) + &
1972 : rho_r(1)%pw_grid%dvol*atomic_grid_weight_grad(row)* &
1973 82734 : smooth_partition_atomic_weight_scale_derivative(weights(iatom))
1974 330936 : DO idir = 1, 3
1975 827340 : DO jdir = 1, idir
1976 496404 : tmp = weight_grad*dweights_dstrain(idir, jdir, iatom)
1977 496404 : virial_xc(jdir, idir) = virial_xc(jdir, idir) + tmp
1978 744606 : IF (idir /= jdir) virial_xc(idir, jdir) = virial_xc(idir, jdir) + tmp
1979 : END DO
1980 : END DO
1981 124416 : feature_pos = feature_pos + 1
1982 : END DO
1983 44928 : CPASSERT(feature_pos == feature_end + 1)
1984 : END DO
1985 : END DO
1986 : END DO
1987 6 : CPASSERT(local_row == features%nflat_local)
1988 :
1989 6 : DEALLOCATE (atom_coords_pbc, dweights_datom, dweights_dstrain, included, weights)
1990 6 : CALL torch_tensor_release(grid_weight_grad_t)
1991 6 : CALL torch_tensor_release(atomic_grid_weight_grad_t)
1992 :
1993 6 : END SUBROUTINE build_smooth_partition_virial
1994 :
1995 : ! **************************************************************************************************
1996 : !> \brief Return the Cartesian coordinate of a regular GPW grid point.
1997 : !> \param pw_grid ...
1998 : !> \param index ...
1999 : !> \return ...
2000 : ! **************************************************************************************************
2001 86319 : FUNCTION native_grid_coordinate(pw_grid, index) RESULT(coord)
2002 : TYPE(pw_grid_type), POINTER :: pw_grid
2003 : INTEGER, DIMENSION(3), INTENT(IN) :: index
2004 : REAL(KIND=dp), DIMENSION(3) :: coord
2005 :
2006 : INTEGER, DIMENSION(3) :: relative_index
2007 :
2008 345276 : relative_index = index - pw_grid%bounds(1, :)
2009 : coord = REAL(relative_index(1), KIND=dp)*pw_grid%dh(:, 1) + &
2010 : REAL(relative_index(2), KIND=dp)*pw_grid%dh(:, 2) + &
2011 345276 : REAL(relative_index(3), KIND=dp)*pw_grid%dh(:, 3)
2012 :
2013 86319 : END FUNCTION native_grid_coordinate
2014 :
2015 : ! **************************************************************************************************
2016 : !> \brief Evaluate a rank-local atom chunk as multiple atom-contiguous Torch subchunks.
2017 : !> \param features ...
2018 : !> \param group ...
2019 : !> \param max_rows ...
2020 : !> \param compute_grads ...
2021 : !> \param exc ...
2022 : !> \param density_grad ...
2023 : !> \param grad_grad ...
2024 : !> \param kin_grad ...
2025 : !> \param collapse_spin_grads ...
2026 : ! **************************************************************************************************
2027 2 : SUBROUTINE evaluate_atom_subchunks(features, group, max_rows, compute_grads, exc, &
2028 : density_grad, grad_grad, kin_grad, collapse_spin_grads)
2029 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
2030 :
2031 : CLASS(mp_comm_type), INTENT(IN) :: group
2032 : INTEGER, INTENT(IN) :: max_rows
2033 : LOGICAL, INTENT(IN) :: compute_grads, collapse_spin_grads
2034 : REAL(KIND=dp), INTENT(OUT) :: exc
2035 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
2036 : INTENT(OUT) :: density_grad, kin_grad
2037 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
2038 : INTENT(OUT) :: grad_grad
2039 :
2040 : INTEGER :: isubchunk, nroute_grad_per_point, &
2041 : nroute_recv_points, nroute_send_points, &
2042 : nsubchunks, phase_handle, subphase_handle
2043 2 : INTEGER, ALLOCATABLE, DIMENSION(:) :: route_grad_return_recv_counts, &
2044 2 : route_grad_return_recv_displs, &
2045 2 : route_grad_return_send_counts, &
2046 2 : route_grad_return_send_displs, &
2047 2 : subchunk_atom_begin, &
2048 2 : subchunk_atom_count, &
2049 2 : subchunk_row_begin, &
2050 2 : subchunk_row_count
2051 : REAL(KIND=dp) :: subchunk_exc
2052 2 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: recv_grad_buffer, send_grad_buffer
2053 2 : TYPE(skala_gpw_feature_type) :: subchunk
2054 : TYPE(torch_tensor_type) :: subchunk_exc_tensor
2055 :
2056 0 : CPASSERT(features%uses_atom_chunks)
2057 2 : CPASSERT(max_rows > 0)
2058 : CALL skala_gpw_atom_subchunk_layout(max_rows, subchunk_atom_begin, subchunk_atom_count, &
2059 2 : subchunk_row_begin, subchunk_row_count)
2060 2 : nsubchunks = SIZE(subchunk_atom_begin)
2061 :
2062 2 : exc = 0.0_dp
2063 2 : IF (compute_grads) THEN
2064 2 : CPASSERT(features%uses_atom_chunk_routing)
2065 6 : nroute_recv_points = SUM(features%route_point_recv_counts)
2066 2 : nroute_send_points = SIZE(features%route_send_local_rows)
2067 6 : CPASSERT(SUM(features%route_point_send_counts) == nroute_send_points)
2068 2 : CPASSERT(SIZE(features%route_chunk_offsets) == nroute_recv_points + 1)
2069 2 : nroute_grad_per_point = ngrad_per_point
2070 2 : IF (collapse_spin_grads) nroute_grad_per_point = ncollapsed_grad_per_point
2071 : ALLOCATE (send_grad_buffer(MAX(1, nroute_grad_per_point*nroute_recv_points)), &
2072 : recv_grad_buffer(MAX(1, nroute_grad_per_point*nroute_send_points)), &
2073 : route_grad_return_send_counts(SIZE(features%route_point_recv_counts)), &
2074 : route_grad_return_send_displs(SIZE(features%route_point_recv_displs)), &
2075 : route_grad_return_recv_counts(SIZE(features%route_point_send_counts)), &
2076 26 : route_grad_return_recv_displs(SIZE(features%route_point_send_displs)))
2077 : route_grad_return_send_counts(:) = &
2078 6 : nroute_grad_per_point*features%route_point_recv_counts
2079 : route_grad_return_send_displs(:) = &
2080 6 : nroute_grad_per_point*features%route_point_recv_displs
2081 : route_grad_return_recv_counts(:) = &
2082 6 : nroute_grad_per_point*features%route_point_send_counts
2083 : route_grad_return_recv_displs(:) = &
2084 6 : nroute_grad_per_point*features%route_point_send_displs
2085 : END IF
2086 :
2087 2 : CALL timeset("skala_gpw_atom_subchunks", phase_handle)
2088 6 : DO isubchunk = 1, nsubchunks
2089 4 : CALL timeset("skala_gpw_atom_subchunk_build", subphase_handle)
2090 : CALL skala_gpw_feature_build_atom_subchunk_bounds(features, subchunk, &
2091 : subchunk_atom_begin(isubchunk), &
2092 : subchunk_atom_count(isubchunk), &
2093 : subchunk_row_begin(isubchunk), &
2094 : subchunk_row_count(isubchunk), &
2095 4 : compute_grads)
2096 4 : CALL timestop(subphase_handle)
2097 4 : CALL timeset("skala_gpw_atom_subchunk_forward", subphase_handle)
2098 : CALL skala_torch_model_get_exc(cached_model, subchunk%inputs, &
2099 : subchunk%grid_weights_t, subchunk_exc_tensor, &
2100 4 : subchunk_exc)
2101 4 : CALL timestop(subphase_handle)
2102 4 : exc = exc + subchunk_exc
2103 4 : IF (compute_grads) THEN
2104 4 : CALL timeset("skala_gpw_atom_subchunk_backward", subphase_handle)
2105 4 : CALL torch_tensor_backward_scalar(subchunk_exc_tensor)
2106 4 : CALL timestop(subphase_handle)
2107 : END IF
2108 4 : CALL timeset("skala_gpw_atom_subchunk_release", subphase_handle)
2109 4 : CALL torch_tensor_release(subchunk_exc_tensor)
2110 4 : CALL skala_gpw_feature_release(subchunk)
2111 18 : CALL timestop(subphase_handle)
2112 : END DO
2113 2 : IF (compute_grads .AND. features%chunk_feature_count > 0) THEN
2114 2 : CALL timeset("skala_gpw_atom_subchunk_grad_pack", subphase_handle)
2115 2 : CALL pack_atom_chunk_grads(features, send_grad_buffer, .TRUE., collapse_spin_grads)
2116 2 : CALL timestop(subphase_handle)
2117 : END IF
2118 2 : CALL timestop(phase_handle)
2119 :
2120 2 : IF (compute_grads) THEN
2121 2 : CALL timeset("skala_gpw_grad_route_comm", phase_handle)
2122 : CALL group%alltoall(send_grad_buffer, route_grad_return_send_counts, &
2123 : route_grad_return_send_displs, recv_grad_buffer, &
2124 2 : route_grad_return_recv_counts, route_grad_return_recv_displs)
2125 2 : CALL timestop(phase_handle)
2126 :
2127 2 : CALL timeset("skala_gpw_grad_route_scatter", phase_handle)
2128 : CALL scatter_routed_atom_chunk_grads(features, recv_grad_buffer, collapse_spin_grads, &
2129 2 : density_grad, grad_grad, kin_grad)
2130 2 : CALL timestop(phase_handle)
2131 :
2132 0 : DEALLOCATE (recv_grad_buffer, route_grad_return_recv_counts, &
2133 0 : route_grad_return_recv_displs, route_grad_return_send_counts, &
2134 2 : route_grad_return_send_displs, send_grad_buffer)
2135 : END IF
2136 2 : DEALLOCATE (subchunk_atom_begin, subchunk_atom_count, subchunk_row_begin, subchunk_row_count)
2137 :
2138 4 : END SUBROUTINE evaluate_atom_subchunks
2139 :
2140 : ! **************************************************************************************************
2141 : !> \brief Select an automatic atom-subchunk row cap.
2142 : !> \param features ...
2143 : !> \param group ...
2144 : !> \return ...
2145 : ! **************************************************************************************************
2146 90 : FUNCTION auto_atom_chunk_max_rows(features, group) RESULT(max_rows)
2147 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
2148 :
2149 : CLASS(mp_comm_type), INTENT(IN) :: group
2150 : INTEGER :: max_rows
2151 :
2152 : INTEGER :: local_rows_max, target_rows
2153 :
2154 90 : local_rows_max = features%chunk_feature_count
2155 90 : CALL group%max(local_rows_max)
2156 90 : IF (local_rows_max <= 0) THEN
2157 90 : max_rows = 0
2158 : RETURN
2159 : END IF
2160 :
2161 90 : IF (group%num_pe > 1) THEN
2162 90 : target_rows = CEILING(REAL(local_rows_max, KIND=dp)/2.0_dp)
2163 : max_rows = atom_chunk_auto_row_quantum* &
2164 90 : ((target_rows + atom_chunk_auto_row_quantum - 1)/atom_chunk_auto_row_quantum)
2165 : ELSE
2166 0 : target_rows = NINT(REAL(local_rows_max, KIND=dp)/4.0_dp)
2167 : max_rows = atom_chunk_auto_row_quantum* &
2168 : MAX(1, NINT(REAL(target_rows, KIND=dp)/ &
2169 0 : REAL(atom_chunk_auto_row_quantum, KIND=dp)))
2170 : END IF
2171 90 : max_rows = MAX(atom_chunk_auto_min_rows, MIN(atom_chunk_auto_max_rows, max_rows))
2172 :
2173 90 : END FUNCTION auto_atom_chunk_max_rows
2174 :
2175 : ! **************************************************************************************************
2176 : !> \brief Map full Torch feature gradients back to this rank's local grid order.
2177 : !> \param features ...
2178 : !> \param density_grad ...
2179 : !> \param grad_grad ...
2180 : !> \param kin_grad ...
2181 : !> \param derivatives Optional Hessian-vector result instead of accumulated gradients.
2182 : ! **************************************************************************************************
2183 10 : SUBROUTINE fetch_local_feature_grads(features, density_grad, grad_grad, kin_grad, derivatives)
2184 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
2185 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
2186 : INTENT(OUT) :: density_grad
2187 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
2188 : INTENT(OUT) :: grad_grad
2189 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
2190 : INTENT(OUT) :: kin_grad
2191 : TYPE(torch_tensor_type), INTENT(IN), OPTIONAL :: derivatives(3)
2192 :
2193 : INTEGER :: feature_pos, i, j, k, local_row, row
2194 10 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: density_grad_all, kin_grad_all
2195 10 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: grad_grad_all
2196 : TYPE(torch_tensor_type) :: density_grad_t, grad_grad_t, kin_grad_t
2197 :
2198 10 : NULLIFY (density_grad_all, grad_grad_all, kin_grad_all)
2199 10 : IF (PRESENT(derivatives)) THEN
2200 2 : CALL torch_tensor_data_ptr(derivatives(1), density_grad_all)
2201 2 : CALL torch_tensor_data_ptr(derivatives(2), grad_grad_all)
2202 2 : CALL torch_tensor_data_ptr(derivatives(3), kin_grad_all)
2203 : ELSE
2204 : CALL get_feature_grad_views(features, density_grad_t, grad_grad_t, kin_grad_t, &
2205 8 : density_grad_all, grad_grad_all, kin_grad_all)
2206 : END IF
2207 10 : CPASSERT(SIZE(density_grad_all, 1) == features%nflat)
2208 10 : CPASSERT(SIZE(density_grad_all, 2) == 2)
2209 10 : CPASSERT(SIZE(grad_grad_all, 1) == features%nflat)
2210 10 : CPASSERT(SIZE(grad_grad_all, 2) == 3)
2211 10 : CPASSERT(SIZE(grad_grad_all, 3) == 2)
2212 10 : CPASSERT(SIZE(kin_grad_all, 1) == features%nflat)
2213 10 : CPASSERT(SIZE(kin_grad_all, 2) == 2)
2214 :
2215 0 : ALLOCATE (density_grad(features%nflat_local, 2), &
2216 0 : grad_grad(features%nflat_local, 3, 2), &
2217 70 : kin_grad(features%nflat_local, 2))
2218 10 : density_grad = 0.0_dp
2219 10 : grad_grad = 0.0_dp
2220 10 : kin_grad = 0.0_dp
2221 10 : local_row = 0
2222 228 : DO k = LBOUND(features%feature_index, 3), UBOUND(features%feature_index, 3)
2223 4798 : DO j = LBOUND(features%feature_index, 2), UBOUND(features%feature_index, 2)
2224 59355 : DO i = LBOUND(features%feature_index, 1), UBOUND(features%feature_index, 1)
2225 46575 : local_row = local_row + 1
2226 137743 : DO feature_pos = features%local_feature_offsets(local_row), &
2227 50769 : features%local_feature_offsets(local_row + 1) - 1
2228 91168 : row = features%local_feature_rows(feature_pos)
2229 91168 : CPASSERT(row >= 1 .AND. row <= features%nflat)
2230 : density_grad(local_row, :) = density_grad(local_row, :) + &
2231 273504 : density_grad_all(row, :)
2232 : grad_grad(local_row, :, :) = grad_grad(local_row, :, :) + &
2233 820512 : grad_grad_all(row, :, :)
2234 320079 : kin_grad(local_row, :) = kin_grad(local_row, :) + kin_grad_all(row, :)
2235 : END DO
2236 : END DO
2237 : END DO
2238 : END DO
2239 10 : CPASSERT(local_row == features%nflat_local)
2240 :
2241 10 : IF (.NOT. PRESENT(derivatives)) THEN
2242 8 : CALL torch_tensor_release(density_grad_t)
2243 8 : CALL torch_tensor_release(grad_grad_t)
2244 8 : CALL torch_tensor_release(kin_grad_t)
2245 : END IF
2246 :
2247 10 : END SUBROUTINE fetch_local_feature_grads
2248 :
2249 : ! **************************************************************************************************
2250 : !> \brief Pack atom-chunk Torch gradients into CP2K communication buffers.
2251 : !> \param features ...
2252 : !> \param TARGET ...
2253 : !> \param route_to_return_positions ...
2254 : !> \param collapse_spin_grads ...
2255 : ! **************************************************************************************************
2256 94 : SUBROUTINE pack_atom_chunk_grads(features, TARGET, route_to_return_positions, &
2257 : collapse_spin_grads)
2258 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
2259 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
2260 : INTENT(INOUT) :: target
2261 : LOGICAL, INTENT(IN) :: route_to_return_positions
2262 : LOGICAL, INTENT(IN), OPTIONAL :: collapse_spin_grads
2263 :
2264 : INTEGER :: base, feature_pos, irow, &
2265 : ngrad_buffer_per_point, point_pos, &
2266 : target_points
2267 : LOGICAL :: my_collapse_spin_grads
2268 94 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: chunk_density_grad, chunk_kin_grad
2269 94 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: chunk_grad_grad
2270 : TYPE(torch_tensor_type) :: density_grad_t, grad_grad_t, kin_grad_t
2271 :
2272 94 : my_collapse_spin_grads = .FALSE.
2273 188 : IF (PRESENT(collapse_spin_grads)) my_collapse_spin_grads = collapse_spin_grads
2274 94 : ngrad_buffer_per_point = ngrad_per_point
2275 94 : IF (my_collapse_spin_grads) ngrad_buffer_per_point = ncollapsed_grad_per_point
2276 :
2277 94 : NULLIFY (chunk_density_grad, chunk_grad_grad, chunk_kin_grad)
2278 : CALL get_feature_grad_views(features, density_grad_t, grad_grad_t, kin_grad_t, &
2279 94 : chunk_density_grad, chunk_grad_grad, chunk_kin_grad)
2280 94 : CPASSERT(MOD(SIZE(TARGET), ngrad_buffer_per_point) == 0)
2281 94 : target_points = SIZE(TARGET)/ngrad_buffer_per_point
2282 94 : CPASSERT(SIZE(chunk_density_grad, 1) == features%chunk_feature_count)
2283 94 : CPASSERT(SIZE(chunk_grad_grad, 1) == features%chunk_feature_count)
2284 94 : CPASSERT(SIZE(chunk_grad_grad, 2) == 3)
2285 94 : CPASSERT(SIZE(chunk_kin_grad, 1) == features%chunk_feature_count)
2286 94 : IF (features%uses_collapsed_rks_dynamic) THEN
2287 88 : CPASSERT(my_collapse_spin_grads)
2288 88 : CPASSERT(SIZE(chunk_density_grad, 2) == 1)
2289 88 : CPASSERT(SIZE(chunk_grad_grad, 3) == 1)
2290 88 : CPASSERT(SIZE(chunk_kin_grad, 2) == 1)
2291 : ELSE
2292 6 : CPASSERT(SIZE(chunk_density_grad, 2) == 2)
2293 6 : CPASSERT(SIZE(chunk_grad_grad, 3) == 2)
2294 6 : CPASSERT(SIZE(chunk_kin_grad, 2) == 2)
2295 : END IF
2296 :
2297 94 : IF (route_to_return_positions) THEN
2298 94 : CPASSERT(target_points == SIZE(features%route_chunk_offsets) - 1)
2299 : !$OMP PARALLEL DO DEFAULT(NONE) &
2300 : !$OMP SHARED(chunk_density_grad, chunk_grad_grad, chunk_kin_grad, features, &
2301 : !$OMP my_collapse_spin_grads, ngrad_buffer_per_point, TARGET, target_points) &
2302 94 : !$OMP PRIVATE(base, feature_pos, irow, point_pos)
2303 : DO point_pos = 1, target_points
2304 : base = ngrad_buffer_per_point*(point_pos - 1)
2305 : TARGET(base + 1:base + ngrad_buffer_per_point) = 0.0_dp
2306 : DO feature_pos = features%route_chunk_offsets(point_pos), &
2307 : features%route_chunk_offsets(point_pos + 1) - 1
2308 : irow = features%route_chunk_rows(feature_pos)
2309 : CPASSERT(irow >= 1 .AND. irow <= features%chunk_feature_count)
2310 : IF (my_collapse_spin_grads) THEN
2311 : IF (features%uses_collapsed_rks_dynamic) THEN
2312 : TARGET(base + 1) = TARGET(base + 1) + &
2313 : 0.5_dp*chunk_density_grad(irow, 1)
2314 : TARGET(base + 2) = TARGET(base + 2) + &
2315 : 0.5_dp*chunk_grad_grad(irow, 1, 1)
2316 : TARGET(base + 3) = TARGET(base + 3) + &
2317 : 0.5_dp*chunk_grad_grad(irow, 2, 1)
2318 : TARGET(base + 4) = TARGET(base + 4) + &
2319 : 0.5_dp*chunk_grad_grad(irow, 3, 1)
2320 : TARGET(base + 5) = TARGET(base + 5) + &
2321 : 0.5_dp*chunk_kin_grad(irow, 1)
2322 : ELSE
2323 : TARGET(base + 1) = TARGET(base + 1) + &
2324 : 0.5_dp*(chunk_density_grad(irow, 1) + &
2325 : chunk_density_grad(irow, 2))
2326 : TARGET(base + 2) = TARGET(base + 2) + &
2327 : 0.5_dp*(chunk_grad_grad(irow, 1, 1) + &
2328 : chunk_grad_grad(irow, 1, 2))
2329 : TARGET(base + 3) = TARGET(base + 3) + &
2330 : 0.5_dp*(chunk_grad_grad(irow, 2, 1) + &
2331 : chunk_grad_grad(irow, 2, 2))
2332 : TARGET(base + 4) = TARGET(base + 4) + &
2333 : 0.5_dp*(chunk_grad_grad(irow, 3, 1) + &
2334 : chunk_grad_grad(irow, 3, 2))
2335 : TARGET(base + 5) = TARGET(base + 5) + &
2336 : 0.5_dp*(chunk_kin_grad(irow, 1) + &
2337 : chunk_kin_grad(irow, 2))
2338 : END IF
2339 : ELSE
2340 : TARGET(base + 1:base + 2) = TARGET(base + 1:base + 2) + &
2341 : chunk_density_grad(irow, :)
2342 : TARGET(base + 3) = TARGET(base + 3) + chunk_grad_grad(irow, 1, 1)
2343 : TARGET(base + 4) = TARGET(base + 4) + chunk_grad_grad(irow, 2, 1)
2344 : TARGET(base + 5) = TARGET(base + 5) + chunk_grad_grad(irow, 3, 1)
2345 : TARGET(base + 6) = TARGET(base + 6) + chunk_grad_grad(irow, 1, 2)
2346 : TARGET(base + 7) = TARGET(base + 7) + chunk_grad_grad(irow, 2, 2)
2347 : TARGET(base + 8) = TARGET(base + 8) + chunk_grad_grad(irow, 3, 2)
2348 : TARGET(base + 9:base + 10) = TARGET(base + 9:base + 10) + &
2349 : chunk_kin_grad(irow, :)
2350 : END IF
2351 : END DO
2352 : END DO
2353 : !$OMP END PARALLEL DO
2354 : ELSE
2355 0 : CPASSERT(target_points >= features%chunk_feature_count)
2356 : !$OMP PARALLEL DO DEFAULT(NONE) &
2357 : !$OMP SHARED(chunk_density_grad, chunk_grad_grad, chunk_kin_grad, features, &
2358 : !$OMP my_collapse_spin_grads, ngrad_buffer_per_point, TARGET) &
2359 0 : !$OMP PRIVATE(base, irow)
2360 : DO irow = 1, features%chunk_feature_count
2361 : base = ngrad_buffer_per_point*(irow - 1)
2362 : IF (my_collapse_spin_grads) THEN
2363 : IF (features%uses_collapsed_rks_dynamic) THEN
2364 : TARGET(base + 1) = 0.5_dp*chunk_density_grad(irow, 1)
2365 : TARGET(base + 2) = 0.5_dp*chunk_grad_grad(irow, 1, 1)
2366 : TARGET(base + 3) = 0.5_dp*chunk_grad_grad(irow, 2, 1)
2367 : TARGET(base + 4) = 0.5_dp*chunk_grad_grad(irow, 3, 1)
2368 : TARGET(base + 5) = 0.5_dp*chunk_kin_grad(irow, 1)
2369 : ELSE
2370 : TARGET(base + 1) = 0.5_dp*(chunk_density_grad(irow, 1) + &
2371 : chunk_density_grad(irow, 2))
2372 : TARGET(base + 2) = 0.5_dp*(chunk_grad_grad(irow, 1, 1) + &
2373 : chunk_grad_grad(irow, 1, 2))
2374 : TARGET(base + 3) = 0.5_dp*(chunk_grad_grad(irow, 2, 1) + &
2375 : chunk_grad_grad(irow, 2, 2))
2376 : TARGET(base + 4) = 0.5_dp*(chunk_grad_grad(irow, 3, 1) + &
2377 : chunk_grad_grad(irow, 3, 2))
2378 : TARGET(base + 5) = 0.5_dp*(chunk_kin_grad(irow, 1) + &
2379 : chunk_kin_grad(irow, 2))
2380 : END IF
2381 : ELSE
2382 : TARGET(base + 1:base + 2) = chunk_density_grad(irow, :)
2383 : TARGET(base + 3) = chunk_grad_grad(irow, 1, 1)
2384 : TARGET(base + 4) = chunk_grad_grad(irow, 2, 1)
2385 : TARGET(base + 5) = chunk_grad_grad(irow, 3, 1)
2386 : TARGET(base + 6) = chunk_grad_grad(irow, 1, 2)
2387 : TARGET(base + 7) = chunk_grad_grad(irow, 2, 2)
2388 : TARGET(base + 8) = chunk_grad_grad(irow, 3, 2)
2389 : TARGET(base + 9:base + 10) = chunk_kin_grad(irow, :)
2390 : END IF
2391 : END DO
2392 : !$OMP END PARALLEL DO
2393 : END IF
2394 :
2395 94 : CALL torch_tensor_release(density_grad_t)
2396 94 : CALL torch_tensor_release(grad_grad_t)
2397 94 : CALL torch_tensor_release(kin_grad_t)
2398 :
2399 94 : END SUBROUTINE pack_atom_chunk_grads
2400 :
2401 : ! **************************************************************************************************
2402 : !> \brief Scatter routed atom-chunk gradients into local grid-row order.
2403 : !> \param features ...
2404 : !> \param recv_grad_buffer ...
2405 : !> \param collapse_spin_grads ...
2406 : !> \param density_grad ...
2407 : !> \param grad_grad ...
2408 : !> \param kin_grad ...
2409 : ! **************************************************************************************************
2410 94 : SUBROUTINE scatter_routed_atom_chunk_grads(features, recv_grad_buffer, collapse_spin_grads, &
2411 : density_grad, grad_grad, kin_grad)
2412 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
2413 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: recv_grad_buffer
2414 : LOGICAL, INTENT(IN) :: collapse_spin_grads
2415 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
2416 : INTENT(OUT) :: density_grad
2417 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
2418 : INTENT(OUT) :: grad_grad
2419 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
2420 : INTENT(OUT) :: kin_grad
2421 :
2422 : INTEGER :: base, local_row, nflat_local, &
2423 : nroute_grad_per_point, nroute_points, &
2424 : point_pos, row_route_pos
2425 :
2426 94 : nflat_local = features%nflat_local
2427 94 : nroute_points = SIZE(features%route_send_local_rows)
2428 94 : nroute_grad_per_point = ngrad_per_point
2429 94 : IF (collapse_spin_grads) nroute_grad_per_point = ncollapsed_grad_per_point
2430 94 : CPASSERT(SIZE(recv_grad_buffer) >= nroute_grad_per_point*nroute_points)
2431 0 : ALLOCATE (density_grad(nflat_local, 2), grad_grad(nflat_local, 3, 2), &
2432 658 : kin_grad(nflat_local, 2))
2433 :
2434 : !$OMP PARALLEL DO DEFAULT(NONE) &
2435 : !$OMP SHARED(collapse_spin_grads, density_grad, features, grad_grad, kin_grad, nflat_local, &
2436 : !$OMP nroute_grad_per_point, nroute_points, recv_grad_buffer) &
2437 94 : !$OMP PRIVATE(base, local_row, point_pos, row_route_pos)
2438 : DO local_row = 1, nflat_local
2439 : density_grad(local_row, :) = 0.0_dp
2440 : grad_grad(local_row, :, :) = 0.0_dp
2441 : kin_grad(local_row, :) = 0.0_dp
2442 : DO row_route_pos = features%route_row_offsets(local_row), &
2443 : features%route_row_offsets(local_row + 1) - 1
2444 : point_pos = features%route_row_positions(row_route_pos)
2445 : CPASSERT(point_pos >= 1 .AND. point_pos <= nroute_points)
2446 : base = nroute_grad_per_point*(point_pos - 1)
2447 : IF (collapse_spin_grads) THEN
2448 : density_grad(local_row, :) = density_grad(local_row, :) + &
2449 : recv_grad_buffer(base + 1)
2450 : grad_grad(local_row, 1, :) = grad_grad(local_row, 1, :) + &
2451 : recv_grad_buffer(base + 2)
2452 : grad_grad(local_row, 2, :) = grad_grad(local_row, 2, :) + &
2453 : recv_grad_buffer(base + 3)
2454 : grad_grad(local_row, 3, :) = grad_grad(local_row, 3, :) + &
2455 : recv_grad_buffer(base + 4)
2456 : kin_grad(local_row, :) = kin_grad(local_row, :) + recv_grad_buffer(base + 5)
2457 : ELSE
2458 : density_grad(local_row, :) = density_grad(local_row, :) + &
2459 : recv_grad_buffer(base + 1:base + 2)
2460 : grad_grad(local_row, 1, 1) = grad_grad(local_row, 1, 1) + &
2461 : recv_grad_buffer(base + 3)
2462 : grad_grad(local_row, 2, 1) = grad_grad(local_row, 2, 1) + &
2463 : recv_grad_buffer(base + 4)
2464 : grad_grad(local_row, 3, 1) = grad_grad(local_row, 3, 1) + &
2465 : recv_grad_buffer(base + 5)
2466 : grad_grad(local_row, 1, 2) = grad_grad(local_row, 1, 2) + &
2467 : recv_grad_buffer(base + 6)
2468 : grad_grad(local_row, 2, 2) = grad_grad(local_row, 2, 2) + &
2469 : recv_grad_buffer(base + 7)
2470 : grad_grad(local_row, 3, 2) = grad_grad(local_row, 3, 2) + &
2471 : recv_grad_buffer(base + 8)
2472 : kin_grad(local_row, :) = kin_grad(local_row, :) + &
2473 : recv_grad_buffer(base + 9:base + 10)
2474 : END IF
2475 : END DO
2476 : END DO
2477 : !$OMP END PARALLEL DO
2478 :
2479 94 : END SUBROUTINE scatter_routed_atom_chunk_grads
2480 :
2481 : ! **************************************************************************************************
2482 : !> \brief Return CPU views of autograd outputs for the SKALA dynamic feature tensors.
2483 : !> \param features ...
2484 : !> \param density_grad_t ...
2485 : !> \param grad_grad_t ...
2486 : !> \param kin_grad_t ...
2487 : !> \param density_grad ...
2488 : !> \param grad_grad ...
2489 : !> \param kin_grad ...
2490 : ! **************************************************************************************************
2491 102 : SUBROUTINE get_feature_grad_views(features, density_grad_t, grad_grad_t, kin_grad_t, &
2492 : density_grad, grad_grad, kin_grad)
2493 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
2494 : TYPE(torch_tensor_type), INTENT(INOUT) :: density_grad_t, grad_grad_t, kin_grad_t
2495 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: density_grad
2496 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: grad_grad
2497 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: kin_grad
2498 :
2499 102 : NULLIFY (density_grad, grad_grad, kin_grad)
2500 : CALL torch_tensor_grad_batch3(features%density_t, features%grad_t, features%kin_t, &
2501 102 : density_grad_t, grad_grad_t, kin_grad_t)
2502 102 : CALL torch_tensor_data_ptr(density_grad_t, density_grad)
2503 102 : CALL torch_tensor_data_ptr(grad_grad_t, grad_grad)
2504 102 : CALL torch_tensor_data_ptr(kin_grad_t, kin_grad)
2505 :
2506 102 : END SUBROUTINE get_feature_grad_views
2507 :
2508 : ! **************************************************************************************************
2509 : !> \brief Fetch atom-chunk gradients and route them back to their local grid owners.
2510 : !> \param features ...
2511 : !> \param group ...
2512 : !> \param density_grad ...
2513 : !> \param grad_grad ...
2514 : !> \param kin_grad ...
2515 : ! **************************************************************************************************
2516 92 : SUBROUTINE fetch_and_gather_atom_chunk_grads(features, group, density_grad, grad_grad, &
2517 : kin_grad)
2518 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
2519 :
2520 : CLASS(mp_comm_type), INTENT(IN) :: group
2521 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
2522 : INTENT(OUT) :: density_grad, kin_grad
2523 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
2524 : INTENT(OUT) :: grad_grad
2525 :
2526 : INTEGER :: base, feature_pos, i, j, k, local_row, &
2527 : nflat_local, nroute_grad_per_point, &
2528 : nroute_recv_points, nroute_send_points, &
2529 : phase_handle, row
2530 92 : INTEGER, ALLOCATABLE, DIMENSION(:) :: route_grad_return_recv_counts, &
2531 92 : route_grad_return_recv_displs, &
2532 92 : route_grad_return_send_counts, &
2533 92 : route_grad_return_send_displs
2534 92 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: chunk_grad_buffer, global_grad_buffer, &
2535 92 : recv_grad_buffer, send_grad_buffer
2536 :
2537 92 : CPASSERT(features%uses_atom_chunks)
2538 :
2539 92 : nflat_local = features%nflat_local
2540 92 : IF (features%uses_atom_chunk_routing) THEN
2541 276 : nroute_recv_points = SUM(features%route_point_recv_counts)
2542 92 : nroute_send_points = SIZE(features%route_send_local_rows)
2543 276 : CPASSERT(SUM(features%route_point_send_counts) == nroute_send_points)
2544 92 : CPASSERT(SIZE(features%route_chunk_offsets) == nroute_recv_points + 1)
2545 :
2546 92 : nroute_grad_per_point = ngrad_per_point
2547 92 : IF (features%uses_collapsed_rks_dynamic) THEN
2548 86 : nroute_grad_per_point = ncollapsed_grad_per_point
2549 : END IF
2550 : ALLOCATE (send_grad_buffer(MAX(1, nroute_grad_per_point*nroute_recv_points)), &
2551 : recv_grad_buffer(MAX(1, nroute_grad_per_point*nroute_send_points)), &
2552 : route_grad_return_send_counts(SIZE(features%route_point_recv_counts)), &
2553 : route_grad_return_send_displs(SIZE(features%route_point_recv_displs)), &
2554 : route_grad_return_recv_counts(SIZE(features%route_point_send_counts)), &
2555 1196 : route_grad_return_recv_displs(SIZE(features%route_point_send_displs)))
2556 : route_grad_return_send_counts(:) = &
2557 276 : nroute_grad_per_point*features%route_point_recv_counts
2558 : route_grad_return_send_displs(:) = &
2559 276 : nroute_grad_per_point*features%route_point_recv_displs
2560 : route_grad_return_recv_counts(:) = &
2561 276 : nroute_grad_per_point*features%route_point_send_counts
2562 : route_grad_return_recv_displs(:) = &
2563 276 : nroute_grad_per_point*features%route_point_send_displs
2564 :
2565 92 : IF (features%chunk_feature_count > 0) THEN
2566 92 : CALL timeset("skala_gpw_grad_torch_pack", phase_handle)
2567 : CALL pack_atom_chunk_grads(features, send_grad_buffer, .TRUE., &
2568 92 : features%uses_collapsed_rks_dynamic)
2569 92 : CALL timestop(phase_handle)
2570 : END IF
2571 :
2572 92 : CALL timeset("skala_gpw_grad_route_comm", phase_handle)
2573 : CALL group%alltoall(send_grad_buffer, route_grad_return_send_counts, &
2574 : route_grad_return_send_displs, recv_grad_buffer, &
2575 92 : route_grad_return_recv_counts, route_grad_return_recv_displs)
2576 92 : CALL timestop(phase_handle)
2577 :
2578 92 : CALL timeset("skala_gpw_grad_route_scatter", phase_handle)
2579 : CALL scatter_routed_atom_chunk_grads(features, recv_grad_buffer, &
2580 : features%uses_collapsed_rks_dynamic, &
2581 92 : density_grad, grad_grad, kin_grad)
2582 92 : CALL timestop(phase_handle)
2583 :
2584 0 : DEALLOCATE (recv_grad_buffer, route_grad_return_recv_counts, &
2585 0 : route_grad_return_recv_displs, route_grad_return_send_counts, &
2586 276 : route_grad_return_send_displs, send_grad_buffer)
2587 : ELSE
2588 : ALLOCATE (chunk_grad_buffer(MAX(1, ngrad_per_point*features%chunk_feature_count)), &
2589 0 : global_grad_buffer(ngrad_per_point*features%nflat))
2590 0 : IF (features%chunk_feature_count > 0) THEN
2591 0 : CALL timeset("skala_gpw_grad_torch_pack", phase_handle)
2592 0 : CALL pack_atom_chunk_grads(features, chunk_grad_buffer, .FALSE.)
2593 0 : CALL timestop(phase_handle)
2594 : END IF
2595 :
2596 0 : CALL timeset("skala_gpw_grad_allgatherv", phase_handle)
2597 : CALL group%allgatherv(chunk_grad_buffer, global_grad_buffer, &
2598 0 : features%chunk_grad_counts, features%chunk_grad_displs)
2599 0 : CALL timestop(phase_handle)
2600 :
2601 0 : CALL timeset("skala_gpw_grad_scatter", phase_handle)
2602 0 : ALLOCATE (density_grad(nflat_local, 2), grad_grad(nflat_local, 3, 2), &
2603 0 : kin_grad(nflat_local, 2))
2604 0 : density_grad = 0.0_dp
2605 0 : grad_grad = 0.0_dp
2606 0 : kin_grad = 0.0_dp
2607 0 : local_row = 0
2608 0 : DO k = LBOUND(features%feature_index, 3), UBOUND(features%feature_index, 3)
2609 0 : DO j = LBOUND(features%feature_index, 2), UBOUND(features%feature_index, 2)
2610 0 : DO i = LBOUND(features%feature_index, 1), UBOUND(features%feature_index, 1)
2611 0 : local_row = local_row + 1
2612 0 : DO feature_pos = features%local_feature_offsets(local_row), &
2613 0 : features%local_feature_offsets(local_row + 1) - 1
2614 0 : row = features%local_feature_rows(feature_pos)
2615 0 : CPASSERT(row >= 1 .AND. row <= features%nflat)
2616 0 : base = ngrad_per_point*(row - 1)
2617 : density_grad(local_row, :) = density_grad(local_row, :) + &
2618 0 : global_grad_buffer(base + 1:base + 2)
2619 : grad_grad(local_row, 1, 1) = grad_grad(local_row, 1, 1) + &
2620 0 : global_grad_buffer(base + 3)
2621 : grad_grad(local_row, 2, 1) = grad_grad(local_row, 2, 1) + &
2622 0 : global_grad_buffer(base + 4)
2623 : grad_grad(local_row, 3, 1) = grad_grad(local_row, 3, 1) + &
2624 0 : global_grad_buffer(base + 5)
2625 : grad_grad(local_row, 1, 2) = grad_grad(local_row, 1, 2) + &
2626 0 : global_grad_buffer(base + 6)
2627 : grad_grad(local_row, 2, 2) = grad_grad(local_row, 2, 2) + &
2628 0 : global_grad_buffer(base + 7)
2629 : grad_grad(local_row, 3, 2) = grad_grad(local_row, 3, 2) + &
2630 0 : global_grad_buffer(base + 8)
2631 : kin_grad(local_row, :) = kin_grad(local_row, :) + &
2632 0 : global_grad_buffer(base + 9:base + 10)
2633 : END DO
2634 : END DO
2635 : END DO
2636 : END DO
2637 0 : CALL timestop(phase_handle)
2638 0 : DEALLOCATE (chunk_grad_buffer, global_grad_buffer)
2639 :
2640 : END IF
2641 :
2642 92 : END SUBROUTINE fetch_and_gather_atom_chunk_grads
2643 :
2644 : ! **************************************************************************************************
2645 : !> \brief Build the native SKALA XC virial from feature gradients.
2646 : !> \param virial_xc ...
2647 : !> \param rho_set ...
2648 : !> \param rho_r ...
2649 : !> \param grad_grad ...
2650 : ! **************************************************************************************************
2651 6 : SUBROUTINE build_virial_from_feature_grads(virial_xc, rho_set, rho_r, grad_grad)
2652 : REAL(KIND=dp), DIMENSION(3, 3), INTENT(INOUT) :: virial_xc
2653 : TYPE(xc_rho_set_type), INTENT(IN) :: rho_set
2654 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
2655 : REAL(KIND=dp), DIMENSION(:, :, :), INTENT(IN) :: grad_grad
2656 :
2657 : INTEGER :: i, idir, ipt, ispin, j, jdir, k, nspins
2658 : INTEGER, DIMENSION(2, 3) :: bo
2659 : REAL(KIND=dp) :: grad_i, tmp
2660 72 : TYPE(cp_3d_r_cp_type), DIMENSION(3) :: drho, drhoa, drhob
2661 :
2662 6 : nspins = SIZE(rho_r)
2663 60 : bo = rho_r(1)%pw_grid%bounds_local
2664 6 : ipt = 0
2665 :
2666 6 : IF (nspins == 1) THEN
2667 6 : CALL xc_rho_set_get(rho_set, drho=drho)
2668 150 : DO k = bo(1, 3), bo(2, 3)
2669 3606 : DO j = bo(1, 2), bo(2, 2)
2670 45072 : DO i = bo(1, 1), bo(2, 1)
2671 41472 : ipt = ipt + 1
2672 169344 : DO idir = 1, 3
2673 124416 : grad_i = 0.5_dp*(grad_grad(ipt, idir, 1) + grad_grad(ipt, idir, 2))
2674 539136 : DO jdir = 1, 3
2675 373248 : tmp = -grad_i*drho(jdir)%array(i, j, k)
2676 497664 : virial_xc(jdir, idir) = virial_xc(jdir, idir) + tmp
2677 : END DO
2678 : END DO
2679 : END DO
2680 : END DO
2681 : END DO
2682 : ELSE
2683 0 : CALL xc_rho_set_get(rho_set, drhoa=drhoa, drhob=drhob)
2684 0 : DO k = bo(1, 3), bo(2, 3)
2685 0 : DO j = bo(1, 2), bo(2, 2)
2686 0 : DO i = bo(1, 1), bo(2, 1)
2687 0 : ipt = ipt + 1
2688 0 : DO idir = 1, 3
2689 0 : DO jdir = 1, 3
2690 : tmp = 0.0_dp
2691 0 : DO ispin = 1, 2
2692 0 : IF (ispin == 1) THEN
2693 0 : tmp = tmp - grad_grad(ipt, idir, ispin)*drhoa(jdir)%array(i, j, k)
2694 : ELSE
2695 0 : tmp = tmp - grad_grad(ipt, idir, ispin)*drhob(jdir)%array(i, j, k)
2696 : END IF
2697 : END DO
2698 0 : virial_xc(jdir, idir) = virial_xc(jdir, idir) + tmp
2699 : END DO
2700 : END DO
2701 : END DO
2702 : END DO
2703 : END DO
2704 : END IF
2705 :
2706 6 : END SUBROUTINE build_virial_from_feature_grads
2707 :
2708 : ! **************************************************************************************************
2709 : !> \brief Print a native SKALA XC virial contribution for diagnostics.
2710 : !> \param label ...
2711 : !> \param delta ...
2712 : !> \param root_rank ...
2713 : ! **************************************************************************************************
2714 0 : SUBROUTINE print_virial_delta(label, delta, root_rank)
2715 : CHARACTER(LEN=*), INTENT(IN) :: label
2716 : REAL(KIND=dp), DIMENSION(3, 3), INTENT(IN) :: delta
2717 : LOGICAL, INTENT(IN) :: root_rank
2718 :
2719 : INTEGER :: i, iw
2720 :
2721 0 : IF (.NOT. root_rank) RETURN
2722 0 : iw = cp_logger_get_default_io_unit()
2723 0 : IF (iw <= 0) RETURN
2724 0 : WRITE (iw, "(T2,A,1X,A)") "SKALA_GPW| XC virial contribution", TRIM(label)
2725 0 : DO i = 1, 3
2726 0 : WRITE (iw, "(T2,A,1X,3ES20.10)") "SKALA_GPW|", delta(i, 1:3)
2727 : END DO
2728 :
2729 : END SUBROUTINE print_virial_delta
2730 :
2731 : ! **************************************************************************************************
2732 : !> \brief Add explicit SKALA coordinate-feature contributions to the XC virial.
2733 : !> \param virial_xc ...
2734 : !> \param features ...
2735 : !> \param atom_coord_grad_t ...
2736 : !> \param grid_coord_grad_t ...
2737 : !> \param root_rank ...
2738 : !> \param print_components ...
2739 : ! **************************************************************************************************
2740 6 : SUBROUTINE build_static_coordinate_virial(virial_xc, features, atom_coord_grad_t, &
2741 : grid_coord_grad_t, root_rank, print_components)
2742 : REAL(KIND=dp), DIMENSION(3, 3), INTENT(INOUT) :: virial_xc
2743 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
2744 : TYPE(torch_tensor_type), INTENT(INOUT) :: atom_coord_grad_t, grid_coord_grad_t
2745 : LOGICAL, INTENT(IN) :: root_rank
2746 : LOGICAL, INTENT(IN), OPTIONAL :: print_components
2747 :
2748 : INTEGER :: feature_pos, i, iatom, idir, iw, j, &
2749 : jdir, k, local_row, row
2750 : LOGICAL :: my_print_components
2751 : REAL(KIND=dp) :: tmp
2752 : REAL(KIND=dp), DIMENSION(3) :: atom_grad_sum, grid_grad_sum
2753 : REAL(KIND=dp), DIMENSION(3, 3) :: atom_virial, grid_virial
2754 6 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: atom_coord_grad, grid_coord_grad
2755 :
2756 6 : my_print_components = .FALSE.
2757 6 : IF (PRESENT(print_components)) my_print_components = print_components
2758 :
2759 6 : NULLIFY (atom_coord_grad, grid_coord_grad)
2760 6 : CALL torch_tensor_grad(features%grid_coords_t, grid_coord_grad_t)
2761 6 : CALL torch_tensor_data_ptr(grid_coord_grad_t, grid_coord_grad)
2762 6 : CALL torch_tensor_data_ptr(atom_coord_grad_t, atom_coord_grad)
2763 :
2764 6 : grid_virial = 0.0_dp
2765 6 : atom_virial = 0.0_dp
2766 6 : grid_grad_sum = 0.0_dp
2767 6 : atom_grad_sum = 0.0_dp
2768 6 : local_row = 0
2769 162 : DO k = LBOUND(features%feature_index, 3), UBOUND(features%feature_index, 3)
2770 3894 : DO j = LBOUND(features%feature_index, 2), UBOUND(features%feature_index, 2)
2771 51984 : DO i = LBOUND(features%feature_index, 1), UBOUND(features%feature_index, 1)
2772 41472 : local_row = local_row + 1
2773 124206 : DO feature_pos = features%local_feature_offsets(local_row), &
2774 44928 : features%local_feature_offsets(local_row + 1) - 1
2775 82734 : row = features%local_feature_rows(feature_pos)
2776 330936 : grid_grad_sum(:) = grid_grad_sum(:) + grid_coord_grad(:, row)
2777 372408 : DO idir = 1, 3
2778 1075542 : DO jdir = 1, 3
2779 744606 : tmp = grid_coord_grad(idir, row)*features%grid_coords(jdir, row)
2780 744606 : grid_virial(idir, jdir) = grid_virial(idir, jdir) + tmp
2781 992808 : virial_xc(idir, jdir) = virial_xc(idir, jdir) + tmp
2782 : END DO
2783 : END DO
2784 : END DO
2785 : END DO
2786 : END DO
2787 : END DO
2788 6 : CPASSERT(local_row == features%nflat_local)
2789 :
2790 6 : IF (root_rank) THEN
2791 9 : DO iatom = 1, SIZE(features%coarse_0_atomic_coords, 2)
2792 24 : atom_grad_sum(:) = atom_grad_sum(:) + atom_coord_grad(:, iatom)
2793 27 : DO idir = 1, 3
2794 78 : DO jdir = 1, 3
2795 54 : tmp = atom_coord_grad(idir, iatom)*features%coarse_0_atomic_coords(jdir, iatom)
2796 54 : atom_virial(idir, jdir) = atom_virial(idir, jdir) + tmp
2797 72 : virial_xc(idir, jdir) = virial_xc(idir, jdir) + tmp
2798 : END DO
2799 : END DO
2800 : END DO
2801 : END IF
2802 :
2803 6 : IF (my_print_components .AND. root_rank) THEN
2804 0 : iw = cp_logger_get_default_io_unit()
2805 0 : IF (iw > 0) THEN
2806 0 : CALL print_virial_delta("static-grid", grid_virial, .TRUE.)
2807 0 : CALL print_virial_delta("static-atom", atom_virial, .TRUE.)
2808 0 : WRITE (iw, "(T2,A,1X,3ES20.10)") "SKALA_GPW| XC coordinate gradient grid sum", &
2809 0 : grid_grad_sum
2810 0 : WRITE (iw, "(T2,A,1X,3ES20.10)") "SKALA_GPW| XC coordinate gradient atom sum", &
2811 0 : atom_grad_sum
2812 0 : WRITE (iw, "(T2,A,1X,3ES20.10)") "SKALA_GPW| XC coordinate gradient total sum", &
2813 0 : grid_grad_sum + atom_grad_sum
2814 : END IF
2815 : END IF
2816 :
2817 6 : CALL torch_tensor_release(grid_coord_grad_t)
2818 :
2819 6 : END SUBROUTINE build_static_coordinate_virial
2820 :
2821 : ! **************************************************************************************************
2822 : !> \brief Add residual SKALA weight-feature contributions to the XC virial.
2823 : !> \param virial_xc ...
2824 : !> \param features ...
2825 : !> \param exc ...
2826 : !> \param grid_weight_grad_t ...
2827 : !> \param atomic_grid_weight_grad_t ...
2828 : !> \param root_rank ...
2829 : !> \param print_components ...
2830 : ! **************************************************************************************************
2831 6 : SUBROUTINE build_weight_virial(virial_xc, features, exc, grid_weight_grad_t, &
2832 : atomic_grid_weight_grad_t, root_rank, print_components)
2833 : REAL(KIND=dp), DIMENSION(3, 3), INTENT(INOUT) :: virial_xc
2834 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
2835 : REAL(KIND=dp), INTENT(IN) :: exc
2836 : TYPE(torch_tensor_type), INTENT(INOUT) :: grid_weight_grad_t, &
2837 : atomic_grid_weight_grad_t
2838 : LOGICAL, INTENT(IN) :: root_rank
2839 : LOGICAL, INTENT(IN), OPTIONAL :: print_components
2840 :
2841 : INTEGER :: feature_pos, i, idir, iw, j, k, &
2842 : local_row, row
2843 : LOGICAL :: my_print_components
2844 : REAL(KIND=dp) :: atomic_tmp, exc_tmp, grid_tmp, tmp
2845 6 : REAL(KIND=dp), DIMENSION(:), POINTER :: atomic_grid_weight_grad, grid_weight_grad
2846 :
2847 6 : my_print_components = .FALSE.
2848 6 : IF (PRESENT(print_components)) my_print_components = print_components
2849 :
2850 6 : NULLIFY (atomic_grid_weight_grad, grid_weight_grad)
2851 6 : CALL torch_tensor_grad(features%grid_weights_t, grid_weight_grad_t)
2852 6 : CALL torch_tensor_grad(features%atomic_grid_weights_t, atomic_grid_weight_grad_t)
2853 6 : CALL torch_tensor_data_ptr(grid_weight_grad_t, grid_weight_grad)
2854 6 : CALL torch_tensor_data_ptr(atomic_grid_weight_grad_t, atomic_grid_weight_grad)
2855 :
2856 6 : grid_tmp = 0.0_dp
2857 6 : atomic_tmp = 0.0_dp
2858 6 : local_row = 0
2859 162 : DO k = LBOUND(features%feature_index, 3), UBOUND(features%feature_index, 3)
2860 3894 : DO j = LBOUND(features%feature_index, 2), UBOUND(features%feature_index, 2)
2861 51984 : DO i = LBOUND(features%feature_index, 1), UBOUND(features%feature_index, 1)
2862 41472 : local_row = local_row + 1
2863 124206 : DO feature_pos = features%local_feature_offsets(local_row), &
2864 44928 : features%local_feature_offsets(local_row + 1) - 1
2865 82734 : row = features%local_feature_rows(feature_pos)
2866 82734 : grid_tmp = grid_tmp + grid_weight_grad(row)*features%grid_weights(row)
2867 : atomic_tmp = atomic_tmp + &
2868 124206 : atomic_grid_weight_grad(row)*features%atomic_grid_weights(row)
2869 : END DO
2870 : END DO
2871 : END DO
2872 : END DO
2873 6 : CPASSERT(local_row == features%nflat_local)
2874 6 : exc_tmp = 0.0_dp
2875 6 : IF (root_rank) exc_tmp = -exc
2876 6 : tmp = grid_tmp + atomic_tmp + exc_tmp
2877 :
2878 6 : IF (my_print_components .AND. root_rank) THEN
2879 0 : iw = cp_logger_get_default_io_unit()
2880 0 : IF (iw > 0) THEN
2881 0 : WRITE (iw, "(T2,A,1X,ES20.10)") "SKALA_GPW| XC virial weight grid", grid_tmp
2882 0 : WRITE (iw, "(T2,A,1X,ES20.10)") "SKALA_GPW| XC virial weight atomic", atomic_tmp
2883 0 : WRITE (iw, "(T2,A,1X,ES20.10)") "SKALA_GPW| XC virial weight final", exc_tmp
2884 0 : WRITE (iw, "(T2,A,1X,ES20.10)") "SKALA_GPW| XC virial weight residual", tmp
2885 : END IF
2886 : END IF
2887 :
2888 24 : DO idir = 1, 3
2889 24 : virial_xc(idir, idir) = virial_xc(idir, idir) + tmp
2890 : END DO
2891 :
2892 6 : CALL torch_tensor_release(grid_weight_grad_t)
2893 6 : CALL torch_tensor_release(atomic_grid_weight_grad_t)
2894 :
2895 6 : END SUBROUTINE build_weight_virial
2896 :
2897 : ! **************************************************************************************************
2898 : !> \brief Fill CP2K VXC real-space arrays from Torch feature gradients.
2899 : !> \param vxc_rho ...
2900 : !> \param vxc_tau ...
2901 : !> \param rho_r ...
2902 : !> \param pw_pool ...
2903 : !> \param density_grad ...
2904 : !> \param grad_grad ...
2905 : !> \param kin_grad ...
2906 : !> \param xc_deriv_method_id ...
2907 : !> \param global_grid_layout ...
2908 : ! **************************************************************************************************
2909 358 : SUBROUTINE build_vxc_from_feature_grads(vxc_rho, vxc_tau, rho_r, pw_pool, &
2910 358 : density_grad, grad_grad, kin_grad, &
2911 : xc_deriv_method_id, global_grid_layout)
2912 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: vxc_rho, vxc_tau, rho_r
2913 : TYPE(pw_pool_type), POINTER :: pw_pool
2914 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: density_grad
2915 : REAL(KIND=dp), DIMENSION(:, :, :), INTENT(IN) :: grad_grad
2916 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: kin_grad
2917 : INTEGER, INTENT(IN) :: xc_deriv_method_id
2918 : LOGICAL, INTENT(IN), OPTIONAL :: global_grid_layout
2919 :
2920 : INTEGER :: i, ipt, ispin, j, k, nspins
2921 : INTEGER, DIMENSION(2, 3) :: bo
2922 : LOGICAL :: my_global_grid_layout, valid_spin_shape
2923 : REAL(KIND=dp) :: dvol_inv
2924 : TYPE(pw_c1d_gs_type) :: tmp_g, vxc_g
2925 1432 : TYPE(pw_r3d_rs_type), DIMENSION(3) :: grad_pw
2926 :
2927 358 : nspins = SIZE(rho_r)
2928 3580 : bo = rho_r(1)%pw_grid%bounds_local
2929 358 : dvol_inv = 1.0_dp/rho_r(1)%pw_grid%dvol
2930 358 : my_global_grid_layout = .FALSE.
2931 358 : IF (PRESENT(global_grid_layout)) my_global_grid_layout = global_grid_layout
2932 254 : IF (my_global_grid_layout) THEN
2933 1016 : CPASSERT(SIZE(density_grad, 1) == PRODUCT(rho_r(1)%pw_grid%npts))
2934 : END IF
2935 358 : CPASSERT(SIZE(kin_grad, 2) == SIZE(density_grad, 2))
2936 358 : CPASSERT(SIZE(grad_grad, 3) == SIZE(density_grad, 2))
2937 358 : CPASSERT(SIZE(grad_grad, 2) == 3)
2938 : valid_spin_shape = SIZE(density_grad, 2) == nspins .OR. &
2939 358 : (nspins == 1 .AND. SIZE(density_grad, 2) == 2)
2940 0 : CPASSERT(valid_spin_shape)
2941 :
2942 2236 : ALLOCATE (vxc_rho(nspins), vxc_tau(nspins))
2943 760 : DO ispin = 1, nspins
2944 402 : CALL pw_pool%create_pw(vxc_rho(ispin))
2945 402 : CALL pw_pool%create_pw(vxc_tau(ispin))
2946 402 : CALL pw_zero(vxc_rho(ispin))
2947 760 : CALL pw_zero(vxc_tau(ispin))
2948 : END DO
2949 :
2950 358 : IF (xc_requires_tmp_g(xc_deriv_method_id) .OR. rho_r(1)%pw_grid%spherical) THEN
2951 358 : CALL pw_pool%create_pw(vxc_g)
2952 358 : IF (.NOT. rho_r(1)%pw_grid%spherical) CALL pw_pool%create_pw(tmp_g)
2953 : END IF
2954 :
2955 760 : DO ispin = 1, nspins
2956 1608 : DO i = 1, 3
2957 1206 : CALL pw_pool%create_pw(grad_pw(i))
2958 1608 : CALL pw_zero(grad_pw(i))
2959 : END DO
2960 :
2961 402 : ipt = 0
2962 8144 : DO k = bo(1, 3), bo(2, 3)
2963 184930 : DO j = bo(1, 2), bo(2, 2)
2964 2632619 : DO i = bo(1, 1), bo(2, 1)
2965 2448091 : IF (my_global_grid_layout) THEN
2966 : ipt = 1 + i - rho_r(1)%pw_grid%bounds(1, 1) + &
2967 : rho_r(1)%pw_grid%npts(1)*( &
2968 : j - rho_r(1)%pw_grid%bounds(1, 2) + &
2969 : rho_r(1)%pw_grid%npts(2)*( &
2970 1159908 : k - rho_r(1)%pw_grid%bounds(1, 3)))
2971 : ELSE
2972 1288183 : ipt = ipt + 1
2973 : END IF
2974 2624877 : IF (nspins == 1) THEN
2975 1773091 : IF (SIZE(density_grad, 2) == 1) THEN
2976 1031658 : vxc_rho(1)%array(i, j, k) = 0.5_dp*dvol_inv*density_grad(ipt, 1)
2977 1031658 : vxc_tau(1)%array(i, j, k) = 0.5_dp*dvol_inv*kin_grad(ipt, 1)
2978 1031658 : grad_pw(1)%array(i, j, k) = 0.5_dp*dvol_inv*grad_grad(ipt, 1, 1)
2979 1031658 : grad_pw(2)%array(i, j, k) = 0.5_dp*dvol_inv*grad_grad(ipt, 2, 1)
2980 1031658 : grad_pw(3)%array(i, j, k) = 0.5_dp*dvol_inv*grad_grad(ipt, 3, 1)
2981 : ELSE
2982 : vxc_rho(1)%array(i, j, k) = 0.5_dp*dvol_inv* &
2983 741433 : (density_grad(ipt, 1) + density_grad(ipt, 2))
2984 : vxc_tau(1)%array(i, j, k) = 0.5_dp*dvol_inv* &
2985 741433 : (kin_grad(ipt, 1) + kin_grad(ipt, 2))
2986 : grad_pw(1)%array(i, j, k) = 0.5_dp*dvol_inv* &
2987 741433 : (grad_grad(ipt, 1, 1) + grad_grad(ipt, 1, 2))
2988 : grad_pw(2)%array(i, j, k) = 0.5_dp*dvol_inv* &
2989 741433 : (grad_grad(ipt, 2, 1) + grad_grad(ipt, 2, 2))
2990 : grad_pw(3)%array(i, j, k) = 0.5_dp*dvol_inv* &
2991 741433 : (grad_grad(ipt, 3, 1) + grad_grad(ipt, 3, 2))
2992 : END IF
2993 : ELSE
2994 675000 : vxc_rho(ispin)%array(i, j, k) = dvol_inv*density_grad(ipt, ispin)
2995 675000 : vxc_tau(ispin)%array(i, j, k) = dvol_inv*kin_grad(ipt, ispin)
2996 675000 : grad_pw(1)%array(i, j, k) = dvol_inv*grad_grad(ipt, 1, ispin)
2997 675000 : grad_pw(2)%array(i, j, k) = dvol_inv*grad_grad(ipt, 2, ispin)
2998 675000 : grad_pw(3)%array(i, j, k) = dvol_inv*grad_grad(ipt, 3, ispin)
2999 : END IF
3000 : END DO
3001 : END DO
3002 : END DO
3003 :
3004 1608 : DO i = 1, 3
3005 1608 : CALL pw_scale(grad_pw(i), -1.0_dp)
3006 : END DO
3007 402 : CALL xc_pw_divergence(xc_deriv_method_id, grad_pw, tmp_g, vxc_g, vxc_rho(ispin))
3008 :
3009 1966 : DO i = 1, 3
3010 1608 : CALL pw_pool%give_back_pw(grad_pw(i))
3011 : END DO
3012 : END DO
3013 :
3014 358 : IF (ASSOCIATED(vxc_g%pw_grid)) CALL pw_pool%give_back_pw(vxc_g)
3015 358 : IF (ASSOCIATED(tmp_g%pw_grid)) CALL pw_pool%give_back_pw(tmp_g)
3016 :
3017 358 : END SUBROUTINE build_vxc_from_feature_grads
3018 :
3019 : ! **************************************************************************************************
3020 : !> \brief Print optional diagnostics for the CP2K-native SKALA GPW feature block.
3021 : !> \param features ...
3022 : !> \param print_active ...
3023 : ! **************************************************************************************************
3024 24 : SUBROUTINE print_native_grid_diagnostics(features, print_active)
3025 : TYPE(skala_gpw_feature_type), INTENT(IN) :: features
3026 : LOGICAL, INTENT(IN) :: print_active
3027 :
3028 : INTEGER :: atom_rows_max, atom_rows_min, &
3029 : chunk_rows_max, chunk_rows_min, iw
3030 : REAL(KIND=dp) :: chunk_imbalance
3031 :
3032 24 : IF (.NOT. print_active) RETURN
3033 :
3034 12 : iw = cp_logger_get_default_io_unit()
3035 12 : IF (iw <= 0) RETURN
3036 : WRITE (UNIT=iw, FMT="(/,T2,A,1X,ES19.11)") &
3037 12 : "SKALA_GPW| Native grid feature electrons", features%electron_count
3038 : WRITE (UNIT=iw, FMT="(T2,A,1X,ES19.11)") &
3039 12 : "SKALA_GPW| Native grid feature spin moment", features%spin_moment
3040 : WRITE (UNIT=iw, FMT="(T2,A,1X,ES19.11)") &
3041 12 : "SKALA_GPW| Native grid feature kinetic integral", features%kinetic_integral
3042 : WRITE (UNIT=iw, FMT="(T2,A,1X,ES19.11)") &
3043 12 : "SKALA_GPW| Native grid feature weight sum", features%grid_weight_sum
3044 12 : IF (ALLOCATED(features%atomic_grid_sizes)) THEN
3045 49 : atom_rows_min = INT(MINVAL(features%atomic_grid_sizes))
3046 49 : atom_rows_max = INT(MAXVAL(features%atomic_grid_sizes))
3047 : WRITE (UNIT=iw, FMT="(T2,A,1X,I0,1X,A,1X,I0,1X,A,1X,I0)") &
3048 12 : "SKALA_GPW| Native grid atom row range", atom_rows_min, "to", &
3049 61 : atom_rows_max, "sum", INT(SUM(features%atomic_grid_sizes))
3050 : END IF
3051 12 : IF (features%uses_atom_chunks) THEN
3052 : WRITE (UNIT=iw, FMT="(T2,A,1X,I0,1X,A,1X,I0)") &
3053 12 : "SKALA_GPW| Native grid atom chunk rows", features%chunk_feature_count, &
3054 24 : "of", features%nflat
3055 12 : IF (ALLOCATED(features%chunk_grad_counts)) THEN
3056 36 : chunk_rows_min = MINVAL(features%chunk_grad_counts)/ngrad_per_point
3057 36 : chunk_rows_max = MAXVAL(features%chunk_grad_counts)/ngrad_per_point
3058 12 : chunk_imbalance = REAL(chunk_rows_max, KIND=dp)/REAL(MAX(1, chunk_rows_min), KIND=dp)
3059 : WRITE (UNIT=iw, FMT="(T2,A,1X,I0,1X,A,1X,I0,1X,A,1X,ES12.5)") &
3060 12 : "SKALA_GPW| Native grid atom chunk row range", chunk_rows_min, &
3061 24 : "to", chunk_rows_max, "imbalance", chunk_imbalance
3062 : END IF
3063 : END IF
3064 :
3065 : END SUBROUTINE print_native_grid_diagnostics
3066 :
3067 : ! **************************************************************************************************
3068 : !> \brief Resolve the GPU/CUDA execution mode for the SKALA_NATIVE path.
3069 : !> \param xc_section ...
3070 : !> \return true when Torch should run on CUDA
3071 : ! **************************************************************************************************
3072 760 : FUNCTION skala_execution_use_cuda(xc_section) RESULT(use_cuda)
3073 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
3074 : LOGICAL :: use_cuda
3075 :
3076 : INTEGER :: gpu_mode
3077 : TYPE(section_vals_type), POINTER :: gauxc_section, native_section
3078 :
3079 760 : gpu_mode = do_gpu_auto
3080 760 : native_section => get_skala_native_section(xc_section)
3081 760 : IF (ASSOCIATED(native_section)) THEN
3082 752 : CALL section_vals_val_get(native_section, "USE_GPU", i_val=gpu_mode)
3083 : ELSE
3084 8 : gauxc_section => get_gauxc_section(xc_section)
3085 8 : IF (ASSOCIATED(gauxc_section)) THEN
3086 8 : CALL section_vals_val_get(gauxc_section, "USE_GPU", i_val=gpu_mode)
3087 : END IF
3088 : END IF
3089 :
3090 : SELECT CASE (gpu_mode)
3091 : CASE (do_gpu_auto)
3092 : #if defined(__LIBTORCH_CUDA)
3093 : use_cuda = .TRUE.
3094 : #else
3095 : use_cuda = .FALSE.
3096 : #endif
3097 : CASE (do_gpu_host)
3098 : use_cuda = .FALSE.
3099 : CASE (do_gpu_device)
3100 0 : use_cuda = .TRUE.
3101 : CASE DEFAULT
3102 760 : CALL cp_abort(__LOCATION__, "Unknown SKALA execution-space value.")
3103 : END SELECT
3104 :
3105 0 : IF (use_cuda) THEN
3106 0 : IF (.NOT. torch_cuda_is_available() .OR. torch_cuda_device_count() <= 0) THEN
3107 : CALL cp_abort(__LOCATION__, &
3108 : "GPU execution was requested, but no usable CUDA device was "// &
3109 0 : "found. Set USE_GPU HOST to force CPU execution.")
3110 : END IF
3111 : END IF
3112 :
3113 760 : END FUNCTION skala_execution_use_cuda
3114 :
3115 : ! **************************************************************************************************
3116 : !> \brief Configure CUDA device selection for the native SKALA GPW Torch path.
3117 : !> \param use_cuda ...
3118 : !> \param group ...
3119 : !> \return selected CUDA device, or -1 when CUDA is disabled
3120 : ! **************************************************************************************************
3121 362 : FUNCTION configure_native_grid_cuda(use_cuda, group) RESULT(selected_device)
3122 : LOGICAL, INTENT(IN) :: use_cuda
3123 :
3124 : CLASS(mp_comm_type), INTENT(IN) :: group
3125 :
3126 : INTEGER :: cuda_device_count, iw, pe, selected_device
3127 362 : INTEGER, ALLOCATABLE, DIMENSION(:) :: selected_devices
3128 :
3129 : selected_device = -1
3130 :
3131 362 : IF (.NOT. use_cuda) RETURN
3132 :
3133 0 : IF (.NOT. torch_cuda_is_available()) THEN
3134 0 : cuda_device_count = 0
3135 : ELSE
3136 0 : cuda_device_count = torch_cuda_device_count()
3137 : END IF
3138 0 : IF (cuda_device_count <= 0) THEN
3139 : CALL cp_abort(__LOCATION__, &
3140 : "GPU execution was requested, but no usable CUDA device was "// &
3141 0 : "found. Set USE_GPU HOST to force CPU execution.")
3142 : END IF
3143 0 : IF (offload_get_device_count() > 0) THEN
3144 0 : selected_device = offload_get_chosen_device()
3145 : ELSE
3146 0 : selected_device = MOD(group%mepos, cuda_device_count)
3147 : END IF
3148 0 : IF (selected_device < 0 .OR. selected_device >= cuda_device_count) THEN
3149 : CALL cp_abort(__LOCATION__, &
3150 : "CP2K selected CUDA device outside the range visible to Torch. "// &
3151 0 : "Check CUDA_VISIBLE_DEVICES and the offload device selection.")
3152 : END IF
3153 :
3154 0 : ALLOCATE (selected_devices(group%num_pe))
3155 0 : CALL group%allgather(selected_device, selected_devices)
3156 :
3157 0 : IF (group%mepos /= 0) THEN
3158 0 : DEALLOCATE (selected_devices)
3159 0 : RETURN
3160 : END IF
3161 : IF (selected_device == logged_cuda_device .AND. &
3162 0 : cuda_device_count == logged_cuda_device_count .AND. &
3163 : group%num_pe == logged_cuda_nproc) THEN
3164 0 : DEALLOCATE (selected_devices)
3165 0 : RETURN
3166 : END IF
3167 :
3168 0 : iw = cp_logger_get_default_io_unit()
3169 0 : IF (iw <= 0) THEN
3170 0 : DEALLOCATE (selected_devices)
3171 0 : RETURN
3172 : END IF
3173 : WRITE (UNIT=iw, FMT="(/,T2,A,1X,I0,1X,A,1X,I0)") &
3174 0 : "SKALA_GPW| Native grid Torch CUDA device", selected_device, &
3175 0 : "of", cuda_device_count
3176 : WRITE (UNIT=iw, FMT="(T2,A)", ADVANCE="NO") &
3177 0 : "SKALA_GPW| Native grid Torch CUDA rank devices"
3178 0 : DO pe = 1, group%num_pe
3179 0 : WRITE (UNIT=iw, FMT="(1X,I0,A,I0)", ADVANCE="NO") pe - 1, ":", selected_devices(pe)
3180 : END DO
3181 0 : WRITE (UNIT=iw, FMT=*)
3182 :
3183 0 : logged_cuda_device = selected_device
3184 0 : logged_cuda_device_count = cuda_device_count
3185 0 : logged_cuda_nproc = group%num_pe
3186 0 : DEALLOCATE (selected_devices)
3187 :
3188 362 : END FUNCTION configure_native_grid_cuda
3189 :
3190 : ! **************************************************************************************************
3191 : !> \brief Load and cache the TorchScript SKALA model.
3192 : !> \param model_path ...
3193 : !> \param cuda_device ...
3194 : !> \param xc_section XC settings, including the native model precision.
3195 : !> \param higher_derivatives Require a model prepared for second input derivatives.
3196 : ! **************************************************************************************************
3197 340 : SUBROUTINE ensure_model_loaded(model_path, cuda_device, xc_section, higher_derivatives)
3198 : CHARACTER(len=*), INTENT(IN) :: model_path
3199 : INTEGER, INTENT(IN) :: cuda_device
3200 : TYPE(section_vals_type), POINTER :: xc_section
3201 : LOGICAL, INTENT(IN), OPTIONAL :: higher_derivatives
3202 :
3203 : INTEGER :: iw
3204 : LOGICAL :: fp64, prepare_derivatives
3205 : TYPE(section_vals_type), POINTER :: native_section
3206 :
3207 340 : fp64 = .TRUE.
3208 340 : prepare_derivatives = .FALSE.
3209 2 : IF (PRESENT(higher_derivatives)) prepare_derivatives = higher_derivatives
3210 340 : native_section => get_skala_native_section(xc_section)
3211 340 : IF (ASSOCIATED(native_section)) THEN
3212 336 : CALL section_vals_val_get(native_section, "MODEL_FP64", l_val=fp64)
3213 : END IF
3214 340 : fp64 = fp64 .OR. prepare_derivatives
3215 :
3216 340 : IF (cached_model_loaded) THEN
3217 : IF (TRIM(cached_model_path) == TRIM(model_path) .AND. &
3218 : cached_model_cuda_device == cuda_device .AND. &
3219 323 : (cached_model_fp64 .EQV. fp64) .AND. &
3220 321 : (.NOT. prepare_derivatives .OR. cached_model_higher_derivatives)) RETURN
3221 2 : CALL skala_torch_model_release(cached_model)
3222 2 : cached_model_loaded = .FALSE.
3223 : END IF
3224 :
3225 : CALL skala_torch_model_load(cached_model, TRIM(model_path), fp64=fp64, &
3226 19 : higher_derivatives=prepare_derivatives)
3227 19 : cached_model_path = model_path
3228 19 : cached_model_cuda_device = cuda_device
3229 19 : cached_model_fp64 = fp64
3230 19 : cached_model_higher_derivatives = prepare_derivatives
3231 19 : cached_model_loaded = .TRUE.
3232 :
3233 19 : IF (cuda_device >= 0) THEN
3234 0 : iw = cp_logger_get_default_io_unit()
3235 0 : IF (iw > 0) THEN
3236 : WRITE (UNIT=iw, FMT="(T2,A,1X,I0)") &
3237 0 : "SKALA_GPW| LibTorch CUDA model active on device", cuda_device
3238 : END IF
3239 : END IF
3240 :
3241 340 : END SUBROUTINE ensure_model_loaded
3242 :
3243 : ! **************************************************************************************************
3244 : !> \brief Resolve the SKALA TorchScript model path from the SKALA_NATIVE subsection.
3245 : !> \param xc_section ...
3246 : !> \param model_path ...
3247 : ! **************************************************************************************************
3248 362 : SUBROUTINE get_skala_model_path(xc_section, model_path)
3249 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
3250 : CHARACTER(len=default_path_length), INTENT(OUT) :: model_path
3251 :
3252 : CHARACTER(len=default_path_length) :: model_key
3253 : INTEGER :: env_status
3254 : LOGICAL :: use_cuda
3255 : TYPE(section_vals_type), POINTER :: gauxc_section, native_section
3256 :
3257 362 : use_cuda = skala_execution_use_cuda(xc_section)
3258 362 : native_section => get_skala_native_section(xc_section)
3259 362 : IF (ASSOCIATED(native_section)) THEN
3260 358 : CALL section_vals_val_get(native_section, "MODEL", c_val=model_path)
3261 : ELSE
3262 4 : gauxc_section => get_gauxc_section(xc_section)
3263 4 : IF (.NOT. ASSOCIATED(gauxc_section)) THEN
3264 0 : CPABORT("Native SKALA GPW requires an XC_FUNCTIONAL%SKALA_NATIVE section")
3265 : END IF
3266 4 : CALL section_vals_val_get(gauxc_section, "MODEL", c_val=model_path)
3267 : END IF
3268 362 : model_key = ADJUSTL(model_path)
3269 362 : CALL uppercase(model_key)
3270 362 : IF (TRIM(model_key) == "NONE" .OR. TRIM(model_key) == "") THEN
3271 0 : CPABORT("SKALA requires MODEL SKALA or a TorchScript model path")
3272 362 : ELSE IF (TRIM(model_key) == "SKALA") THEN
3273 362 : IF (use_cuda) THEN
3274 0 : CALL GET_ENVIRONMENT_VARIABLE("SKALA_CUDA_MODEL", model_path, STATUS=env_status)
3275 0 : IF (env_status /= 0 .OR. LEN_TRIM(model_path) == 0) THEN
3276 : CALL cp_abort(__LOCATION__, &
3277 0 : "GPU execution of SKALA requires SKALA_CUDA_MODEL")
3278 : END IF
3279 : ELSE
3280 362 : CALL GET_ENVIRONMENT_VARIABLE("SKALA_MODEL", model_path, STATUS=env_status)
3281 362 : IF (env_status /= 0 .OR. LEN_TRIM(model_path) == 0) THEN
3282 : CALL cp_abort(__LOCATION__, &
3283 0 : "SKALA requires the SKALA_MODEL environment variable")
3284 : END IF
3285 : END IF
3286 : END IF
3287 :
3288 362 : END SUBROUTINE get_skala_model_path
3289 :
3290 : ! **************************************************************************************************
3291 : !> \brief Release all module-level SKALA/Torch caches.
3292 : ! **************************************************************************************************
3293 1492 : SUBROUTINE skala_gpw_release_caches()
3294 1492 : IF (cached_model_loaded) THEN
3295 17 : CALL skala_torch_model_release(cached_model)
3296 17 : cached_model_loaded = .FALSE.
3297 : END IF
3298 1492 : IF (cached_atom_cuda_tensors_active) THEN
3299 0 : CALL torch_tensor_release(cached_atom_cuda_tensors%grid_coords_t)
3300 0 : CALL torch_tensor_release(cached_atom_cuda_tensors%grid_weights_t)
3301 0 : CALL torch_tensor_release(cached_atom_cuda_tensors%atomic_grid_weights_t)
3302 0 : CALL torch_tensor_release(cached_atom_cuda_tensors%atomic_coords_t)
3303 0 : CALL torch_tensor_release(cached_atom_cuda_tensors%density_t)
3304 0 : CALL torch_tensor_release(cached_atom_cuda_tensors%grad_t)
3305 0 : CALL torch_tensor_release(cached_atom_cuda_tensors%kin_t)
3306 0 : cached_atom_cuda_tensors_active = .FALSE.
3307 : END IF
3308 1492 : CALL skala_gpw_features_release_cache()
3309 1492 : END SUBROUTINE skala_gpw_release_caches
3310 :
3311 : ! **************************************************************************************************
3312 : !> \brief Return the CP2K-native SKALA functional subsection, if present.
3313 : !> \param xc_section ...
3314 : !> \return ...
3315 : ! **************************************************************************************************
3316 699073 : FUNCTION get_skala_native_section(xc_section) RESULT(native_section)
3317 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
3318 : TYPE(section_vals_type), POINTER :: native_section
3319 :
3320 : INTEGER :: ifun
3321 : TYPE(section_vals_type), POINTER :: functionals, xc_fun
3322 :
3323 699073 : NULLIFY (native_section)
3324 699073 : IF (.NOT. ASSOCIATED(xc_section)) RETURN
3325 :
3326 699073 : functionals => section_vals_get_subs_vals(xc_section, "XC_FUNCTIONAL")
3327 699073 : IF (.NOT. ASSOCIATED(functionals)) RETURN
3328 :
3329 699073 : ifun = 0
3330 : DO
3331 1331122 : ifun = ifun + 1
3332 1331122 : xc_fun => section_vals_get_subs_vals2(functionals, i_section=ifun)
3333 1331122 : IF (.NOT. ASSOCIATED(xc_fun)) EXIT
3334 1331122 : IF (xc_fun%section%name == "SKALA_NATIVE") THEN
3335 : native_section => xc_fun
3336 : EXIT
3337 : END IF
3338 : END DO
3339 :
3340 : END FUNCTION get_skala_native_section
3341 :
3342 : ! **************************************************************************************************
3343 : !> \brief Return the first GAUXC functional subsection, if present.
3344 : !> \param xc_section ...
3345 : !> \return ...
3346 : ! **************************************************************************************************
3347 73218 : FUNCTION get_gauxc_section(xc_section) RESULT(gauxc_section)
3348 : TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
3349 : TYPE(section_vals_type), POINTER :: gauxc_section
3350 :
3351 : INTEGER :: ifun
3352 : TYPE(section_vals_type), POINTER :: functionals, xc_fun
3353 :
3354 73218 : NULLIFY (gauxc_section)
3355 73218 : IF (.NOT. ASSOCIATED(xc_section)) RETURN
3356 :
3357 73218 : functionals => section_vals_get_subs_vals(xc_section, "XC_FUNCTIONAL")
3358 73218 : IF (.NOT. ASSOCIATED(functionals)) RETURN
3359 :
3360 73218 : ifun = 0
3361 : DO
3362 131062 : ifun = ifun + 1
3363 131062 : xc_fun => section_vals_get_subs_vals2(functionals, i_section=ifun)
3364 131062 : IF (.NOT. ASSOCIATED(xc_fun)) EXIT
3365 131062 : IF (xc_fun%section%name == "GAUXC") THEN
3366 : gauxc_section => xc_fun
3367 : EXIT
3368 : END IF
3369 : END DO
3370 :
3371 : END FUNCTION get_gauxc_section
3372 :
3373 0 : END MODULE skala_gpw_functional
|