Skip to content

Commit 7d590d3

Browse files
Fix tests
1 parent 2d316cc commit 7d590d3

File tree

4 files changed

+57
-66
lines changed

4 files changed

+57
-66
lines changed

src/basic.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,22 +620,21 @@ end
620620
# Out-of-place: v is action vector, u is update vector
621621
function (L::AddedOperator)(v::AbstractVecOrMat, u, p, t; kwargs...)
622622
# We don't need to update coefficients of L, as op(v, u, p, t) will do it for each op
623-
sum(op -> iszero(op) ? zero(v) : op(v, u, p, t; kwargs...), L.ops)
623+
sum(op -> op(v, u, p, t; kwargs...), L.ops)
624624
end
625625

626626
# In-place: w is destination, v is action vector, u is update vector
627627
@generated function (L::AddedOperator)(w::AbstractVecOrMat, v::AbstractVecOrMat, u, p, t; kwargs...)
628628
# We don't need to update coefficients of L, as op(w, v, u, p, t) will do it for each op
629629

630-
T = L.parameters[1]
631630
ops_types = L.parameters[2].parameters
632631
N = length(ops_types)-1
633632

634633
quote
635634
L.ops[1](w, v, u, p, t; kwargs...)
636635
Base.@nexprs $N i->begin
637636
op = L.ops[i+1]
638-
op(w, v, u, p, t, one($T), one($T); kwargs...)
637+
op(w, v, u, p, t, true, true; kwargs...)
639638
end
640639
w
641640
end
@@ -653,7 +652,7 @@ end
653652
L.ops[1](w, v, u, p, t, α, β; kwargs...)
654653
Base.@nexprs $N i->begin
655654
op = L.ops[i+1]
656-
op(w, v, u, p, t, α, one($T); kwargs...)
655+
op(w, v, u, p, t, α, true; kwargs...)
657656
end
658657
w
659658
end

src/matrix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function (L::MatrixOperator)(w::AbstractVecOrMat, v::AbstractVecOrMat, u, p, t;
201201
end
202202

203203
# In-place with scaling: w = α*(L*v) + β*w
204-
function (L::MatrixOperator)(w::AbstractVecOrMat, v::AbstractVecOrMat, u, p, t, α, β; kwargs...)
204+
Base.@constprop :aggressive function (L::MatrixOperator)(w::AbstractVecOrMat, v::AbstractVecOrMat, u, p, t, α, β; kwargs...)
205205
update_coefficients!(L, u, p, t; kwargs...)
206206
mul!(w, L.A, v, α, β)
207207
end

test/downstream/Project.toml

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/downstream/alloccheck.jl

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,59 @@
1-
using SciMLOperators, AllocCheck, Random, SparseArrays, Test
1+
using SciMLOperators, Random, SparseArrays, Test
22
using SciMLOperators: IdentityOperator,
33
NullOperator,
44
ScaledOperator,
55
AddedOperator
6-
Random.seed!(0)
7-
N = 8
8-
K = 12
9-
A = rand(N, N) |> MatrixOperator
10-
B = rand(N, N) |> MatrixOperator
11-
C = rand(N, N) |> MatrixOperator
12-
α = rand()
13-
β = rand()
14-
u = rand(N, K) # Update vector
15-
v = rand(N, K) # Action vector
16-
w = zeros(N, K) # Output vector
17-
p = ()
18-
t = 0
19-
op = AddedOperator(A, B)
20-
21-
# Define a function to test allocations with the new interface
22-
@check_allocs ignore_throw = true function apply_op!(H, w, v, u, p, t)
23-
H(w, v, u, p, t)
24-
return nothing
25-
end
26-
27-
if VERSION >= v"1.12-beta"
28-
apply_op!(op, w, v, u, p, t)
29-
else
30-
@test_throws AllocCheckFailure apply_op!(op, w, v, u, p, t)
31-
end
32-
33-
for T in (Float32, Float64, ComplexF32, ComplexF64)
34-
N = 100
35-
A1_sparse = MatrixOperator(sprand(T, N, N, 5 / N))
36-
A2_sparse = MatrixOperator(sprand(T, N, N, 5 / N))
37-
A3_sparse = MatrixOperator(sprand(T, N, N, 5 / N))
38-
39-
A1_dense = MatrixOperator(rand(T, N, N))
40-
A2_dense = MatrixOperator(rand(T, N, N))
41-
A3_dense = MatrixOperator(rand(T, N, N))
42-
43-
coeff1(a, u, p, t) = sin(p.ω * t)
44-
coeff2(a, u, p, t) = cos(p.ω * t)
45-
coeff3(a, u, p, t) = sin(p.ω * t) * cos(p.ω * t)
46-
47-
c1 = ScalarOperator(rand(T), coeff1)
48-
c2 = ScalarOperator(rand(T), coeff2)
49-
c3 = ScalarOperator(rand(T), coeff3)
50-
51-
H_sparse = c1 * A1_sparse + c2 * A2_sparse + c3 * A3_sparse
52-
H_dense = c1 * A1_dense + c2 * A2_dense + c3 * A3_dense
53-
54-
u = rand(T, N)
55-
v = rand(T, N)
56-
w = similar(u)
57-
p == 0.1,)
58-
t = 0.1
596

