-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatherv.jl
58 lines (40 loc) · 1.51 KB
/
gatherv.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
# EXAMPLE: gatherv
# here we have 3 ranks
# Gatherv!(sendbuf, recvbuf, comm::Comm; root::Integer=0)
# Each process sends the contents of the buffer sendbuf to the root process. The root stores elements in rank order in the buffer recvbuf.
# sendbuf should be a Buffer object
using MPI
MPI.Init()
comm = MPI.COMM_WORLD
rank = MPI.Comm_rank(comm)
comm_size = MPI.Comm_size(comm)
root = 0
if rank == root
output = ones(3,6)*999 #same size, uninitialized
counts = [3,6,9] # number of elements in each rank
output_vbuf = VBuffer(output, counts) # VBuffer for gather
else
output_vbuf = VBuffer(nothing)
end
local_matrix = ones(3,rank+1)*(rank+1) # rank0: [1 rank1: [2 2 rank2: [3 3 3
# 1 2 2 3 3 3
# 1] 2 2] 3 3 3]
@show rank,local_matrix
MPI.Gatherv!(local_matrix, output_vbuf, root, comm) #Gatherv!(sendbuf, recvbuf,..), recvbuf on the root process should be a VBuffer
if rank == root
println()
println("Final matrix")
println("================")
@show output
end
# Module julia/1.8.2 loaded
# (rank, local_matrix) = (0, [1.0; 1.0; 1.0;;])
# (rank, local_matrix) = (2, [3.0 3.0 3.0; 3.0 3.0 3.0; 3.0 3.0 3.0])
# (rank, local_matrix) = (1, [2.0 2.0; 2.0 2.0; 2.0 2.0])
# Final matrix
# ================
# output = [1.0 2.0 2.0 3.0 3.0 3.0;
# 1.0 2.0 2.0 3.0 3.0 3.0;
# 1.0 2.0 2.0 3.0 3.0 3.0]
#
# Note that the tyepe must match.