-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpi_arrays.jl
57 lines (42 loc) · 1.24 KB
/
mpi_arrays.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
include(joinpath("MPIArrays.jl", "src", "MPIArrays.jl"))
using .MPIArrays, MPI
using Random
# Set up MPI
MPI.Init()
comm = MPI.COMM_WORLD
rank = MPI.Comm_rank(comm)
size = MPI.Comm_size(comm)
println("Hello world, I am rank $(rank) of $(size) on $(gethostname())")
MPI.Barrier(comm)
# Size of the matrix
N = 30
# Create an uninitialized matrix and vector
x = MPIArray{Float64}(N)
A = MPIArray{Float64}(N,N)
# Set random values by applying the `rand!` function to each local element in x and A
forlocalpart!(rand!, x)
forlocalpart!(rand!, A)
# Make sure every process finished initializing the coefficients
sync(A, x)
b = A*x
# Print result
println("$(rank): b=$(b)")
# Vector example
y = MPIArray{Float64}(4)
# Each rank get 2 (consecutive) indices into the y-vector
index = rank*2 + 1
yblock = y[index : index + 1]
println("$(rank): $(yblock) / $([i for i in index : index + 1])")
# Get "view" into block
ymat = getblock(yblock)
# Write into view
ymat[1:2] .= rank
# Syncronize changes back to block
putblock!(ymat, yblock)
# Ensure that all ranks have completed the `putblock!` operation
MPI.Barrier(comm)
# Show output
println("$(rank): $(yblock)")
# Show global indexing
gb = GlobalBlock(ymat, yblock)
println("$(rank): gb[$(index)] = $(gb[index])")