Skip to content

Commit

Permalink
Add test for MA.broadcast! with sparse array (#289)
Browse files Browse the repository at this point in the history
* Add test for MA.broadcast! with sparse array

* Add tests

* Fix format

* Fix tests for Julia v1.6
  • Loading branch information
blegat authored Jun 8, 2024
1 parent 120a1b9 commit 0dc38d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ end

# If A is `Symmetric`, we cannot do a normal broadcast because we might modify
# the same entry twice. See https://github.com/jump-dev/JuMP.jl/issues/2102
function broadcast!(op::F, A::Array, args::Vararg{Any,N}) where {F<:Function,N}
function broadcast!(
op::F,
A::Union{Array,SparseArrays.AbstractSparseArray},
args::Vararg{Any,N},
) where {F<:Function,N}
bc = Broadcast.broadcasted(op, A, args...)
instantiated = Broadcast.instantiate(bc)
return copyto!(A, mutable_broadcasted(instantiated))
Expand Down
10 changes: 10 additions & 0 deletions test/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ end
"Cannot sum or substract matrices of axes `$(axes(A))` and `$(axes(B))` into a matrix of axes `$(axes(output))`, expected axes `$(axes(B))`.",
)
@test_throws err MA.operate_to!(output, +, A, B)
err = DimensionMismatch(
"Cannot sum or substract a matrix of axes `$(axes(A))` into a matrix of axes `$(axes(output))`, expected axes `$(axes(A))`.",
)
@test_throws err MA.operate_to!(output, +, A)
err = DimensionMismatch(
"Cannot sum or substract a matrix of axes `$(axes(A))` into a matrix of axes `$(axes(output))`, expected axes `$(axes(A))`.",
)
@test_throws err MA.operate_to!(output, -, A)
end
@testset "unsupported_product" begin
unsupported_product()
Expand Down Expand Up @@ -471,6 +479,8 @@ function test_sparse_vector_sum(::Type{T}) where {T}
x = SparseArrays.sparsevec([1, 3], T[5, 7])
y = copy(x)
z = copy(y)
alloc_test_le(() -> MA.operate!(+, y, z), 200)
alloc_test_le(() -> MA.operate!(-, y, z), 200)
alloc_test(() -> MA.operate_to!(x, +, y, z), 0)
alloc_test(() -> MA.operate_to!(x, -, y, z), 0)
alloc_test(() -> MA.operate_to!(x, +, y), 0)
Expand Down

0 comments on commit 0dc38d7

Please sign in to comment.