Bumped into a fun subtle issue where broadcasting with JLArrays and views behaves differently from their Array counterparts.
The specific case is this:
julia> using JLArrays
julia> A = -jl(ones(3, 3));
julia> A .*= sign.(view(A, 1:4:9)) # incorrect result!
3×3 JLArray{Float64, 2}:
1.0 -1.0 -1.0
1.0 1.0 -1.0
1.0 1.0 1.0
julia> B = -ones(3, 3);
julia> B .*= sign.(view(B, 1:4:9)) # correct result!
3×3 Matrix{Float64}:
1.0 1.0 1.0
1.0 1.0 1.0
1.0 1.0 1.0
From what I understand, the issue here is that broadcasting has some machinery to detect aliasing in its in/outputs, which ensures that the second case (B) works (I didn't delve deeply into this, but I think an independent copy is made of the view which then ensures writing into B does not alter reading from the view).
This machinery doesn't seem to propagate fully through the view of the JLArray though.
This came up in QuantumKitHub/MatrixAlgebraKit.jl#219.
To be honest, I'm not sure if this is really a case of "bug" or just more of a wrong usage from my part, as I actually hadn't intended for the view and the output to share data.
I was more surprised that this wasn't noticed in our tests for Arrays because there this is caught, while here it isn't.
I mostly just wanted to report this in case this wasn't known.
Bumped into a fun subtle issue where broadcasting with
JLArrays and views behaves differently from theirArraycounterparts.The specific case is this:
From what I understand, the issue here is that broadcasting has some machinery to detect aliasing in its in/outputs, which ensures that the second case (
B) works (I didn't delve deeply into this, but I think an independent copy is made of the view which then ensures writing intoBdoes not alter reading from the view).This machinery doesn't seem to propagate fully through the view of the
JLArraythough.This came up in QuantumKitHub/MatrixAlgebraKit.jl#219.
To be honest, I'm not sure if this is really a case of "bug" or just more of a wrong usage from my part, as I actually hadn't intended for the view and the output to share data.
I was more surprised that this wasn't noticed in our tests for
Arrays because there this is caught, while here it isn't.I mostly just wanted to report this in case this wasn't known.