Skip to content

Commit f76fc84

Browse files
committed
Fixed issue in mul! for block arrays
1 parent 5366d9a commit f76fc84

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Fixed `BlockPMatrix{V}(::UndefInitializer, rows, cols)` constructor dropping the `cols` argument, causing a `MethodError` at runtime. Since PR[#194](https://github.com/gridap/GridapDistributed.jl/pull/194).
1313
- Fixed `local_views(::BlockPMatrix, rows, cols)` indexing 1D block-range vectors with a 2D `CartesianIndex`, causing `BoundsError` for any multi-field problem with ≥2 fields. Since PR[#194](https://github.com/gridap/GridapDistributed.jl/pull/194).
14+
- Fixed `mul!(y::BlockPVector, A::BlockPMatrix, x::BlockPVector, α, β)` computing `α*β*(A*x)` instead of `α*(A*x) + β*y`; the 3-arg `mul!` was also updated to correctly zero `y` before accumulating. Since PR[#194](https://github.com/gridap/GridapDistributed.jl/pull/194).
1415

1516
## [0.4.11] - 2026-02-20
1617

src/BlockPartitionedArrays.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,23 @@ end
329329

330330
function LinearAlgebra.mul!(y::BlockPVector,A::BlockPMatrix,x::BlockPVector)
331331
o = one(eltype(A))
332-
mul!(y,A,x,o,o)
332+
z = zero(eltype(A))
333+
mul!(y,A,x,o,z)
333334
end
334335

335336
function LinearAlgebra.mul!(y::BlockPVector,A::BlockPMatrix,x::BlockPVector::Number::Number)
336337
yb, Ab, xb = blocks(y), blocks(A), blocks(x)
337-
z = zero(eltype(y))
338338
o = one(eltype(A))
339+
z = zero(eltype(y))
339340
for i in 1:blocksize(A,1)
340-
fill!(yb[i],z)
341+
if iszero(β)
342+
fill!(yb[i],z)
343+
else
344+
rmul!(yb[i],β)
345+
end
341346
for j in 1:blocksize(A,2)
342347
mul!(yb[i],Ab[i,j],xb[j],α,o)
343348
end
344-
rmul!(yb[i],β)
345349
end
346350
return y
347351
end

test/BlockPartitionedArraysTests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ function main(distribute, parts)
6666

6767
maximum(abs, v)
6868
minimum(abs, v)
69+
70+
# BUG4 fix: mul!(y,A,x,α,β) must compute α*(A*x) + β*y, not α*β*(A*x)
71+
# With A=0: y ← α*(0*x) + β*y_old = β*y_old
72+
fill!(v, 1.0)
73+
v4 = similar(v, block_range)
74+
fill!(v4, 3.0)
75+
LinearAlgebra.fillstored!(m, 0.0)
76+
mul!(v4, m, v, 2.0, 2.0) # y ← 2*(0*x) + 2*[3,...] = [6,...]
77+
@test norm(v4) 6.0 * sqrt(2*n_global)
6978
end
7079

7180
end # module

test/BlockSparseMatrixAssemblersTests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function is_same_vector(x::BlockPVector,y::PVector,Ub,U)
2121
res = map(1:num_fields(Ub)) do i
2222
xi = restrict_to_field(Ub,x_fespace,i)
2323
yi = restrict_to_field(U,y_fespace,i)
24-
xi yi
24+
norm(xi - yi) < 1.0e-10
2525
end
2626
return all(res)
2727
end

0 commit comments

Comments
 (0)