-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished Implementation of Reshape and Permutedims (#8)
* Implementation of Reshape and Permutedims * Add unit tests * Make definition a macro
- Loading branch information
Showing
4 changed files
with
206 additions
and
55 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,7 +1,7 @@ | ||
name = "DiskArrays" | ||
uuid = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3" | ||
authors = ["Fabian Gans <[email protected]>"] | ||
version = "0.1.0" | ||
version = "0.1.1" | ||
|
||
[deps] | ||
|
||
|
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,93 @@ | ||
import Base: _throw_dmrs | ||
import DiskArrays: splittuple | ||
#Reshaping is really not trivial, because the access pattern would completely change for reshaped arrays, | ||
#rectangles would not remain rectangles in the parent array. However, we can support the case where only | ||
#singleton dimensions are added, later we could allow more special cases like joining two dimensions to one | ||
struct ReshapedDiskArray{T,N,P<:AbstractArray,M} <: AbstractDiskArray{T,N} | ||
parent::P | ||
keepdim::NTuple{M,Int} | ||
newsize::NTuple{N,Int} | ||
end | ||
Base.size(r::ReshapedDiskArray) = r.newsize | ||
haschunks(a::ReshapedDiskArray) = haschunks(a.parent) | ||
eachchunk(a::ReshapedDiskArray{<:Any,N}) where N = map(eachchunk(a.parent)) do j | ||
r = toRanges(j) | ||
inow::Int = 0 | ||
ntuple(N) do i | ||
if i in a.keepdim | ||
inow+=1 | ||
r[inow] | ||
else | ||
1:1 | ||
end | ||
end::NTuple{N,UnitRange{Int}} | ||
end | ||
function DiskArrays.readblock!(a::ReshapedDiskArray,aout,i...) | ||
inew = tuple_tuple_getindex(i,a.keepdim) | ||
DiskArrays.readblock!(a.parent,reshape(aout,map(length,inew)),inew...) | ||
nothing | ||
end | ||
tuple_tuple_getindex(t,i) = _ttgi((),t,i...) | ||
_ttgi(o,t,i1,irest...) = _ttgi((o...,t[i1]),t,irest...) | ||
_ttgi(o,t,i1) = (o...,t[i1]) | ||
function DiskArrays.writeblock!(a::ReshapedDiskArray,v,i...) | ||
inew = tuple_tuple_getindex(i,a.keepdim) | ||
DiskArrays.writeblock!(a.parent,reshape(v,map(length,inew)),inew...) | ||
nothing | ||
end | ||
function reshape_disk(parent, dims) | ||
n = length(parent) | ||
ndims(parent) > length(dims) && error("For DiskArrays, reshape is restricted to adding singleton dimensions") | ||
prod(dims) == n || _throw_dmrs(n, "size", dims) | ||
ipassed::Int=0 | ||
s = size(parent) | ||
keepdim = map(s) do snow | ||
while true | ||
ipassed += 1 | ||
d = dims[ipassed] | ||
if d>1 | ||
d != snow && error("For DiskArrays, reshape is restricted to adding singleton dimensions") | ||
return ipassed | ||
end | ||
end | ||
end | ||
ReshapedDiskArray{eltype(parent),length(dims),typeof(parent),ndims(parent)}(parent,keepdim,dims) | ||
end | ||
|
||
import Base: _throw_dmrs | ||
import DiskArrays: splittuple, toRanges | ||
import Base.PermutedDimsArrays: genperm | ||
struct PermutedDiskArray{T,N,P<:PermutedDimsArray} <: AbstractDiskArray{T,N} | ||
a::P | ||
end | ||
function permutedims_disk(a,perm) | ||
pd = PermutedDimsArray(a,perm) | ||
PermutedDiskArray{eltype(a),ndims(a),typeof(pd)}(pd) | ||
end | ||
Base.size(r::PermutedDiskArray) = size(r.a) | ||
haschunks(a::PermutedDiskArray) = haschunks(a.a.parent) | ||
eachchunk(a::PermutedDiskArray{T,N,<:PermutedDimsArray{T,N,perm,iperm}}) where {T,N,perm,iperm} = map(j->CartesianIndices(genperm(toRanges(j),perm)),eachchunk(a.a.parent)) | ||
function DiskArrays.readblock!(a::PermutedDiskArray{T,N,<:PermutedDimsArray{T,N,perm,iperm}},aout,i...) where {T,N,perm,iperm} | ||
inew = genperm(i, iperm) | ||
DiskArrays.readblock!(a.a.parent,PermutedDimsArray(aout,iperm),inew...) | ||
nothing | ||
end | ||
function DiskArrays.writeblock!(a::PermutedDiskArray{T,N,<:PermutedDimsArray{T,N,perm,iperm}},v,i...) where {T,N,perm,iperm} | ||
inew = genperm(i, iperm) | ||
DiskArrays.writeblock!(a.a.parent,PermutedDimsArray(v,iperm),inew...) | ||
nothing | ||
end | ||
|
||
macro implement_reshape(t) | ||
quote | ||
Base._reshape(parent::$t, dims::NTuple{N,Int}) where N = | ||
reshape_disk(parent, dims) | ||
end | ||
end | ||
|
||
macro implement_permutedims(t) | ||
quote | ||
Base.permutedims(parent::$t, perm) = | ||
permutedims_disk(parent, perm) | ||
end | ||
end |
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
f84f40a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
f84f40a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/9703
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via: