Skip to content

Commit 0643bb0

Browse files
authored
Merge pull request #59 from tensor4all/58-overflow-in-cachedfunction
Use fixed-size Integers from BitIntegers for CachedFunction instead o…
2 parents f753895 + b12b411 commit 0643bb0

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ authors = ["Ritter.Marc <[email protected]>, Hiroshi Shinaoka <
44
version = "0.9.15"
55

66
[deps]
7+
BitIntegers = "c3b6d118-76ef-56ca-8cc7-ebb389d030a1"
78
EllipsisNotation = "da5c29d0-fa7d-589e-88eb-ea29b0a81949"
89
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
910
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
1011

1112
[compat]
13+
BitIntegers = "0.3.5"
1214
EllipsisNotation = "1"
1315
QuadGK = "2.9"
1416
julia = "1.6"

src/TensorCrossInterpolation.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module TensorCrossInterpolation
22

33
using LinearAlgebra
44
using EllipsisNotation
5+
using BitIntegers
56
import QuadGK
67

78
# To add a method for rank(tci)

src/cachedfunction.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function using the usual function call syntax.
55
The type `K` denotes the type of the keys used to cache function values, which could be an integer type. This defaults to `UInt128`. A safer but slower alternative is `BigInt`, which is better suited for functions with a large number of arguments.
66
`CachedFunction` does not support batch evaluation of function values.
77
"""
8-
struct CachedFunction{ValueType,K<:Union{UInt32,UInt64,UInt128,BigInt}} <: BatchEvaluator{ValueType}
8+
struct CachedFunction{ValueType,K<:Union{UInt32,UInt64,UInt128,BigInt,BitIntegers.AbstractBitUnsigned}} <: BatchEvaluator{ValueType}
99
f::Function
1010
localdims::Vector{Int}
1111
cache::Dict{K,ValueType}
@@ -15,8 +15,11 @@ struct CachedFunction{ValueType,K<:Union{UInt32,UInt64,UInt128,BigInt}} <: Batch
1515
for n in 2:length(localdims)
1616
coeffs[n] = localdims[n-1] * coeffs[n-1]
1717
end
18+
if K == BigInt
19+
@warn "Using BigInt for keys. This is SUPER slower and uses more memory. The use of BigInt is kept only for compatibility with older code. Use BitIntegers.UInt256 or bigger integer types with fixed size instead."
20+
end
1821
if K != BigInt
19-
sum(coeffs .* (localdims .- 1)) < typemax(K) || error("Too many dimensions. Use BigInt instead of UInt128.")
22+
sum(coeffs .* (localdims .- 1)) < typemax(K) || error("Overflow in CachedFunction. Use ValueType = a bigger type with fixed size, e.g., BitIntegers.UInt256")
2023
end
2124
new(f, localdims, cache, coeffs)
2225
end

test/test_cachedfunction.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Test
22
import TensorCrossInterpolation as TCI
33
import TensorCrossInterpolation: BatchEvaluator, MultiIndex
4-
4+
import BitIntegers
55
struct TestF <: TCI.BatchEvaluator{Float64}
66
end
77

@@ -96,7 +96,7 @@ end
9696
f(x) = 1.0
9797
nint = 4
9898
N = 64 * nint
99-
cf = TCI.CachedFunction{Float64,BigInt}(f, fill(2, N))
99+
cf = TCI.CachedFunction{Float64,BitIntegers.UInt512}(f, fill(2, N))
100100
x = ones(Int, N)
101101
@test cf(x) == 1.0
102102
@test TCI._key(cf, x) == 0

0 commit comments

Comments
 (0)