Skip to content
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

Use copy instead of similar to respect sparsity structure? #544

Closed
gdalle opened this issue Oct 5, 2024 · 3 comments
Closed

Use copy instead of similar to respect sparsity structure? #544

gdalle opened this issue Oct 5, 2024 · 3 comments

Comments

@gdalle
Copy link
Owner

gdalle commented Oct 5, 2024

In these two spots, I use similar to preallocate a structured Jacobian / Hessian.

Pros:

  • No need to fill the values
  • Allows specifying the eltype
  • Guarantees that we get a mutable object

Cons:

I think it's still worth using similar because of the mutability argument. If the semantics of similar are broken for e.g. banded matrices, that's where they should be fixed.


Related:

@ChrisRackauckas
Copy link

copy cannot change the eltype, but you can do one(eltype(x)) .* sparsity_pattern(prep) to effectively generate a copy.

@gdalle
Copy link
Owner Author

gdalle commented Oct 5, 2024

It's kinda meh performance-wise though:

julia> using BenchmarkTools

julia> A = ones(Bool, 100, 100);

julia> f(A) = similar(A, Float64)
f (generic function with 1 method)

julia> g(A) = 1.0 .* A
g (generic function with 1 method)

julia> @btime f($A);
  342.742 ns (2 allocations: 78.17 KiB)

julia> @btime g($A);
  13.408 μs (2 allocations: 78.17 KiB)

@gdalle
Copy link
Owner Author

gdalle commented Oct 7, 2024

Closing this because it's not worth the performance hit. Better get the guilty packages to fix their similar dispatches

@gdalle gdalle closed this as not planned Won't fix, can't repro, duplicate, stale Oct 7, 2024
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

No branches or pull requests

2 participants