Skip to content

Commit 1be1cc9

Browse files
committed
test allocations
1 parent 233326c commit 1be1cc9

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

test/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
44
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
55
NLPModelsTest = "7998695d-6960-4d3a-85c4-e1bceb8cd856"
66
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
7+
8+
[compat]
9+
CUDA = "5.8"
10+
NLPModels = "0.21"
11+
NLPModelsTest = "0.10"

test/allocs.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@testset "Allocations" begin
2+
@testset "Allocations for hs5" begin
3+
@testset "hs5" begin
4+
include("problems/hs5.jl")
5+
test_zero_allocations(hs5())
6+
end
7+
end
8+
9+
@testset "Allocations for hs6" begin
10+
@testset "hs6" begin
11+
include("problems/hs6.jl")
12+
test_zero_allocations(hs6())
13+
end
14+
end
15+
end

test/problems/hs5.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@ hs5() = hs5(Float64)
1919
hs5(::Type{T}) where {T <: Number} = hs5(Vector{T})
2020
function hs5(::Type{V}) where {V}
2121
T = eltype(V)
22-
hprod(hv, x, v; obj_weight = one(T)) =
23-
(hv .= (-sin(x[1] + x[2]) * (v[1] + v[2]) .+ 2 * V([v[1] - v[2]; v[2] - v[1]])) * obj_weight)
22+
hprod(hv, x, v; obj_weight = one(T)) = begin
23+
hv[1] = v[1] - v[2]
24+
hv[2] = v[2] - v[1]
25+
hv .*= 2 * obj_weight
26+
hv .+= -sin(x[1] + x[2]) * (v[1] + v[2])
27+
end
2428
hess_coord(vals, x; obj_weight = one(T)) = begin
2529
vals[1] = vals[3] = -sin(x[1] + x[2]) + 2
2630
vals[2] = -sin(x[1] + x[2]) - 2
2731
vals .*= obj_weight
2832
end
2933
f(x) = sin(x[1] + x[2]) + (x[1] - x[2])^2 - 3x[1] / 2 + 5x[2] / 2 + 1
30-
grad(gx, x) =
31-
(gx .= cos(x[1] + x[2]) .+ 2 * (x[1] - x[2]) * V([1; -1]) + V([-15 // 10; 25 // 10]))
34+
grad(gx, x) = begin
35+
gx[1] = -15 // 10 + 2 * (x[1] - x[2])
36+
gx[2] = 25 // 10 - 2 * (x[1] - x[2])
37+
gx .+= cos(x[1] + x[2])
38+
end
3239
objgrad(gx, x) = f(x), grad(gx, x)
3340
return NLPModel(
3441
fill!(V(undef, 2), 0),

test/problems/hs6.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function hs6(::Type{V}) where {V}
2525
return NLPModel(
2626
V([-12 // 10; 1]),
2727
x -> (1 - x[1])^2;
28-
grad = (gx, x) -> gx .= V([2 * (x[1] - 1); 0]),
28+
grad = (gx, x) -> (gx[1] = 2 * (x[1] - 1); gx[2] = 0; gx),
2929
# objgrad explicitly not implemented
3030
hprod = hprod,
3131
hess_coord = ([1], [1], hess_coord),

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ using CUDA, Test
44

55
include("nlpmodelstest.jl")
66
include("notimplemented.jl")
7+
include("allocs.jl")

0 commit comments

Comments
 (0)