-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImport.jl
317 lines (246 loc) · 10.2 KB
/
Import.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
export Import
export sourceMap, targetMap, distributor, isLocallyComplete
export permuteToLIDs, permuteFromLIDs, exportLIDs, remoteLIDs, remotePIDs, numSameIDs
"""
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distribution.
"""
struct Import{GID <: Integer, PID <:Integer, LID <: Integer}
importData::ImportExportData{GID, PID, LID}
#default constructor appeared to accept a pair of BlockMaps
function Import{GID, PID, LID}(
importData::ImportExportData{GID, PID, LID}) where {
GID <: Integer, PID <: Integer, LID <: Integer}
new(importData)
end
end
## Constructors ##
function Import(source::BlockMap{GID, PID, LID}, target::BlockMap{GID, PID, LID},
userRemotePIDs::AbstractArray{PID}, remoteGIDs::AbstractArray{GID},
userExportLIDs::AbstractArray{LID}, userExportPIDs::AbstractArray{PID},
useRemotePIDGID::Bool
) where {GID <: Integer, PID <: Integer, LID <: Integer}
importData = ImportExportData(source, target)
remoteLIDs = JuliaPetra.remoteLIDs(importData)
if !userRemotePIDGID
empty!(remoteGIDs)
empty!(remoteLIDs)
end
getIDSource(data, remoteGIDs, !userRemotePIDGID)
if length(remoteGIDs) > 0 && !isDistributed(source)
throw(InvalidArgumentError("Target has remote LIDs but source is not distributed globally"))
end
(remotePIDs, _) = remoteIDList(source, remoteGIDs)
remoteProcIDs = (useRemotePIDGID) ? userRemotePIDs : remotePIDs
if !(length(remoteProcIDs) == length(remoteGIDs) && length(remoteGIDs) == length(remoteLIDs))
throw(InvalidArgumentError("Size miss match on remoteProcIDs, remoteGIDs and remoteLIDs"))
end
# ensure remoteProcIDs[i], remoteGIDs[i] and remoteLIDs[i] refer to the same thing
order = sortperm(remoteProcIDs)
permute!(remoteProcIDs, order)
permute!(remoteGIDs, order)
permute!(remoteLIDs, order)
exportPIDs = Array{PID, 1}(undef, length(userExportPIDs))
exportLIDs = Array{PID, 1}(undef, length(userExportPIDs))
#need the funcitons with these names, not the variables
JuliaPetra.remoteLIDs(importData, remoteLIDs)
JuliaPetra.exportPIDs(importData, exportPIDs)
JuliaPetra.exportLIDs(importData, exportLIDs)
locallyComplete = true
for i = 1:length(userExportPIDs)
if userExportPIDs[i] == 0
locallyComplete = false
end
exportPIDs[i] = userExportPIDs[i]
exportLIDs[i] = userExportLIDs[i]
end
isLocallyComplete(importData, locallyComplete)
#TODO create and upgrade to createFromSendsAndRecvs
#createFromSendsAndRecvs(distributor(importData), exportPIDs, remoteProcIDs)
createFromRecvs(distributor(importData), remoteGIDs, remotePIDs)
Import(importData)
end
function Import(source::BlockMap{GID, PID, LID}, target::BlockMap{GID, PID, LID}, remotePIDs::Union{AbstractArray{PID}, Nothing}=nothing) where {GID <: Integer, PID <: Integer, LID <: Integer}
impor = Import{GID, PID, LID}(ImportExportData(source, target))
remoteGIDs = setupSamePermuteRemote(impor)
if distributedGlobal(source)
setupExport(impor, remoteGIDs, remotePIDs)
end
impor
end
## internal construction methods ##
function setupSamePermuteRemote(impor::Import{GID, PID, LID}) where {GID <: Integer, PID <: Integer, LID <: Integer}
data = impor.importData
remoteGIDs = Array{GID, 1}(undef, 0)
getIDSources(data, remoteGIDs)
if length(remoteLIDs(data)) != 0 && !distributedGlobal(sourceMap(impor))
isLocallyComplete(data, false)
warn("Target has remote LIDs but source is not distributed globally. " *
"Importing a submap of the target map")
end
remoteGIDs
end
function getIDSources(data, remoteGIDs, useRemotes=true)
source = sourceMap(data)
target = targetMap(data)
sourceGIDs = myGlobalElements(source)
targetGIDs = myGlobalElements(target)
numSrcGIDs = length(sourceGIDs)
numTgtGIDs = length(targetGIDs)
numGIDs = min(numSrcGIDs, numTgtGIDs)
numSameGIDs = 1
while numSameGIDs <= numGIDs && sourceGIDs[numSameGIDs] == targetGIDs[numSameGIDs]
numSameGIDs += 1
end
numSameGIDs -= 1
numSameIDs(data, numSameGIDs)
permuteToLIDs = JuliaPetra.permuteToLIDs(data)
permuteFromLIDs = JuliaPetra.permuteFromLIDs(data)
remoteLIDs = JuliaPetra.remoteLIDs(data)
for tgtLID = (numSameGIDs+1):numTgtGIDs
curTargetGID = targetGIDs[tgtLID]
srcLID = lid(source, curTargetGID)
if srcLID != 0
push!(permuteToLIDs, tgtLID)
push!(permuteFromLIDs, srcLID)
elseif useRemotes
push!(remoteGIDs, curTargetGID)
push!(remoteLIDs, tgtLID)
end
end
end
function setupExport(impor::Import{GID, PID, LID}, remoteGIDs::AbstractArray{GID}, userRemotePIDs::Union{AbstractArray{PID}, Nothing}) where {GID <: Integer, PID <: Integer, LID <: Integer}
data = impor.importData
source = sourceMap(impor)
useRemotePIDs = userRemotePIDs !== nothing
# Sanity Checks
if useRemotePIDs && length(userRemotePIDs) != length(remoteGIDs)
throw(InvalidArgumentError("remotePIDs must either be null " *
"or match the size of remoteGIDs."))
end
missingGID = 0
if !useRemotePIDs
newRemotePIDs = Array{PID, 1}(undef, length(remoteGIDs))
(remoteProcIDs, remoteLIDs) = remoteIDList(source, remoteGIDs)
for e in remoteLIDs
if e == 0
missingGID += 1
end
end
else
remoteProcIDs = userRemotePIDs
end
#line 688
if missingGID != 0
isLocallyComplete(data, false)
warn("Source map was un-able to figure out which process owns one " *
"or more of the GIDs in the list of remote GIDs. This probably " *
"means that there is at least one GID owned by some process in " *
"the target map which is not owned by any process in the source " *
"Map. (That is, the source and target maps do not contain the " *
"same set of GIDs globally")
#ignore remote GIDs that aren't owned by any process in the source Map
numInvalidRemote = missingGID
totalNumRemote = length(remoteGIDs)
if numInvalidRemote == totalNumRemote
#if all remotes are invalid, can delete them all
empty!(remoteProcIDs)
empty!(remoteGIDs)
empty!(JuliaPetra.remoteLIDs(data))
else
numValidRemote = 1
remoteLIDs = JuliaPetra.remoteLIDs(data)
for r = 1:totalNumRemote
if remoteProcIds[r] != 0
remoteProcIds[numValidRemote] = remoteProcIDs[r]
remoteGIDs[numValidRemote] = remoteGIDs[r]
remoteLIDs[numValidRemote] = remoteLIDs[r]
numValidRemote += 1
end
end
numValidRemote -= 1
if numValidRemote != totalNumRemote - numInvalidRemote
throw(InvalidStateError("numValidRemote = $numValidRemote " *
"!= totalNumRemote - numInvalidRemote " *
"= $(totalNumRemote - numInvalidRemote)"))
end
resize!(remoteProcIDs, numValidRemote)
resize!(remoteGIDs, numValidRemote)
resize!(remoteLIDs, numValidRemote)
end
end
order = sortperm(remoteProcIDs)
permute!(remoteProcIDs, order)
permute!(remoteGIDs, order)
permute!(remoteLIDs, order)
(exportGIDs, exportPIDs) = createFromRecvs(distributor(data), remoteGIDs, remoteProcIDs)
JuliaPetra.exportPIDs(data, exportPIDs)
numExportIDs = length(exportGIDs)
if numExportIDs > 0
exportLIDs = JuliaPetra.exportLIDs(data)
resize!(exportLIDs, numExportIDs)
for k in 1:numExportIDs
exportLIDs[k] = lid(source, exportGIDs[k])
end
end
end
## Getters ##
"""
Get the source map for the given ImportExportData
"""
function sourceMap(impor::Import{GID, PID, LID})::BlockMap{GID, PID, LID} where GID <: Integer where PID <:Integer where LID <: Integer
impor.importData.source
end
"""
Get the target map for the given ImportExportData
"""
function targetMap(impor::Import{GID, PID, LID})::BlockMap{GID, PID, LID} where GID <: Integer where PID <:Integer where LID <: Integer
impor.importData.target
end
"""
List of elements in the target map that are permuted.
"""
function permuteToLIDs(impor::Import{GID, PID, LID})::Array{LID} where GID <: Integer where PID <:Integer where LID <: Integer
impor.importData.permuteToLIDs
end
"""
List of elements in the source map that are permuted.
"""
function permuteFromLIDs(impor::Import{GID, PID, LID})::Array{LID} where GID <: Integer where PID <:Integer where LID <: Integer
impor.importData.permuteFromLIDs
end
"""
List of elements in the target map that are coming from other processors
"""
function remoteLIDs(impor::Import{GID, PID, LID})::Array{LID} where GID <: Integer where PID <: Integer where LID <: Integer
impor.importData.remoteLIDs
end
"""
List of elements that will be sent to other processors
"""
function exportLIDs(impor::Import{GID, PID, LID})::Array{LID} where GID <: Integer where PID <: Integer where LID <: Integer
impor.importData.exportLIDs
end
"""
List of processors to which elements will be sent `exportLID[i]` will be sent to processor `exportPIDs[i]`
"""
function exportPIDs(impor::Import{GID, PID, LID})::Array{PID} where GID <: Integer where PID <: Integer where LID <: Integer
impor.importData.exportPIDs
end
"""
Returns the number of elements that are identical between the source and target maps, up to the first different ID
"""
function numSameIDs(impor::Import{GID, PID, LID})::LID where GID <: Integer where PID <: Integer where LID <: Integer
impor.importData.numSameIDs
end
"""
Returns the distributor being used
"""
function distributor(impor::Import{GID, PID, LID})::Distributor{GID, PID, LID} where GID <: Integer where PID <: Integer where LID <: Integer
impor.importData.distributor
end
"""
Returns whether the import or export is locally complete
"""
function isLocallyComplete(impor::Import{GID, PID, LID})::Bool where GID <: Integer where PID <: Integer where LID <: Integer
impor.importData.isLocallyComplete
end