forked from JuliaSmoothOptimizers/RegularizedOptimization.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aeefa25
commit c388384
Showing
3 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
@wrappedallocs(expr) | ||
Given an expression, this macro wraps that expression inside a new function | ||
which will evaluate that expression and measure the amount of memory allocated | ||
by the expression. Wrapping the expression in a new function allows for more | ||
accurate memory allocation detection when using global variables (e.g. when | ||
at the REPL). | ||
This code is based on that of https://github.com/JuliaAlgebra/TypedPolynomials.jl/blob/master/test/runtests.jl | ||
For example, `@wrappedallocs(x + y)` produces: | ||
```julia | ||
function g(x1, x2) | ||
@allocated x1 + x2 | ||
end | ||
g(x, y) | ||
``` | ||
You can use this macro in a unit test to verify that a function does not | ||
allocate: | ||
``` | ||
@test @wrappedallocs(x + y) == 0 | ||
``` | ||
""" | ||
macro wrappedallocs(expr) | ||
argnames = [gensym() for a in expr.args] | ||
quote | ||
function g($(argnames...)) | ||
@allocated $(Expr(expr.head, argnames...)) | ||
end | ||
$(Expr(:call, :g, [esc(a) for a in expr.args]...)) | ||
end | ||
end | ||
|
||
|
||
# Test non allocating solve! | ||
@testset "allocs" begin | ||
for (h, h_name) ∈ ((NormL0(λ), "l0"), (NormL1(λ), "l1")) | ||
for solver ∈ (:R2Solver, ) | ||
reg_nlp = RegularizedNLPModel(bpdn, h) | ||
solver = eval(solver)(reg_nlp) | ||
stats = GenericExecutionStats(bpdn, solver_specific = Dict{Symbol, Float64}()) | ||
@test @wrappedallocs(solve!(solver, reg_nlp, stats)) == 0 | ||
end | ||
end | ||
end |