26 USE iso_fortran_env,
ONLY: iostat_end
28 USE libxs,
ONLY: libxs_vpack, libxs_vunpack, libxs_vpack_maxbits, libxs_datatype_f64
30#include "./base/base_uses.f90"
43#define CACHE_SIZE 1024
45 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'hfx_compression_methods'
49 INTEGER,
PARAMETER,
PRIVATE :: group_size = 8
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]
87 INTEGER(int_8) :: value
91 INTEGER :: memory_usage
92 LOGICAL :: use_disk_storage
93 INTEGER(int_8),
OPTIONAL :: max_val_memory
95 INTEGER(int_8) :: int_val
97 int_val =
value + shifts(nbits - 1)
99 IF (cache%element_counter /= cache_size)
THEN
100 cache%data(cache%element_counter) = int_val
101 cache%element_counter = cache%element_counter + 1
103 cache%data(cache_size) = int_val
104 CALL hfx_compress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage, &
106 cache%element_counter = 1
123 SUBROUTINE hfx_compress_cache(full_array, container, nbits, memory_usage, use_disk_storage, &
125 INTEGER(int_8) :: full_array(*)
127 INTEGER,
INTENT(IN) :: nbits
128 INTEGER :: memory_usage
129 LOGICAL :: use_disk_storage
130 INTEGER(int_8),
OPTIONAL :: max_val_memory
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
137 IF (group_size > 0)
THEN
139 values = full_array(1:cache_size) - shifts(nbits - 1)
142 nwords = int(libxs_vpack(record(2:), values, nbits))
144 nwords =
ints2groups(nbits, group_size, record(2:), values)
147 CALL hfx_container_put(container, record, nwords + 1, memory_usage, use_disk_storage, max_val_memory)
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
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
162 WRITE (container%unit) container%current%data
164 memory_usage = memory_usage + 1
165 container%file_counter = container%file_counter + 1
168 ALLOCATE (container%current%next)
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
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
181 END SUBROUTINE hfx_compress_cache
198 INTEGER(int_8) :: value
202 INTEGER :: memory_usage
203 LOGICAL :: use_disk_storage
205 IF (cache%element_counter /= cache_size)
THEN
206 value = cache%data(cache%element_counter)
207 cache%element_counter = cache%element_counter + 1
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
214 value =
value - shifts(nbits - 1)
230 SUBROUTINE hfx_decompress_cache(full_array, container, nbits, memory_usage, use_disk_storage)
231 INTEGER(int_8) :: full_array(*)
233 INTEGER,
INTENT(IN) :: nbits
234 INTEGER :: memory_usage
235 LOGICAL :: use_disk_storage
237 INTEGER :: end_idx, increment_counter, nwords, &
238 start_idx, stat, tmp_elements, &
240 INTEGER(int_8) :: nread
241 INTEGER(int_8),
DIMENSION(CACHE_SIZE) :: values
242 INTEGER(int_8),
DIMENSION(CACHE_SIZE+64) :: record
244 cpassert(
ASSOCIATED(container%current))
246 IF (group_size > 0)
THEN
247 CALL hfx_container_get(container, record, 1, memory_usage, use_disk_storage)
248 nwords = int(record(1))
251 cpassert(nwords <
SIZE(record))
252 CALL hfx_container_get(container, record(2:), nwords, memory_usage, use_disk_storage)
254 nread = int(libxs_vunpack(values, record(2:nwords + 1)), kind=
int_8)
258 cpassert(nread == nwords)
259 full_array(1:cache_size) = values + shifts(nbits - 1)
261 cpassert(0 == nwords)
262 full_array(1:cache_size) = 0
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
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
282 READ (container%unit, iostat=stat) container%current%data
284 memory_usage = memory_usage + 1
285 container%file_counter = container%file_counter + 1
287 container%current => container%current%next
288 memory_usage = memory_usage + 1
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
295 END SUBROUTINE hfx_decompress_cache
307 SUBROUTINE hfx_container_put(container, words, nwords, memory_usage, use_disk_storage, max_val_memory)
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
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
323 IF (container%element_counter > cache_size)
THEN
324 IF (use_disk_storage)
THEN
325 WRITE (container%unit) container%current%data
327 memory_usage = memory_usage + 1
328 container%file_counter = container%file_counter + 1
330 ALLOCATE (container%current%next)
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
337 container%element_counter = 1
340 END SUBROUTINE hfx_container_put
350 SUBROUTINE hfx_container_get(container, words, nwords, memory_usage, use_disk_storage)
352 INTEGER(int_8),
DIMENSION(*),
INTENT(OUT) :: words
353 INTEGER,
INTENT(IN) :: nwords
354 INTEGER :: memory_usage
355 LOGICAL :: use_disk_storage
357 INTEGER :: i, m, stat
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
365 IF (container%element_counter > cache_size)
THEN
366 IF (use_disk_storage)
THEN
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
373 container%current => container%current%next
374 memory_usage = memory_usage + 1
376 container%element_counter = 1
379 END SUBROUTINE hfx_container_get
395 INTEGER :: memory_usage
396 LOGICAL :: do_disk_storage
398 cache%element_counter = 1
399 container%current => container%first
400 container%element_counter = 1
402 container%file_counter = 1
403 IF (do_disk_storage)
THEN
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
427 INTEGER :: memory_usage
428 LOGICAL :: use_disk_storage
430 CALL hfx_decompress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
431 cache%element_counter = 1
450 INTEGER :: memory_usage
451 LOGICAL :: use_disk_storage
453 CALL hfx_compress_cache(cache%data(1), container, nbits, memory_usage, use_disk_storage)
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
483 REAL(
dp) :: values(*)
484 INTEGER,
INTENT(IN) :: nints, nbits
487 REAL(
dp),
INTENT(IN) :: eps_schwarz, pmax_entry
488 INTEGER :: memory_usage
489 LOGICAL :: use_disk_storage
491 INTEGER :: end_idx, i, start_idx, tmp_elements
492 INTEGER(int_8) :: shift, tmp
493 REAL(
dp) :: eps_schwarz_inv, factor
495 eps_schwarz_inv = 1.0_dp/eps_schwarz
496 factor = eps_schwarz/pmax_entry
498 shift = shifts(nbits - 1)
500 start_idx = cache%element_counter
501 end_idx = start_idx + nints - 1
502 IF (end_idx < cache_size)
THEN
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
511 cache%data(i + start_idx - 1) = shift
514 cache%element_counter = end_idx + 1
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
525 cache%data(i + start_idx - 1) = shift
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
537 cache%data(i - tmp_elements) = shift
540 cache%element_counter = nints - tmp_elements + 1
563 REAL(
dp) :: values(*)
564 INTEGER,
INTENT(IN) :: nints, nbits
567 REAL(
dp),
INTENT(IN) :: eps_schwarz, pmax_entry
568 INTEGER :: memory_usage
569 LOGICAL :: use_disk_storage
571 INTEGER :: end_idx, i, start_idx, tmp_elements
572 INTEGER(int_8) :: shift
575 factor = eps_schwarz/pmax_entry
577 shift = shifts(nbits - 1)
579 start_idx = cache%element_counter
580 end_idx = start_idx + nints - 1
582 IF (end_idx < cache_size)
THEN
584 values(i) = factor*real(cache%data(i + start_idx - 1) - shift,
dp)
586 cache%element_counter = end_idx + 1
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)
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)
596 cache%element_counter = nints - tmp_elements + 1
611 SUBROUTINE hfx_compress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
614 REAL(
dp),
INTENT(IN) :: eps
615 INTEGER :: memory_usage
616 LOGICAL :: use_disk_storage
619 INTEGER(int_8),
DIMENSION(CACHE_SIZE + 64) :: record
622 INTEGER(int_8) :: amax
623 INTEGER(int_8),
DIMENSION(CACHE_SIZE) :: values
629 nwords = int(libxs_vpack(record(2:), cache%data, libxs_vpack_maxbits(libxs_datatype_f64, 0.5_dp*eps, 0)))
634 IF (abs(cache%data(i)) > eps)
THEN
635 IF (abs(cache%data(i))*eps_inv >= 4611686018427387904.0_dp)
THEN
636 CALL cp_abort(__location__, &
637 "Overflow during tensor compression. Please use a larger EPS_FILTER or EPS_STORAGE_SCALING")
639 values(i) = nint(cache%data(i)*eps_inv, kind=
int_8)
643 amax = max(amax, abs(values(i)))
645 nbits = int(bit_size(amax)) - leadz(amax) + 1
646 nwords =
ints2groups(nbits, group_size, record(2:), values)
648 cpassert(0 < nwords .AND. nwords <
SIZE(record))
650 CALL hfx_container_put(container, record, nwords + 1, memory_usage, use_disk_storage)
651 END SUBROUTINE hfx_compress_real_cache
661 SUBROUTINE hfx_decompress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
664 REAL(
dp),
INTENT(IN) :: eps
665 INTEGER :: memory_usage
666 LOGICAL :: use_disk_storage
669 INTEGER(int_8) :: nread
670 INTEGER(int_8),
DIMENSION(CACHE_SIZE + 64) :: record
672 INTEGER(int_8),
DIMENSION(CACHE_SIZE) :: values
675 cpassert(
ASSOCIATED(container%current))
676 CALL hfx_container_get(container, record, 1, memory_usage, use_disk_storage)
677 nwords = int(record(1))
680 cpassert(nwords <
SIZE(record))
681 CALL hfx_container_get(container, record(2:), nwords, memory_usage, use_disk_storage)
684 nread = int(libxs_vunpack(cache%data, record(2:nwords + 1)), kind=
int_8)
687 cache%data(:) = eps*real(values,
dp)
689 cpassert(nread == nwords)
691 cache%data(:) = 0.0_dp
693 END SUBROUTINE hfx_decompress_real_cache
707 REAL(
dp) :: values(*)
708 INTEGER,
INTENT(IN) :: nints
711 REAL(
dp),
INTENT(IN) :: eps
712 INTEGER :: memory_usage
713 LOGICAL :: use_disk_storage
715 INTEGER :: end_idx, start_idx, tmp_elements
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
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
744 REAL(
dp) :: values(*)
745 INTEGER,
INTENT(IN) :: nints
748 REAL(
dp),
INTENT(IN) :: eps
749 INTEGER :: memory_usage
750 LOGICAL :: use_disk_storage
752 INTEGER :: end_idx, start_idx, tmp_elements
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
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
780 REAL(
dp),
INTENT(IN) :: eps
781 INTEGER :: memory_usage
782 LOGICAL :: use_disk_storage
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
806 REAL(
dp),
INTENT(IN) :: eps
807 INTEGER :: memory_usage
808 LOGICAL :: use_disk_storage
810 CALL hfx_decompress_real_cache(cache, container, eps, memory_usage, use_disk_storage)
811 cache%element_counter = 1
825 INTEGER :: memory_usage
826 LOGICAL :: do_disk_storage
828 cache%element_counter = 1
829 container%current => container%first
830 container%element_counter = 1
832 container%file_counter = 1
833 IF (do_disk_storage)
THEN
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
Utility routines to open and close files.
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.
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
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.
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public dp