-
-
Notifications
You must be signed in to change notification settings - Fork 402
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
Containers do not support view #2998
Comments
Annoyingly, it works if the indices are julia> x = Containers.@container([i=1:2], 0)
2-element Vector{Int64}:
0
0
julia> view(x, :)
2-element view(::Vector{Int64}, :) with eltype Int64:
0
0
julia> x = Containers.@container([i=2:3], 0)
1-dimensional DenseAxisArray{Int64,1,...} with index sets:
Dimension 1, 2:3
And data, a 2-element Vector{Int64}:
0
0
julia> v = view(x, :)
2-element view(::JuMP.Containers.DenseAxisArray{Int64, 1, Tuple{UnitRange{Int64}}, Tuple{JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}}}, :) with eltype Int64 with indices 2:3:
0
0
julia> v[1] = 1
ERROR: BoundsError: attempt to access 2-element view(::JuMP.Containers.DenseAxisArray{Int64, 1, Tuple{UnitRange{Int64}}, Tuple{JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}}}, :) with eltype Int64 with indices 2:3 at index [1]
Stacktrace:
[1] throw_boundserror(A::SubArray{Int64, 1, JuMP.Containers.DenseAxisArray{Int64, 1, Tuple{UnitRange{Int64}}, Tuple{JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}}}, Tuple{Base.Slice{UnitRange{Int64}}}, false}, I::Tuple{Int64})
@ Base ./abstractarray.jl:651
[2] checkbounds
@ ./abstractarray.jl:616 [inlined]
[3] setindex!(V::SubArray{Int64, 1, JuMP.Containers.DenseAxisArray{Int64, 1, Tuple{UnitRange{Int64}}, Tuple{JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}}}, Tuple{Base.Slice{UnitRange{Int64}}}, false}, x::Int64, I::Int64)
@ Base ./subarray.jl:316
[4] top-level scope
@ REPL[44]:1
julia> v[2] = 1
1
julia> v
2-element view(::JuMP.Containers.DenseAxisArray{Int64, 1, Tuple{UnitRange{Int64}}, Tuple{JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}}}, :) with eltype Int64 with indices 2:3:
1
0
julia> x
1-dimensional DenseAxisArray{Int64,1,...} with index sets:
Dimension 1, 2:3
And data, a 2-element Vector{Int64}:
1
0 |
Seems like over-riding But I don't know how to intercept a method where the method signature doesn't match. I guess the fundamental problem is that view assumes: julia> x
1-dimensional DenseAxisArray{Float64,1,...} with index sets:
Dimension 1, [:a, :b]
And data, a 2-element Vector{Float64}:
1.0
2.0
julia> eachindex(IndexLinear(), x)
2-element Vector{Symbol}:
:a
:b returns an |
For example, this is clearly wrong: julia> c = Containers.@container([i=1:4, j=2:3], i + j)
2-dimensional DenseAxisArray{Int64,2,...} with index sets:
Dimension 1, Base.OneTo(4)
Dimension 2, 2:3
And data, a 4×2 Matrix{Int64}:
3 4
4 5
5 6
6 7
julia> eachindex(IndexLinear(), c)
Base.OneTo(8) |
function Base.view(A::DenseAxisArray{T,N}, idx...) where {T,N}
new_indices = Base.to_index(A, idx)
new_axes = _getindex_recurse(A.axes, new_indices, _is_range)
return DenseAxisArray(view(A.data, new_indices...), new_axes...)
end But the code does not work because we use |
I'd prefer not too, because we'd need a new type parameter in |
@metab0t what is the motivation for using a |
For performance reasons: julia> using JuMP
julia> A = Containers.@container([i=1:100, j=1:200], i+j, container=Array);
julia> DA = Containers.@container([i=1:100, j=1:200], i+j, container=Containers.DenseAxisArray);
julia> size(A)
(100, 200)
julia> size(DA)
(100, 200)
julia> using BenchmarkTools
julia> @btime A[:, 50];
212.571 ns (2 allocations: 912 bytes)
julia> @btime @view A[:, 50];
94.044 ns (2 allocations: 64 bytes)
julia> @btime DA[:, 50];
257.864 ns (5 allocations: 1008 bytes) |
Sure. But where does this show up as a problem when modeling?
…On Tue, 2 Aug 2022, 7:18 pm Y. Yang, ***@***.***> wrote:
For performance reasons:
julia> using JuMP
julia> A = ***@***.***([i=1:100, j=1:200], i+j, container=Array);
julia> DA = ***@***.***([i=1:100, j=1:200], i+j, container=Containers.DenseAxisArray);
julia> size(A)
(100, 200)
julia> size(DA)
(100, 200)
julia> using BenchmarkTools
julia> @Btime A[:, 50];
212.571 ns (2 allocations: 912 bytes)
julia> @Btime @view A[:, 50];
94.044 ns (2 allocations: 64 bytes)
julia> @Btime DA[:, 50];
257.864 ns (5 allocations: 1008 bytes)
—
Reply to this email directly, view it on GitHub
<#2998 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB6MQJN23ATEOYF6HEHFFQDVXDDS7ANCNFSM5YERJXQA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Sometimes we want to extract a row or column of variables to treat it like a 1-d array. for i in axes(variables, 1)
@constraint(LinearAlgebra.dot(f(i), variables[i, :]) <= 0.0)
end |
Less convenient, but you could go something like for (i, key) in enumerate(axes(variables, 1))
@constraint(
model,
LinearAlgebra.dot(f(key), view(variables.data, i, :)) <= 0.0,
)
end
# or
for i in axes(x, 1)
c = f(i)
@constraint(model, sum(c[k] * x[i, k] for k in axes(x, 2)) <= 0)
end |
Yes, we can bypass In fact, The problem is what |
It would have to be something like |
Unfortunately, I don't have a benchmark for this issue now. |
I took another look at this. I'm going to close as won't-fix. Supporting views of containers has a number of technical challenges (hard to add without breaking existing code) and is a very rare request. I will re-open if this comes up again in the future. |
Reopening because we need to make this work for #3151. |
I don't know if there's an easy way to make it work for |
I agree. |
From Pierre Pasquet on slack:
We should throw a nicer error message, since this might be something that people do more often in future.
The text was updated successfully, but these errors were encountered: