-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
ArrayDist storing distribution info for darrays. Deprecated d* functions... #5452
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
abstract ArrayDist | ||
|
||
type SharedDist{N} <: ArrayDist | ||
dims::NTuple{N,Int} | ||
pids::Vector{Int} | ||
end | ||
|
||
type ChunkedDist {N} <: ArrayDist | ||
dims::NTuple{N,Int} | ||
pids::Vector{Int} | ||
|
||
# indexes held by piece i | ||
indexes::Array{NTuple{N,Range1{Int}},N} | ||
|
||
# cuts[d][i] = first index of chunk i in dimension d | ||
cuts::Vector{Vector{Int}} | ||
|
||
dist::NTuple{N,Int} | ||
end | ||
|
||
function SharedDist(dims; pids=workers()) | ||
SharedDist{length(dims)}(dims, pids) | ||
end | ||
SharedDist(dims::Integer...; kwargs...) = SharedDist(dims; kwargs...) | ||
|
||
function ChunkedDist(dims; pids=workers(), dist=distbylargestdim(dims, length(pids))) | ||
if isa(dist, Array) | ||
dist = ntuple(x->dist[x], length(dist)) | ||
end | ||
|
||
npids = length(pids) | ||
nchunks = prod(dist) | ||
|
||
if nchunks > npids | ||
error("Total requested number of chunks is greater than the number of workers") | ||
elseif nchunks < npids | ||
pids = pids[1:nchunks] | ||
end | ||
|
||
idxs, cuts = chunk_idxs([dims...], dist) | ||
assert(dims == map(last,last(idxs))) | ||
|
||
ChunkedDist{length(dims)}(dims, pids, idxs, cuts, dist) | ||
end | ||
|
||
ChunkedDist(dims::Integer...; kwargs...) = ChunkedDist(dims; kwargs...) | ||
|
||
## chunk index utilities ## | ||
|
||
# decide how to divide each dimension | ||
# returns size of chunks array | ||
# allocates largest factor to largest dim | ||
function distbylargestdim(dims, ngroups) | ||
dims = [dims...] | ||
chunks = ones(Int, length(dims)) | ||
f = sort!(collect(keys(factor(ngroups))), rev=true) | ||
k = 1 | ||
while ngroups > 1 | ||
# repeatedly allocate largest factor to largest dim | ||
if ngroups%f[k] != 0 | ||
k += 1 | ||
if k > length(f) | ||
break | ||
end | ||
end | ||
fac = f[k] | ||
(d, dno) = findmax(dims) | ||
# resolve ties to highest dim | ||
dno = last(find(dims .== d)) | ||
if dims[dno] >= fac | ||
dims[dno] = div(dims[dno], fac) | ||
chunks[dno] *= fac | ||
end | ||
ngroups = div(ngroups,fac) | ||
end | ||
chunks | ||
end | ||
|
||
# get array of start indexes for dividing sz into nc chunks | ||
function distbylargestdim(sz::Int, nc::Int) | ||
if sz >= nc | ||
iround(linspace(1, sz+1, nc+1)) | ||
else | ||
[[1:(sz+1)], zeros(Int, nc-sz)] | ||
end | ||
end | ||
|
||
|
||
# compute indexes array for dividing dims into chunks | ||
function chunk_idxs(dims, chunks) | ||
cuts = map(distbylargestdim, dims, chunks) | ||
n = length(dims) | ||
idxs = Array(NTuple{n,Range1{Int}},chunks...) | ||
cartesianmap(tuple(chunks...)) do cidx... | ||
idxs[cidx...] = ntuple(n, i->(cuts[i][cidx[i]]:cuts[i][cidx[i]+1]-1)) | ||
end | ||
idxs, cuts | ||
end | ||
|
||
|
||
size(ad::ArrayDist) = ad.dims | ||
procs(ad::ArrayDist) = ad.pids | ||
nprocs(ad::ArrayDist) = length(ad.pids) | ||
|
||
getindex(cdist::ChunkedDist, i::Int) = cdist.indexes[i] | ||
getindex(cdist::ChunkedDist, i::Int...) = cdist.indexes[i...] | ||
|
||
dist(cdist::ChunkedDist) = cdist.dist | ||
|
||
# find which piece holds index (I...) | ||
function locate{N}(cdist::ChunkedDist{N}, I::Int...) | ||
ntuple(N, i->searchsortedlast(cdist.cuts[i], I[i])) | ||
end | ||
|
||
localpartindex(cdist::ChunkedDist) = findfirst(cdist.pids, myid()) | ||
|
||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This seems to be inconsistent with
getindex
below; i.e. the size does not give the valid range of indexes forgetindex
. That could be confusing.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.
You are right. How about
size(ChunkedDist)
returning the size of the indexes, whilesize(SharedDist)
returns the length of thepids
vector. Both describe the size of the distribution.size(DArray)
andsize(SharedArray)
will return the size of the complete array. There will not be a function to retrieve dims from anArrayDist
- folks have to usead.dims
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.
Just pushed an update.