Skip to content

Commit 965a2b9

Browse files
authored
Merge pull request #121 from JordiManyer/bugfix-similar-psparse
Bugfix: `Base.similar` for `PSparseMatrix`
2 parents dc1a3b9 + 07ba2b8 commit 965a2b9

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Function `partition_from_color`.
13+
- `Base.copyto!` and `Base.copy!` for `PSparseMatrix`.
14+
15+
### Fixed
16+
17+
- Bugfix: `Base.similar` methods for `PSparseMatrix` not working.
1318

1419
## [0.3.3] - 2023-08-09
1520

src/p_sparse_matrix.jl

+19-3
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,31 @@ function Base.similar(a::PSparseMatrix,::Type{T},inds::Tuple{<:PRange,<:PRange})
397397
matrix_partition = map(partition(a),partition(rows),partition(cols)) do values, row_indices, col_indices
398398
allocate_local_values(values,T,row_indices,col_indices)
399399
end
400-
PSparseMatrix(values,row_partition,col_partition)
400+
PSparseMatrix(matrix_partition,partition(rows),partition(cols))
401401
end
402402

403403
function Base.similar(::Type{<:PSparseMatrix{V}},inds::Tuple{<:PRange,<:PRange}) where V
404404
rows,cols = inds
405-
matrix_partition = map(partition(a),partition(rows),partition(cols)) do values, row_indices, col_indices
405+
matrix_partition = map(partition(rows),partition(cols)) do row_indices, col_indices
406406
allocate_local_values(V,row_indices,col_indices)
407407
end
408-
PSparseMatrix(matrix_partition,row_partition,col_partition)
408+
PSparseMatrix(matrix_partition,partition(rows),partition(cols))
409+
end
410+
411+
function Base.copy!(a::PSparseMatrix,b::PSparseMatrix)
412+
@assert size(a) == size(b)
413+
copyto!(a,b)
414+
end
415+
416+
function Base.copyto!(a::PSparseMatrix,b::PSparseMatrix)
417+
if partition(axes(a,1)) === partition(axes(b,1)) && partition(axes(a,2)) === partition(axes(b,2))
418+
map(copy!,partition(a),partition(b))
419+
elseif matching_own_indices(axes(a,1),axes(b,1)) && matching_own_indices(axes(a,2),axes(b,2))
420+
map(copy!,own_values(a),own_values(b))
421+
else
422+
error("Trying to copy a PSparseMatrix into another one with a different data layout. This case is not implemented yet. It would require communications.")
423+
end
424+
a
409425
end
410426

411427
function LinearAlgebra.fillstored!(a::PSparseMatrix,v)

test/p_sparse_matrix_tests.jl

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ function p_sparse_matrix_tests(distribute)
6060
@test all( values .== 6 )
6161
end
6262

63+
_A = similar(A)
64+
_A = similar(A,eltype(A),axes(A))
65+
#_A = similar(typeof(A),axes(A)) # This should work, but fails down the line in SparseArrays.jl
66+
copy!(_A,A)
67+
6368
LinearAlgebra.fillstored!(A,1.0)
6469
fill!(x,3.0)
6570
mul!(b,A,x)

0 commit comments

Comments
 (0)