Skip to content

Commit 59ba728

Browse files
move wrappedallocs macro to utils file
1 parent 18b22ba commit 59ba728

File tree

3 files changed

+35
-41
lines changed

3 files changed

+35
-41
lines changed

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const global bpdn, bpdn_nls, sol = bpdn_model(compound)
1818
const global bpdn2, bpdn_nls2, sol2 = bpdn_model(compound, bounds = true)
1919
const global λ = norm(grad(bpdn, zeros(bpdn.meta.nvar)), Inf) / 10
2020

21+
include("utils.jl")
2122
include("test_AL.jl")
2223

2324
for (mod, mod_name) ((x -> x, "exact"), (LSR1Model, "lsr1"), (LBFGSModel, "lbfgs"))

test/test_allocs.jl

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,3 @@
1-
"""
2-
@wrappedallocs(expr)
3-
4-
Given an expression, this macro wraps that expression inside a new function
5-
which will evaluate that expression and measure the amount of memory allocated
6-
by the expression. Wrapping the expression in a new function allows for more
7-
accurate memory allocation detection when using global variables (e.g. when
8-
at the REPL).
9-
10-
This code is based on that of https://github.com/JuliaAlgebra/TypedPolynomials.jl/blob/master/test/runtests.jl
11-
12-
For example, `@wrappedallocs(x + y)` produces:
13-
14-
```julia
15-
function g(x1, x2)
16-
@allocated x1 + x2
17-
end
18-
g(x, y)
19-
```
20-
21-
You can use this macro in a unit test to verify that a function does not
22-
allocate:
23-
24-
```
25-
@test @wrappedallocs(x + y) == 0
26-
```
27-
"""
28-
macro wrappedallocs(expr)
29-
kwargs = [a for a in expr.args if isa(a, Expr)]
30-
args = [a for a in expr.args if isa(a, Symbol)]
31-
32-
argnames = [gensym() for a in args]
33-
kwargs_dict = Dict{Symbol, Any}(a.args[1] => a.args[2] for a in kwargs if a.head == :kw)
34-
quote
35-
function g($(argnames...); kwargs_dict...)
36-
$(Expr(expr.head, argnames..., kwargs...)) # Call the function twice to make the allocated macro more stable
37-
@allocated $(Expr(expr.head, argnames..., kwargs...))
38-
end
39-
$(Expr(:call, :g, [esc(a) for a in args]...))
40-
end
41-
end
421

432
# Test non allocating solve!
443
@testset "NLP allocs" begin

test/utils.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
@wrappedallocs(expr)
3+
4+
Given an expression, this macro wraps that expression inside a new function
5+
which will evaluate that expression and measure the amount of memory allocated
6+
by the expression. Wrapping the expression in a new function allows for more
7+
accurate memory allocation detection when using global variables (e.g. when
8+
at the REPL).
9+
10+
This code is based on that of https://github.com/JuliaAlgebra/TypedPolynomials.jl/blob/master/test/runtests.jl
11+
12+
You can use this macro in a unit test to verify that a function does not
13+
allocate:
14+
15+
```
16+
@test @wrappedallocs(x + y) == 0
17+
```
18+
"""
19+
macro wrappedallocs(expr)
20+
kwargs = [a for a in expr.args if isa(a, Expr)]
21+
args = [a for a in expr.args if isa(a, Symbol)]
22+
23+
argnames = [gensym() for a in args]
24+
kwargs_dict = Dict{Symbol, Any}(a.args[1] => a.args[2] for a in kwargs if a.head == :kw)
25+
quote
26+
function g($(argnames...); kwargs_dict...)
27+
$(Expr(expr.head, argnames..., kwargs...)) # Call the function twice to make the allocated macro more stable
28+
@allocated $(Expr(expr.head, argnames..., kwargs...))
29+
end
30+
$(Expr(:call, :g, [esc(a) for a in args]...))
31+
end
32+
end
33+
34+
# Construct the brock-rosenberg problem.

0 commit comments

Comments
 (0)