(git:691081d)
Loading...
Searching...
No Matches
qs_native_grid_cache.F
Go to the documentation of this file.
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!> \brief Bounded geometry-only cache for native atom-grid interpolation.
9 USE cell_types, ONLY: cell_type
10 USE kinds, ONLY: dp,&
11 int_8
13
14 IMPLICIT NONE
15 PRIVATE
16
19 INTEGER(KIND=int_8), PARAMETER :: cache_budget = 128_int_8*1024*1024
20
22 INTEGER :: relative_index(native_grid_interp_npts, 3) = 0
23 REAL(dp) :: weight(native_grid_interp_npts, 3) = 0.0_dp
24 LOGICAL :: valid(native_grid_interp_npts, 3) = .false.
25 LOGICAL :: active = .false.
27
28 TYPE stencil_entry_type
30 REAL(dp) :: point(3) = 0.0_dp
31 LOGICAL :: ready = .false.
32 END TYPE stencil_entry_type
33
35 PRIVATE
36 TYPE(stencil_entry_type), ALLOCATABLE :: entries(:)
37 REAL(dp) :: dh_inv(3, 3) = 0.0_dp, hmat(3, 3) = 0.0_dp
38 INTEGER :: npts(3) = 0, periodic(3) = 0, nrows = -1
39 LOGICAL :: wrap = .false., prepared = .false.
41
45CONTAINS
46! **************************************************************************************************
47!> \brief Prepare outside parallel regions. Cache a bounded prefix and compute the rest normally.
48!> \param cache per-QS-environment cache
49!> \param grid current auxiliary grid
50!> \param cell current cell and periodicity
51!> \param nrows local atom-grid row count
52!> \param wrap wrap the auxiliary cell in nonperiodic directions
53!> \param max_bytes optional memory budget for testing, in bytes
54! **************************************************************************************************
55 SUBROUTINE prepare_native_grid_cache(cache, grid, cell, nrows, wrap, max_bytes)
56 TYPE(native_grid_cache_type), INTENT(INOUT) :: cache
57 TYPE(pw_grid_type), INTENT(IN) :: grid
58 TYPE(cell_type), INTENT(IN) :: cell
59 INTEGER, INTENT(IN) :: nrows
60 LOGICAL, INTENT(IN) :: wrap
61 INTEGER(KIND=int_8), INTENT(IN), OPTIONAL :: max_bytes
62
63 INTEGER :: count, ierr
64 INTEGER(KIND=int_8) :: budget, bytes_per_entry
65 LOGICAL :: unchanged
66 TYPE(stencil_entry_type) :: entry
67
68 budget = cache_budget
69 IF (PRESENT(max_bytes)) budget = max(0_int_8, max_bytes)
70 bytes_per_entry = int((storage_size(entry) + 7)/8, int_8)
71 count = int(min(int(max(0, nrows), int_8), budget/bytes_per_entry))
72 unchanged = .false.
73 IF (ALLOCATED(cache%entries)) THEN
74 unchanged = SIZE(cache%entries) == count
75 IF (.NOT. unchanged) CALL release_native_grid_cache(cache)
76 END IF
77 IF (.NOT. ALLOCATED(cache%entries)) THEN
78 ALLOCATE (cache%entries(count), stat=ierr)
79 IF (ierr /= 0) RETURN
80 END IF
81 unchanged = unchanged .AND. cache%prepared .AND. cache%nrows == nrows
82 unchanged = unchanged .AND. all(cache%dh_inv == grid%dh_inv) .AND. all(cache%npts == grid%npts)
83 unchanged = unchanged .AND. all(cache%hmat == cell%hmat) .AND. all(cache%periodic == cell%perd)
84 unchanged = unchanged .AND. (cache%wrap .EQV. wrap)
85 IF (.NOT. unchanged) cache%entries%ready = .false.
86 cache%dh_inv = grid%dh_inv
87 cache%npts = grid%npts
88 cache%hmat = cell%hmat
89 cache%periodic = cell%perd
90 cache%nrows = nrows
91 cache%wrap = wrap
92 cache%prepared = .true.
93 END SUBROUTINE prepare_native_grid_cache
94
95! **************************************************************************************************
96!> \brief Release geometry storage, including when the QS environment is only partly released.
97!> \param cache ...
98! **************************************************************************************************
99 SUBROUTINE release_native_grid_cache(cache)
100 TYPE(native_grid_cache_type), INTENT(INOUT) :: cache
101
102 IF (ALLOCATED(cache%entries)) DEALLOCATE (cache%entries)
103 cache%prepared = .false.
104 END SUBROUTINE release_native_grid_cache
105
106! **************************************************************************************************
107!> \brief Read a stencil only on an exact coordinate match. Safe for concurrent readers.
108!> \param cache ...
109!> \param row local grid row
110!> \param point current Cartesian coordinates, including atom motion and quadrature changes
111!> \param stencil cached interpolation stencil on a hit
112!> \return whether the cached stencil matches
113! **************************************************************************************************
114 FUNCTION fetch_native_grid_stencil(cache, row, point, stencil) RESULT(hit)
115 TYPE(native_grid_cache_type), INTENT(IN) :: cache
116 INTEGER, INTENT(IN) :: row
117 REAL(dp), INTENT(IN) :: point(3)
119 INTENT(OUT) :: stencil
120 LOGICAL :: hit
121
122 hit = .false.
123 IF (.NOT. cache%prepared .OR. .NOT. ALLOCATED(cache%entries)) RETURN
124 IF (row < 1 .OR. row > SIZE(cache%entries)) RETURN
125 IF (.NOT. cache%entries(row)%ready) RETURN
126 IF (any(cache%entries(row)%point /= point)) RETURN
127 stencil = cache%entries(row)%stencil
128 hit = .true.
129 END FUNCTION fetch_native_grid_stencil
130
131! **************************************************************************************************
132!> \brief Store from the unique forward owner of a row, never from concurrent adjoint readers.
133!> \param cache ...
134!> \param row local grid row
135!> \param point current Cartesian coordinates
136!> \param stencil complete value stencil, not an indices-only stencil
137! **************************************************************************************************
138 SUBROUTINE store_native_grid_stencil(cache, row, point, stencil)
139 TYPE(native_grid_cache_type), INTENT(INOUT) :: cache
140 INTEGER, INTENT(IN) :: row
141 REAL(dp), INTENT(IN) :: point(3)
143 INTENT(IN) :: stencil
144
145 IF (.NOT. cache%prepared .OR. .NOT. ALLOCATED(cache%entries)) RETURN
146 IF (row < 1 .OR. row > SIZE(cache%entries)) RETURN
147 cache%entries(row)%stencil = stencil
148 cache%entries(row)%point = point
149 cache%entries(row)%ready = .true.
150 END SUBROUTINE store_native_grid_stencil
151END MODULE qs_native_grid_cache
Handles all functions related to the CELL.
Definition cell_types.F:15
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
Bounded geometry-only cache for native atom-grid interpolation.
logical function, public fetch_native_grid_stencil(cache, row, point, stencil)
Read a stencil only on an exact coordinate match. Safe for concurrent readers.
subroutine, public store_native_grid_stencil(cache, row, point, stencil)
Store from the unique forward owner of a row, never from concurrent adjoint readers.
subroutine, public release_native_grid_cache(cache)
Release geometry storage, including when the QS environment is only partly released.
integer, parameter, public native_grid_interp_npts
integer, parameter, public native_grid_interp_offset_max
subroutine, public prepare_native_grid_cache(cache, grid, cell, nrows, wrap, max_bytes)
Prepare outside parallel regions. Cache a bounded prefix and compute the rest normally.
integer, parameter, public native_grid_interp_offset_min
Type defining parameters related to the simulation cell.
Definition cell_types.F:60