Skip to content

Broadcasting into a PetscVec runs on the local arrays - #300

Merged
filoferra merged 1 commit into
JuliaParallel:mainfrom
filoferra:vec-broadcast
Sep 29, 2026
Merged

filoferra merged 1 commit into
JuliaParallel:mainfrom
filoferra:vec-broadcast

Conversation

@filoferra

Copy link
Copy Markdown
Collaborator

Base.broadcastable fell back to collect (one VecGetValues per entry) and copyto! wrote the result back with one VecSetValues per entry. At 10 000 entries a MatShell with that body took 139 000 allocations and 1.56 ms; now 9 and 0.002 ms.

broadcastable returns the Vec itself, and dest .= f.(args...) checks out the local arrays of dest and of every PetscVec argument, broadcasts over them, and hands them back. x .= x .+ y reuses the array already checked out. On several ranks the broadcast covers the entries each rank owns, so a Julia array in the expression must have the local length.

Tested serially (results, aliasing, a Julia array argument, DimensionMismatch, a MatShell body, and a fixed allocation count at two sizes) and on 4 ranks. Out-of-place w = 2 .* x is unchanged; it needs its own broadcast style and left for later.

broadcastable now returns the Vec itself, and dest .= f.(args...) checks out the local arrays of dest and of every Vec among the arguments, broadcasts over them and hands them back. At 10 000 entries a
MatShell doing y .= 2 .* x goes from 139 000 allocations to 9, and from 1.56 ms to 0.002 ms.
@filoferra
filoferra merged commit d279eb6 into JuliaParallel:main Sep 29, 2026
5 checks passed
@filoferra
filoferra deleted the vec-broadcast branch September 29, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant