-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMPIDistributor.jl
615 lines (480 loc) · 17 KB
/
MPIDistributor.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import MPI
using Serialization
export MPIDistributor
"""
MPIDistributor{GID, PID, LID}(comm::MPIComm{GID, PID, LID})
Creates an Distributor to work with MPIComm. Created by
createDistributor(::MPIComm{GID, PID, LID})
"""
mutable struct MPIDistributor{GID <: Integer, PID <: Integer, LID <: Integer} <: Distributor{GID, PID, LID}
comm::MPIComm{GID, PID, LID}
lengths_to::Vector{GID}
procs_to::Vector{PID}
indices_to::Vector{GID}
lengths_from::Vector{GID}
procs_from::Vector{PID}
indices_from::Vector{GID}
resized::Bool
sizes::Vector{GID}
sizes_to::Vector{GID}
starts_to::Vector{GID}
#starts_to_ptr::Array{Integer}
#indices_to_ptr::Array{Integer}
sizes_from::Vector{GID}
starts_from::Vector{GID}
#sizes_from_ptr::Array{Integer}
#starts_from_ptr::Array{Integer}
numRecvs::GID
numSends::GID
numExports::GID
selfMsg::GID
maxSendLength::GID
totalRecvLength::GID
request::Vector{MPI.Request}
status::Vector{MPI.Status}
#sendArray::Array{UInt8}
planReverse::Union{MPIDistributor{GID, PID, LID}, Nothing}
importObjs::Union{Vector{<:Vector}, Nothing}
datatype::DataType
#never seem to be used
#lastRoundBytesSend::Integer
#lastRoundBytesRecv::Integer
function MPIDistributor(comm::MPIComm{GID, PID, LID}) where GID <: Integer where PID <: Integer where LID <: Integer
new{GID, PID, LID}(comm, [], [], [], [], [], [], false, [], [], [], [], [],
0, 0, 0, 0, 0, 0, [], [], nothing,
nothing, Nothing)
end
end
#### internal methods ####
function createSendStructure(dist::MPIDistributor{GID, PID, LID}, pid::PID,
nProcs::PID, exportPIDs::AbstractArray{PID}
) where {GID <: Integer, PID <: Integer, LID <: Integer}
numExports = length(exportPIDs)
dist.numExports = numExports
starts = zeros(GID, nProcs+1)
nactive = 0
noSendBuff = true
numDeadIndices::GID = 0 #for GIDs not owned by any processors
for i = 1:numExports
if noSendBuff && i > 1 && exportPIDs[i] < exportPIDs[i-1]
noSendBuff = false
end
if exportPIDs[i] >= 1
starts[exportPIDs[i]] += 1
nactive += 1
else
numDeadIndices += 1
end
end
dist.selfMsg = starts[pid] != 0
dist.numSends = 0
if noSendBuff #grouped by processor, no send buffer or indices_to needed
for i = 1:nProcs
if starts[i] > 0
dist.numSends += 1
end
end
dist.procs_to = Vector{PID}(undef, dist.numSends)
dist.starts_to = Vector{GID}(undef, dist.numSends)
dist.lengths_to = Vector{GID}(undef, dist.numSends)
index = numDeadIndices+1
for i = 1:dist.numSends
dist.starts_to[i] = index
proc = exportPIDs[index]
dist.procs_to[i] = proc
index += starts[proc]
end
perm = sortperm(dist.procs_to)
dist.procs_to = dist.procs_to[perm]
dist.starts_to = dist.starts_to[perm]
# line 430
dist.maxSendLength = 0
for i = 1:dist.numSends
proc = dist.procs_to[i]
dist.lengths_to[i] = starts[proc]
if (proc != pid) && (dist.lengths_to[i] > dist.maxSendLength)
maxSendLength = dist.lengths_to[i]
end
end
else #not grouped by processor, need send buffer and indices_to
if starts[1] != 0
dist.numSends = 1
end
for i = 2:nProcs
if starts[i] != 0
dist.numSends += 1
end
starts[i] += starts[i-1]
end
for i = nProcs:-1:2
starts[i] = starts[i-1] + 1
end
starts[1] = 1
if nactive > 0
dist.indices_to = Array{GID, 1}(undef, nactive)
end
for i = 1:numExports
if exportPIDs[i] >= 1
dist.indices_to[starts[exportPIDs[i]]] = i
starts[exportPIDs[i]] += 1
end
end
#reconstruct starts array to index into indices_to
for i = nProcs:-1:2
starts[i] = starts[i-1]
end
starts[1] = 1
starts[nProcs+1] = nactive+1
if dist.numSends > 0
dist.lengths_to = Vector{GID}(undef, dist.numSends)
dist.procs_to = Vector{PID}(undef, dist.numSends)
dist.starts_to = Vector{GID}(undef, dist.numSends)
end
j::GID = 1
dist.maxSendLength = 0
for i = 1:nProcs
if starts[i+1] != starts[i]
dist.lengths_to[j] = starts[i+1] - starts[i]
dist.starts_to[j] = starts[i]
if (i != pid) && (dist.lengths_to[j] > dist.maxSendLength)
dist.maxSendLength = dist.lengths_to[j]
end
dist.procs_to[j] = i
j += 1
end
end
end
dist.numSends -= dist.selfMsg
dist
end
function computeRecvs(dist::MPIDistributor{GID, PID, LID}, myProc::PID, nProcs::PID) where GID <: Integer where PID <: Integer where LID <: Integer
msgCount = zeros(Int, nProcs)
for proc in dist.procs_to
msgCount[proc] += 1
end
#bug fix for reduce-scatter bug applied since no reduce_scatter is present in julia's MPI
rawCounts = MPI.Reduce(msgCount, MPI.SUM, 0, dist.comm.mpiComm)
if rawCounts isa Nothing
counts = Int[]
else
counts = rawCounts
end
totalRecvs = MPI.Scatter(counts, 1, 0, dist.comm.mpiComm)[1]
dist.lengths_from = zeros(Int, totalRecvs)
dist.procs_from = zeros(PID, totalRecvs)
#using NEW_COMM_PATTERN (see line 590)
if dist.request == []
dist.request = Vector{MPI.Request}(undef, totalRecvs - dist.selfMsg)
end
#line 616
lengthWrappers = [Array{Int, 1}(undef, 1) for i in 1:(totalRecvs - dist.selfMsg)]
for i = 1:(totalRecvs - dist.selfMsg)
dist.request[i] = MPI.Irecv!(lengthWrappers[i], MPI.ANY_SOURCE, MPI.ANY_TAG, dist.comm.mpiComm)
end
barrier(dist.comm)
for i = 1:(dist.numSends+dist.selfMsg)
if dist.procs_to[i] != myProc
#have to use Rsend in MPIUtil
MPI_Rsend(dist.lengths_to[i], dist.procs_to[i]-1, 1, dist.comm.mpiComm)
else
dist.lengths_from[totalRecvs] = dist.lengths_to[i]
dist.procs_from[totalRecvs] = myProc
end
end
if totalRecvs > dist.selfMsg
dist.status = MPI.Waitall!(dist.request)
end
for i = 1:(totalRecvs - dist.selfMsg)
dist.lengths_from[i] = lengthWrappers[i][1]
end
for i = 1:(totalRecvs - dist.selfMsg)
dist.procs_from[i] = MPI.Get_source(dist.status[i])+1
end
perm = sortperm(dist.procs_from)
dist.procs_from = dist.procs_from[perm]
dist.lengths_from = dist.lengths_from[perm]
dist.starts_from = Vector{GID}(undef, totalRecvs)
j = GID(1)
for i = 1:totalRecvs
dist.starts_from[i] = j
j += dist.lengths_from[i]
end
dist.totalRecvLength = 0
for i = 1:totalRecvs
dist.totalRecvLength += dist.lengths_from[i]
end
dist.numRecvs = totalRecvs - dist.selfMsg
dist
end
function computeSends(dist::MPIDistributor{GID, PID, LID},
remoteGIDs::AbstractArray{GID, 1}, remotePIDs::AbstractArray{PID, 1}
)::Tuple{AbstractArray{GID, 1}, AbstractArray{PID, 1}
} where {GID <:Integer, PID <:Integer, LID <:Integer}
numImports = length(remoteGIDs)
tmpPlan = MPIDistributor(dist.comm)
importObjs = Vector{Tuple{GID, PID}}(undef, numImports)
for i = 1:numImports
importObjs[i] = (remoteGIDs[i], myPid(dist.comm))#remotePIDs[i])
end
numExports = createFromSends(tmpPlan, copy(remotePIDs))
exportIDs = Vector{GID}(undef, numExports)
exportProcs = Vector{PID}(undef, numExports)
exportObjs = resolve(tmpPlan, importObjs)
for i = 1:numExports
exportIDs[i] = exportObjs[i][1]
exportProcs[i] = exportObjs[i][2]
end
(exportIDs, exportProcs)
end
"""
Creates a reverse distributor for the given MPIDistributor
"""
function createReverseDistributor(dist::MPIDistributor{GID, PID, LID}
) where {GID <: Integer, PID <: Integer, LID <: Integer}
myProc = myPid(dist.comm)
if dist.planReverse === nothing
totalSendLength = reduce(+, dist.lengths_to)
maxRecvLength::GID = 0
for i = 1:dist.numRecvs
if dist.procs_from[i] != myProc
maxRecvLength = max(maxRecvLength, dist.lengths_from[i])
end
end
reverse = MPIDistributor(dist.comm)
dist.planReverse = reverse
reverse.lengths_to = dist.lengths_from
reverse.procs_to = dist.procs_from
reverse.indices_to = dist.indices_from
reverse.starts_to = dist.starts_from
reverse.lengths_from = dist.lengths_to
reverse.procs_from = dist.procs_to
reverse.indices_from = dist.indices_to
reverse.starts_from = dist.starts_to
reverse.numSends = dist.numRecvs
reverse.numRecvs = dist.numSends
reverse.selfMsg = dist.selfMsg
reverse.maxSendLength = maxRecvLength
reverse.totalRecvLength = totalSendLength
reverse.request = Vector{MPI.Request}(undef, reverse.numRecvs)
reverse.status = Vector{MPI.Status}(undef, reverse.numRecvs)
end
nothing
end
#### Internal Utilities ####
"""
arraytobytes(value::Vector{T})::Vector{UInt8}
Converts the given vector to a list of bytes.
"""
function arraytobytes(value::AbstractVector{T}) where T
if isbitstype(T)
# if T is a bits type, then just use the buffer as it
sz = sizeof(value)
bytes_ptr = convert(Ptr{UInt8}, Base.unsafe_convert(Ptr{T}, value))
unsafe_wrap(Array, bytes_ptr, sz)
else
# otherwise write using an IOBuffer
buffer = IOBuffer(;sizehint=sizeof(value))
write(buffer, value)
take!(buffer)
end
end
function arraytobytes(value::AbstractVector{<:AbstractVector})
buffer = IOBuffer()
for v in value
write(buffer, UInt32(length(v)))
for elt in v
write(buffer, elt)
end
end
take!(buffer)
end
"""
bytestoarray(bytes::Vector{UInt8}, ::Type{T})::Vector{T} where T
Converts a list of bytes to a vector with contents of the given type
"""
function bytestoarray(bytes::AbstractVector{UInt8}, ::Type{T}) where T
sz = fld(length(bytes), sizeof(T))
value = Vector{T}(undef, sz)
if isbitstype(T)
bytes_ptr = convert(Ptr{T}, Base.unsafe_convert(Ptr{UInt8}, bytes))
value_ptr = Base.unsafe_convert(Ptr{T}, value)
Base.unsafe_copyto!(value_ptr, bytes_ptr, sz)
else
buffer = IOBuffer(bytes)
i = 1
while !eof(buffer)
value[i] = read(buffer, T)
i+=1
end
end
value
end
function bytestoarray(bytes::AbstractVector{UInt8}, ::Type{Vector{T}}) where T
buffer = IOBuffer(bytes)
value = Vector{Vector{T}}()
while !eof(buffer)
len = read(buffer, UInt32)
elt = Vector{T}(undef, len)
for i in 1:len
elt[i] = read(buffer, T)
end
push!(value, elt)
end
value
end
#### General Interface ####
getComm(dist::MPIDistributor) = dist.comm
#### Distributor interface ####
function createFromSends(dist::MPIDistributor{GID, PID, LID}, exportPIDs::AbstractArray{PID, 1})::Integer where GID <:Integer where PID <:Integer where LID <:Integer
pid = myPid(dist.comm)
nProcs = numProc(dist.comm)
createSendStructure(dist, pid, nProcs, exportPIDs)
computeRecvs(dist, pid, nProcs)
if dist.numRecvs > 0
if dist.request == []
dist.request = Vector{MPI.Request}(undef, dist.numRecvs)
dist.status = Vector{MPI.Status}(undef, dist.numRecvs)
end
end
dist.totalRecvLength
end
function createFromRecvs(dist::MPIDistributor{GID, PID, LID},
remoteGIDs::AbstractArray{GID, 1}, remotePIDs::AbstractArray{PID, 1}
)::Tuple{AbstractArray{GID, 1}, AbstractArray{PID, 1}
} where {GID <: Integer, PID <: Integer, LID <: Integer}
if length(remoteGIDs) != length(remotePIDs)
throw(InvalidArgumentError("remote lists must be the same length"))
end
(exportGIDs, exportPIDs) = computeSends(dist, remoteGIDs, remotePIDs)
createFromSends(dist, exportPIDs)
exportGIDs, exportPIDs
end
function resolvePosts(dist::MPIDistributor{GID, PID, LID}, exportObjs::AbstractArray{T, 1}) where {T, GID<:Integer, PID<:Integer, LID<:Integer}
myProc = myPid(dist.comm)
dist.datatype = T
nBlocks::GID = dist.numSends + dist.selfMsg
procIndex::GID = 1
while procIndex <= nBlocks && dist.procs_to[procIndex] < myProc
procIndex += 1
end
if procIndex == nBlocks
procIndex = 1
end
exportBytes = Vector{Vector{UInt8}}(undef, dist.numRecvs + dist.selfMsg)
j::GID = 1
if length(dist.indices_to) == 0 #data already grouped by processor
for i = 1:nBlocks
# note that the range needs to be signed ints because Julia doesn't treat unsigened ints as first class
exportBytes[i] = arraytobytes(view(exportObjs, Int64(j):Int64(j+dist.lengths_to[i]-1)))
j += dist.lengths_to[i]
end
else #data not grouped by proc, must be grouped first
for i = 1:nBlocks
j = dist.starts_to[i]
sendArray = Array{T}(undef, dist.lengths_to[i])
for k = 1:dist.lengths_to[i]
sendArray[k] = exportObjs[dist.indices_to[j+k-1]]
end
exportBytes[i] = arraytobytes(sendArray)
end
end
## get sizes of data begin received ##
lengthRequests = Vector{MPI.Request}(undef, dist.numRecvs)
lengths = Vector{Vector{Int}}(undef, dist.numRecvs + dist.selfMsg)
for i = 1:dist.numRecvs + dist.selfMsg
lengths[i] = Vector{Int}(undef, 1)
end
j = 1
for i = 1:dist.numRecvs + dist.selfMsg
if dist.procs_from[i] != myProc
lengthRequests[j] = MPI.Irecv!(lengths[i], dist.procs_from[i]-1, MPI.ANY_TAG, dist.comm.mpiComm)
j += 1
end
end
barrier(dist.comm)
for i = 1:dist.numSends + dist.selfMsg
p = procIndex + 1
if p > nBlocks
p -= nBlocks
end
if dist.procs_to[i] != myProc
MPI_Rsend(length(exportBytes[i]), dist.procs_to[i]-1, 2, dist.comm.mpiComm)
else
lengths[i][1] = length(exportBytes[i])
end
end
MPI.Waitall!(lengthRequests)
#at this point `lengths` should contain the sizes of incoming data
importObjs = Vector{Vector{UInt8}}(undef, dist.numRecvs+dist.selfMsg)
for i = 1:length(importObjs)
importObjs[i] = Vector{UInt8}(undef, lengths[i][1])
end
dist.importObjs = importObjs
## back to the regularly scheduled program ##
k::GID = 0
j = 0
selfRecvAddress::GID = 0
for i = 1:dist.numRecvs + dist.selfMsg
if dist.procs_from[i] != myProc
MPI.Irecv!(importObjs[i], dist.procs_from[i]-1, MPI.ANY_TAG, dist.comm.mpiComm)
else
selfRecvAddress = i
end
end
barrier(dist.comm)
#line 844
selfNum::GID = 0
# if dist.indices_to == [] #data already grouped by processor
for i = 1:nBlocks
p = i + procIndex - 1
if p > nBlocks
p -= nBlocks
end
if dist.procs_to[p] != myProc
MPI_Rsend(exportBytes[p], dist.procs_to[p]-1, 3, dist.comm.mpiComm)
else
selfNum = p
end
end
if dist.selfMsg != 0
importObjs[selfRecvAddress] = exportBytes[selfNum]
end
nothing
end
function resolveWaits(dist::MPIDistributor)::Array
barrier(dist.comm)#run into issues deserializing otherwise
if dist.numRecvs> 0
dist.status = MPI.Waitall!(dist.request)
end
if dist.importObjs === nothing
throw(InvalidStateError("Cannot resolve waits when no posts have been made"))
end
T = dist.datatype
barrier(dist.comm)#run into issues deserializing otherwise
importObjs = dist.importObjs::Vector{Vector{UInt8}}
deserializedObjs = Vector{Vector{T}}(undef, length(importObjs))
for i = 1:length(importObjs)
deserializedObjs[i] = bytestoarray(importObjs[i], T)
end
results = deserializedObjs
dist.importObjs = nothing
reduce(vcat, results; init = T[])
end
function resolveReversePosts(dist::MPIDistributor{GID, PID, LID},
exportObjs::AbstractVector{T}
) where {GID <: Integer, PID <: Integer, LID <: Integer, T}
if dist.indices_to != []
throw(InvalidStateError("Cannot do reverse comm when data is not blocked by processor"))
end
if dist.planReverse === nothing
createReverseDistributor(dist)
end
resolvePosts(dist.planReverse, exportObjs)
end
function resolveReverseWaits(dist::MPIDistributor{GID, PID, LID}
)::Vector where {GID <: Integer, PID <: Integer, LID <: Integer}
if dist.planReverse === nothing
throw(InvalidStateError("Cannot resolve reverse waits if there is no reverse plan"))
end
resolveWaits(dist.planReverse)
end