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

Don't overload * for linearindexing type computations #11579

Merged
merged 1 commit into from
Jun 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ linearindexing{T<:AbstractArray}(::Type{T}) = LinearSlow()
linearindexing{T<:Array}(::Type{T}) = LinearFast()
linearindexing{T<:Range}(::Type{T}) = LinearFast()

*(::LinearFast, ::LinearFast) = LinearFast()
*(::LinearSlow, ::LinearFast) = LinearSlow()
*(::LinearFast, ::LinearSlow) = LinearSlow()
*(::LinearSlow, ::LinearSlow) = LinearSlow()
linearindexing(A::AbstractArray, B::AbstractArray) = linearindexing(linearindexing(A), linearindexing(B))
linearindexing(A::AbstractArray, B::AbstractArray...) = linearindexing(linearindexing(A), linearindexing(B...))
linearindexing(::LinearFast, ::LinearFast) = LinearFast()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like linearindexing for this; it makes sense. I haven't tried this, but could it be simplified with just two methods?

linearindexing(::Union(LinearFast, LinearSlow)...) = LinearSlow()
linearindexing(::LinearFast...) = LinearFast()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not if I understand this correctly...
If I'm right, I think @timholy needs to add a comment explaining why that is all spelled out that way, to avoid some later well meaning julian from optimizing away his optimization...
If you have A, B, C, D, and A, B, C are LinearFast, and D is LinearSlow, the way he has it does two fast operations, and one slow, but yours does 3 slow...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you only need the two-argument form. But yes, it could be written with the Union. I'll change that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timholy Was I correct about how using the Varargs form would actually make things slower?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ScottPJones #11248. But should be fine if it is inlined.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I am correct, until the very nice #11248 gets merged in (hopefully soon)? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errr. #11248 is just a report. not a merge request....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darn!!! I guess I was just too hopeful. Do you think you (or somebody) will be tackling that issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make all operations slow, if any are slow... The way it is now, some can stay fast

Sent from my iPhone

On Jun 5, 2015, at 5:03 AM, Matt Bauman notifications@github.com wrote:

In base/abstractarray.jl:

@@ -113,10 +113,12 @@ linearindexing{T<:AbstractArray}(::Type{T}) = LinearSlow()
linearindexing{T<:Array}(::Type{T}) = LinearFast()
linearindexing{T<:Range}(::Type{T}) = LinearFast()

-(::LinearFast, ::LinearFast) = LinearFast()
-
(::LinearSlow, ::LinearFast) = LinearSlow()
-(::LinearFast, ::LinearSlow) = LinearSlow()
-
(::LinearSlow, ::LinearSlow) = LinearSlow()
+linearindexing(A::AbstractArray, B::AbstractArray) = linearindexing(linearindexing(A), linearindexing(B))
+linearindexing(A::AbstractArray, B::AbstractArray...) = linearindexing(linearindexing(A), linearindexing(B...))
+linearindexing(::LinearFast, ::LinearFast) = LinearFast()
I like linearindexing for this; it makes sense. I haven't tried this, but could it be simplified with just two methods?

linearindexing(::Union(LinearFast, LinearSlow)...) = LinearSlow()
linearindexing(::LinearFast...) = LinearFast()

Reply to this email directly or view it on GitHub.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, LinearFast() means "linear indexing is fast." When that's not true, we use CartesianIndex, which is also fast 😄. Linear indexing is every-so-slightly (well, maybe a little more than slightly) faster than cartesian indexing for certain array types (most observable with high-dimensional arrays), but it's catastrophically slower for others. So all this logic is simply to use linear indexing when we know it's safe (LinearFast), but use cartesian indexing (LinearSlow) whenever we're unsure or know that linear would be a bad idea.

All this code we're discussing is only about defining and reading traits; once the iteration scheme has been selected, it has no further impact on the cost of indexing.

linearindexing(::LinearIndexing, ::LinearIndexing) = LinearSlow()

# The real @inline macro is not available this early in the bootstrap, so this
# internal macro splices the meta Expr directly into the function body.
Expand Down Expand Up @@ -385,9 +385,14 @@ eachindex(::LinearFast, A::AbstractArray) = 1:length(A)

