-
Notifications
You must be signed in to change notification settings - Fork 37
Add undefmatrix
(similar to zeromatrix
)
#353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This is potentially much faster since it doesn't have to initialize values. For constructing a 1000x1000 `Matrix{Float64}` I benchmark this to be 10x faster which is very important because in cases like an autoswitching solver, you often want to preallocate a jacobian matrix, but might never use it if the equation isn't stiff.
Co-authored-by: Chris Elrod <[email protected]>
Codecov Report
@@ Coverage Diff @@
## master #353 +/- ##
=======================================
Coverage 90.27% 90.27%
=======================================
Files 9 9
Lines 1337 1337
=======================================
Hits 1207 1207
Misses 130 130
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
JuliaArrays/ArrayInterface.jl#353 adds an `undefmatrix`which is significantly faster than `zeromatrix` since it doesn't have to initialize values. This matters for autostiff solvers on non-stiff equations since in these cases filling the jacobian matrix with zeros can be as expensive as the entire ODE solve.
Should we merge this (so i can make the PR that adds this to ODE solvers)? |
This doesn't get the type correct for static arrays, and it doesn't have tests. |
What should I be testing here? Array, CuArray, SArray? |
Ah the first version worked with static array and sparse (and the performance difference is only there for large values anyway). |
I've added tests for base types. The StaticArrays issue is arguably their own fault. They haven't defined a form of |
The problem is probably the call to |
help?> similar
search: similar
similar(array, [element_type=eltype(array)], [dims=size(array)])
Create an uninitialized mutable array with the given element type and size, based upon the given source array. The
second and third arguments are both optional, defaulting to the given array's eltype and size. The dimensions may be
specified either as a single tuple argument or as a series of integer arguments.
Custom AbstractArray subtypes may choose which specific array type is best-suited to return for the given element type
and dimensionality. If they do not specialize this method, the default is an Array{element_type}(undef, dims...).
For example, similar(1:10, 1, 4) returns an uninitialized Array{Int,2} since ranges are neither mutable nor support 2
dimensions: |
By Creating an |
Add it to the docs |
Another test failure. |
Co-authored-by: Christopher Rackauckas <[email protected]>
Bro. |
fair. This time I've actually guaranteed that tests pass on my computer. |
function ArrayInterface.undefmatrix(::MArray{S, T, N, L}) where {S, T, N, L} | ||
return MMatrix{L, L, T, L*L}(undef) | ||
end | ||
# SArray doesn't have an undef constructor and is going to be small enough that this is fine. | ||
function ArrayInterface.undefmatrix(s::SArray) | ||
v = vec(s) | ||
return v.*v' | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could probably be staticarrayscore?
This is potentially much faster since it doesn't have to initialize values. For constructing a 1000x1000
Matrix{Float64}
I benchmark this to be 10x faster which is very important because in cases like an autoswitching solver, you often want to preallocate a jacobian matrix, but might never use it if the equation isn't stiff.