-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement optimized methods for ReshapedArrays
- Loading branch information
Showing
7 changed files
with
162 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
module ArrayIterationPlayground | ||
|
||
import Base: getindex, setindex!, start, next, done, length, eachindex, show, parent | ||
using Base: ReshapedArray, linearindexing, LinearFast, LinearSlow | ||
import Base: getindex, setindex!, start, next, done, length, eachindex, show, parent, isless | ||
using Base: ReshapedArray, ReshapedIndex, linearindexing, LinearFast, LinearSlow, LinearIndexing | ||
using Base.PermutedDimsArrays: PermutedDimsArray | ||
|
||
export inds, index, value, stored, each, sync | ||
|
||
include("types.jl") | ||
include("core.jl") | ||
include("reshaped.jl") | ||
include("sparse.jl") | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# optimized methods for ReshapedArrays | ||
|
||
# column iteration | ||
start(iter::ContigCartIterator) = iter.columnrange.start | ||
function next(iter::ContigCartIterator, state) | ||
item, newstate = next(iter.arrayrange, state) | ||
ReshapedIndex(item), newstate | ||
end | ||
done(iter::ContigCartIterator, state) = isless(iter.columnrange.stop, state) | ||
|
||
function _contiguous_iterator{AA<:ReshapedArray}(W::ArrayIndexingWrapper{AA}, ::LinearSlow) | ||
fi, li = firstlast(W) | ||
A = parent(W) | ||
f = Base.ind2sub_rs(A.mi, fi) | ||
l = Base.ind2sub_rs(A.mi, li) | ||
ContigCartIterator(CartesianRange(inds(parent(A))), | ||
CartesianRange(CartesianIndex(f), CartesianIndex(l))) | ||
end | ||
|
||
# Branching implementation | ||
# @inline isless{N}(I1::CartesianIndex{N}, I2::CartesianIndex{N}) = _isless(I1.I, I2.I) | ||
# @inline function _isless{N}(I1::NTuple{N,Int}, I2::NTuple{N,Int}) | ||
# i1, i2 = I1[N], I2[N] | ||
# isless(i1, i2) && return true | ||
# isless(i2, i1) && return false | ||
# _isless(Base.front(I1), Base.front(I2)) | ||
# end | ||
# _isless(::Tuple{}, ::Tuple{}) = false | ||
|
||
# Select implementation | ||
# @inline isless{N}(I1::CartesianIndex{N}, I2::CartesianIndex{N}) = _isless(0, I1.I, I2.I) | ||
# @inline function _isless{N}(ret, I1::NTuple{N,Int}, I2::NTuple{N,Int}) | ||
# newret = ifelse(ret==0, icmp(I1[N], I2[N]), ret) | ||
# _isless(newret, Base.front(I1), Base.front(I2)) | ||
# end | ||
# _isless(ret, ::Tuple{}, ::Tuple{}) = ifelse(ret==1, true, false) | ||
# icmp(a, b) = ifelse(isless(a,b), 1, ifelse(a==b, 0, -1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
A = rand(3,4) | ||
AIP.checksame_inds(A, A) | ||
@test_throws DimensionMismatch AIP.checksame_inds(A, A') | ||
@test isa(AIP.storageorder(A), AIP.FirstToLast) | ||
@test isa(@inferred(AIP.storageorder(A)), AIP.FirstToLast) | ||
R = reshape(A, (2,3,2)) | ||
@test isa(AIP.storageorder(R), AIP.FirstToLast) | ||
@test isa(@inferred(AIP.storageorder(R)), AIP.FirstToLast) | ||
S = sub(A, 1:3, 1:4) | ||
R = reshape(S, (2,3,2)) | ||
@test isa(AIP.storageorder(R), AIP.FirstToLast) | ||
@test isa(@inferred(AIP.storageorder(R)), AIP.FirstToLast) | ||
B = PermutedDimsArray(A, [2,1]) | ||
@test isa(AIP.storageorder(B), AIP.OtherOrder{(2,1)}) | ||
@test isa(@inferred(AIP.storageorder(B)), AIP.OtherOrder{(2,1)}) | ||
R = reshape(B, (2,2,3)) | ||
@test isa(AIP.storageorder(R), AIP.NoOrder) | ||
@test isa(@inferred(AIP.storageorder(R)), AIP.NoOrder) | ||
|
||
@test @inferred(AIP.contiguous_index((:, :))) == AIP.Contiguous() | ||
@test @inferred(AIP.contiguous_index((:, 3))) == AIP.Contiguous() | ||
@test @inferred(AIP.contiguous_index((3, :))) == AIP.NonContiguous() | ||
@test @inferred(AIP.contiguous_index((3, 3))) == AIP.Contiguous() | ||
@test @inferred(AIP.contiguous_index((:, :, 3)) ) == AIP.Contiguous() | ||
@test @inferred(AIP.contiguous_index((:, :, 1:2))) == AIP.Contiguous() | ||
@test @inferred(AIP.contiguous_index((:, 3, :)) ) == AIP.NonContiguous() | ||
@test @inferred(AIP.contiguous_index((:, 3, 1:2))) == AIP.NonContiguous() | ||
|
||
@test isless(CartesianIndex((1,1)), CartesianIndex((2,1))) | ||
@test isless(CartesianIndex((1,1)), CartesianIndex((1,2))) | ||
@test isless(CartesianIndex((2,1)), CartesianIndex((1,2))) | ||
@test !isless(CartesianIndex((1,2)), CartesianIndex((2,1))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters