JuliaParallel / JuliaParallel/PETSc.jl
Block setindex! m[rows, cols] = matrix throws MethodError
- Dominant language
- Julia
- Stars
- 182
- Forks
- 46
- Avg merge
- 15h
- Merged PRs (30d)
- 18
Description
Setting a block of matrix entries at once throws a `MethodError`. Single entries work:
```julia
using PETSc
pl = PETSc.getlib(PetscScalar = Float64)
PETSc.initialize(pl)
m = PETSc.MatSeqAIJ(pl, 2, 2, 2)
m[1, 1] = 1.0 # fine
m[1:2, 1:2] = [1.0 2.0; 3.0 4.0]
# ERROR: MethodError: no method matching MatSetValues(
# ::Type{PetscLibType{Float64, Int64, String}}, ::PetscMat{...},
# ::Int64, ::Vector{Int64}, ::Int64, ::Vector{Int64}, ::Matrix{Float64}, ::InsertMode)
```
`mat.jl:160` builds `petsc_vals = PetscScalar.(vals)`, which stays a `Matrix` when `vals` is a matrix, but the generated `MatSetValues` at `Mat_wrappers.jl:1875` is typed `v::Vector{$PetscScalar}`. The single-entry method just above it works because it wraps its value in `[value]`.
There is also an ordering question the method does not address: `MatSetValues` reads the block row-major, so a Julia `Matrix` needs transposing before `vec`, not just flattening.
I hit this filling a dense Jacobian in a TS `IJacobian` callback. Doing one batched `MatSetValues` instead of `n^2` single-entry calls cut a solve from 90 ms to 20 ms at n = 200, so the batched path is worth having. Working around it for now by calling `LibPETSc.MatSetValues` directly with `vec` of a transposed buffer.
The same `Matrix`-vs-`Vector` mismatch looks like it would affect the `m[i, cols] = vals` row method at `mat.jl:179` only if `vals` is a matrix; that one passes a vector, so it is probably fine.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the block-assignment method at mat.jl:160 and compare it with the single-entry method above, then inspect the generated MatSetValues wrapper at Mat_wrappers.jl:1875. Run the issue’s Julia reproducer and verify that m[rows, cols] = matrix accepts a Matrix, preserves PETSc’s row-major block ordering, and supports the batched path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100