(git:27c966b)
Loading...
Searching...
No Matches
hfx_compression_methods.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
8! **************************************************************************************************
9!> \brief routines and types for Hartree-Fock-Exchange
10!> \par History
11!> 11.2006 created [Manuel Guidon]
12!> \author Manuel Guidon
13! **************************************************************************************************
15 USE cp_files, ONLY: close_file, &
17 USE hfx_types, ONLY: hfx_cache_type, &
20 USE kinds, ONLY: dp, &
21 int_8
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
38 ! real values quantized and packed together (records of a cache), within an absolute error
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
69CONTAINS
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 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 int_val = value + shifts(nbits - 1)
98
99 IF (cache%element_counter /= cache_size) THEN
100 cache%data(cache%element_counter) = int_val
101 cache%element_counter = cache%element_counter + 1
102 ELSE
103 cache%data(cache_size) = int_val
104 CALL hfx_compress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage, &
105 max_val_memory)
106 cache%element_counter = 1
107 END IF
108 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 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 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 nwords = int(libxs_vpack(record(2:), values, nbits))
143#else
144 nwords = ints2groups(nbits, group_size, record(2:), values)
145#endif
146 record(1) = nwords
147 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 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 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 IF (cache%element_counter /= cache_size) THEN
206 value = cache%data(cache%element_counter)
207 cache%element_counter = cache%element_counter + 1
208 ELSE
209 value = cache%data(cache_size)
210 CALL hfx_decompress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
211 cache%element_counter = 1
212 END IF
213
214 value = value - shifts(nbits - 1)
215
216 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 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 cpassert(ASSOCIATED(container%current))
245
246 IF (group_size > 0) THEN
247 CALL hfx_container_get(container, record, 1, memory_usage, use_disk_storage)
248 nwords = int(record(1))
249 ! a zero word is no record (nothing stored), which decodes as zeros like the fixed-width format
250 IF (0 < nwords) THEN
251 cpassert(nwords < SIZE(record))
252 CALL hfx_container_get(container, record(2:), nwords, memory_usage, use_disk_storage)
253#if defined(__LIBXS)
254 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 cpassert(nread == nwords)
259 full_array(1:cache_size) = values + shifts(nbits - 1)
260 ELSE
261 cpassert(0 == nwords)
262 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 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 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 i = 0
318 DO WHILE (i < nwords)
319 m = min(nwords - i, cache_size - container%element_counter + 1)
320 container%current%data(container%element_counter:container%element_counter + m - 1) = words(i + 1:i + m)
321 container%element_counter = container%element_counter + m
322 i = i + m
323 IF (container%element_counter > cache_size) THEN
324 IF (use_disk_storage) THEN
325 WRITE (container%unit) container%current%data
326!$OMP ATOMIC
327 memory_usage = memory_usage + 1
328 container%file_counter = container%file_counter + 1
329 ELSE
330 ALLOCATE (container%current%next)
331!$OMP ATOMIC
332 memory_usage = memory_usage + 1
333 container%current%next%next => null()
334 container%current => container%current%next
335 IF (PRESENT(max_val_memory)) max_val_memory = max_val_memory + 1
336 END IF
337 container%element_counter = 1
338 END IF
339 END DO
340 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 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 i = 0
360 DO WHILE (i < nwords)
361 m = min(nwords - i, cache_size - container%element_counter + 1)
362 words(i + 1:i + m) = container%current%data(container%element_counter:container%element_counter + m - 1)
363 container%element_counter = container%element_counter + m
364 i = i + m
365 IF (container%element_counter > cache_size) THEN
366 IF (use_disk_storage) THEN
367 ! a full last entry is followed by none: the end of the file is fine
368 READ (container%unit, iostat=stat) container%current%data
369 IF (stat /= 0 .AND. stat /= iostat_end) cpabort("Failed to read HFX container")
370 memory_usage = memory_usage + 1
371 container%file_counter = container%file_counter + 1
372 ELSE
373 container%current => container%current%next
374 memory_usage = memory_usage + 1
375 END IF
376 container%element_counter = 1
377 END IF
378 END DO
379 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 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 cache%element_counter = 1
399 container%current => container%first
400 container%element_counter = 1
401 memory_usage = 1
402 container%file_counter = 1
403 IF (do_disk_storage) THEN
404 CALL close_file(container%unit)
405 CALL open_file(file_name=container%filename, file_status="OLD", file_form="UNFORMATTED", file_action="READ", &
406 unit_number=container%unit)
407 READ (container%unit) container%current%data
408 END IF
409 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 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 CALL hfx_decompress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
431 cache%element_counter = 1
432 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 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 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 IF (use_disk_storage) THEN
457 IF (container%element_counter /= 1) THEN
458 WRITE (container%unit) container%current%data
459 memory_usage = memory_usage + 1
460 container%file_counter = container%file_counter + 1
461 END IF
462 END IF
463 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 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 eps_schwarz_inv = 1.0_dp/eps_schwarz
496 factor = eps_schwarz/pmax_entry
497
498 shift = shifts(nbits - 1)
499
500 start_idx = cache%element_counter
501 end_idx = start_idx + nints - 1
502 IF (end_idx < cache_size) THEN
503 DO i = 1, nints
504 values(i) = values(i)*pmax_entry
505 IF (abs(values(i)) > eps_schwarz) THEN
506 tmp = nint(values(i)*eps_schwarz_inv, kind=int_8)
507 cache%data(i + start_idx - 1) = tmp + shift
508 values(i) = tmp*factor
509 ELSE
510 values(i) = 0.0_dp
511 cache%data(i + start_idx - 1) = shift
512 END IF
513 END DO
514 cache%element_counter = end_idx + 1
515 ELSE
516 tmp_elements = cache_size - start_idx + 1
517 DO i = 1, tmp_elements
518 values(i) = values(i)*pmax_entry
519 IF (abs(values(i)) > eps_schwarz) THEN
520 tmp = nint(values(i)*eps_schwarz_inv, kind=int_8)
521 cache%data(i + start_idx - 1) = tmp + shift
522 values(i) = tmp*factor
523 ELSE
524 values(i) = 0.0_dp
525 cache%data(i + start_idx - 1) = shift
526 END IF
527 END DO
528 CALL hfx_compress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
529 DO i = tmp_elements + 1, nints
530 values(i) = values(i)*pmax_entry
531 IF (abs(values(i)) > eps_schwarz) THEN
532 tmp = nint(values(i)*eps_schwarz_inv, kind=int_8)
533 cache%data(i - tmp_elements) = tmp + shift
534 values(i) = tmp*factor
535 ELSE
536 values(i) = 0.0_dp
537 cache%data(i - tmp_elements) = shift
538 END IF
539 END DO
540 cache%element_counter = nints - tmp_elements + 1
541 END IF
542 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 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 factor = eps_schwarz/pmax_entry
576
577 shift = shifts(nbits - 1)
578
579 start_idx = cache%element_counter
580 end_idx = start_idx + nints - 1
581
582 IF (end_idx < cache_size) THEN
583 DO i = 1, nints
584 values(i) = factor*real(cache%data(i + start_idx - 1) - shift, dp)
585 END DO
586 cache%element_counter = end_idx + 1
587 ELSE
588 tmp_elements = cache_size - start_idx + 1
589 DO i = 1, tmp_elements
590 values(i) = factor*real(cache%data(i + start_idx - 1) - shift, dp)
591 END DO
592 CALL hfx_decompress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
593 DO i = tmp_elements + 1, nints
594 values(i) = factor*real(cache%data(i - tmp_elements) - shift, dp)
595 END DO
596 cache%element_counter = nints - tmp_elements + 1
597 END IF
598 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 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 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 cpassert(0 < nwords .AND. nwords < SIZE(record))
649 record(1) = nwords
650 CALL hfx_container_put(container, record, nwords + 1, memory_usage, use_disk_storage)
651 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 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 cpassert(ASSOCIATED(container%current))
676 CALL hfx_container_get(container, record, 1, memory_usage, use_disk_storage)
677 nwords = int(record(1))
678 ! a zero word is no record (nothing stored), which decodes as zeros
679 IF (0 < nwords) THEN
680 cpassert(nwords < SIZE(record))
681 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 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 cpassert(nread == nwords)
690 ELSE
691 cache%data(:) = 0.0_dp
692 END IF
693 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 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 cpassert(nints <= cache_size)
718 start_idx = cache%element_counter
719 end_idx = start_idx + nints - 1
720 IF (end_idx < cache_size) THEN
721 cache%data(start_idx:end_idx) = values(1:nints)
722 cache%element_counter = end_idx + 1
723 ELSE
724 tmp_elements = cache_size - start_idx + 1
725 cache%data(start_idx:cache_size) = values(1:tmp_elements)
726 CALL hfx_compress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
727 cache%data(1:nints - tmp_elements) = values(tmp_elements + 1:nints)
728 cache%element_counter = nints - tmp_elements + 1
729 END IF
730 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 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 cpassert(nints <= cache_size)
755 start_idx = cache%element_counter
756 end_idx = start_idx + nints - 1
757 IF (end_idx < cache_size) THEN
758 values(1:nints) = cache%data(start_idx:end_idx)
759 cache%element_counter = end_idx + 1
760 ELSE
761 tmp_elements = cache_size - start_idx + 1
762 values(1:tmp_elements) = cache%data(start_idx:cache_size)
763 CALL hfx_decompress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
764 values(tmp_elements + 1:nints) = cache%data(1:nints - tmp_elements)
765 cache%element_counter = nints - tmp_elements + 1
766 END IF
767 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 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 cache%data(cache%element_counter:cache_size) = 0.0_dp
785 CALL hfx_compress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
786 IF (use_disk_storage) THEN
787 IF (container%element_counter /= 1) THEN
788 WRITE (container%unit) container%current%data
789 memory_usage = memory_usage + 1
790 container%file_counter = container%file_counter + 1
791 END IF
792 END IF
793 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 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 CALL hfx_decompress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
811 cache%element_counter = 1
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 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 cache%element_counter = 1
829 container%current => container%first
830 container%element_counter = 1
831 memory_usage = 1
832 container%file_counter = 1
833 IF (do_disk_storage) THEN
834 CALL close_file(container%unit)
835 CALL open_file(file_name=container%filename, file_status="OLD", file_form="UNFORMATTED", file_action="READ", &
836 unit_number=container%unit)
837 READ (container%unit) container%current%data
838 END IF
840
842
Utility routines to open and close files.
Definition cp_files.F:22
subroutine, public open_file(file_name, unit_number, file_status, file_form, file_action, file_position, file_pad, file_access, debug)
Opens a file and returns its logical unit number.
Definition cp_files.F:197
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
Definition cp_files.F:64
Contains routines for data compression: packing integers of Nbits bits each into a stream of 64-bit w...
subroutine, public bits2ints_specific(nbits, ndata, packed_data, full_data)
Unpacks integers from a stream of 64-bit words, Nbits bits per integer.
integer function, public ints2groups(nbits, ngroup, packed_data, full_data)
Packs integers at a width per group, the width the group's largest magnitude needs: header bytes (the...
integer function, public groups2ints(packed_data, full_data)
Unpacks integers that ints2groups packed (signed, the sign unfolded); the group size is read from the...
subroutine, public ints2bits_specific(nbits, ndata, packed_data, full_data)
Packs integers into a stream of 64-bit words, Nbits bits per integer.
routines and types for Hartree-Fock-Exchange
subroutine, public hfx_flush_last_real_cache(cache, container, eps, memory_usage, use_disk_storage)
Packs the last cache of real values (unused values as zeros) into the container.
subroutine, public hfx_get_mult_real_elements(values, nints, cache, container, eps, memory_usage, use_disk_storage)
Gets real values from a cache (at most a cache full); an exhausted cache is refilled from the next re...
subroutine, public hfx_add_single_cache_element(value, nbits, cache, container, memory_usage, use_disk_storage, max_val_memory)
This routine adds an int_8 value to a cache. If the cache is full a compression routine is invoked an...
subroutine, public hfx_get_mult_cache_elements(values, nints, nbits, cache, container, eps_schwarz, pmax_entry, memory_usage, use_disk_storage)
This routine returns a bunch real values from a cache. If the cache is empty a decompression routine ...
subroutine, public hfx_flush_last_cache(nbits, cache, container, memory_usage, use_disk_storage)
This routine compresses the last probably not yet compressed cache into a container
subroutine, public hfx_get_single_cache_element(value, nbits, cache, container, memory_usage, use_disk_storage)
This routine returns an int_8 value from a cache. If the cache is empty a decompression routine is in...
subroutine, public hfx_decompress_first_cache(nbits, cache, container, memory_usage, use_disk_storage)
This routine decompresses the first bunch of data in a container and copies them into a cache
subroutine, public hfx_add_mult_real_elements(values, nints, cache, container, eps, memory_usage, use_disk_storage)
Adds real values to a cache (at most a cache full); a full cache is packed into the container,...
subroutine, public hfx_add_mult_cache_elements(values, nints, nbits, cache, container, eps_schwarz, pmax_entry, memory_usage, use_disk_storage)
This routine adds an a few real values to a cache. If the cache is full a compression routine is invo...
subroutine, public hfx_reset_cache_and_container(cache, container, memory_usage, do_disk_storage)
This routine resets the containers list pointer to the first element and moves the element counters o...
subroutine, public hfx_decompress_first_real_cache(cache, container, eps, memory_usage, use_disk_storage)
Fills a cache of real values with the first record of a container.
subroutine, public hfx_reset_real_cache_and_container(cache, container, memory_usage, do_disk_storage)
Moves a cache of real values and its container back to the beginning (as hfx_reset_cache_and_containe...
Types and set/get functions for HFX.
Definition hfx_types.F:16
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