-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBasicDirectory.jl
251 lines (198 loc) · 7.78 KB
/
BasicDirectory.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
export BasicDirectory
"""
BasicDirectory(map::BlockMap)
Creates a BasicDirectory, which implements the methods of Directory with
basic implmentations
"""
mutable struct BasicDirectory{GID <: Integer, PID <:Integer, LID <: Integer} <: Directory{GID, PID, LID}
map::BlockMap{GID, PID, LID}
directoryMap::Union{BlockMap, Nothing}
procList::Vector{PID}
procListLists::Vector{Vector{PID}}
entryOnMultipleProcs::Bool
localIndexList::Vector{LID}
allMinGIDs::Vector{GID}
function BasicDirectory{GID, PID, LID}(map::BlockMap{GID, PID, LID}) where {GID, PID, LID}
if !(distributedGlobal(map))
new(map, nothing, [], [], numProc(getComm(map))!=1, [], [])
elseif linearMap(map)
commObj = getComm(map)
allMinGIDs = gatherAll(commObj, minMyGID(map))
allMinGIDs = vcat(allMinGIDs, [1+maxAllGID(map)])
entryOnMultipleProcs = length(Set(allMinGIDs)) != length(allMinGIDs)
new(map, nothing, [], [], entryOnMultipleProcs, [], allMinGIDs)
else
generateContent(
new(map, nothing, [], [], false, [], []),
map)
end
end
end
"""
internal method to assist constructor
"""
function generateContent(dir::BasicDirectory{GID, PID, LID}, map::BlockMap{GID, PID, LID}) where GID <: Integer where PID <: Integer where LID <: Integer
minAllGID = minAllGID(map)
maxAllGID = maxAllGID(map)
dirNumGlobalElements = maxAllGID - minAllGID + 1
directoryMap = BlockMap(dirNumGlobalElements, minAllGID, commObj)
dir.directoryMap = directoryMap
dirNumMyElements = numMyElements(dir.directoryMap)
if dirNumMyElements > 0
dir.procList = Vector{PID}(undef, dirNumMyElements)
dir.localIndexList = Vector{LID}(undef, dirNumMyElements)
fill!(dir.procList, -1)
fill!(dir.localIndexList, -1)
else
dir.procList = []
dir.localIndexList = []
end
map_numMyElements = numMyElements(map)
map_myGlobalElements = myGlobalElements(map)
sendProcs = remoteIDList(directoryMap, map_numMyElements, myGlobalElements(map))
distributor = createDistributor(commObj)
numRecvs = createFromSends(distributor, map_numMyElements, sendProcs)
exportElements = Array{Tuple{GID, PID, LID}}(numMyElements)
myPIDVal = myPID(commObj)
for i = 1:numMyElements
exportElements[i] = (map_myGlobalElements[i], myPIDVal, i)
end
importElements = resolve(distributor, exportElements)
for i = 1:numRecvs
currLID = lid(directoryMap, importElements[i][1])
@assert currLID > 0 //internal error
proc = importElements[i][2]
if dir.procList[currLID] >= 0
if dir.procList[currLID] != proc
if dir.procListLists == []
numProcLists = numMyElements(directoryMap)
procListLists = Vector{Vector{PID}}(undef, numProcLists)
fill!(procListLists, PID[])
end
l = procListLists[currLID]
index = searchsortedfirst(l, procList[currLID])
insert(l, index, procList[currLID])
index = searchsortedfirst(l, proc)
insert(l, index, proc)
dir.procList[currLID] = dir.procListLists[curr_LID][1]
end
else
dir.procList[currLID] = proc
end
dir.localIndexList[currLID] = importElements[i][3]
end
globalVal = maxAll(commObj, numProcLists)
dir.entryOnMultipleProcs = globalval > 0 ? true : false;
dir
end
function getDirectoryEntries(directory::BasicDirectory{GID, PID, LID}, map::BlockMap{GID, PID, LID}, globalEntries::AbstractVector{GID},
high_rank_sharing_procs::Bool)::Tuple{Vector{PID}, Vector{LID}} where GID <: Integer where PID <: Integer where LID <: Integer
numEntries = length(globalEntries)
procs = Vector{PID}(undef, numEntries)
localEntries = Vector{LID}(undef, numEntries)
if !distributedGlobal(map)
myPIDVal = myPid(getComm(map))
for i = 1:numEntries
lidVal = lid(map, globalEntries[i])
if lidVal == 0
procs[i] = 0
warn("GID $(globalEntries[i]) is not part of this map")
else
procs[i] = myPIDVal
end
localEntries[i] = lidVal
end
elseif linearMap(map)
minAllGIDVal = minAllGID(map)
maxAllGIDVal = maxAllGID(map)
numProcVal = numProc(getComm(map))
n_over_p = numGlobalElements(map)/numProcVal
allMinGIDs_list = copy(directory.allMinGIDs)
order = sortperm(allMinGIDs_list)
permute!(allMinGIDs_list, order)
for i = 1:numEntries
lid = 0
proc = 0
gid = globalEntries[i]
if gid < minAllGIDVal || gid > maxAllGIDVal
throw(InvalidArgumentError("GID=$gid out of valid range [$minAllGIDVal, $maxAllGIDVal]"))
end
#guess uniform distribution and start a little above it
proc1 = min(GID(fld(gid, max(n_over_p, 1)) + 2), numProcVal)
proc1 = 1
found = false
while proc1 >= 1 && proc1 <= numProcVal
if allMinGIDs_list[proc1] <= gid
if (gid < allMinGIDs_list[proc1+1])
found = true
break
else
proc1 += 1
end
else
proc1 -= 1
end
end
if found
proc = order[proc1]
lid = gid - allMinGIDs_list[proc1] + 1
end
procs[i] = proc
localEntries[i] = lid
end
else #general case
distributor = createDistributor(getComm(map))
dirProcs = remoteIDList(map, numEntries, globalEntries)
numMissing = 0
for i = 1:numEntries
if dirProcs[i] == 0
procs[i] = 0
localEntries[i] = 0
numMissing += 1
end
end
(sendGIDs, sendPIDs) = createFromRecvs(distrbutor, globalEntries, dirProcs)
numSends = length(sendGIDs)
if numSends > 0
exports = Array{Tuple{GID, PID, LID}}(numSends)
for i = 1:numSends
currGID = sendGIDs[i]
exports[i][1] = currGID
currLID = lid(map, currGID)
@assert currLID > 0 #internal error
if !high_rank_sharing_procs
exports[i][2] = procList[currLID]
else
numProcLists = numMyElements(directory.directoryMap)
if numProcLists > 0
num = length(directory.procListLists[currLID])
if num > 1
exports[i][2] = directory.procListLists[currLID][num]
else
exports[i][2] = directory.procList[currLID]
end
else
exports[i][2] = directory.procList[currLID]
end
end
exports[i][3] = directory.localIndexList[currLID]
end
end
numRecv = numEntries - numMissing
imports = resolve(distributor, exports)
offsets = sortperm(globalEntries)
sortedGE = globalEntries[offsets]
for i = 1:numRecv
currLID = imports[i][1]
j = searchsortedfirst(sortedGE, currLID)
if j > 0
procs[offsets[j]] = imports[i][2]
localEntries[offsets[j]] = imports[i][3]
end
end
end
(procs, localEntries)
end
function gidsAllUniquelyOwned(directory::BasicDirectory)
!directory.entryOnMultipleProcs
end