Skip to content

Commit 12554a7

Browse files
committed
estimate memory
1 parent 18c5d64 commit 12554a7

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

docs/src/performancetips.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ julia> sizeof(TruncatedPoly{5,Float64,Float64})
4949
48
5050
```
5151

52+
One can use [`estimate_memory`](@ref) to get a good estimation of peak memory in bytes.
53+
```julia
54+
julia> estimate_memory(problem, GraphPolynomial(; method=:finitefield))
55+
297616
56+
57+
julia> estimate_memory(problem, GraphPolynomial(; method=:polynomial))
58+
71427840
59+
```
60+
It means one only needs 298 KB memory to find the graph polynomial with the finite field approach,
61+
but needs 71 MB memory to find the graph polynomial using the [`Polynomial`](@ref) type.
62+
5263
!!! note
5364
* The actual run time memory can be several times larger than the size of the maximum tensor.
5465
There is no constant bound for the factor, an empirical value for it is 3x.

docs/src/ref.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ getixsv
105105
getiyv
106106
timespace_complexity
107107
timespacereadwrite_complexity
108+
estimate_memory
108109
@ein_str
109110
GreedyMethod
110111
TreeSA

src/interfaces.jl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -392,19 +392,29 @@ Memory estimation in number of bytes to compute certain `property` of a `problem
392392
`T` is the base type.
393393
"""
394394
function estimate_memory(problem::GraphProblem, property::AbstractProperty; T=Float64)
395-
if property isa SingleConfigMax{true} || property isa SingleConfigMin{true}
396-
# TODO: fix this
397-
@warn "The estimation of memory might be unreliable for the bounded computation of a single solution due to the caching."
398-
end
399-
estimate_memory(tensor_element_type(T, length(labels(problem)), nflavor(problem), property), problem)
395+
_estimate_memory(tensor_element_type(T, length(labels(problem)), nflavor(problem), property), problem)
396+
end
397+
function estimate_memory(problem::GraphProblem, ::Union{SingleConfigMax{true},SingleConfigMin{true}}; T=Float64)
398+
tc, sc, rw = timespacereadwrite_complexity(problem.code, _size_dict(problem))
399+
# caching all tensors is equivalent to counting the total number of writes
400+
return ceil(Int, exp2(rw - 1)) * sizeof(Tropical{T})
401+
end
402+
function estimate_memory(problem::GraphProblem, ::GraphPolynomial{:polynomial}; T=Float64)
403+
# this is the upper bound
404+
return peak_memory(problem.code, _size_dict(problem)) * (sizeof(T) * length(labels(problem)))
405+
end
406+
407+
function _size_dict(problem)
408+
lbs = labels(problem)
409+
nf = nflavor(problem)
410+
return Dict([lb=>nf for lb in lbs])
400411
end
401-
function estimate_memory(::Type{ET}, problem::GraphProblem) where ET
412+
413+
function _estimate_memory(::Type{ET}, problem::GraphProblem) where ET
402414
if !isbitstype(ET) && !(ET <: Mod)
403415
@warn "Target tensor element type `$ET` is not a bits type, the estimation of memory might be unreliable."
404416
end
405-
lbs = labels(problem)
406-
nf = nflavor(problem)
407-
return peak_memory(problem.code, Dict([lb=>nf for lb in lbs])) * sizeof(ET)
417+
return peak_memory(problem.code, _size_dict(problem)) * sizeof(ET)
408418
end
409419

410420
for (PROP, ET) in [(:SizeMax, :(Tropical{T})), (:SizeMin, :(Tropical{T})),

test/interfaces.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ end
192192
@show property
193193
ET = GraphTensorNetworks.tensor_element_type(Float32, 10, 2, property)
194194
@test eltype(solve(gp, property, T=Float32)) <: ET
195+
@test estimate_memory(gp, property) isa Integer
195196
end
196197
@test GraphTensorNetworks.tensor_element_type(Float32, 10, 2, GraphPolynomial(method=:polynomial)) == Polynomial{Float32, :x}
197198
@test sizeof(GraphTensorNetworks.tensor_element_type(Float32, 10, 2, GraphPolynomial(method=:fitting))) == 4
@@ -201,4 +202,8 @@ end
201202
@test GraphTensorNetworks.tensor_element_type(Float32, 10, 2, SingleConfigMin(;bounded=true)) == Tropical{Float32}
202203

203204
@test estimate_memory(gp, SizeMax()) * 2 == estimate_memory(gp, CountingMax())
205+
@test estimate_memory(gp, SingleConfigMax(bounded=true)) > estimate_memory(gp, SingleConfigMax(bounded=false))
206+
@test estimate_memory(gp, ConfigsMax(bounded=true)) == estimate_memory(gp, SingleConfigMax(bounded=false))
207+
@test estimate_memory(gp, GraphPolynomial(method=:fitting); T=Float32) * 4 == estimate_memory(gp, GraphPolynomial(method=:fft))
208+
@test estimate_memory(gp, GraphPolynomial(method=:finitefield)) * 10 == estimate_memory(gp, GraphPolynomial(method=:polynomial); T=Float32)
204209
end

0 commit comments

Comments
 (0)