-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample02_gather_array_ND.py
More file actions
56 lines (46 loc) · 1.8 KB
/
Copy pathexample02_gather_array_ND.py
File metadata and controls
56 lines (46 loc) · 1.8 KB
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
"""
Summary: A simple MPI program to demonstrate
the AutoParallelizePy utilities:
function get_subarray_ND
function gather_array_ND
class domainDecomposeND
Aims: Create a 2D array to work as our test
input data. Use get_subarray_ND for each proc
to take a chunk of the data according to some
domain decomposition scheme and use gather_array_ND
to gather all the subarrays back into another
array on the main rank which recovers the original
array.
"""
import time
start = time.time()
import numpy as np
from mpi4py import *
import AutoParallelizePy as APP
# Initialize the MPI environment
#◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈
# Init Parallel #◈
comm = MPI.COMM_WORLD #◈
size = comm.Get_size() #◈
rank = comm.Get_rank() #◈
mainrank = 0 #◈
#◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈◈
# Create the 2D input array
arrShape = [57,89]
Arr = np.arange(np.prod(arrShape)).reshape(arrShape) * np.pi
# Configure the domain decomposition scheme such that
# both axes of the 2D data is parallelized
parallel_axes = [0,1]
domDecompND = APP.domainDecomposeND(size,arrShape,parallel_axes)
# Have each proc to take a chunk of the input data
myArr = APP.get_subarray_ND(rank,domDecompND,Arr)
# Gather the subarrays back on the mainrank and compare
# with the original data
gatheredArrOnMainRank = APP.gather_array_ND(comm, rank, mainrank, domDecompND, myArr, 'float')
if rank == mainrank:
print("")
if np.all(gatheredArrOnMainRank == Arr):
print("The original data was successfully reconstructed!")
else:
print("Failed!")
print("Running example02 took ",time.time() - start, " seconds!")