-
-
Notifications
You must be signed in to change notification settings - Fork 76
DArray: CAQR and it's subroutines and helpers #658
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
base: master
Are you sure you want to change the base?
Changes from all commits
50c41e7
f7f7c92
22a3422
d75e738
c1f44d6
40345b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import Base: cat | ||
import Random: MersenneTwister | ||
export partition | ||
import LinearAlgebra: UniformScaling | ||
|
||
mutable struct AllocateArray{T,N} <: ArrayOp{T,N} | ||
eltype::Type{T} | ||
|
@@ -83,17 +84,15 @@ function stage(ctx, a::AllocateArray) | |
chunks = map(CartesianIndices(a.domainchunks)) do I | ||
x = a.domainchunks[I] | ||
i = LinearIndices(a.domainchunks)[I] | ||
args = a.want_index ? (i, size(x)) : (size(x),) | ||
|
||
if isnothing(a.procgrid) | ||
scope = get_compute_scope() | ||
else | ||
scope = ExactScope(a.procgrid[CartesianIndex(mod1.(Tuple(I), size(a.procgrid))...)]) | ||
end | ||
if a.want_index | ||
Dagger.@spawn compute_scope=scope allocate_array(a.f, a.eltype, i, args...) | ||
Dagger.@spawn compute_scope=scope allocate_array(a.f, a.eltype, i, size(x)) | ||
else | ||
Dagger.@spawn compute_scope=scope allocate_array(a.f, a.eltype, args...) | ||
Dagger.@spawn compute_scope=scope allocate_array(a.f, a.eltype, size(x)) | ||
end | ||
end | ||
return DArray(a.eltype, a.domain, a.domainchunks, chunks, a.partitioning) | ||
|
@@ -159,6 +158,7 @@ Base.zeros(p::BlocksOrAuto, dims::Dims; assignment::AssignmentType = :arbitrary) | |
Base.zeros(::AutoBlocks, eltype::Type, dims::Dims; assignment::AssignmentType = :arbitrary) = | ||
zeros(auto_blocks(dims), eltype, dims; assignment) | ||
|
||
|
||
function Base.zero(x::DArray{T,N}) where {T,N} | ||
dims = ntuple(i->x.domain.indexes[i].stop, N) | ||
sd = first(x.subdomains) | ||
|
@@ -167,6 +167,39 @@ function Base.zero(x::DArray{T,N}) where {T,N} | |
return _to_darray(a) | ||
end | ||
|
||
function _allocate_diag(i,T, _dims, subdomain) | ||
sA = zeros(T, _dims) | ||
if !isempty(intersect(subdomain.indexes[1], subdomain.indexes[2])) | ||
for j in range(1, min(_dims[1], _dims[2])) | ||
sA[j,j] = one(T) | ||
end | ||
end | ||
return sA | ||
end | ||
|
||
function DMatrix(p::BlocksOrAuto, s::UniformScaling, dims::Dims, assignment::AssignmentType = :arbitrary) | ||
d = ArrayDomain(map(x->1:x, dims)) | ||
sd = partition(p, d) | ||
T = eltype(s) | ||
a = AllocateArray(T, (i, T, _dims) -> _allocate_diag(i, T, _dims, sd[i]), true, d, partition(p, d), p, assignment) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid capturing |
||
return _to_darray(a) | ||
end | ||
DMatrix(p::BlocksOrAuto, s::UniformScaling, dims::Integer...; assignment::AssignmentType = :arbitrary) = | ||
DMatrix(p, s, dims; assignment) | ||
DMatrix(::AutoBlocks, s::UniformScaling, dims::Dims; assignment::AssignmentType = :arbitrary) = | ||
DMatrix(auto_blocks(dims), s::UniformScaling, dims; assignment) | ||
|
||
function DArray{T}(p::BlocksOrAuto, ::UndefInitializer, dims::Dims; assignment::AssignmentType = :arbitrary) where {T} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've got a PR almost done that adds |
||
d = ArrayDomain(map(x->1:x, dims)) | ||
a = AllocateArray(T, AllocateUndef{T}(), false, d, partition(p, d), p, assignment) | ||
return _to_darray(a) | ||
end | ||
|
||
DArray{T}(p::BlocksOrAuto, ::UndefInitializer, dims::Integer...; assignment::AssignmentType = :arbitrary) where {T} = | ||
DArray{T}(p, undef, dims; assignment) | ||
DArray{T}(p::AutoBlocks, ::UndefInitializer, dims::Dims; assignment::AssignmentType = :arbitrary) where {T} = | ||
DArray{T}(auto_blocks(dims), undef, dims; assignment) | ||
|
||
function Base.view(A::AbstractArray{T,N}, p::Blocks{N}) where {T,N} | ||
d = ArrayDomain(Base.index_shape(A)) | ||
dc = partition(p, d) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,9 @@ ArrayDomain((1:15), (1:80)) | |
alignfirst(a::ArrayDomain) = | ||
ArrayDomain(map(r->1:length(r), indexes(a))) | ||
|
||
alignfirst(a::CartesianIndices{N}) where N = | ||
ArrayDomain(map(r->1:length(r), a.indices)) | ||
|
||
function size(a::ArrayDomain, dim) | ||
idxs = indexes(a) | ||
length(idxs) < dim ? 1 : length(idxs[dim]) | ||
|
@@ -365,7 +368,7 @@ function group_indices(cumlength, idxs::AbstractRange) | |
end | ||
|
||
_cumsum(x::AbstractArray) = length(x) == 0 ? Int[] : cumsum(x) | ||
function lookup_parts(A::DArray, ps::AbstractArray, subdmns::DomainBlocks{N}, d::ArrayDomain{N}) where N | ||
function lookup_parts(A::DArray, ps::AbstractArray, subdmns::DomainBlocks{N}, d::ArrayDomain{N}; slice::Bool=false) where N | ||
groups = map(group_indices, subdmns.cumlength, indexes(d)) | ||
sz = map(length, groups) | ||
pieces = Array{Any}(undef, sz) | ||
|
@@ -379,15 +382,17 @@ function lookup_parts(A::DArray, ps::AbstractArray, subdmns::DomainBlocks{N}, d: | |
out_dmn = DomainBlocks(ntuple(x->1,Val(N)), out_cumlength) | ||
return pieces, out_dmn | ||
end | ||
function lookup_parts(A::DArray, ps::AbstractArray, subdmns::DomainBlocks{N}, d::ArrayDomain{S}) where {N,S} | ||
function lookup_parts(A::DArray, ps::AbstractArray, subdmns::DomainBlocks{N}, d::ArrayDomain{S}; slice::Bool=false) where {N,S} | ||
if S != 1 | ||
throw(BoundsError(A, d.indexes)) | ||
end | ||
inds = CartesianIndices(A)[d.indexes...] | ||
new_d = ntuple(i->first(inds).I[i]:last(inds).I[i], N) | ||
return lookup_parts(A, ps, subdmns, ArrayDomain(new_d)) | ||
return lookup_parts(A, ps, subdmns, ArrayDomain(new_d); slice) | ||
end | ||
|
||
lookup_parts(A::DArray, ps::AbstractArray, subdmns::DomainBlocks{N}, d::CartesianIndices; slice::Bool=false) where {N} = lookup_parts(A, ps, subdmns, ArrayDomain(d.indices); slice) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see how |
||
|
||
""" | ||
Base.fetch(c::DArray) | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
### getindex | ||||||
#get index | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let's leave this unless you have a good reason for the change |
||||||
|
||||||
const GETINDEX_CACHE = TaskLocalValue{Dict{Tuple,Any}}(()->Dict{Tuple,Any}()) | ||||||
const GETINDEX_CACHE_SIZE = ScopedValue{Int}(0) | ||||||
|
@@ -36,6 +36,7 @@ with_index_caching(f, size::Integer=1) = with(f, GETINDEX_CACHE_SIZE=>size) | |||||
# Return the value | ||||||
return part[offset_idx...] | ||||||
end | ||||||
|
||||||
function partition_for(A::DArray, idx::NTuple{N,Int}) where N | ||||||
part_idx = zeros(Int, N) | ||||||
offset_idx = zeros(Int, N) | ||||||
|
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.
Otherwise it becomes a positional argument.
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.
Can you add some basic tests for these constructors?