28#include "./base/base_uses.f90"
34 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'skala_gpw_features'
35 REAL(KIND=
dp),
PARAMETER,
PRIVATE :: layout_tol = 1.0e-12_dp
36 INTEGER,
PARAMETER,
PRIVATE :: ndynamic_per_point = 10, nrks_dynamic_per_point = 5, &
37 nstatic_per_point = 5, ngrad_per_point = 10
40 REAL(kind=
dp),
PARAMETER,
PRIVATE :: smooth_partition_eps = 1.0e-12_dp
46 TYPE skala_gpw_layout_cache_type
47 INTEGER :: chunk_atom_begin = 1, chunk_atom_end = 0, &
48 chunk_feature_begin = 1, &
49 chunk_feature_count = 0, chunk_natom = 0, &
50 natom = 0, nflat = 0, nflat_local = 0, &
51 npoint = 0, nproc = 0, &
53 INTEGER,
DIMENSION(2, 3) :: bo = 0, bounds = 0
54 INTEGER,
DIMENSION(3) :: npts = 0
55 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: dynamic_counts, dynamic_displs, &
56 chunk_feature_counts, chunk_feature_displs, &
57 chunk_grad_counts, chunk_grad_displs, &
58 feature_counts, feature_displs, &
59 feature_source_points, global_to_feature, &
60 local_feature_counts, local_feature_offsets, &
61 local_feature_points, local_feature_rows, &
62 route_grad_return_recv_counts, &
63 route_grad_return_recv_displs, &
64 route_grad_return_send_counts, &
65 route_grad_return_send_displs, &
66 route_local_dest, chunk_return_positions, &
67 route_point_recv_counts, &
68 route_point_recv_displs, &
69 route_point_send_counts, &
70 route_point_send_displs, &
72 INTEGER,
ALLOCATABLE,
DIMENSION(:, :, :) :: feature_index
73 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:) :: atomic_grid_sizes, chunk_atomic_grid_sizes, &
75 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:) :: local_feature_indices
76 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:, :) :: atomic_grid_size_bound_shape, &
77 chunk_atomic_grid_size_bound_shape
105 REAL(kind=
dp) :: dvol = 0.0_dp, weight_sum = 0.0_dp, &
106 weight_sumsq = 0.0_dp
107 REAL(kind=
dp),
DIMENSION(3, 3) :: cell_hmat = 0.0_dp, dh = 0.0_dp
108 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: atomic_grid_weights, chunk_atomic_grid_weights, &
109 chunk_grid_weights, grid_weights
110 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: atom_coords, chunk_coarse_0_atomic_coords, &
111 chunk_grid_coords, coarse_0_atomic_coords, &
113 LOGICAL :: active = .false., has_weights = .false., &
114 chunk_dynamic_input_views_active = .false., &
115 chunk_dynamic_tensors_active = .false., &
116 chunk_inputs_active = .false., &
117 chunk_inputs_use_collapsed_rks = .false., &
118 chunk_static_tensors_active = .false., &
119 dynamic_tensors_active = .false., &
120 inputs_active = .false., &
121 static_tensors_active = .false.
122 END TYPE skala_gpw_layout_cache_type
125 INTEGER :: chunk_feature_count = 0, nflat = 0, &
142 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: chunk_grad_counts, chunk_grad_displs, &
143 local_feature_counts, local_feature_offsets, &
144 local_feature_rows, &
145 chunk_return_positions, &
146 route_grad_return_recv_counts, &
147 route_grad_return_recv_displs, &
148 route_grad_return_send_counts, &
149 route_grad_return_send_displs, &
150 route_point_recv_counts, &
151 route_point_recv_displs, &
152 route_point_send_counts, &
153 route_point_send_displs, &
154 route_send_local_rows
155 INTEGER,
ALLOCATABLE,
DIMENSION(:, :, :) :: feature_index
156 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:) :: atomic_grid_sizes
157 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:, :) :: atomic_grid_size_bound_shape
158 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: atomic_grid_weights, grid_weights
159 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: chunk_density, chunk_kin, &
160 coarse_0_atomic_coords, density, &
162 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: chunk_grad, grad
163 REAL(kind=
dp) :: electron_count = 0.0_dp, &
164 grid_weight_sum = 0.0_dp, &
166 LOGICAL :: active = .false., owns_coordinate_tensor = .false., &
167 owns_grid_coordinate_tensor = .false., &
168 owns_weight_tensors = .false., &
169 owns_dynamic_tensors = .true., &
170 owns_inputs = .true., &
171 owns_static_tensors = .true., &
172 uses_atom_chunk_routing = .false., &
173 uses_atom_chunks = .false., &
174 uses_collapsed_rks_dynamic = .false.
197 requires_grad, weights, requires_coordinate_grad, &
198 requires_stress_grad, use_atom_chunks, route_atom_chunks, &
205 LOGICAL,
INTENT(IN),
OPTIONAL :: requires_grad
207 LOGICAL,
INTENT(IN),
OPTIONAL :: requires_coordinate_grad, &
208 requires_stress_grad, use_atom_chunks, &
210 INTEGER,
INTENT(IN),
OPTIONAL :: atom_partition
212 INTEGER :: handle, i, ipt, ispin, j, k, local_row, my_atom_partition, &
213 ndynamic_local_per_point, nflat, nflat_local, nspins, phase_handle, real_base, row
214 INTEGER,
DIMENSION(2, 3) :: bo
215 LOGICAL :: collapse_spin_dynamics, my_requires_coordinate_grad, my_requires_grad, &
216 my_requires_stress_grad, my_route_atom_chunks, my_use_atom_chunks, &
217 use_atom_chunk_protocol, use_atom_chunk_routing
218 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: global_dynamic, local_dynamic
219 REAL(kind=
dp),
DIMENSION(:, :, :),
POINTER :: rho, rhoa, rhob, tau_a, tau_b, tau_total
223 CALL timeset(
"skala_gpw_feature_build", handle)
225 my_requires_grad = .false.
226 IF (
PRESENT(requires_grad)) my_requires_grad = requires_grad
227 my_requires_coordinate_grad = .false.
228 IF (
PRESENT(requires_coordinate_grad))
THEN
229 my_requires_coordinate_grad = requires_coordinate_grad
231 my_requires_stress_grad = .false.
232 IF (
PRESENT(requires_stress_grad)) my_requires_stress_grad = requires_stress_grad
233 my_use_atom_chunks = .false.
234 IF (
PRESENT(use_atom_chunks)) my_use_atom_chunks = use_atom_chunks
235 my_route_atom_chunks = .false.
236 IF (
PRESENT(route_atom_chunks)) my_route_atom_chunks = route_atom_chunks
238 IF (
PRESENT(atom_partition)) my_atom_partition = atom_partition
241 CALL cp_abort(__location__,
"Unknown native SKALA atom-partition mode.")
243 cpassert(
ASSOCIATED(cell))
244 cpassert(
ASSOCIATED(particle_set))
245 cpassert(
SIZE(rho_r) == 1 .OR.
SIZE(rho_r) == 2)
246 cpassert(
ASSOCIATED(rho_r(1)%pw_grid))
247 pw_grid => rho_r(1)%pw_grid
250 bo = pw_grid%bounds_local
251 nflat_local = pw_grid%ngpts_local
253 CALL timeset(
"skala_gpw_pre_release", phase_handle)
255 CALL timestop(phase_handle)
257 CALL timeset(
"skala_gpw_layout_cache", phase_handle)
258 CALL ensure_layout_cache(pw_grid, particle_set, cell, weights, my_atom_partition)
259 CALL timestop(phase_handle)
261 use_atom_chunk_protocol = my_use_atom_chunks .AND. &
262 .NOT. (my_requires_coordinate_grad .OR. my_requires_stress_grad)
263 use_atom_chunk_routing = use_atom_chunk_protocol .AND. my_route_atom_chunks
264 collapse_spin_dynamics = nspins == 1 .AND. use_atom_chunk_routing
265 ndynamic_local_per_point = ndynamic_per_point
266 IF (collapse_spin_dynamics) ndynamic_local_per_point = nrks_dynamic_per_point
267 ALLOCATE (local_dynamic(ndynamic_local_per_point*nflat_local))
268 local_dynamic = 0.0_dp
270 CALL timeset(
"skala_gpw_pack_local", phase_handle)
271 IF (nspins == 1)
THEN
274 CALL xc_rho_set_get(rho_set, rhoa=rhoa, rhob=rhob, drhoa=drhoa, drhob=drhob, &
275 tau_a=tau_a, tau_b=tau_b)
279 DO k = bo(1, 3), bo(2, 3)
280 DO j = bo(1, 2), bo(2, 2)
281 DO i = bo(1, 1), bo(2, 1)
282 local_row = local_row + 1
283 real_base = ndynamic_local_per_point*(local_row - 1)
285 IF (nspins == 1)
THEN
286 IF (collapse_spin_dynamics)
THEN
287 local_dynamic(real_base + 1) = 0.5_dp*rho(i, j, k)
288 local_dynamic(real_base + 2) = 0.5_dp*drho(1)%array(i, j, k)
289 local_dynamic(real_base + 3) = 0.5_dp*drho(2)%array(i, j, k)
290 local_dynamic(real_base + 4) = 0.5_dp*drho(3)%array(i, j, k)
291 local_dynamic(real_base + 5) = 0.5_dp*tau_total(i, j, k)
293 local_dynamic(real_base + 1) = 0.5_dp*rho(i, j, k)
294 local_dynamic(real_base + 2) = 0.5_dp*rho(i, j, k)
296 local_dynamic(real_base + 2 + 3*(ispin - 1) + 1) = &
297 0.5_dp*drho(1)%array(i, j, k)
298 local_dynamic(real_base + 2 + 3*(ispin - 1) + 2) = &
299 0.5_dp*drho(2)%array(i, j, k)
300 local_dynamic(real_base + 2 + 3*(ispin - 1) + 3) = &
301 0.5_dp*drho(3)%array(i, j, k)
302 local_dynamic(real_base + 8 + ispin) = 0.5_dp*tau_total(i, j, k)
306 local_dynamic(real_base + 1) = rhoa(i, j, k)
307 local_dynamic(real_base + 2) = rhob(i, j, k)
308 local_dynamic(real_base + 3) = drhoa(1)%array(i, j, k)
309 local_dynamic(real_base + 4) = drhoa(2)%array(i, j, k)
310 local_dynamic(real_base + 5) = drhoa(3)%array(i, j, k)
311 local_dynamic(real_base + 6) = drhob(1)%array(i, j, k)
312 local_dynamic(real_base + 7) = drhob(2)%array(i, j, k)
313 local_dynamic(real_base + 8) = drhob(3)%array(i, j, k)
314 local_dynamic(real_base + 9) = tau_a(i, j, k)
315 local_dynamic(real_base + 10) = tau_b(i, j, k)
320 CALL timestop(phase_handle)
322 CALL timeset(
"skala_gpw_copy_layout", phase_handle)
323 CALL copy_cached_layout(features, my_requires_coordinate_grad .OR. my_requires_stress_grad, &
324 my_requires_stress_grad .OR. &
326 (my_requires_coordinate_grad .OR. my_requires_stress_grad)))
327 CALL timestop(phase_handle)
329 IF (use_atom_chunk_routing)
THEN
330 CALL timeset(
"skala_gpw_route_dyn", phase_handle)
331 CALL route_atom_chunk_dynamics(features, local_dynamic, pw_grid%para%group, &
332 collapse_spin_dynamics)
333 features%uses_atom_chunk_routing = .true.
334 features%uses_atom_chunks = .true.
335 CALL timestop(phase_handle)
337 ALLOCATE (global_dynamic(ndynamic_per_point*
cached_layout%npoint))
338 CALL timeset(
"skala_gpw_allgatherv", phase_handle)
339 CALL pw_grid%para%group%allgatherv(local_dynamic, global_dynamic, &
342 CALL timestop(phase_handle)
344 CALL timeset(
"skala_gpw_reorder_dyn", phase_handle)
345 ALLOCATE (features%density(nflat, 2), features%grad(nflat, 3, 2), &
346 features%kin(nflat, 2))
347 features%density = 0.0_dp
348 features%grad = 0.0_dp
349 features%kin = 0.0_dp
353 real_base = ndynamic_per_point*(ipt - 1)
354 features%density(row, :) = global_dynamic(real_base + 1:real_base + 2)
355 features%grad(row, 1, 1) = global_dynamic(real_base + 3)
356 features%grad(row, 2, 1) = global_dynamic(real_base + 4)
357 features%grad(row, 3, 1) = global_dynamic(real_base + 5)
358 features%grad(row, 1, 2) = global_dynamic(real_base + 6)
359 features%grad(row, 2, 2) = global_dynamic(real_base + 7)
360 features%grad(row, 3, 2) = global_dynamic(real_base + 8)
361 features%kin(row, :) = global_dynamic(real_base + 9:real_base + 10)
363 CALL timestop(phase_handle)
366 CALL timeset(
"skala_gpw_feature_sums", phase_handle)
367 IF (features%uses_atom_chunks)
THEN
368 features%electron_count = 0.0_dp
369 features%spin_moment = 0.0_dp
370 IF (features%chunk_feature_count > 0)
THEN
371 IF (features%uses_collapsed_rks_dynamic)
THEN
372 features%electron_count = sum(2.0_dp*features%chunk_density(:, 1)* &
375 features%electron_count = sum((features%chunk_density(:, 1) + &
376 features%chunk_density(:, 2))* &
378 features%spin_moment = sum((features%chunk_density(:, 1) - &
379 features%chunk_density(:, 2))* &
383 CALL pw_grid%para%group%sum(features%electron_count)
384 CALL pw_grid%para%group%sum(features%spin_moment)
386 features%electron_count = sum((features%density(:, 1) + features%density(:, 2))* &
387 features%grid_weights)
388 features%spin_moment = sum((features%density(:, 1) - features%density(:, 2))* &
389 features%grid_weights)
391 features%grid_weight_sum = sum(features%grid_weights)
392 CALL timestop(phase_handle)
394 CALL timeset(
"skala_gpw_tensor_update", phase_handle)
395 IF (use_atom_chunk_protocol .AND. .NOT. features%uses_atom_chunks)
THEN
396 IF (features%chunk_feature_count > 0)
CALL extract_atom_chunk_dynamics(features)
397 features%uses_atom_chunks = .true.
399 IF (.NOT. features%uses_atom_chunks .OR. features%chunk_feature_count > 0)
THEN
400 CALL add_feature_tensors(features, my_requires_grad, my_requires_coordinate_grad, &
401 my_requires_stress_grad, &
402 features%uses_atom_chunks, &
403 requires_weight_grad= &
405 (my_requires_coordinate_grad .OR. my_requires_stress_grad)))
408 features%owns_coordinate_tensor = .false.
409 features%owns_grid_coordinate_tensor = .false.
410 features%owns_weight_tensors = .false.
411 features%owns_dynamic_tensors = .false.
412 features%owns_inputs = .false.
413 features%owns_static_tensors = .false.
415 CALL timestop(phase_handle)
416 features%active = .true.
418 IF (
ALLOCATED(global_dynamic))
DEALLOCATE (global_dynamic)
419 DEALLOCATE (local_dynamic)
420 CALL timestop(handle)
432 SUBROUTINE ensure_layout_cache(pw_grid, particle_set, cell, weights, atom_partition)
437 INTEGER,
INTENT(IN),
OPTIONAL :: atom_partition
439 INTEGER :: my_atom_partition, phase_handle
440 LOGICAL :: cache_matches
443 IF (
PRESENT(atom_partition)) my_atom_partition = atom_partition
444 IF (
PRESENT(weights))
THEN
445 CALL timeset(
"skala_gpw_layout_match", phase_handle)
446 cache_matches = layout_cache_matches(pw_grid, particle_set, cell, weights, &
448 CALL timestop(phase_handle)
449 IF (cache_matches)
RETURN
450 CALL timeset(
"skala_gpw_layout_rebuild", phase_handle)
451 CALL rebuild_layout_cache(pw_grid, particle_set, cell, weights, my_atom_partition)
452 CALL timestop(phase_handle)
454 CALL timeset(
"skala_gpw_layout_match", phase_handle)
455 cache_matches = layout_cache_matches(pw_grid, particle_set, cell, &
456 atom_partition=my_atom_partition)
457 CALL timestop(phase_handle)
458 IF (cache_matches)
RETURN
459 CALL timeset(
"skala_gpw_layout_rebuild", phase_handle)
460 CALL rebuild_layout_cache(pw_grid, particle_set, cell, &
461 atom_partition=my_atom_partition)
462 CALL timestop(phase_handle)
465 END SUBROUTINE ensure_layout_cache
476 FUNCTION layout_cache_matches(pw_grid, particle_set, cell, weights, atom_partition)
RESULT(matches)
481 INTEGER,
INTENT(IN),
OPTIONAL :: atom_partition
484 INTEGER :: iatom, my_atom_partition
485 LOGICAL :: weights_match
488 IF (
PRESENT(atom_partition)) my_atom_partition = atom_partition
491 IF (
cached_layout%atom_partition /= my_atom_partition)
RETURN
498 IF (abs(
cached_layout%dvol - pw_grid%dvol) > layout_tol)
RETURN
499 IF (any(abs(
cached_layout%dh - pw_grid%dh) > layout_tol))
RETURN
500 IF (any(abs(
cached_layout%cell_hmat - cell%hmat) > layout_tol))
RETURN
503 DO iatom = 1,
SIZE(particle_set)
504 IF (any(abs(
cached_layout%atom_coords(:, iatom) - particle_set(iatom)%r) > layout_tol))
RETURN
507 IF (
PRESENT(weights))
THEN
508 weights_match = layout_weights_match(pw_grid, weights)
510 weights_match = layout_weights_match(pw_grid)
512 IF (.NOT. weights_match)
RETURN
516 END FUNCTION layout_cache_matches
524 FUNCTION layout_weights_match(pw_grid, weights)
RESULT(matches)
529 LOGICAL :: has_weights
530 REAL(kind=
dp) :: weight_sum, weight_sumsq
534 IF (
PRESENT(weights))
THEN
535 CALL weights_signature(weights, has_weights, weight_sum, weight_sumsq)
537 CALL weights_signature(has_weights=has_weights, weight_sum=weight_sum, &
538 weight_sumsq=weight_sumsq)
542 IF (abs(
cached_layout%weight_sum - weight_sum) > layout_tol)
RETURN
543 IF (abs(
cached_layout%weight_sumsq - weight_sumsq) > layout_tol)
RETURN
547 END FUNCTION layout_weights_match
557 SUBROUTINE rebuild_layout_cache(pw_grid, particle_set, cell, weights, atom_partition)
562 INTEGER,
INTENT(IN),
OPTIONAL :: atom_partition
564 INTEGER :: feature_local, i, iatom, ipt, j, k, local_row, max_grid_size, max_local_features, &
565 my_atom_partition, natom, nfeature_local, nflat, nflat_local, npoint, nproc, owner, pe, &
566 pe_index, phase_handle, row, source_global, source_local, static_base
567 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: atom_offset, atom_position, chunk_atom_begin, &
568 chunk_atom_end, cursor, feature_counts, feature_displs, global_owner, &
569 global_source_points, local_feature_counts_tmp, local_owner, local_source_global, &
570 local_source_points, point_counts, point_displs, static_counts, static_displs
571 INTEGER,
DIMENSION(2, 3) :: bo
572 LOGICAL :: has_weights
573 REAL(kind=
dp) :: base_weight, included_sum, &
574 partition_weight, weight_sum, &
576 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: distances, global_static, local_static, &
578 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: atom_coords_pbc, atom_image_coords
579 REAL(kind=
dp),
DIMENSION(3) :: grid_point, owner_coord
584 IF (
PRESENT(atom_partition)) my_atom_partition = atom_partition
585 natom =
SIZE(particle_set)
586 bo = pw_grid%bounds_local
587 nflat_local = pw_grid%ngpts_local
588 nproc = pw_grid%para%group%num_pe
589 pe_index = pw_grid%para%group%mepos + 1
591 IF (
PRESENT(weights))
THEN
592 CALL weights_signature(weights, has_weights, weight_sum, weight_sumsq)
594 CALL weights_signature(has_weights=has_weights, weight_sum=weight_sum, &
595 weight_sumsq=weight_sumsq)
598 max_local_features = nflat_local
600 max_local_features = nflat_local*natom
602 ALLOCATE (local_owner(max_local_features), &
603 local_source_points(max_local_features), &
604 local_static(nstatic_per_point*max_local_features), &
605 local_feature_counts_tmp(nflat_local), feature_counts(nproc), &
606 feature_displs(nproc), point_counts(nproc), point_displs(nproc), &
607 static_counts(nproc), static_displs(nproc), atom_coords_pbc(3, natom), &
608 atom_image_coords(3, natom), distances(natom), partition_weights(natom))
613 local_static = 0.0_dp
614 local_feature_counts_tmp = 0
616 atom_coords_pbc(:, iatom) =
pbc(particle_set(iatom)%r, cell, positive_range=.true.)
619 CALL timeset(
"skala_gpw_layout_local", phase_handle)
622 DO k = bo(1, 3), bo(2, 3)
623 DO j = bo(1, 2), bo(2, 2)
624 DO i = bo(1, 1), bo(2, 1)
625 local_row = local_row + 1
626 grid_point = grid_coordinate(pw_grid, [i, j, k])
627 base_weight = pw_grid%dvol
628 IF (
PRESENT(weights))
THEN
629 IF (
ASSOCIATED(weights)) base_weight = base_weight*weights%array(i, j, k)
634 owner = nearest_atom(grid_point, atom_coords_pbc, cell)
635 owner_coord = atom_coords_pbc(:, owner)
636 nfeature_local = nfeature_local + 1
637 local_feature_counts_tmp(local_row) = 1
638 local_owner(nfeature_local) = owner
639 local_source_points(nfeature_local) = local_row
640 static_base = nstatic_per_point*(nfeature_local - 1)
641 local_static(static_base + 1:static_base + 3) = grid_point
642 local_static(static_base + 4) = base_weight
643 local_static(static_base + 5) = base_weight
645 CALL smooth_atom_partition(grid_point, atom_coords_pbc, cell, &
646 partition_weights, atom_image_coords, distances)
647 included_sum = sum(partition_weights, mask=partition_weights > smooth_partition_eps)
648 IF (included_sum <= 0.0_dp)
THEN
649 owner = nearest_atom(grid_point, atom_coords_pbc, cell)
650 partition_weights = 0.0_dp
651 partition_weights(owner) = 1.0_dp
652 included_sum = 1.0_dp
655 IF (partition_weights(iatom) <= smooth_partition_eps) cycle
656 partition_weight = partition_weights(iatom)/included_sum
657 nfeature_local = nfeature_local + 1
658 local_feature_counts_tmp(local_row) = &
659 local_feature_counts_tmp(local_row) + 1
660 local_owner(nfeature_local) = iatom
661 local_source_points(nfeature_local) = local_row
662 static_base = nstatic_per_point*(nfeature_local - 1)
663 local_static(static_base + 1:static_base + 3) = grid_point
664 local_static(static_base + 4) = base_weight*partition_weight
665 local_static(static_base + 5) = base_weight
671 CALL timestop(phase_handle)
675 CALL timeset(
"skala_gpw_layout_gather", phase_handle)
676 CALL pw_grid%para%group%allgather(nflat_local, point_counts)
677 CALL counts_to_displs(point_counts, point_displs)
678 npoint = sum(point_counts)
679 CALL pw_grid%para%group%allgather(nfeature_local, feature_counts)
680 CALL counts_to_displs(feature_counts, feature_displs)
682 static_counts(pe) = nstatic_per_point*feature_counts(pe)
683 static_displs(pe) = nstatic_per_point*feature_displs(pe)
685 nflat = sum(feature_counts)
686 ALLOCATE (global_owner(nflat), global_source_points(nflat), &
687 global_static(nstatic_per_point*nflat), local_source_global(nfeature_local))
688 DO feature_local = 1, nfeature_local
689 local_source_global(feature_local) = point_displs(pe_index) + local_source_points(feature_local)
691 CALL pw_grid%para%group%allgatherv(local_owner(1:nfeature_local), global_owner, feature_counts, &
693 CALL pw_grid%para%group%allgatherv(local_source_global, global_source_points, feature_counts, &
695 CALL pw_grid%para%group%allgatherv(local_static(1:nstatic_per_point*nfeature_local), &
696 global_static, static_counts, &
698 CALL timestop(phase_handle)
719 cached_layout%local_feature_indices(nfeature_local), atom_offset(natom + 1), &
720 atom_position(natom), chunk_atom_begin(nproc), chunk_atom_end(nproc), &
724 cached_layout%dynamic_counts(:) = ndynamic_per_point*point_counts
725 cached_layout%dynamic_displs(:) = ndynamic_per_point*point_displs
728 cached_layout%local_feature_counts(:) = local_feature_counts_tmp
730 DO local_row = 1, nflat_local
735 cursor(:) =
cached_layout%local_feature_offsets(1:nflat_local)
737 CALL timeset(
"skala_gpw_layout_atom_sort", phase_handle)
744 atom_offset(iatom + 1) = atom_offset(iatom) + int(
cached_layout%atomic_grid_sizes(iatom))
747 atom_position(iatom) = atom_offset(iatom)
750 CALL build_atom_chunks(
cached_layout%atomic_grid_sizes, atom_offset, nproc, &
751 chunk_atom_begin, chunk_atom_end, &
766 cached_layout%atomic_grid_size_bound_shape(0, max_grid_size), &
775 cached_layout%coarse_0_atomic_coords(:, iatom) = atom_coords_pbc(:, iatom)
779 owner = global_owner(ipt)
780 row = atom_position(owner)
781 atom_position(owner) = atom_position(owner) + 1
782 source_global = global_source_points(ipt)
784 IF (
cached_layout%global_to_feature(source_global) == 0)
THEN
787 static_base = nstatic_per_point*(ipt - 1)
788 cached_layout%grid_coords(:, row) = global_static(static_base + 1:static_base + 3)
789 cached_layout%grid_weights(row) = global_static(static_base + 4)
790 cached_layout%atomic_grid_weights(row) = global_static(static_base + 5)
791 source_local = source_global - point_displs(pe_index)
792 IF (source_local >= 1 .AND. source_local <= nflat_local)
THEN
793 feature_local = cursor(source_local)
794 cursor(source_local) = cursor(source_local) + 1
796 cached_layout%local_feature_points(feature_local) = source_local
803 DO k = bo(1, 3), bo(2, 3)
804 DO j = bo(1, 2), bo(2, 2)
805 DO i = bo(1, 1), bo(2, 1)
812 DO feature_local = 1, nfeature_local
816 CALL timestop(phase_handle)
817 CALL timeset(
"skala_gpw_layout_chunk_routes", phase_handle)
821 CALL timestop(phase_handle)
838 CALL timeset(
"skala_gpw_layout_tensors", phase_handle)
840 CALL timestop(phase_handle)
843 DEALLOCATE (atom_coords_pbc, atom_image_coords, atom_offset, atom_position, &
844 chunk_atom_begin, chunk_atom_end, cursor, feature_counts, feature_displs, &
845 global_owner, global_source_points, global_static, local_feature_counts_tmp, &
846 distances, local_owner, local_source_global, local_source_points, &
847 local_static, partition_weights, point_counts, point_displs, static_counts, &
850 END SUBROUTINE rebuild_layout_cache
856 SUBROUTINE build_static_layout_tensors(cache)
857 TYPE(skala_gpw_layout_cache_type),
INTENT(INOUT) :: cache
859 cpassert(.NOT. cache%static_tensors_active)
872 cache%atomic_grid_size_bound_shape)
879 CALL torch_dict_insert(cache%static_inputs,
"grid_weights", cache%grid_weights_t)
881 cache%atomic_grid_weights_t)
883 cache%atomic_grid_sizes_t)
885 cache%atomic_grid_size_bound_shape_t)
886 cache%static_tensors_active = .true.
888 IF (cache%chunk_feature_count > 0)
THEN
889 cpassert(.NOT. cache%chunk_static_tensors_active)
895 cache%chunk_atomic_grid_weights)
898 cache%chunk_atomic_grid_sizes)
901 cache%chunk_coarse_0_atomic_coords)
904 cache%chunk_atomic_grid_size_bound_shape)
911 cache%chunk_grid_coords_t)
913 cache%chunk_grid_weights_t)
915 cache%chunk_atomic_grid_weights_t)
917 cache%chunk_atomic_grid_sizes_t)
918 CALL torch_dict_insert(cache%chunk_static_inputs,
"atomic_grid_size_bound_shape", &
919 cache%chunk_atomic_grid_size_bound_shape_t)
920 cache%chunk_static_tensors_active = .true.
923 END SUBROUTINE build_static_layout_tensors
931 SUBROUTINE copy_cached_layout(features, needs_coordinate_array, needs_grid_coordinate_array)
933 LOGICAL,
INTENT(IN) :: needs_coordinate_array, &
934 needs_grid_coordinate_array
938 ALLOCATE (features%feature_index(lbound(
cached_layout%feature_index, 1): &
945 ALLOCATE (features%local_feature_counts(
cached_layout%nflat_local), &
946 features%local_feature_offsets(
cached_layout%nflat_local + 1), &
947 features%local_feature_rows(
SIZE(
cached_layout%local_feature_rows)))
949 features%feature_index(:, :, :) =
cached_layout%feature_index
951 features%local_feature_counts(:) =
cached_layout%local_feature_counts
952 features%local_feature_offsets(:) =
cached_layout%local_feature_offsets
953 features%local_feature_rows(:) =
cached_layout%local_feature_rows
956 features%chunk_feature_count =
cached_layout%chunk_feature_count
959 features%atomic_grid_sizes(:) =
cached_layout%atomic_grid_sizes
960 IF (needs_grid_coordinate_array)
THEN
964 features%atomic_grid_weights(:) =
cached_layout%atomic_grid_weights
968 features%route_grad_return_recv_counts(
cached_layout%nproc), &
969 features%route_grad_return_recv_displs(
cached_layout%nproc), &
970 features%route_grad_return_send_counts(
cached_layout%nproc), &
971 features%route_grad_return_send_displs(
cached_layout%nproc), &
976 features%route_send_local_rows(
SIZE(
cached_layout%route_send_local_rows)))
977 features%chunk_grad_counts(:) =
cached_layout%chunk_grad_counts
978 features%chunk_grad_displs(:) =
cached_layout%chunk_grad_displs
979 features%route_grad_return_recv_counts(:) =
cached_layout%route_grad_return_recv_counts
980 features%route_grad_return_recv_displs(:) =
cached_layout%route_grad_return_recv_displs
981 features%route_grad_return_send_counts(:) =
cached_layout%route_grad_return_send_counts
982 features%route_grad_return_send_displs(:) =
cached_layout%route_grad_return_send_displs
983 features%route_point_recv_counts(:) =
cached_layout%route_point_recv_counts
984 features%route_point_recv_displs(:) =
cached_layout%route_point_recv_displs
985 features%route_point_send_counts(:) =
cached_layout%route_point_send_counts
986 features%route_point_send_displs(:) =
cached_layout%route_point_send_displs
987 features%route_send_local_rows(:) =
cached_layout%route_send_local_rows
988 IF (needs_coordinate_array)
THEN
989 ALLOCATE (features%coarse_0_atomic_coords(3,
cached_layout%natom))
990 features%coarse_0_atomic_coords(:, :) =
cached_layout%coarse_0_atomic_coords
993 END SUBROUTINE copy_cached_layout
1005 SUBROUTINE build_atom_chunks(atomic_grid_sizes, atom_offset, nproc, chunk_atom_begin, &
1006 chunk_atom_end, chunk_feature_counts, chunk_feature_displs)
1007 INTEGER(KIND=int_8),
DIMENSION(:),
INTENT(IN) :: atomic_grid_sizes
1008 INTEGER,
DIMENSION(:),
INTENT(IN) :: atom_offset
1009 INTEGER,
INTENT(IN) :: nproc
1010 INTEGER,
DIMENSION(:),
INTENT(OUT) :: chunk_atom_begin, chunk_atom_end, &
1011 chunk_feature_counts, &
1012 chunk_feature_displs
1014 INTEGER :: best_limit, count, displ, end_atom, lower_limit, max_end_atom, midpoint, natom, &
1015 next_atom, next_count, pe, ranks_left, target_chunks, total_count, upper_limit
1017 natom =
SIZE(atomic_grid_sizes)
1018 chunk_atom_begin = natom + 1
1019 chunk_atom_end = natom
1020 chunk_feature_counts = 0
1021 chunk_feature_displs = 0
1022 IF (natom == 0)
RETURN
1024 target_chunks = min(nproc, natom)
1025 total_count = atom_offset(natom + 1) - 1
1026 lower_limit = maxval(int(atomic_grid_sizes))
1027 lower_limit = max(lower_limit, (total_count + target_chunks - 1)/target_chunks)
1028 upper_limit = total_count
1029 best_limit = upper_limit
1030 DO WHILE (lower_limit <= upper_limit)
1031 midpoint = (lower_limit + upper_limit)/2
1032 IF (atom_chunks_fit_limit(atomic_grid_sizes, midpoint, target_chunks))
THEN
1033 best_limit = midpoint
1034 upper_limit = midpoint - 1
1036 lower_limit = midpoint + 1
1043 chunk_feature_displs(pe) = displ
1044 IF (pe > target_chunks .OR. next_atom > natom) cycle
1046 ranks_left = target_chunks - pe + 1
1047 chunk_atom_begin(pe) = next_atom
1048 max_end_atom = natom - ranks_left + 1
1049 end_atom = next_atom
1050 count = int(atomic_grid_sizes(end_atom))
1051 DO WHILE (end_atom < max_end_atom)
1052 next_count = count + int(atomic_grid_sizes(end_atom + 1))
1053 IF (next_count > best_limit)
EXIT
1054 end_atom = end_atom + 1
1058 chunk_atom_end(pe) = end_atom
1059 chunk_feature_counts(pe) = atom_offset(end_atom + 1) - atom_offset(next_atom)
1060 displ = displ + chunk_feature_counts(pe)
1061 next_atom = end_atom + 1
1064 cpassert(displ == atom_offset(natom + 1) - 1)
1066 END SUBROUTINE build_atom_chunks
1075 FUNCTION atom_chunks_fit_limit(atomic_grid_sizes, limit, nchunks)
RESULT(fits)
1076 INTEGER(KIND=int_8),
DIMENSION(:),
INTENT(IN) :: atomic_grid_sizes
1077 INTEGER,
INTENT(IN) :: limit, nchunks
1080 INTEGER :: atom_count, chunk_count, iatom, &
1084 IF (
SIZE(atomic_grid_sizes) == 0)
THEN
1091 DO iatom = 1,
SIZE(atomic_grid_sizes)
1092 atom_count = int(atomic_grid_sizes(iatom))
1093 IF (atom_count > limit)
RETURN
1094 IF (chunk_count + atom_count > limit)
THEN
1095 used_chunks = used_chunks + 1
1096 chunk_count = atom_count
1098 chunk_count = chunk_count + atom_count
1101 fits = used_chunks <= nchunks
1103 END FUNCTION atom_chunks_fit_limit
1112 FUNCTION feature_row_chunk_owner(row, counts, displs)
RESULT(owner)
1113 INTEGER,
INTENT(IN) :: row
1114 INTEGER,
DIMENSION(:),
INTENT(IN) :: counts, displs
1120 DO pe = 1,
SIZE(counts)
1121 IF (row > displs(pe) .AND. row <= displs(pe) + counts(pe))
THEN
1127 END FUNCTION feature_row_chunk_owner
1134 SUBROUTINE counts_to_displs(counts, displs)
1135 INTEGER,
DIMENSION(:),
INTENT(IN) :: counts
1136 INTEGER,
DIMENSION(:),
INTENT(OUT) :: displs
1141 DO pe = 2,
SIZE(counts)
1142 displs(pe) = displs(pe - 1) + counts(pe - 1)
1145 END SUBROUTINE counts_to_displs
1153 SUBROUTINE build_atom_chunk_routes(cache, local_to_global, group)
1154 TYPE(skala_gpw_layout_cache_type),
INTENT(INOUT) :: cache
1155 INTEGER,
DIMENSION(:),
INTENT(IN) :: local_to_global
1159 INTEGER :: chunk_row, dest, local_feature, point_pos, row
1160 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: cursor, recv_meta, send_meta
1162 ALLOCATE (cache%route_local_dest(
SIZE(local_to_global)), &
1163 cache%route_send_local_rows(
SIZE(local_to_global)), &
1164 cache%chunk_return_positions(cache%chunk_feature_count), &
1165 cursor(
SIZE(cache%route_point_send_counts)))
1166 cache%route_point_send_counts = 0
1167 cache%route_send_local_rows = 0
1168 cache%chunk_return_positions = 0
1169 DO local_feature = 1,
SIZE(local_to_global)
1170 dest = feature_row_chunk_owner(local_to_global(local_feature), &
1171 cache%chunk_feature_counts, &
1172 cache%chunk_feature_displs)
1174 cache%route_local_dest(local_feature) = dest
1175 cache%route_point_send_counts(dest) = cache%route_point_send_counts(dest) + 1
1177 CALL counts_to_displs(cache%route_point_send_counts, cache%route_point_send_displs)
1178 cursor(:) = cache%route_point_send_displs + 1
1179 DO local_feature = 1,
SIZE(local_to_global)
1180 dest = cache%route_local_dest(local_feature)
1181 point_pos = cursor(dest)
1182 cursor(dest) = cursor(dest) + 1
1183 cache%route_send_local_rows(point_pos) = cache%local_feature_points(local_feature)
1185 CALL group%alltoall(cache%route_point_send_counts, cache%route_point_recv_counts, 1)
1186 CALL counts_to_displs(cache%route_point_recv_counts, cache%route_point_recv_displs)
1188 ALLOCATE (send_meta(
SIZE(local_to_global)), recv_meta(cache%chunk_feature_count))
1189 cursor(:) = cache%route_point_send_displs + 1
1190 DO local_feature = 1,
SIZE(local_to_global)
1191 dest = cache%route_local_dest(local_feature)
1192 point_pos = cursor(dest)
1193 cursor(dest) = cursor(dest) + 1
1194 send_meta(point_pos) = local_to_global(local_feature)
1196 CALL group%alltoall(send_meta, cache%route_point_send_counts, &
1197 cache%route_point_send_displs, recv_meta, &
1198 cache%route_point_recv_counts, &
1199 cache%route_point_recv_displs)
1200 DO point_pos = 1, cache%chunk_feature_count
1201 row = recv_meta(point_pos)
1202 chunk_row = row - cache%chunk_feature_begin + 1
1203 cpassert(chunk_row >= 1 .AND. chunk_row <= cache%chunk_feature_count)
1204 cache%chunk_return_positions(chunk_row) = point_pos
1207 cache%route_grad_return_send_counts(:) = ngrad_per_point*cache%route_point_recv_counts
1208 cache%route_grad_return_send_displs(:) = ngrad_per_point*cache%route_point_recv_displs
1209 cache%route_grad_return_recv_counts(:) = ngrad_per_point*cache%route_point_send_counts
1210 cache%route_grad_return_recv_displs(:) = ngrad_per_point*cache%route_point_send_displs
1212 cpassert(sum(cache%route_point_send_counts) ==
SIZE(local_to_global))
1213 cpassert(sum(cache%route_point_recv_counts) == cache%chunk_feature_count)
1214 cpassert(all(cache%route_send_local_rows > 0))
1215 cpassert(all(cache%chunk_return_positions > 0))
1217 DEALLOCATE (cursor, recv_meta, send_meta)
1219 END SUBROUTINE build_atom_chunk_routes
1225 SUBROUTINE build_atom_chunk_layout(cache)
1226 TYPE(skala_gpw_layout_cache_type),
INTENT(INOUT) :: cache
1228 INTEGER :: irow, max_grid_size, row_begin, row_end
1230 IF (cache%chunk_feature_count <= 0 .OR. cache%chunk_natom <= 0)
RETURN
1232 row_begin = cache%chunk_feature_begin
1233 row_end = row_begin + cache%chunk_feature_count - 1
1234 ALLOCATE (cache%chunk_grid_coords(3, cache%chunk_feature_count), &
1235 cache%chunk_grid_weights(cache%chunk_feature_count), &
1236 cache%chunk_atomic_grid_weights(cache%chunk_feature_count), &
1237 cache%chunk_atomic_grid_sizes(cache%chunk_natom), &
1238 cache%chunk_coarse_0_atomic_coords(3, cache%chunk_natom), &
1239 cache%chunk_feature_indices(cache%chunk_feature_count))
1240 cache%chunk_grid_coords(:, :) = cache%grid_coords(:, row_begin:row_end)
1241 cache%chunk_grid_weights(:) = cache%grid_weights(row_begin:row_end)
1242 cache%chunk_atomic_grid_weights(:) = cache%atomic_grid_weights(row_begin:row_end)
1243 cache%chunk_atomic_grid_sizes(:) = &
1244 cache%atomic_grid_sizes(cache%chunk_atom_begin:cache%chunk_atom_end)
1245 cache%chunk_coarse_0_atomic_coords(:, :) = &
1246 cache%coarse_0_atomic_coords(:, cache%chunk_atom_begin:cache%chunk_atom_end)
1248 max_grid_size = maxval(int(cache%chunk_atomic_grid_sizes))
1249 ALLOCATE (cache%chunk_atomic_grid_size_bound_shape(0, max_grid_size))
1250 cache%chunk_atomic_grid_size_bound_shape = 0_int_8
1251 DO irow = 1, cache%chunk_feature_count
1252 cache%chunk_feature_indices(irow) = int(irow - 1, kind=
int_8)
1255 END SUBROUTINE build_atom_chunk_layout
1264 SUBROUTINE route_atom_chunk_dynamics(features, local_dynamic, group, collapse_spin_dynamics)
1266 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: local_dynamic
1269 LOGICAL,
INTENT(IN) :: collapse_spin_dynamics
1271 INTEGER :: chunk_row, dest, dyn_base, local_feature, local_row, &
1272 ndynamic_route_per_point, nrecv, nsend, &
1274 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: cursor, recv_counts, recv_displs, &
1275 send_counts, send_displs
1276 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: recv_dynamic, send_dynamic
1282 ndynamic_route_per_point = ndynamic_per_point
1283 IF (collapse_spin_dynamics) ndynamic_route_per_point = nrks_dynamic_per_point
1285 ALLOCATE (send_dynamic(max(1, ndynamic_route_per_point*nsend)), &
1286 recv_dynamic(max(1, ndynamic_route_per_point*nrecv)), &
1290 send_counts(:) = ndynamic_route_per_point*
cached_layout%route_point_send_counts
1291 send_displs(:) = ndynamic_route_per_point*
cached_layout%route_point_send_displs
1292 recv_counts(:) = ndynamic_route_per_point*
cached_layout%route_point_recv_counts
1293 recv_displs(:) = ndynamic_route_per_point*
cached_layout%route_point_recv_displs
1295 DO local_feature = 1, nsend
1297 point_pos = cursor(dest)
1298 cursor(dest) = cursor(dest) + 1
1299 dyn_base = ndynamic_route_per_point*(point_pos - 1)
1300 local_row =
cached_layout%local_feature_points(local_feature)
1301 src_base = ndynamic_route_per_point*(local_row - 1)
1302 send_dynamic(dyn_base + 1:dyn_base + ndynamic_route_per_point) = &
1303 local_dynamic(src_base + 1:src_base + ndynamic_route_per_point)
1306 CALL group%alltoall(send_dynamic, send_counts, send_displs, recv_dynamic, recv_counts, &
1309 features%uses_collapsed_rks_dynamic = collapse_spin_dynamics
1311 IF (collapse_spin_dynamics)
THEN
1312 ALLOCATE (features%chunk_density(
cached_layout%chunk_feature_count, 1), &
1313 features%chunk_grad(
cached_layout%chunk_feature_count, 3, 1), &
1315 features%chunk_return_positions(
cached_layout%chunk_feature_count))
1317 ALLOCATE (features%chunk_density(
cached_layout%chunk_feature_count, 2), &
1318 features%chunk_grad(
cached_layout%chunk_feature_count, 3, 2), &
1320 features%chunk_return_positions(
cached_layout%chunk_feature_count))
1322 features%chunk_return_positions(:) =
cached_layout%chunk_return_positions
1326 cpassert(point_pos >= 1 .AND. point_pos <=
cached_layout%chunk_feature_count)
1327 dyn_base = ndynamic_route_per_point*(point_pos - 1)
1328 IF (collapse_spin_dynamics)
THEN
1329 features%chunk_density(chunk_row, 1) = recv_dynamic(dyn_base + 1)
1330 features%chunk_grad(chunk_row, 1, 1) = recv_dynamic(dyn_base + 2)
1331 features%chunk_grad(chunk_row, 2, 1) = recv_dynamic(dyn_base + 3)
1332 features%chunk_grad(chunk_row, 3, 1) = recv_dynamic(dyn_base + 4)
1333 features%chunk_kin(chunk_row, 1) = recv_dynamic(dyn_base + 5)
1335 features%chunk_density(chunk_row, :) = recv_dynamic(dyn_base + 1:dyn_base + 2)
1336 features%chunk_grad(chunk_row, 1, 1) = recv_dynamic(dyn_base + 3)
1337 features%chunk_grad(chunk_row, 2, 1) = recv_dynamic(dyn_base + 4)
1338 features%chunk_grad(chunk_row, 3, 1) = recv_dynamic(dyn_base + 5)
1339 features%chunk_grad(chunk_row, 1, 2) = recv_dynamic(dyn_base + 6)
1340 features%chunk_grad(chunk_row, 2, 2) = recv_dynamic(dyn_base + 7)
1341 features%chunk_grad(chunk_row, 3, 2) = recv_dynamic(dyn_base + 8)
1342 features%chunk_kin(chunk_row, :) = recv_dynamic(dyn_base + 9:dyn_base + 10)
1345 cpassert(all(features%chunk_return_positions > 0))
1348 DEALLOCATE (cursor, recv_counts, recv_displs, recv_dynamic, send_counts, send_displs, &
1351 END SUBROUTINE route_atom_chunk_dynamics
1357 SUBROUTINE extract_atom_chunk_dynamics(features)
1360 INTEGER :: row_begin, row_end
1365 ALLOCATE (features%chunk_density(
cached_layout%chunk_feature_count, 2), &
1366 features%chunk_grad(
cached_layout%chunk_feature_count, 3, 2), &
1368 features%chunk_density(:, :) = features%density(row_begin:row_end, :)
1369 features%chunk_grad(:, :, :) = features%grad(row_begin:row_end, :, :)
1370 features%chunk_kin(:, :) = features%kin(row_begin:row_end, :)
1372 END SUBROUTINE extract_atom_chunk_dynamics
1381 SUBROUTINE weights_signature(weights, has_weights, weight_sum, weight_sumsq)
1383 LOGICAL,
INTENT(OUT) :: has_weights
1384 REAL(kind=
dp),
INTENT(OUT) :: weight_sum, weight_sumsq
1386 has_weights = .false.
1388 weight_sumsq = 0.0_dp
1389 IF (
PRESENT(weights))
THEN
1390 IF (
ASSOCIATED(weights))
THEN
1391 has_weights = .true.
1392 weight_sum = sum(weights%array)
1393 weight_sumsq = sum(weights%array*weights%array)
1397 END SUBROUTINE weights_signature
1403 SUBROUTINE release_layout_cache(cache)
1404 TYPE(skala_gpw_layout_cache_type),
INTENT(INOUT) :: cache
1406 IF (cache%inputs_active)
THEN
1408 cache%inputs_active = .false.
1411 IF (cache%chunk_inputs_active)
THEN
1413 cache%chunk_inputs_active = .false.
1416 IF (cache%dynamic_tensors_active)
THEN
1420 cache%dynamic_tensors_active = .false.
1423 IF (cache%chunk_dynamic_tensors_active)
THEN
1424 IF (cache%chunk_dynamic_input_views_active)
THEN
1428 cache%chunk_dynamic_input_views_active = .false.
1433 cache%chunk_dynamic_tensors_active = .false.
1436 IF (cache%static_tensors_active)
THEN
1445 cache%static_tensors_active = .false.
1448 IF (cache%chunk_static_tensors_active)
THEN
1457 cache%chunk_static_tensors_active = .false.
1460 IF (
ALLOCATED(cache%chunk_feature_counts))
DEALLOCATE (cache%chunk_feature_counts)
1461 IF (
ALLOCATED(cache%chunk_feature_displs))
DEALLOCATE (cache%chunk_feature_displs)
1462 IF (
ALLOCATED(cache%chunk_grad_counts))
DEALLOCATE (cache%chunk_grad_counts)
1463 IF (
ALLOCATED(cache%chunk_grad_displs))
DEALLOCATE (cache%chunk_grad_displs)
1464 IF (
ALLOCATED(cache%route_grad_return_recv_counts))
THEN
1465 DEALLOCATE (cache%route_grad_return_recv_counts)
1467 IF (
ALLOCATED(cache%route_grad_return_recv_displs))
THEN
1468 DEALLOCATE (cache%route_grad_return_recv_displs)
1470 IF (
ALLOCATED(cache%route_grad_return_send_counts))
THEN
1471 DEALLOCATE (cache%route_grad_return_send_counts)
1473 IF (
ALLOCATED(cache%route_grad_return_send_displs))
THEN
1474 DEALLOCATE (cache%route_grad_return_send_displs)
1476 IF (
ALLOCATED(cache%route_local_dest))
DEALLOCATE (cache%route_local_dest)
1477 IF (
ALLOCATED(cache%chunk_return_positions))
DEALLOCATE (cache%chunk_return_positions)
1478 IF (
ALLOCATED(cache%route_point_recv_counts))
DEALLOCATE (cache%route_point_recv_counts)
1479 IF (
ALLOCATED(cache%route_point_recv_displs))
DEALLOCATE (cache%route_point_recv_displs)
1480 IF (
ALLOCATED(cache%route_point_send_counts))
DEALLOCATE (cache%route_point_send_counts)
1481 IF (
ALLOCATED(cache%route_point_send_displs))
DEALLOCATE (cache%route_point_send_displs)
1482 IF (
ALLOCATED(cache%route_send_local_rows))
DEALLOCATE (cache%route_send_local_rows)
1483 IF (
ALLOCATED(cache%dynamic_counts))
DEALLOCATE (cache%dynamic_counts)
1484 IF (
ALLOCATED(cache%dynamic_displs))
DEALLOCATE (cache%dynamic_displs)
1485 IF (
ALLOCATED(cache%feature_counts))
DEALLOCATE (cache%feature_counts)
1486 IF (
ALLOCATED(cache%feature_displs))
DEALLOCATE (cache%feature_displs)
1487 IF (
ALLOCATED(cache%feature_source_points))
DEALLOCATE (cache%feature_source_points)
1488 IF (
ALLOCATED(cache%global_to_feature))
DEALLOCATE (cache%global_to_feature)
1489 IF (
ALLOCATED(cache%feature_index))
DEALLOCATE (cache%feature_index)
1490 IF (
ALLOCATED(cache%atomic_grid_sizes))
DEALLOCATE (cache%atomic_grid_sizes)
1491 IF (
ALLOCATED(cache%chunk_atomic_grid_sizes))
DEALLOCATE (cache%chunk_atomic_grid_sizes)
1492 IF (
ALLOCATED(cache%chunk_feature_indices))
DEALLOCATE (cache%chunk_feature_indices)
1493 IF (
ALLOCATED(cache%local_feature_counts))
DEALLOCATE (cache%local_feature_counts)
1494 IF (
ALLOCATED(cache%local_feature_indices))
DEALLOCATE (cache%local_feature_indices)
1495 IF (
ALLOCATED(cache%local_feature_offsets))
DEALLOCATE (cache%local_feature_offsets)
1496 IF (
ALLOCATED(cache%local_feature_points))
DEALLOCATE (cache%local_feature_points)
1497 IF (
ALLOCATED(cache%local_feature_rows))
DEALLOCATE (cache%local_feature_rows)
1498 IF (
ALLOCATED(cache%atomic_grid_size_bound_shape))
THEN
1499 DEALLOCATE (cache%atomic_grid_size_bound_shape)
1501 IF (
ALLOCATED(cache%chunk_atomic_grid_size_bound_shape))
THEN
1502 DEALLOCATE (cache%chunk_atomic_grid_size_bound_shape)
1504 IF (
ALLOCATED(cache%atomic_grid_weights))
DEALLOCATE (cache%atomic_grid_weights)
1505 IF (
ALLOCATED(cache%chunk_atomic_grid_weights))
DEALLOCATE (cache%chunk_atomic_grid_weights)
1506 IF (
ALLOCATED(cache%chunk_grid_weights))
DEALLOCATE (cache%chunk_grid_weights)
1507 IF (
ALLOCATED(cache%grid_weights))
DEALLOCATE (cache%grid_weights)
1508 IF (
ALLOCATED(cache%atom_coords))
DEALLOCATE (cache%atom_coords)
1509 IF (
ALLOCATED(cache%chunk_coarse_0_atomic_coords))
THEN
1510 DEALLOCATE (cache%chunk_coarse_0_atomic_coords)
1512 IF (
ALLOCATED(cache%coarse_0_atomic_coords))
DEALLOCATE (cache%coarse_0_atomic_coords)
1513 IF (
ALLOCATED(cache%chunk_grid_coords))
DEALLOCATE (cache%chunk_grid_coords)
1514 IF (
ALLOCATED(cache%grid_coords))
DEALLOCATE (cache%grid_coords)
1516 cache%chunk_atom_begin = 1
1517 cache%chunk_atom_end = 0
1518 cache%chunk_feature_begin = 1
1519 cache%chunk_feature_count = 0
1520 cache%chunk_natom = 0
1523 cache%nflat_local = 0
1531 cache%weight_sum = 0.0_dp
1532 cache%weight_sumsq = 0.0_dp
1533 cache%cell_hmat = 0.0_dp
1535 cache%active = .false.
1536 cache%has_weights = .false.
1537 cache%chunk_dynamic_tensors_active = .false.
1538 cache%chunk_dynamic_input_views_active = .false.
1539 cache%chunk_inputs_active = .false.
1540 cache%chunk_inputs_use_collapsed_rks = .false.
1541 cache%chunk_static_tensors_active = .false.
1542 cache%dynamic_tensors_active = .false.
1543 cache%inputs_active = .false.
1544 cache%static_tensors_active = .false.
1546 END SUBROUTINE release_layout_cache
1555 IF (features%active)
THEN
1556 IF (features%owns_dynamic_tensors)
THEN
1557 IF (features%uses_collapsed_rks_dynamic)
THEN
1566 IF (features%owns_static_tensors)
THEN
1573 IF (features%owns_grid_coordinate_tensor)
THEN
1576 IF (features%owns_weight_tensors)
THEN
1580 IF (features%owns_static_tensors .OR. features%owns_coordinate_tensor)
THEN
1584 features%active = .false.
1585 features%owns_coordinate_tensor = .false.
1586 features%owns_grid_coordinate_tensor = .false.
1587 features%owns_weight_tensors = .false.
1588 features%owns_dynamic_tensors = .true.
1589 features%owns_inputs = .true.
1590 features%owns_static_tensors = .true.
1591 features%uses_atom_chunk_routing = .false.
1592 features%uses_atom_chunks = .false.
1593 features%uses_collapsed_rks_dynamic = .false.
1596 IF (
ALLOCATED(features%chunk_density))
DEALLOCATE (features%chunk_density)
1597 IF (
ALLOCATED(features%chunk_grad))
DEALLOCATE (features%chunk_grad)
1598 IF (
ALLOCATED(features%chunk_kin))
DEALLOCATE (features%chunk_kin)
1599 IF (
ALLOCATED(features%density))
DEALLOCATE (features%density)
1600 IF (
ALLOCATED(features%grad))
DEALLOCATE (features%grad)
1601 IF (
ALLOCATED(features%kin))
DEALLOCATE (features%kin)
1602 IF (
ALLOCATED(features%chunk_grad_counts))
DEALLOCATE (features%chunk_grad_counts)
1603 IF (
ALLOCATED(features%chunk_grad_displs))
DEALLOCATE (features%chunk_grad_displs)
1604 IF (
ALLOCATED(features%chunk_return_positions))
DEALLOCATE (features%chunk_return_positions)
1605 IF (
ALLOCATED(features%route_grad_return_recv_counts))
THEN
1606 DEALLOCATE (features%route_grad_return_recv_counts)
1608 IF (
ALLOCATED(features%route_grad_return_recv_displs))
THEN
1609 DEALLOCATE (features%route_grad_return_recv_displs)
1611 IF (
ALLOCATED(features%route_grad_return_send_counts))
THEN
1612 DEALLOCATE (features%route_grad_return_send_counts)
1614 IF (
ALLOCATED(features%route_grad_return_send_displs))
THEN
1615 DEALLOCATE (features%route_grad_return_send_displs)
1617 IF (
ALLOCATED(features%route_point_recv_counts))
THEN
1618 DEALLOCATE (features%route_point_recv_counts)
1620 IF (
ALLOCATED(features%route_point_recv_displs))
THEN
1621 DEALLOCATE (features%route_point_recv_displs)
1623 IF (
ALLOCATED(features%route_point_send_counts))
THEN
1624 DEALLOCATE (features%route_point_send_counts)
1626 IF (
ALLOCATED(features%route_point_send_displs))
THEN
1627 DEALLOCATE (features%route_point_send_displs)
1629 IF (
ALLOCATED(features%route_send_local_rows))
DEALLOCATE (features%route_send_local_rows)
1630 IF (
ALLOCATED(features%feature_index))
DEALLOCATE (features%feature_index)
1631 IF (
ALLOCATED(features%local_feature_counts))
DEALLOCATE (features%local_feature_counts)
1632 IF (
ALLOCATED(features%local_feature_offsets))
DEALLOCATE (features%local_feature_offsets)
1633 IF (
ALLOCATED(features%local_feature_rows))
DEALLOCATE (features%local_feature_rows)
1634 IF (
ALLOCATED(features%grid_coords))
DEALLOCATE (features%grid_coords)
1635 IF (
ALLOCATED(features%grid_weights))
DEALLOCATE (features%grid_weights)
1636 IF (
ALLOCATED(features%atomic_grid_weights))
DEALLOCATE (features%atomic_grid_weights)
1637 IF (
ALLOCATED(features%atomic_grid_sizes))
DEALLOCATE (features%atomic_grid_sizes)
1638 IF (
ALLOCATED(features%coarse_0_atomic_coords))
DEALLOCATE (features%coarse_0_atomic_coords)
1639 IF (
ALLOCATED(features%atomic_grid_size_bound_shape))
THEN
1640 DEALLOCATE (features%atomic_grid_size_bound_shape)
1642 features%chunk_feature_count = 0
1644 features%nflat_local = 0
1646 features%uses_atom_chunk_routing = .false.
1647 features%uses_collapsed_rks_dynamic = .false.
1657 INTEGER,
INTENT(IN) :: max_rows
1658 INTEGER :: nsubchunks
1660 INTEGER :: atom_rows, iatom, rows
1665 IF (max_rows <= 0)
THEN
1672 atom_rows = int(
cached_layout%chunk_atomic_grid_sizes(iatom))
1673 IF (rows > 0 .AND. rows + atom_rows > max_rows)
THEN
1674 nsubchunks = nsubchunks + 1
1677 rows = rows + atom_rows
1679 IF (rows > 0) nsubchunks = nsubchunks + 1
1680 nsubchunks = max(1, nsubchunks)
1693 max_rows, requires_grad)
1696 INTEGER,
INTENT(IN) :: subchunk_index, max_rows
1697 LOGICAL,
INTENT(IN) :: requires_grad
1699 INTEGER :: atom_begin, atom_count, atom_end, &
1700 max_grid_size, row_begin, row_count, &
1704 cpassert(parent%uses_atom_chunks)
1705 CALL atom_subchunk_bounds(subchunk_index, max_rows, atom_begin, atom_end, &
1707 atom_count = atom_end - atom_begin + 1
1708 row_count = row_end - row_begin + 1
1709 cpassert(atom_count > 0)
1710 cpassert(row_count > 0)
1711 mark_used(requires_grad)
1712 max_grid_size = maxval(int(
cached_layout%chunk_atomic_grid_sizes(atom_begin:atom_end)))
1714 ALLOCATE (features%atomic_grid_size_bound_shape(0, max_grid_size))
1715 features%atomic_grid_size_bound_shape = 0_int_8
1717 features%chunk_feature_count = row_count
1718 features%nflat = parent%nflat
1719 features%nflat_local = parent%nflat_local
1720 features%grid_weight_sum = sum(
cached_layout%chunk_grid_weights(row_begin:row_end))
1721 features%uses_atom_chunks = .true.
1722 features%uses_atom_chunk_routing = parent%uses_atom_chunk_routing
1723 CALL add_subchunk_feature_tensors(parent, features, atom_begin, atom_count, row_begin, &
1725 features%active = .true.
1738 SUBROUTINE atom_subchunk_bounds(subchunk_index, max_rows, atom_begin, atom_end, &
1740 INTEGER,
INTENT(IN) :: subchunk_index, max_rows
1741 INTEGER,
INTENT(OUT) :: atom_begin, atom_end, row_begin, row_end
1743 INTEGER :: atom_rows, current_subchunk, iatom, &
1746 cpassert(subchunk_index > 0)
1747 cpassert(max_rows > 0)
1754 current_subchunk = 1
1758 atom_rows = int(
cached_layout%chunk_atomic_grid_sizes(iatom))
1759 IF (rows > 0 .AND. rows + atom_rows > max_rows)
THEN
1760 IF (current_subchunk == subchunk_index)
THEN
1761 atom_end = iatom - 1
1762 row_end = row_cursor - 1
1765 current_subchunk = current_subchunk + 1
1767 row_begin = row_cursor
1770 rows = rows + atom_rows
1771 row_cursor = row_cursor + atom_rows
1774 IF (current_subchunk == subchunk_index)
THEN
1776 row_end = row_cursor - 1
1780 cpabort(
"Requested native SKALA atom subchunk does not exist.")
1782 END SUBROUTINE atom_subchunk_bounds
1793 SUBROUTINE add_subchunk_feature_tensors(parent, features, atom_begin, atom_count, row_begin, &
1797 INTEGER,
INTENT(IN) :: atom_begin, atom_count, row_begin, &
1801 cpassert(parent%active)
1802 cpassert(
ALLOCATED(features%atomic_grid_size_bound_shape))
1804 features%owns_coordinate_tensor = .false.
1805 features%owns_dynamic_tensors = .true.
1806 features%owns_inputs = .true.
1807 features%owns_static_tensors = .true.
1808 features%uses_collapsed_rks_dynamic = parent%uses_collapsed_rks_dynamic
1811 row_count, features%grid_coords_t)
1813 row_count, features%grid_weights_t)
1815 row_count, features%atomic_grid_weights_t)
1817 atom_count, features%atomic_grid_sizes_t)
1819 atom_begin - 1, atom_count, features%coarse_0_atomic_coords_t)
1821 features%atomic_grid_size_bound_shape)
1827 IF (features%uses_collapsed_rks_dynamic)
THEN
1835 CALL torch_dict_insert(features%inputs,
"grid_weights", features%grid_weights_t)
1837 features%atomic_grid_weights_t)
1839 features%atomic_grid_sizes_t)
1841 features%atomic_grid_size_bound_shape_t)
1842 IF (features%uses_collapsed_rks_dynamic)
THEN
1852 features%coarse_0_atomic_coords_t)
1854 END SUBROUTINE add_subchunk_feature_tensors
1861 SUBROUTINE add_owned_feature_tensors(features, requires_grad)
1863 LOGICAL,
INTENT(IN) :: requires_grad
1865 cpassert(
ALLOCATED(features%chunk_density))
1866 cpassert(
ALLOCATED(features%chunk_grad))
1867 cpassert(
ALLOCATED(features%chunk_kin))
1868 cpassert(
ALLOCATED(features%grid_coords))
1869 cpassert(
ALLOCATED(features%grid_weights))
1870 cpassert(
ALLOCATED(features%atomic_grid_weights))
1871 cpassert(
ALLOCATED(features%atomic_grid_sizes))
1872 cpassert(
ALLOCATED(features%atomic_grid_size_bound_shape))
1873 cpassert(
ALLOCATED(features%coarse_0_atomic_coords))
1875 features%owns_coordinate_tensor = .false.
1876 features%owns_dynamic_tensors = .true.
1877 features%owns_inputs = .true.
1878 features%owns_static_tensors = .true.
1889 features%coarse_0_atomic_coords)
1892 features%atomic_grid_size_bound_shape)
1903 CALL torch_dict_insert(features%inputs,
"grid_weights", features%grid_weights_t)
1905 features%atomic_grid_weights_t)
1907 features%atomic_grid_sizes_t)
1909 features%atomic_grid_size_bound_shape_t)
1914 features%coarse_0_atomic_coords_t)
1916 END SUBROUTINE add_owned_feature_tensors
1927 SUBROUTINE add_feature_tensors(features, requires_grad, requires_coordinate_grad, &
1928 requires_stress_grad, use_atom_chunks, requires_weight_grad)
1930 LOGICAL,
INTENT(IN) :: requires_grad, requires_coordinate_grad, &
1931 requires_stress_grad, use_atom_chunks
1932 LOGICAL,
INTENT(IN),
OPTIONAL :: requires_weight_grad
1934 LOGICAL :: my_requires_weight_grad
1936 my_requires_weight_grad = .false.
1937 IF (
PRESENT(requires_weight_grad)) my_requires_weight_grad = requires_weight_grad
1940 features%owns_static_tensors = .false.
1941 features%owns_coordinate_tensor = .false.
1942 features%owns_grid_coordinate_tensor = .false.
1943 features%owns_weight_tensors = .false.
1944 features%owns_dynamic_tensors = .false.
1945 features%owns_inputs = .true.
1946 IF (use_atom_chunks)
THEN
1947 cpassert(.NOT. requires_coordinate_grad)
1948 cpassert(.NOT. requires_stress_grad)
1949 cpassert(.NOT. my_requires_weight_grad)
1952 features%grid_weights_t =
cached_layout%chunk_grid_weights_t
1953 features%atomic_grid_weights_t =
cached_layout%chunk_atomic_grid_weights_t
1954 features%atomic_grid_sizes_t =
cached_layout%chunk_atomic_grid_sizes_t
1955 features%atomic_grid_size_bound_shape_t = &
1957 features%local_feature_indices_t =
cached_layout%chunk_feature_indices_t
1961 features%uses_collapsed_rks_dynamic))
THEN
1965 IF (.NOT. features%uses_collapsed_rks_dynamic .AND. &
1974 features%chunk_density, requires_grad=requires_grad)
1977 requires_grad=requires_grad)
1980 requires_grad=requires_grad)
1984 IF (features%uses_collapsed_rks_dynamic .AND. &
1994 IF (features%uses_collapsed_rks_dynamic)
THEN
1995 features%density_input_t =
cached_layout%chunk_density_input_t
2002 IF (features%uses_collapsed_rks_dynamic)
THEN
2004 features%density_input_t)
2006 features%grad_input_t)
2008 features%kin_input_t)
2019 cached_layout%chunk_inputs_use_collapsed_rks = features%uses_collapsed_rks_dynamic
2023 features%owns_inputs = .false.
2024 features%coarse_0_atomic_coords_t =
cached_layout%chunk_coarse_0_atomic_coords_t
2026 IF (.NOT. requires_stress_grad .AND. .NOT. my_requires_weight_grad)
THEN
2029 features%atomic_grid_weights_t =
cached_layout%atomic_grid_weights_t
2031 features%atomic_grid_sizes_t =
cached_layout%atomic_grid_sizes_t
2032 features%atomic_grid_size_bound_shape_t =
cached_layout%atomic_grid_size_bound_shape_t
2033 features%local_feature_indices_t =
cached_layout%local_feature_indices_t
2036 requires_grad=requires_grad)
2039 requires_grad=requires_grad)
2042 requires_grad=requires_grad)
2046 IF (requires_coordinate_grad .OR. requires_stress_grad .OR. my_requires_weight_grad)
THEN
2047 IF (requires_stress_grad .OR. my_requires_weight_grad)
THEN
2049 IF (requires_stress_grad)
THEN
2053 features%owns_grid_coordinate_tensor = .true.
2061 features%atomic_grid_weights)
2063 CALL torch_dict_insert(features%inputs,
"grid_weights", features%grid_weights_t)
2065 features%atomic_grid_weights_t)
2067 features%atomic_grid_sizes_t)
2069 features%atomic_grid_size_bound_shape_t)
2070 features%owns_weight_tensors = .true.
2088 features%owns_inputs = .false.
2089 features%coarse_0_atomic_coords_t =
cached_layout%coarse_0_atomic_coords_t
2093 IF (requires_coordinate_grad .OR. requires_stress_grad)
THEN
2094 cpassert(.NOT. use_atom_chunks)
2096 features%coarse_0_atomic_coords)
2099 features%coarse_0_atomic_coords_t)
2100 features%owns_coordinate_tensor = .true.
2103 END SUBROUTINE add_feature_tensors
2111 FUNCTION grid_coordinate(pw_grid, index)
RESULT(coord)
2113 INTEGER,
DIMENSION(3),
INTENT(IN) :: index
2114 REAL(kind=
dp),
DIMENSION(3) :: coord
2116 INTEGER,
DIMENSION(3) :: relative_index
2118 relative_index = index - pw_grid%bounds(1, :)
2119 coord = real(relative_index(1), kind=
dp)*pw_grid%dh(:, 1) + &
2120 REAL(relative_index(2), kind=
dp)*pw_grid%dh(:, 2) + &
2121 REAL(relative_index(3), kind=
dp)*pw_grid%dh(:, 3)
2123 END FUNCTION grid_coordinate
2134 SUBROUTINE smooth_atom_partition(grid_point, atom_coords, cell, weights, atom_image_coords, &
2136 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: grid_point
2137 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: atom_coords
2139 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: weights
2140 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(OUT) :: atom_image_coords
2141 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: distances
2143 INTEGER :: iatom, jatom, natom
2144 REAL(kind=
dp) :: mu, rab, rsum, switch, total
2145 REAL(kind=
dp),
DIMENSION(3) :: rij
2146 REAL(kind=
dp),
DIMENSION(3, SIZE(atom_coords, 2)) :: partition_atom_coords
2148 natom =
SIZE(atom_coords, 2)
2149 cpassert(
SIZE(weights) == natom)
2150 cpassert(
SIZE(atom_image_coords, 1) == 3)
2151 cpassert(
SIZE(atom_image_coords, 2) == natom)
2152 cpassert(
SIZE(distances) == natom)
2155 atom_image_coords(:, iatom) = &
2156 nearest_image_coordinate(atom_coords(:, iatom), grid_point, cell)
2157 partition_atom_coords(:, iatom) = &
2158 nearest_atom_image_coordinate(atom_coords(:, iatom), grid_point, cell)
2159 rij = grid_point - partition_atom_coords(:, iatom)
2160 distances(iatom) = sqrt(sum(rij**2))
2164 DO iatom = 1, natom - 1
2165 DO jatom = iatom + 1, natom
2166 rij = partition_atom_coords(:, iatom) - partition_atom_coords(:, jatom)
2167 rab = sqrt(sum(rij**2))
2168 IF (rab <= layout_tol) cycle
2169 mu = (distances(iatom) - distances(jatom))/rab
2170 mu = max(-1.0_dp, min(1.0_dp, mu))
2171 switch = 0.5_dp*(1.0_dp - becke_shape(mu))
2172 weights(iatom) = weights(iatom)*switch
2173 weights(jatom) = weights(jatom)*(1.0_dp - switch)
2177 total = sum(weights)
2178 IF (total > 0.0_dp)
THEN
2179 weights = weights/total
2184 IF (distances(iatom) < rsum)
THEN
2185 rsum = distances(iatom)
2190 weights(jatom) = 1.0_dp
2193 END SUBROUTINE smooth_atom_partition
2206 weights, included, dweights_datom, &
2208 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: grid_point
2209 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: atom_coords
2211 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: weights
2212 LOGICAL,
DIMENSION(:),
INTENT(OUT) :: included
2213 REAL(kind=
dp),
DIMENSION(:, :, :),
INTENT(OUT) :: dweights_datom, dweights_dstrain
2215 INTEGER :: iatom, idir, jatom, jdir, natom
2216 REAL(kind=
dp) :: dist_diff, ds_dmu, included_sum, mu, &
2217 mu_raw, one_minus_switch, rab, rsum, &
2219 REAL(kind=
dp),
DIMENSION(3) :: dmu_atom_i, dmu_atom_j, ds_atom_i, &
2220 ds_atom_j, pair, unit_pair
2221 REAL(kind=
dp),
DIMENSION(3, 3) :: dmu_strain, ds_strain, mean_strain
2222 REAL(kind=
dp),
DIMENSION(3, SIZE(atom_coords, 2), &
SIZE(atom_coords, 2)) :: log_weight_atom
2223 REAL(kind=
dp),
DIMENSION(3, SIZE(atom_coords, 2)) :: mean_atom, partition_atom_coords, rvecs, &
2226 DIMENSION(3, 3, SIZE(atom_coords, 2)) :: log_weight_strain
2227 REAL(kind=
dp),
DIMENSION(SIZE(atom_coords, 2)) :: distances, normalized_weights, &
2230 natom =
SIZE(atom_coords, 2)
2231 cpassert(
SIZE(weights) == natom)
2232 cpassert(
SIZE(included) == natom)
2233 cpassert(
SIZE(dweights_datom, 1) == 3)
2234 cpassert(
SIZE(dweights_datom, 2) == natom)
2235 cpassert(
SIZE(dweights_datom, 3) == natom)
2236 cpassert(
SIZE(dweights_dstrain, 1) == 3)
2237 cpassert(
SIZE(dweights_dstrain, 2) == 3)
2238 cpassert(
SIZE(dweights_dstrain, 3) == natom)
2242 dweights_datom = 0.0_dp
2243 dweights_dstrain = 0.0_dp
2244 raw_weights = 1.0_dp
2245 log_weight_atom = 0.0_dp
2246 log_weight_strain = 0.0_dp
2249 partition_atom_coords(:, iatom) = &
2250 nearest_atom_image_coordinate(atom_coords(:, iatom), grid_point, cell)
2251 rvecs(:, iatom) = grid_point - partition_atom_coords(:, iatom)
2252 distances(iatom) = sqrt(sum(rvecs(:, iatom)**2))
2253 IF (distances(iatom) > layout_tol)
THEN
2254 unit_rvecs(:, iatom) = rvecs(:, iatom)/distances(iatom)
2256 unit_rvecs(:, iatom) = 0.0_dp
2260 DO iatom = 1, natom - 1
2261 DO jatom = iatom + 1, natom
2262 pair = partition_atom_coords(:, iatom) - partition_atom_coords(:, jatom)
2263 rab = sqrt(sum(pair**2))
2264 IF (rab <= layout_tol) cycle
2265 unit_pair = pair/rab
2266 dist_diff = distances(iatom) - distances(jatom)
2267 mu_raw = dist_diff/rab
2268 mu = max(-1.0_dp, min(1.0_dp, mu_raw))
2269 switch = 0.5_dp*(1.0_dp - becke_shape(mu))
2270 one_minus_switch = 1.0_dp - switch
2272 IF (abs(mu_raw) < 1.0_dp)
THEN
2273 ds_dmu = -0.5_dp*becke_shape_derivative(mu)
2277 IF (abs(ds_dmu) > 0.0_dp .AND. switch > tiny(1.0_dp) .AND. &
2278 one_minus_switch > tiny(1.0_dp))
THEN
2279 dmu_atom_i = (-unit_rvecs(:, iatom)*rab - dist_diff*unit_pair)/rab**2
2280 dmu_atom_j = (unit_rvecs(:, jatom)*rab + dist_diff*unit_pair)/rab**2
2281 ds_atom_i = ds_dmu*dmu_atom_i
2282 ds_atom_j = ds_dmu*dmu_atom_j
2283 log_weight_atom(:, iatom, iatom) = &
2284 log_weight_atom(:, iatom, iatom) + ds_atom_i/switch
2285 log_weight_atom(:, iatom, jatom) = &
2286 log_weight_atom(:, iatom, jatom) - ds_atom_i/one_minus_switch
2287 log_weight_atom(:, jatom, iatom) = &
2288 log_weight_atom(:, jatom, iatom) + ds_atom_j/switch
2289 log_weight_atom(:, jatom, jatom) = &
2290 log_weight_atom(:, jatom, jatom) - ds_atom_j/one_minus_switch
2294 dmu_strain(idir, jdir) = &
2295 ((unit_rvecs(idir, iatom)*rvecs(jdir, iatom) - &
2296 unit_rvecs(idir, jatom)*rvecs(jdir, jatom))*rab - &
2297 dist_diff*unit_pair(idir)*pair(jdir))/rab**2
2300 ds_strain = ds_dmu*dmu_strain
2301 log_weight_strain(:, :, iatom) = &
2302 log_weight_strain(:, :, iatom) + ds_strain/switch
2303 log_weight_strain(:, :, jatom) = &
2304 log_weight_strain(:, :, jatom) - ds_strain/one_minus_switch
2307 raw_weights(iatom) = raw_weights(iatom)*switch
2308 raw_weights(jatom) = raw_weights(jatom)*one_minus_switch
2312 total = sum(raw_weights)
2313 IF (total > 0.0_dp)
THEN
2314 normalized_weights = raw_weights/total
2315 included = normalized_weights > smooth_partition_eps
2320 IF (distances(iatom) < rsum)
THEN
2321 rsum = distances(iatom)
2325 included(jatom) = .true.
2326 weights(jatom) = 1.0_dp
2330 included_sum = sum(raw_weights, mask=included)
2331 IF (included_sum <= 0.0_dp)
THEN
2335 IF (distances(iatom) < rsum)
THEN
2336 rsum = distances(iatom)
2341 included(jatom) = .true.
2343 weights(jatom) = 1.0_dp
2348 IF (included(iatom)) weights(iatom) = raw_weights(iatom)/included_sum
2352 mean_strain = 0.0_dp
2354 IF (.NOT. included(iatom)) cycle
2355 mean_strain = mean_strain + weights(iatom)*log_weight_strain(:, :, iatom)
2357 mean_atom(:, jatom) = mean_atom(:, jatom) + &
2358 weights(iatom)*log_weight_atom(:, jatom, iatom)
2363 IF (.NOT. included(iatom)) cycle
2364 dweights_dstrain(:, :, iatom) = &
2365 weights(iatom)*(log_weight_strain(:, :, iatom) - mean_strain)
2367 dweights_datom(:, jatom, iatom) = &
2368 weights(iatom)*(log_weight_atom(:, jatom, iatom) - mean_atom(:, jatom))
2379 PURE FUNCTION becke_shape(mu)
RESULT(val)
2380 REAL(kind=
dp),
INTENT(IN) :: mu
2381 REAL(kind=
dp) :: val
2387 val = 0.5_dp*val*(3.0_dp - val*val)
2390 END FUNCTION becke_shape
2397 PURE FUNCTION becke_shape_derivative(mu)
RESULT(val)
2398 REAL(kind=
dp),
INTENT(IN) :: mu
2399 REAL(kind=
dp) :: val
2407 val = val*1.5_dp*(1.0_dp - x*x)
2408 x = 0.5_dp*x*(3.0_dp - x*x)
2411 END FUNCTION becke_shape_derivative
2420 FUNCTION nearest_atom_image_coordinate(atom_coord, grid_point, cell)
RESULT(coord)
2421 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: atom_coord, grid_point
2423 REAL(kind=
dp),
DIMENSION(3) :: coord
2425 REAL(kind=
dp) :: dx, dy, dz
2427 IF (cell%orthorhombic)
THEN
2428 dx = atom_coord(1) - grid_point(1)
2429 dy = atom_coord(2) - grid_point(2)
2430 dz = atom_coord(3) - grid_point(3)
2431 dx = dx - cell%hmat(1, 1)*cell%perd(1)*anint(cell%h_inv(1, 1)*dx)
2432 dy = dy - cell%hmat(2, 2)*cell%perd(2)*anint(cell%h_inv(2, 2)*dy)
2433 dz = dz - cell%hmat(3, 3)*cell%perd(3)*anint(cell%h_inv(3, 3)*dz)
2434 coord = grid_point + [dx, dy, dz]
2436 coord = grid_point +
pbc(grid_point, atom_coord, cell)
2439 END FUNCTION nearest_atom_image_coordinate
2448 FUNCTION nearest_image_coordinate(owner_coord, grid_point, cell)
RESULT(coord)
2449 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: owner_coord, grid_point
2451 REAL(kind=
dp),
DIMENSION(3) :: coord
2453 REAL(kind=
dp) :: dx, dy, dz
2455 IF (cell%orthorhombic)
THEN
2456 dx = grid_point(1) - owner_coord(1)
2457 dy = grid_point(2) - owner_coord(2)
2458 dz = grid_point(3) - owner_coord(3)
2459 dx = dx - cell%hmat(1, 1)*cell%perd(1)*anint(cell%h_inv(1, 1)*dx)
2460 dy = dy - cell%hmat(2, 2)*cell%perd(2)*anint(cell%h_inv(2, 2)*dy)
2461 dz = dz - cell%hmat(3, 3)*cell%perd(3)*anint(cell%h_inv(3, 3)*dz)
2462 coord = owner_coord + [dx, dy, dz]
2464 coord = owner_coord +
pbc(owner_coord, grid_point, cell)
2467 END FUNCTION nearest_image_coordinate
2476 FUNCTION nearest_atom(grid_point, atom_coords, cell)
RESULT(owner)
2477 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: grid_point
2478 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: atom_coords
2483 REAL(kind=
dp) :: best_r2, dx, dy, dz, r2
2484 REAL(kind=
dp),
DIMENSION(3) :: rij
2487 best_r2 = huge(1.0_dp)
2488 IF (cell%orthorhombic)
THEN
2489 DO iatom = 1,
SIZE(atom_coords, 2)
2490 dx = grid_point(1) - atom_coords(1, iatom)
2491 dy = grid_point(2) - atom_coords(2, iatom)
2492 dz = grid_point(3) - atom_coords(3, iatom)
2493 dx = dx - cell%hmat(1, 1)*cell%perd(1)*anint(cell%h_inv(1, 1)*dx)
2494 dy = dy - cell%hmat(2, 2)*cell%perd(2)*anint(cell%h_inv(2, 2)*dy)
2495 dz = dz - cell%hmat(3, 3)*cell%perd(3)*anint(cell%h_inv(3, 3)*dz)
2496 r2 = dx*dx + dy*dy + dz*dz
2497 IF (r2 < best_r2)
THEN
2503 DO iatom = 1,
SIZE(atom_coords, 2)
2504 rij =
pbc(grid_point, atom_coords(:, iatom), cell)
2506 IF (r2 < best_r2)
THEN
2513 END FUNCTION nearest_atom
Handles all functions related to the CELL.
various utilities that regard array of different kinds: output, allocation,... maybe it is not a good...
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public dp
Interface to the message passing library MPI.
Define the data structure for the particle information.
Build SKALA TorchScript feature dictionaries from CP2K GPW real-space grids.
subroutine, public skala_gpw_feature_build_atom_subchunk(parent, features, subchunk_index, max_rows, requires_grad)
Build an atom-contiguous subchunk feature bundle from a rank-local atom chunk.
integer, parameter, public skala_gpw_atom_partition_hard
subroutine, public skala_gpw_feature_release(features)
Release Torch objects and backing arrays owned by a feature bundle.
integer function, public skala_gpw_atom_subchunk_count(max_rows)
Return how many atom-contiguous subchunks the cached rank chunk needs.
subroutine, public skala_gpw_feature_build(features, rho_set, rho_r, particle_set, cell, requires_grad, weights, requires_coordinate_grad, requires_stress_grad, use_atom_chunks, route_atom_chunks, atom_partition)
Build a flat SKALA molecular feature dictionary from a local GPW grid.
type(skala_gpw_layout_cache_type), save cached_layout
subroutine, public skala_gpw_smooth_partition_derivatives(grid_point, atom_coords, cell, weights, included, dweights_datom, dweights_dstrain)
Build smooth atom weights and their atom/cell deformation derivatives.
integer, parameter, public skala_gpw_atom_partition_smooth
subroutine, public torch_dict_release(dict)
Releases a Torch dictionary and all its ressources.
subroutine, public torch_tensor_narrow(tensor, dim, start_index, length, result)
Creates a view of a contiguous tensor slice.
subroutine, public torch_tensor_to_device_leaf(tensor, requires_grad)
Moves a tensor to the active Torch device and makes it an autograd leaf.
subroutine, public torch_dict_create(dict)
Creates an empty Torch dictionary.
subroutine, public torch_tensor_expand_dim(tensor, dim, extent, result)
Creates an expanded tensor view along one singleton dimension.
subroutine, public torch_dict_insert(dict, key, tensor)
Inserts a Torch tensor into a Torch dictionary.
subroutine, public torch_dict_clone(source, target)
Clones a Torch dictionary.
subroutine, public torch_tensor_release(tensor)
Releases a Torch tensor and all its ressources.
subroutine, public xc_rho_set_get(rho_set, can_return_null, rho, drho, norm_drho, rhoa, rhob, norm_drhoa, norm_drhob, rho_1_3, rhoa_1_3, rhob_1_3, laplace_rho, laplace_rhoa, laplace_rhob, drhoa, drhob, rho_cutoff, drho_cutoff, tau_cutoff, tau, tau_a, tau_b, local_bounds)
returns the various attributes of rho_set
Type defining parameters related to the simulation cell.
represent a pointer to a contiguous 3d array
represent a density, with all the representation and data needed to perform a functional evaluation