60-
@test_throws AllocCheckFailure apply_op!(H_sparse, w, v, u, p, t)
61-
@test_throws AllocCheckFailure apply_op!(H_dense, w, v, u, p, t)
7+
@testset "Allocations Check" begin
8+
Random.seed!(0)
9+
N = 8
10+
K = 12
11+
A = rand(N, N) |> MatrixOperator
12+
B = rand(N, N) |> MatrixOperator
13+
u = rand(N, K) # Update vector
14+
v = rand(N, K) # Action vector
15+
w = zeros(N, K) # Output vector
16+
p = ()
17+
t = 0
18+
op = AddedOperator(A, B)
19+
20+
function apply_op!(H, w, v, u, p, t)
21+
H(w, v, u, p, t)
22+
return nothing
23+
end
24+
25+
test_apply_noalloc(H, w, v, u, p, t) = @test (@allocations apply_op!(H, w, v, u, p, t)) == 0
26+
27+
test_apply_noalloc(op, w, v, u, p, t)
28+
29+
for T in (Float32, Float64, ComplexF32, ComplexF64)
30+
N = 100
31+
A1_sparse = MatrixOperator(sprand(T, N, N, 5 / N))
32+
A2_sparse = MatrixOperator(sprand(T, N, N, 5 / N))
33+
A3_sparse = MatrixOperator(sprand(T, N, N, 5 / N))
34+
35+
A1_dense = MatrixOperator(rand(T, N, N))
36+
A2_dense = MatrixOperator(rand(T, N, N))
37+
A3_dense = MatrixOperator(rand(T, N, N))
38+
39+
coeff1(a, u, p, t) = sin(p.ω * t)
40+
coeff2(a, u, p, t) = cos(p.ω * t)
41+
coeff3(a, u, p, t) = sin(p.ω * t) * cos(p.ω * t)
42+
43+
c1 = ScalarOperator(rand(T), coeff1)
44+
c2 = ScalarOperator(rand(T), coeff2)
45+
c3 = ScalarOperator(rand(T), coeff3)
46+
47+
H_sparse = c1 * A1_sparse + c2 * A2_sparse + c3 * A3_sparse
48+
H_dense = c1 * A1_dense + c2 * A2_dense + c3 * A3_dense
49+
50+
u = rand(T, N)
51+
v = rand(T, N)
52+
w = similar(u)
53+
p == 0.1,)
54+
t = 0.1
55+
56+
test_apply_noalloc(H_sparse, w, v, u, p, t)
57+
test_apply_noalloc(H_dense, w, v, u, p, t)
58+
end
6259
end

0 commit comments

Comments
 (0)