function eachindex(A::AbstractArray, B::AbstractArray)
@_inline_meta
eachindex(linearindexing(A)*linearindexing(B), A, B)
eachindex(linearindexing(A,B), A, B)
end
function eachindex(A::AbstractArray, B::AbstractArray...)
@_inline_meta
eachindex(linearindexing(A,B...), A, B...)
end
eachindex(::LinearFast, A::AbstractArray, B::AbstractArray) = 1:max(length(A),length(B))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: why does this use max? Does that cause a BoundsError if the arrays are different sizes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out reducedim.jl for a fun example.

The idea is that, in cases where sizes differ, you should visit "each index," which means you should essentially traverse the bounding box. CartesianIndexes support min, so you can safely say A[min(I, Imin)] and thereby implement reductions and broadcasting.

eachindex(::LinearFast, A::AbstractArray, B::AbstractArray...) = 1:max(length(A), map(length, B)...)

isempty(a::AbstractArray) = (length(a) == 0)

Expand Down
10 changes: 7 additions & 3 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ ndims(R::CartesianRange) = length(R.start)
:($meta; CartesianRange(CartesianIndex{$N}($(startargs...)), CartesianIndex{$N}($(stopargs...))))
end

@generated function eachindex{S,T,M,N}(::LinearSlow, A::AbstractArray{S,M}, B::AbstractArray{T,N})
K = max(M,N)
@generated function eachindex(::LinearSlow, A::AbstractArray, B::AbstractArray...)
K = max(ndims(A), map(ndims, B)...)
startargs = fill(1, K)
stopargs = [:(max(size(A,$i),size(B,$i))) for i=1:K]
stopargs = Array(Expr, K)
for i = 1:K
Bargs = [:(size(B[$j],$i)) for j = 1:length(B)]
stopargs[i] = :(max(size(A,$i),$(Bargs...)))
end
meta = Expr(:meta, :inline)
:($meta; CartesianRange(CartesianIndex{$K}($(startargs...)), CartesianIndex{$K}($(stopargs...))))
end
Expand Down
9 changes: 8 additions & 1 deletion doc/stdlib/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Basic functions

Returns the number of elements in A

.. function:: eachindex(A)
.. function:: eachindex(A...)

Creates an iterable object for visiting each index of an AbstractArray ``A`` in an efficient manner. For array types that have opted into fast linear indexing (like ``Array``), this is simply the range ``1:length(A)``. For other array types, this returns a specialized Cartesian range to efficiently index into the array with indices specified for every dimension. Example for a sparse 2-d array::

Expand Down Expand Up @@ -59,6 +59,13 @@ Basic functions
(iter.I_1,iter.I_2) = (2,3)
A[iter] = 0.8090413606455655

If you supply more than one ``AbstractArray`` argument, ``eachindex``
will create an iterable object that is fast for all arguments (a
``UnitRange`` if all inputs have fast linear indexing, a
CartesianRange otherwise). If the arrays have different sizes and/or
dimensionalities, ``eachindex`` returns an interable that spans the
largest range along each dimension.

.. function:: Base.linearindexing(A)

``linearindexing`` defines how an AbstractArray most efficiently accesses its elements. If ``Base.linearindexing(A)`` returns ``Base.LinearFast()``, this means that linear indexing with only one index is an efficient operation. If it instead returns ``Base.LinearSlow()`` (by default), this means that the array intrinsically accesses its elements with indices specified for every dimension. Since converting a linear index to multiple indexing subscripts is typically very expensive, this provides a traits-based mechanism to enable efficient generic code for all array types.
Expand Down
6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,12 @@ R = CartesianRange((0,3))
R = CartesianRange((3,0))
@test done(R, start(R)) == true

@test eachindex(Base.LinearSlow(),zeros(3),zeros(2,2),zeros(2,2,2),zeros(2,2)) == CartesianRange((3,2,2))
@test eachindex(Base.LinearFast(),zeros(3),zeros(2,2),zeros(2,2,2),zeros(2,2)) == 1:8
@test eachindex(zeros(3),sub(zeros(3,3),1:2,1:2),zeros(2,2,2),zeros(2,2)) == CartesianRange((3,2,2))
@test eachindex(zeros(3),zeros(2,2),zeros(2,2,2),zeros(2,2)) == 1:8


#rotates

a = [1 0 0; 0 0 0]
Expand Down