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 routines and types for Hartree-Fock-Exchange
10 : !> \par History
11 : !> 11.2006 created [Manuel Guidon]
12 : !> \author Manuel Guidon
13 : ! **************************************************************************************************
14 : MODULE hfx_compression_methods
15 : USE cp_files, ONLY: close_file, &
16 : open_file
17 : USE hfx_types, ONLY: hfx_cache_type, &
18 : hfx_container_type, &
19 : hfx_real_cache_type
20 : USE kinds, ONLY: dp, &
21 : int_8
22 : USE hfx_compression_core_methods, ONLY: bits2ints_specific, &
23 : groups2ints, &
24 : ints2bits_specific, &
25 : ints2groups
26 : USE iso_fortran_env, ONLY: iostat_end
27 : #if defined(__LIBXS)
28 : USE libxs, ONLY: libxs_vpack, libxs_vunpack, libxs_vpack_maxbits, LIBXS_DATATYPE_F64
29 : #endif
30 : #include "./base/base_uses.f90"
31 :
32 : IMPLICIT NONE
33 : PRIVATE
34 : PUBLIC :: hfx_add_single_cache_element, hfx_get_single_cache_element, &
35 : hfx_reset_cache_and_container, hfx_decompress_first_cache, &
36 : hfx_flush_last_cache, hfx_add_mult_cache_elements, &
37 : hfx_get_mult_cache_elements
38 : ! real values quantized and packed together (records of a cache), within an absolute error
39 : PUBLIC :: hfx_add_mult_real_elements, hfx_get_mult_real_elements, &
40 : hfx_flush_last_real_cache, hfx_decompress_first_real_cache, &
41 : hfx_reset_real_cache_and_container
42 :
43 : #define CACHE_SIZE 1024
44 :
45 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'hfx_compression_methods'
46 :
47 : ! values per group stored at one width (records of a cache), zero or negative: one width per
48 : ! container; the Fortran path takes multiples of 8 up to 248 (LIBXS chooses its own format)
49 : INTEGER, PARAMETER, PRIVATE :: group_size = 8
50 :
51 : INTEGER(kind=int_8), PARAMETER :: ugly_duck = ISHFT(1_int_8, 63)
52 : INTEGER(int_8), PARAMETER :: shifts(0:63) = &
53 : [1_int_8, 2_int_8, 4_int_8, 8_int_8, 16_int_8, 32_int_8, 64_int_8, 128_int_8, 256_int_8, &
54 : 512_int_8, 1024_int_8, 2048_int_8, 4096_int_8, 8192_int_8, 16384_int_8, 32768_int_8, &
55 : 65536_int_8, 131072_int_8, 262144_int_8, 524288_int_8, 1048576_int_8, 2097152_int_8, &
56 : 4194304_int_8, 8388608_int_8, 16777216_int_8, 33554432_int_8, 67108864_int_8, &
57 : 134217728_int_8, 268435456_int_8, 536870912_int_8, 1073741824_int_8, 2147483648_int_8, &
58 : 4294967296_int_8, 8589934592_int_8, 17179869184_int_8, 34359738368_int_8, 68719476736_int_8, &
59 : 137438953472_int_8, 274877906944_int_8, 549755813888_int_8, 1099511627776_int_8, 2199023255552_int_8, &
60 : 4398046511104_int_8, 8796093022208_int_8, 17592186044416_int_8, 35184372088832_int_8, 70368744177664_int_8, &
61 : 140737488355328_int_8, 281474976710656_int_8, 562949953421312_int_8, 1125899906842624_int_8, &
62 : 2251799813685248_int_8, 4503599627370496_int_8, 9007199254740992_int_8, 18014398509481984_int_8, &
63 : 36028797018963968_int_8, 72057594037927936_int_8, 144115188075855872_int_8, 288230376151711744_int_8, &
64 : 576460752303423488_int_8, 1152921504606846976_int_8, 2305843009213693952_int_8, &
65 : 4611686018427387904_int_8, ugly_duck]
66 :
67 : !***
68 :
69 : CONTAINS
70 :
71 : ! **************************************************************************************************
72 : !> \brief - This routine adds an int_8 value to a cache. If the cache is full
73 : !> a compression routine is invoked and the cache is cleared
74 : !> \param value value to be added to the cache
75 : !> \param nbits number of bits to be stored
76 : !> \param cache cache to which we want to add
77 : !> \param container container that contains the compressed elements
78 : !> \param memory_usage ...
79 : !> \param use_disk_storage ...
80 : !> \param max_val_memory ...
81 : !> \par History
82 : !> 10.2007 created [Manuel Guidon]
83 : !> \author Manuel Guidon
84 : ! **************************************************************************************************
85 4661198 : SUBROUTINE hfx_add_single_cache_element(value, nbits, cache, container, memory_usage, use_disk_storage, &
86 : max_val_memory)
87 : INTEGER(int_8) :: value
88 : INTEGER :: nbits
89 : TYPE(hfx_cache_type) :: cache
90 : TYPE(hfx_container_type) :: container
91 : INTEGER :: memory_usage
92 : LOGICAL :: use_disk_storage
93 : INTEGER(int_8), OPTIONAL :: max_val_memory
94 :
95 : INTEGER(int_8) :: int_val
96 :
97 4661198 : int_val = value + shifts(nbits - 1)
98 :
99 4661198 : IF (cache%element_counter /= CACHE_SIZE) THEN
100 4657705 : cache%data(cache%element_counter) = int_val
101 4657705 : cache%element_counter = cache%element_counter + 1
102 : ELSE
103 3493 : cache%data(CACHE_SIZE) = int_val
104 : CALL hfx_compress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage, &
105 3493 : max_val_memory)
106 3493 : cache%element_counter = 1
107 : END IF
108 4661198 : END SUBROUTINE hfx_add_single_cache_element
109 :
110 : ! **************************************************************************************************
111 : !> \brief - This routine compresses a full cache and stores its values
112 : !> in a container. If necessary, a new list entry is allocated
113 : !> \param full_array values from the cache
114 : !> \param container linked list, that stores the compressed values
115 : !> \param nbits number of bits to be stored
116 : !> \param memory_usage ...
117 : !> \param use_disk_storage ...
118 : !> \param max_val_memory ...
119 : !> \par History
120 : !> 10.2007 created [Manuel Guidon]
121 : !> \author Manuel Guidon
122 : ! **************************************************************************************************
123 323923 : SUBROUTINE hfx_compress_cache(full_array, container, nbits, memory_usage, use_disk_storage, &
124 : max_val_memory)
125 : INTEGER(int_8) :: full_array(*)
126 : TYPE(hfx_container_type) :: container
127 : INTEGER, INTENT(IN) :: nbits
128 : INTEGER :: memory_usage
129 : LOGICAL :: use_disk_storage
130 : INTEGER(int_8), OPTIONAL :: max_val_memory
131 :
132 : INTEGER :: end_idx, increment_counter, nwords, &
133 : start_idx, tmp_elements, tmp_nints
134 : INTEGER(int_8), DIMENSION(CACHE_SIZE) :: values
135 : INTEGER(int_8), DIMENSION(CACHE_SIZE+64) :: record
136 :
137 : IF (group_size > 0) THEN
138 : ! a record is its length in words followed by the values at a width per group
139 332021075 : values = full_array(1:CACHE_SIZE) - shifts(nbits - 1)
140 : #if defined(__LIBXS)
141 : ! default level (LIBXS_PACK in the environment), at most CACHE_SIZE+51 words for 64 bits
142 323923 : nwords = INT(libxs_vpack(record(2:), values, nbits))
143 : #else
144 : nwords = ints2groups(nbits, group_size, record(2:), values)
145 : #endif
146 323923 : record(1) = nwords
147 323923 : CALL hfx_container_put(container, record, nwords + 1, memory_usage, use_disk_storage, max_val_memory)
148 : ELSE
149 : start_idx = container%element_counter
150 : increment_counter = (nbits*CACHE_SIZE + 63)/64
151 : end_idx = start_idx + increment_counter - 1
152 : IF (end_idx < CACHE_SIZE) THEN
153 : CALL ints2bits_specific(nbits, CACHE_SIZE, container%current%data(start_idx), full_array(1))
154 : container%element_counter = container%element_counter + increment_counter
155 : ELSE
156 : !! We have to fill the container first with the remaining number of bits
157 : tmp_elements = CACHE_SIZE - start_idx + 1
158 : tmp_nints = (tmp_elements*64)/nbits
159 : CALL ints2bits_specific(nbits, tmp_nints, container%current%data(start_idx), full_array(1))
160 : IF (use_disk_storage) THEN
161 : !! write to file
162 : WRITE (container%unit) container%current%data
163 : !$OMP ATOMIC
164 : memory_usage = memory_usage + 1
165 : container%file_counter = container%file_counter + 1
166 : ELSE
167 : !! Allocate new list entry
168 : ALLOCATE (container%current%next)
169 : !$OMP ATOMIC
170 : memory_usage = memory_usage + 1
171 : container%current%next%next => NULL()
172 : container%current => container%current%next
173 : IF (PRESENT(max_val_memory)) max_val_memory = max_val_memory + 1
174 : END IF
175 : !! compress remaining ints
176 : CALL ints2bits_specific(nbits, CACHE_SIZE - tmp_nints, container%current%data(1), full_array(tmp_nints + 1))
177 : container%element_counter = 1 + (nbits*(CACHE_SIZE - tmp_nints) + 63)/64
178 : END IF
179 : END IF
180 :
181 323923 : END SUBROUTINE hfx_compress_cache
182 :
183 : ! **************************************************************************************************
184 : !> \brief - This routine returns an int_8 value from a cache. If the cache is empty
185 : !> a decompression routine is invoked and the cache is refilled with decompressed
186 : !> values from a container
187 : !> \param value value to be retained from the cache
188 : !> \param nbits number of bits with which the value has been compressed
189 : !> \param cache cache from which we get the value
190 : !> \param container container that contains the compressed elements
191 : !> \param memory_usage ...
192 : !> \param use_disk_storage ...
193 : !> \par History
194 : !> 10.2007 created [Manuel Guidon]
195 : !> \author Manuel Guidon
196 : ! **************************************************************************************************
197 70848167 : SUBROUTINE hfx_get_single_cache_element(value, nbits, cache, container, memory_usage, use_disk_storage)
198 : INTEGER(int_8) :: value
199 : INTEGER :: nbits
200 : TYPE(hfx_cache_type) :: cache
201 : TYPE(hfx_container_type) :: container
202 : INTEGER :: memory_usage
203 : LOGICAL :: use_disk_storage
204 :
205 70848167 : IF (cache%element_counter /= CACHE_SIZE) THEN
206 70794526 : value = cache%data(cache%element_counter)
207 70794526 : cache%element_counter = cache%element_counter + 1
208 : ELSE
209 53641 : value = cache%data(CACHE_SIZE)
210 53641 : CALL hfx_decompress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
211 53641 : cache%element_counter = 1
212 : END IF
213 :
214 70848167 : value = value - shifts(nbits - 1)
215 :
216 70848167 : END SUBROUTINE hfx_get_single_cache_element
217 :
218 : ! **************************************************************************************************
219 : !> \brief - This routine decompresses data from a container in order to fill
220 : !> a cache.
221 : !> \param full_array values to be retained from container
222 : !> \param container linked list, that stores the compressed values
223 : !> \param nbits number of bits with which the values have been stored
224 : !> \param memory_usage ...
225 : !> \param use_disk_storage ...
226 : !> \par History
227 : !> 10.2007 created [Manuel Guidon]
228 : !> \author Manuel Guidon
229 : ! **************************************************************************************************
230 4493058 : SUBROUTINE hfx_decompress_cache(full_array, container, nbits, memory_usage, use_disk_storage)
231 : INTEGER(int_8) :: full_array(*)
232 : TYPE(hfx_container_type) :: container
233 : INTEGER, INTENT(IN) :: nbits
234 : INTEGER :: memory_usage
235 : LOGICAL :: use_disk_storage
236 :
237 : INTEGER :: end_idx, increment_counter, nwords, &
238 : start_idx, stat, tmp_elements, &
239 : tmp_nints
240 : INTEGER(int_8) :: nread
241 : INTEGER(int_8), DIMENSION(CACHE_SIZE) :: values
242 : INTEGER(int_8), DIMENSION(CACHE_SIZE+64) :: record
243 :
244 4493058 : CPASSERT(ASSOCIATED(container%current))
245 :
246 : IF (group_size > 0) THEN
247 4493058 : CALL hfx_container_get(container, record, 1, memory_usage, use_disk_storage)
248 4493058 : nwords = INT(record(1))
249 : ! a zero word is no record (nothing stored), which decodes as zeros like the fixed-width format
250 4493058 : IF (0 < nwords) THEN
251 4486416 : CPASSERT(nwords < SIZE(record))
252 4486416 : CALL hfx_container_get(container, record(2:), nwords, memory_usage, use_disk_storage)
253 : #if defined(__LIBXS)
254 4486416 : nread = INT(libxs_vunpack(values, record(2:nwords + 1)), KIND=int_8)
255 : #else
256 : nread = INT(groups2ints(record(2:nwords + 1), values), KIND=int_8)
257 : #endif
258 4486416 : CPASSERT(nread == nwords)
259 4598576400 : full_array(1:CACHE_SIZE) = values + shifts(nbits - 1)
260 : ELSE
261 6642 : CPASSERT(0 == nwords)
262 6808050 : full_array(1:CACHE_SIZE) = 0
263 : END IF
264 : ELSE
265 : start_idx = container%element_counter
266 : increment_counter = (nbits*CACHE_SIZE + 63)/64
267 : end_idx = start_idx + increment_counter - 1
268 : IF (end_idx < CACHE_SIZE) THEN
269 : CALL bits2ints_specific(nbits, CACHE_SIZE, container%current%data(start_idx), full_array(1))
270 : container%element_counter = container%element_counter + increment_counter
271 : ELSE
272 : !! We have to fill the container first with the remaining number of bits
273 : tmp_elements = CACHE_SIZE - start_idx + 1
274 : tmp_nints = (tmp_elements*64)/nbits
275 : CALL bits2ints_specific(nbits, tmp_nints, container%current%data(start_idx), full_array(1))
276 : IF (use_disk_storage) THEN
277 : !! it could happen, that we are at the end of a file and we try to read
278 : !! This happens in case a container has fully been filled in the compression step
279 : !! but no other was needed for the current bit size
280 : !! Therefore we can safely igonore an eof error
281 : ! We still need to ask for it to read the data correctly, so we mark it as used
282 : READ (container%unit, IOSTAT=stat) container%current%data
283 : MARK_USED(stat)
284 : memory_usage = memory_usage + 1
285 : container%file_counter = container%file_counter + 1
286 : ELSE
287 : container%current => container%current%next
288 : memory_usage = memory_usage + 1
289 : END IF
290 : !! decompress remaining ints
291 : CALL bits2ints_specific(nbits, CACHE_SIZE - tmp_nints, container%current%data(1), full_array(tmp_nints + 1))
292 : container%element_counter = 1 + (nbits*(CACHE_SIZE - tmp_nints) + 63)/64
293 : END IF
294 : END IF
295 4493058 : END SUBROUTINE hfx_decompress_cache
296 :
297 : ! **************************************************************************************************
298 : !> \brief Appends words to a container: a full list entry is written to file or followed by a
299 : !> new one (as hfx_compress_cache does for packed values).
300 : !> \param container linked list, that stores the compressed values
301 : !> \param words words to be appended
302 : !> \param nwords number of words
303 : !> \param memory_usage ...
304 : !> \param use_disk_storage ...
305 : !> \param max_val_memory ...
306 : ! **************************************************************************************************
307 570667 : SUBROUTINE hfx_container_put(container, words, nwords, memory_usage, use_disk_storage, max_val_memory)
308 : TYPE(hfx_container_type) :: container
309 : INTEGER(int_8), DIMENSION(*), INTENT(IN) :: words
310 : INTEGER, INTENT(IN) :: nwords
311 : INTEGER :: memory_usage
312 : LOGICAL :: use_disk_storage
313 : INTEGER(int_8), OPTIONAL :: max_val_memory
314 :
315 : INTEGER :: i, m
316 :
317 570667 : i = 0
318 1243292 : DO WHILE (i < nwords)
319 672625 : m = MIN(nwords - i, CACHE_SIZE - container%element_counter + 1)
320 204059049 : container%current%data(container%element_counter:container%element_counter + m - 1) = words(i + 1:i + m)
321 672625 : container%element_counter = container%element_counter + m
322 672625 : i = i + m
323 1243292 : IF (container%element_counter > CACHE_SIZE) THEN
324 102617 : IF (use_disk_storage) THEN
325 3554 : WRITE (container%unit) container%current%data
326 3554 : !$OMP ATOMIC
327 : memory_usage = memory_usage + 1
328 3554 : container%file_counter = container%file_counter + 1
329 : ELSE
330 101638638 : ALLOCATE (container%current%next)
331 99063 : !$OMP ATOMIC
332 : memory_usage = memory_usage + 1
333 99063 : container%current%next%next => NULL()
334 99063 : container%current => container%current%next
335 99063 : IF (PRESENT(max_val_memory)) max_val_memory = max_val_memory + 1
336 : END IF
337 102617 : container%element_counter = 1
338 : END IF
339 : END DO
340 570667 : END SUBROUTINE hfx_container_put
341 :
342 : ! **************************************************************************************************
343 : !> \brief Reads words from a container, mirroring hfx_container_put.
344 : !> \param container linked list, that stores the compressed values
345 : !> \param words words to be read
346 : !> \param nwords number of words
347 : !> \param memory_usage ...
348 : !> \param use_disk_storage ...
349 : ! **************************************************************************************************
350 10309992 : SUBROUTINE hfx_container_get(container, words, nwords, memory_usage, use_disk_storage)
351 : TYPE(hfx_container_type) :: container
352 : INTEGER(int_8), DIMENSION(*), INTENT(OUT) :: words
353 : INTEGER, INTENT(IN) :: nwords
354 : INTEGER :: memory_usage
355 : LOGICAL :: use_disk_storage
356 :
357 : INTEGER :: i, m, stat
358 :
359 10309992 : i = 0
360 21356119 : DO WHILE (i < nwords)
361 11046127 : m = MIN(nwords - i, CACHE_SIZE - container%element_counter + 1)
362 2156745319 : words(i + 1:i + m) = container%current%data(container%element_counter:container%element_counter + m - 1)
363 11046127 : container%element_counter = container%element_counter + m
364 11046127 : i = i + m
365 21356119 : IF (container%element_counter > CACHE_SIZE) THEN
366 741453 : IF (use_disk_storage) THEN
367 : ! a full last entry is followed by none: the end of the file is fine
368 28344 : READ (container%unit, IOSTAT=stat) container%current%data
369 28344 : IF (stat /= 0 .AND. stat /= iostat_end) CPABORT("Failed to read HFX container")
370 28344 : memory_usage = memory_usage + 1
371 28344 : container%file_counter = container%file_counter + 1
372 : ELSE
373 713109 : container%current => container%current%next
374 713109 : memory_usage = memory_usage + 1
375 : END IF
376 741453 : container%element_counter = 1
377 : END IF
378 : END DO
379 10309992 : END SUBROUTINE hfx_container_get
380 :
381 : ! **************************************************************************************************
382 : !> \brief - This routine resets the containers list pointer to the first element and
383 : !> moves the element counters of container and cache to the beginning
384 : !> \param cache cache from which we get the value
385 : !> \param container container that contains the compressed elements
386 : !> \param memory_usage ...
387 : !> \param do_disk_storage ...
388 : !> \par History
389 : !> 10.2007 created [Manuel Guidon]
390 : !> \author Manuel Guidon
391 : ! **************************************************************************************************
392 2852255 : SUBROUTINE hfx_reset_cache_and_container(cache, container, memory_usage, do_disk_storage)
393 : TYPE(hfx_cache_type) :: cache
394 : TYPE(hfx_container_type) :: container
395 : INTEGER :: memory_usage
396 : LOGICAL :: do_disk_storage
397 :
398 2852255 : cache%element_counter = 1
399 2852255 : container%current => container%first
400 2852255 : container%element_counter = 1
401 2852255 : memory_usage = 1
402 2852255 : container%file_counter = 1
403 2852255 : IF (do_disk_storage) THEN
404 3510 : CALL close_file(container%unit)
405 : CALL open_file(file_name=container%filename, file_status="OLD", file_form="UNFORMATTED", file_action="READ", &
406 3510 : unit_number=container%unit)
407 3510 : READ (container%unit) container%current%data
408 : END IF
409 2852255 : END SUBROUTINE hfx_reset_cache_and_container
410 :
411 : ! **************************************************************************************************
412 : !> \brief - This routine decompresses the first bunch of data in a container and
413 : !> copies them into a cache
414 : !> \param nbits number of bits with which the data has been stored
415 : !> \param cache array where we want to decompress the data
416 : !> \param container container that contains the compressed elements
417 : !> \param memory_usage ...
418 : !> \param use_disk_storage ...
419 : !> \par History
420 : !> 10.2007 created [Manuel Guidon]
421 : !> \author Manuel Guidon
422 : ! **************************************************************************************************
423 2680271 : SUBROUTINE hfx_decompress_first_cache(nbits, cache, container, memory_usage, use_disk_storage)
424 : INTEGER :: nbits
425 : TYPE(hfx_cache_type) :: cache
426 : TYPE(hfx_container_type) :: container
427 : INTEGER :: memory_usage
428 : LOGICAL :: use_disk_storage
429 :
430 2680271 : CALL hfx_decompress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
431 2680271 : cache%element_counter = 1
432 2680271 : END SUBROUTINE hfx_decompress_first_cache
433 :
434 : ! **************************************************************************************************
435 : !> \brief - This routine compresses the last probably not yet compressed cache into
436 : !> a container
437 : !> \param nbits number of bits with which the data has been stored
438 : !> \param cache array where we want to decompress the data
439 : !> \param container container that contains the compressed elements
440 : !> \param memory_usage ...
441 : !> \param use_disk_storage ...
442 : !> \par History
443 : !> 10.2007 created [Manuel Guidon]
444 : !> \author Manuel Guidon
445 : ! **************************************************************************************************
446 171984 : SUBROUTINE hfx_flush_last_cache(nbits, cache, container, memory_usage, use_disk_storage)
447 : INTEGER :: nbits
448 : TYPE(hfx_cache_type) :: cache
449 : TYPE(hfx_container_type) :: container
450 : INTEGER :: memory_usage
451 : LOGICAL :: use_disk_storage
452 :
453 171984 : CALL hfx_compress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
454 :
455 : !!If we store to file, we have to make sure, that the last container is also written to disk
456 171984 : IF (use_disk_storage) THEN
457 390 : IF (container%element_counter /= 1) THEN
458 390 : WRITE (container%unit) container%current%data
459 390 : memory_usage = memory_usage + 1
460 390 : container%file_counter = container%file_counter + 1
461 : END IF
462 : END IF
463 171984 : END SUBROUTINE hfx_flush_last_cache
464 :
465 : ! **************************************************************************************************
466 : !> \brief - This routine adds an a few real values to a cache. If the cache is full
467 : !> a compression routine is invoked and the cache is cleared
468 : !> \param values values to be added to the cache
469 : !> \param nints ...
470 : !> \param nbits number of bits to be stored
471 : !> \param cache cache to which we want to add
472 : !> \param container container that contains the compressed elements
473 : !> \param eps_schwarz ...
474 : !> \param pmax_entry ...
475 : !> \param memory_usage ...
476 : !> \param use_disk_storage ...
477 : !> \par History
478 : !> 10.2007 created [Manuel Guidon]
479 : !> \author Manuel Guidon
480 : ! **************************************************************************************************
481 4522461 : SUBROUTINE hfx_add_mult_cache_elements(values, nints, nbits, cache, container, eps_schwarz, pmax_entry, memory_usage, &
482 : use_disk_storage)
483 : REAL(dp) :: values(*)
484 : INTEGER, INTENT(IN) :: nints, nbits
485 : TYPE(hfx_cache_type) :: cache
486 : TYPE(hfx_container_type) :: container
487 : REAL(dp), INTENT(IN) :: eps_schwarz, pmax_entry
488 : INTEGER :: memory_usage
489 : LOGICAL :: use_disk_storage
490 :
491 : INTEGER :: end_idx, i, start_idx, tmp_elements
492 : INTEGER(int_8) :: shift, tmp
493 : REAL(dp) :: eps_schwarz_inv, factor
494 :
495 4522461 : eps_schwarz_inv = 1.0_dp/eps_schwarz
496 4522461 : factor = eps_schwarz/pmax_entry
497 :
498 4522461 : shift = shifts(nbits - 1)
499 :
500 4522461 : start_idx = cache%element_counter
501 4522461 : end_idx = start_idx + nints - 1
502 4522461 : IF (end_idx < CACHE_SIZE) THEN
503 117055822 : DO i = 1, nints
504 112681807 : values(i) = values(i)*pmax_entry
505 117055822 : IF (ABS(values(i)) > eps_schwarz) THEN
506 56560074 : tmp = NINT(values(i)*eps_schwarz_inv, KIND=int_8)
507 56560074 : cache%data(i + start_idx - 1) = tmp + shift
508 56560074 : values(i) = tmp*factor
509 : ELSE
510 56121733 : values(i) = 0.0_dp
511 56121733 : cache%data(i + start_idx - 1) = shift
512 : END IF
513 : END DO
514 4374015 : cache%element_counter = end_idx + 1
515 : ELSE
516 148446 : tmp_elements = CACHE_SIZE - start_idx + 1
517 29865223 : DO i = 1, tmp_elements
518 29716777 : values(i) = values(i)*pmax_entry
519 29865223 : IF (ABS(values(i)) > eps_schwarz) THEN
520 12116362 : tmp = NINT(values(i)*eps_schwarz_inv, KIND=int_8)
521 12116362 : cache%data(i + start_idx - 1) = tmp + shift
522 12116362 : values(i) = tmp*factor
523 : ELSE
524 17600415 : values(i) = 0.0_dp
525 17600415 : cache%data(i + start_idx - 1) = shift
526 : END IF
527 : END DO
528 148446 : CALL hfx_compress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
529 20047018 : DO i = tmp_elements + 1, nints
530 19898572 : values(i) = values(i)*pmax_entry
531 20047018 : IF (ABS(values(i)) > eps_schwarz) THEN
532 8903410 : tmp = NINT(values(i)*eps_schwarz_inv, KIND=int_8)
533 8903410 : cache%data(i - tmp_elements) = tmp + shift
534 8903410 : values(i) = tmp*factor
535 : ELSE
536 10995162 : values(i) = 0.0_dp
537 10995162 : cache%data(i - tmp_elements) = shift
538 : END IF
539 : END DO
540 148446 : cache%element_counter = nints - tmp_elements + 1
541 : END IF
542 4522461 : END SUBROUTINE hfx_add_mult_cache_elements
543 :
544 : ! **************************************************************************************************
545 : !> \brief - This routine returns a bunch real values from a cache. If the cache is empty
546 : !> a decompression routine is invoked and the cache is refilled with decompressed
547 : !> values from a container
548 : !> \param values value to be retained from the cache
549 : !> \param nints number of values to be retained
550 : !> \param nbits number of bits with which the value has been compressed
551 : !> \param cache cache from which we get the value
552 : !> \param container container that contains the compressed elements
553 : !> \param eps_schwarz threshold for storage
554 : !> \param pmax_entry multiplication factor for values
555 : !> \param memory_usage ...
556 : !> \param use_disk_storage ...
557 : !> \par History
558 : !> 10.2007 created [Manuel Guidon]
559 : !> \author Manuel Guidon
560 : ! **************************************************************************************************
561 70962848 : SUBROUTINE hfx_get_mult_cache_elements(values, nints, nbits, cache, container, eps_schwarz, pmax_entry, memory_usage, &
562 : use_disk_storage)
563 : REAL(dp) :: values(*)
564 : INTEGER, INTENT(IN) :: nints, nbits
565 : TYPE(hfx_cache_type) :: cache
566 : TYPE(hfx_container_type) :: container
567 : REAL(dp), INTENT(IN) :: eps_schwarz, pmax_entry
568 : INTEGER :: memory_usage
569 : LOGICAL :: use_disk_storage
570 :
571 : INTEGER :: end_idx, i, start_idx, tmp_elements
572 : INTEGER(int_8) :: shift
573 : REAL(dp) :: factor
574 :
575 70962848 : factor = eps_schwarz/pmax_entry
576 :
577 70962848 : shift = shifts(nbits - 1)
578 :
579 70962848 : start_idx = cache%element_counter
580 70962848 : end_idx = start_idx + nints - 1
581 :
582 70962848 : IF (end_idx < CACHE_SIZE) THEN
583 1391750208 : DO i = 1, nints
584 1391750208 : values(i) = factor*REAL(cache%data(i + start_idx - 1) - shift, dp)
585 : END DO
586 69203702 : cache%element_counter = end_idx + 1
587 : ELSE
588 1759146 : tmp_elements = CACHE_SIZE - start_idx + 1
589 396560757 : DO i = 1, tmp_elements
590 396560757 : values(i) = factor*REAL(cache%data(i + start_idx - 1) - shift, dp)
591 : END DO
592 1759146 : CALL hfx_decompress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
593 259909608 : DO i = tmp_elements + 1, nints
594 259909608 : values(i) = factor*REAL(cache%data(i - tmp_elements) - shift, dp)
595 : END DO
596 1759146 : cache%element_counter = nints - tmp_elements + 1
597 : END IF
598 70962848 : END SUBROUTINE hfx_get_mult_cache_elements
599 :
600 : ! **************************************************************************************************
601 : !> \brief Packs a full cache of real values into a container: a record is its length in words
602 : !> followed by the values quantized within eps: LIBXS keeps every value within half of eps
603 : !> (a power of two as step, not above eps), else values up to eps become zero and the others
604 : !> multiples of eps (within half of eps), packed at a width per group.
605 : !> \param cache cache whose values are packed (all of them)
606 : !> \param container linked list, that stores the compressed values
607 : !> \param eps absolute error of a value
608 : !> \param memory_usage ...
609 : !> \param use_disk_storage ...
610 : ! **************************************************************************************************
611 246744 : SUBROUTINE hfx_compress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
612 : TYPE(hfx_real_cache_type) :: cache
613 : TYPE(hfx_container_type) :: container
614 : REAL(dp), INTENT(IN) :: eps
615 : INTEGER :: memory_usage
616 : LOGICAL :: use_disk_storage
617 :
618 : INTEGER :: nwords
619 : INTEGER(int_8), DIMENSION(CACHE_SIZE + 64) :: record
620 : #if !defined(__LIBXS)
621 : INTEGER :: i, nbits
622 : INTEGER(int_8) :: amax
623 : INTEGER(int_8), DIMENSION(CACHE_SIZE) :: values
624 : REAL(dp) :: eps_inv
625 : #endif
626 :
627 : #if defined(__LIBXS)
628 : ! at most CACHE_SIZE+51 words (64 bits each and a header); half of eps: a step not coarser than eps
629 246744 : nwords = INT(libxs_vpack(record(2:), cache%data, libxs_vpack_maxbits(LIBXS_DATATYPE_F64, 0.5_dp*eps, 0)))
630 : #else
631 : eps_inv = 1.0_dp/eps
632 : amax = 0
633 : DO i = 1, CACHE_SIZE
634 : IF (ABS(cache%data(i)) > eps) THEN
635 : IF (ABS(cache%data(i))*eps_inv >= 4611686018427387904.0_dp) THEN ! 2^62
636 : CALL cp_abort(__LOCATION__, &
637 : "Overflow during tensor compression. Please use a larger EPS_FILTER or EPS_STORAGE_SCALING")
638 : END IF
639 : values(i) = NINT(cache%data(i)*eps_inv, KIND=int_8)
640 : ELSE
641 : values(i) = 0
642 : END IF
643 : amax = MAX(amax, ABS(values(i)))
644 : END DO
645 : nbits = INT(BIT_SIZE(amax)) - LEADZ(amax) + 1 ! a sign bit more
646 : nwords = ints2groups(nbits, group_size, record(2:), values)
647 : #endif
648 246744 : CPASSERT(0 < nwords .AND. nwords < SIZE(record))
649 246744 : record(1) = nwords
650 246744 : CALL hfx_container_put(container, record, nwords + 1, memory_usage, use_disk_storage)
651 246744 : END SUBROUTINE hfx_compress_real_cache
652 :
653 : ! **************************************************************************************************
654 : !> \brief Unpacks the next record of a container into a cache of real values.
655 : !> \param cache cache to be filled (all of it)
656 : !> \param container linked list, that stores the compressed values
657 : !> \param eps absolute error of a value (as when packed)
658 : !> \param memory_usage ...
659 : !> \param use_disk_storage ...
660 : ! **************************************************************************************************
661 665259 : SUBROUTINE hfx_decompress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
662 : TYPE(hfx_real_cache_type) :: cache
663 : TYPE(hfx_container_type) :: container
664 : REAL(dp), INTENT(IN) :: eps
665 : INTEGER :: memory_usage
666 : LOGICAL :: use_disk_storage
667 :
668 : INTEGER :: nwords
669 : INTEGER(int_8) :: nread
670 : INTEGER(int_8), DIMENSION(CACHE_SIZE + 64) :: record
671 : #if !defined(__LIBXS)
672 : INTEGER(int_8), DIMENSION(CACHE_SIZE) :: values
673 : #endif
674 :
675 665259 : CPASSERT(ASSOCIATED(container%current))
676 665259 : CALL hfx_container_get(container, record, 1, memory_usage, use_disk_storage)
677 665259 : nwords = INT(record(1))
678 : ! a zero word is no record (nothing stored), which decodes as zeros
679 665259 : IF (0 < nwords) THEN
680 665259 : CPASSERT(nwords < SIZE(record))
681 665259 : CALL hfx_container_get(container, record(2:), nwords, memory_usage, use_disk_storage)
682 : #if defined(__LIBXS)
683 : MARK_USED(eps) ! within the record
684 665259 : nread = INT(libxs_vunpack(cache%data, record(2:nwords + 1)), KIND=int_8)
685 : #else
686 : nread = INT(groups2ints(record(2:nwords + 1), values), KIND=int_8)
687 : cache%data(:) = eps*REAL(values, dp)
688 : #endif
689 665259 : CPASSERT(nread == nwords)
690 : ELSE
691 0 : cache%data(:) = 0.0_dp
692 : END IF
693 665259 : END SUBROUTINE hfx_decompress_real_cache
694 :
695 : ! **************************************************************************************************
696 : !> \brief Adds real values to a cache (at most a cache full); a full cache is packed into the
697 : !> container, then the cache takes the remaining values.
698 : !> \param values values to be added
699 : !> \param nints number of values (at most CACHE_SIZE)
700 : !> \param cache cache to which the values are added
701 : !> \param container container that contains the compressed values
702 : !> \param eps absolute error of a value
703 : !> \param memory_usage ...
704 : !> \param use_disk_storage ...
705 : ! **************************************************************************************************
706 757237 : SUBROUTINE hfx_add_mult_real_elements(values, nints, cache, container, eps, memory_usage, use_disk_storage)
707 : REAL(dp) :: values(*)
708 : INTEGER, INTENT(IN) :: nints
709 : TYPE(hfx_real_cache_type) :: cache
710 : TYPE(hfx_container_type) :: container
711 : REAL(dp), INTENT(IN) :: eps
712 : INTEGER :: memory_usage
713 : LOGICAL :: use_disk_storage
714 :
715 : INTEGER :: end_idx, start_idx, tmp_elements
716 :
717 757237 : CPASSERT(nints <= CACHE_SIZE)
718 757237 : start_idx = cache%element_counter
719 757237 : end_idx = start_idx + nints - 1
720 757237 : IF (end_idx < CACHE_SIZE) THEN
721 96731115 : cache%data(start_idx:end_idx) = values(1:nints)
722 541363 : cache%element_counter = end_idx + 1
723 : ELSE
724 215874 : tmp_elements = CACHE_SIZE - start_idx + 1
725 69842418 : cache%data(start_idx:CACHE_SIZE) = values(1:tmp_elements)
726 215874 : CALL hfx_compress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
727 65291593 : cache%data(1:nints - tmp_elements) = values(tmp_elements + 1:nints)
728 215874 : cache%element_counter = nints - tmp_elements + 1
729 : END IF
730 757237 : END SUBROUTINE hfx_add_mult_real_elements
731 :
732 : ! **************************************************************************************************
733 : !> \brief Gets real values from a cache (at most a cache full); an exhausted cache is refilled
734 : !> from the next record of the container.
735 : !> \param values values to be retained from the cache
736 : !> \param nints number of values (at most CACHE_SIZE)
737 : !> \param cache cache from which the values are taken
738 : !> \param container container that contains the compressed values
739 : !> \param eps absolute error of a value (as when packed)
740 : !> \param memory_usage ...
741 : !> \param use_disk_storage ...
742 : ! **************************************************************************************************
743 2128411 : SUBROUTINE hfx_get_mult_real_elements(values, nints, cache, container, eps, memory_usage, use_disk_storage)
744 : REAL(dp) :: values(*)
745 : INTEGER, INTENT(IN) :: nints
746 : TYPE(hfx_real_cache_type) :: cache
747 : TYPE(hfx_container_type) :: container
748 : REAL(dp), INTENT(IN) :: eps
749 : INTEGER :: memory_usage
750 : LOGICAL :: use_disk_storage
751 :
752 : INTEGER :: end_idx, start_idx, tmp_elements
753 :
754 2128411 : CPASSERT(nints <= CACHE_SIZE)
755 2128411 : start_idx = cache%element_counter
756 2128411 : end_idx = start_idx + nints - 1
757 2128411 : IF (end_idx < CACHE_SIZE) THEN
758 307053700 : values(1:nints) = cache%data(start_idx:end_idx)
759 1544932 : cache%element_counter = end_idx + 1
760 : ELSE
761 583479 : tmp_elements = CACHE_SIZE - start_idx + 1
762 164726796 : values(1:tmp_elements) = cache%data(start_idx:CACHE_SIZE)
763 583479 : CALL hfx_decompress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
764 156822182 : values(tmp_elements + 1:nints) = cache%data(1:nints - tmp_elements)
765 583479 : cache%element_counter = nints - tmp_elements + 1
766 : END IF
767 2128411 : END SUBROUTINE hfx_get_mult_real_elements
768 :
769 : ! **************************************************************************************************
770 : !> \brief Packs the last cache of real values (unused values as zeros) into the container.
771 : !> \param cache cache to be packed
772 : !> \param container container that contains the compressed values
773 : !> \param eps absolute error of a value
774 : !> \param memory_usage ...
775 : !> \param use_disk_storage ...
776 : ! **************************************************************************************************
777 30870 : SUBROUTINE hfx_flush_last_real_cache(cache, container, eps, memory_usage, use_disk_storage)
778 : TYPE(hfx_real_cache_type) :: cache
779 : TYPE(hfx_container_type) :: container
780 : REAL(dp), INTENT(IN) :: eps
781 : INTEGER :: memory_usage
782 : LOGICAL :: use_disk_storage
783 :
784 21804711 : cache%data(cache%element_counter:CACHE_SIZE) = 0.0_dp
785 30870 : CALL hfx_compress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
786 30870 : IF (use_disk_storage) THEN
787 0 : IF (container%element_counter /= 1) THEN
788 0 : WRITE (container%unit) container%current%data
789 0 : memory_usage = memory_usage + 1
790 0 : container%file_counter = container%file_counter + 1
791 : END IF
792 : END IF
793 30870 : END SUBROUTINE hfx_flush_last_real_cache
794 :
795 : ! **************************************************************************************************
796 : !> \brief Fills a cache of real values with the first record of a container.
797 : !> \param cache cache to be filled
798 : !> \param container container that contains the compressed values
799 : !> \param eps absolute error of a value (as when packed)
800 : !> \param memory_usage ...
801 : !> \param use_disk_storage ...
802 : ! **************************************************************************************************
803 81780 : SUBROUTINE hfx_decompress_first_real_cache(cache, container, eps, memory_usage, use_disk_storage)
804 : TYPE(hfx_real_cache_type) :: cache
805 : TYPE(hfx_container_type) :: container
806 : REAL(dp), INTENT(IN) :: eps
807 : INTEGER :: memory_usage
808 : LOGICAL :: use_disk_storage
809 :
810 81780 : CALL hfx_decompress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
811 81780 : cache%element_counter = 1
812 81780 : END SUBROUTINE hfx_decompress_first_real_cache
813 :
814 : ! **************************************************************************************************
815 : !> \brief Moves a cache of real values and its container back to the beginning (as
816 : !> hfx_reset_cache_and_container).
817 : !> \param cache cache of real values
818 : !> \param container container that contains the compressed values
819 : !> \param memory_usage ...
820 : !> \param do_disk_storage ...
821 : ! **************************************************************************************************
822 112650 : SUBROUTINE hfx_reset_real_cache_and_container(cache, container, memory_usage, do_disk_storage)
823 : TYPE(hfx_real_cache_type) :: cache
824 : TYPE(hfx_container_type) :: container
825 : INTEGER :: memory_usage
826 : LOGICAL :: do_disk_storage
827 :
828 112650 : cache%element_counter = 1
829 112650 : container%current => container%first
830 112650 : container%element_counter = 1
831 112650 : memory_usage = 1
832 112650 : container%file_counter = 1
833 112650 : IF (do_disk_storage) THEN
834 0 : CALL close_file(container%unit)
835 : CALL open_file(file_name=container%filename, file_status="OLD", file_form="UNFORMATTED", file_action="READ", &
836 0 : unit_number=container%unit)
837 0 : READ (container%unit) container%current%data
838 : END IF
839 112650 : END SUBROUTINE hfx_reset_real_cache_and_container
840 :
841 : END MODULE hfx_compression_methods
842 :
|