Repository navigation
Expand file tree
/
Copy pathMPIJoin_Send_Recive.py
More file actions
213 lines (166 loc) · 6.01 KB
/
Copy pathMPIJoin_Send_Recive.py
File metadata and controls
213 lines (166 loc) · 6.01 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
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
# __ __ _____ _____ _ _ _ _ _
# | \/ | __ \_ _| | | | | | | | | (_)
# | \ / | |__) || | | |__| | __ _ ___| |__ | | ___ _ _ __
# | |\/| | ___/ | | | __ |/ _` / __| '_ \ _ | |/ _ \| | '_ \
# | | | | | _| |_ | | | | (_| \__ \ | | | | |__| | (_) | | | | |
# |_| |_|_| |_____| |_| |_|\__,_|___/_| |_| \____/ \___/|_|_| |_|
# The MPI hash join algorithm without timing for clarity
# This algorithm makes use of the mpi4py library to implement a distributed MPI join algorithm.
# This File makes use of the point to point paradigm through the use of the send and recv commands, found within mpi
# Input execution command: python MPI_Clean.py <table1FileName> <x> <table1FileName> <x> <tableoutFileName> <BenchmarkFileName>
# Please ensure that the table1, table2 and tableout file name are in JSON format
# Please specify the name of the BenchmarkFileName and ensure that it is a .txt file
# Where x and y are the column indices to join on.
from __future__ import unicode_literals
import json
from mpi4py import MPI
from collections import defaultdict
from prettytable import PrettyTable
import time
import sys
# MPI setup
comm = MPI.COMM_WORLD
size = comm.Get_size()
workers = size - 1
rank = comm.Get_rank()
name = MPI.Get_processor_name()
root = 0
# Define global timing variables
startTimeHash = time.time()
endTimeHash = time.time()
startTimeJoin = time.time()
endTimeJoin = time.time()
startTimeHashJoinFunction = time.time()
endTimeJoinHashJoinFunction = time.time()
startTimeReadFile1 = time.time()
endTimeJoinReadFile1 = time.time()
startTimeReadFile2 = time.time()
endTimeJoinReadFile2 = time.time()
startTimeSend = time.time()
endTimeSend = time.time()
startTimeRecv = time.time()
endTimeRecv = time.time()
def hashJoin(table1, index1, table2, index2):
h = defaultdict(list)
# hash phase
startTimeHash = time.time()
for s in table1:
h[s[index1]].append(s)
endTimeHash = time.time()
# join phase
startTimeJoin = time.time()
finalSubJoin = []
for r in table2:
for s in h[r[index2]]:
finalSubJoin.append((s, r))
endTimeJoin = time.time()
return finalSubJoin
def printArray(array):
for row in array:
print(row)
def chunkify(lst,n):
return [lst[i::n] for i in range(n)]
# function not used, because the
def distribute(length, num_nodes):
div = length // num_nodes # divide and floor
rem = length % num_nodes
i = 0
node = 1
output = []
while i < length:
if rem != 0:
start = i
i = (i + div) + 1
end = i
rem = rem - 1
else:
start = i
i = i + div
end = i
output.append((start, end))
node = node + 1
return output
if len(sys.argv) != 7:
if rank == 0:
print("Please specify 4 parameters when running the program")
sys.exit()
if rank == 0:
# Start the timer
startTimeFullRun = time.time()
table1FileName = str(sys.argv[1])
table2FileName = str(sys.argv[3])
startTimeReadFile1 = time.time()
table1 = []
with open(table1FileName, 'r') as table1File:
table1 = json.load(table1File)
table2 = []
with open(table2FileName, 'r') as table2File:
table2 = json.load(table2File)
endTimeJoinReadFile1 = time.time()
elapsedTimeRead1 = str((endTimeJoinReadFile1 - startTimeReadFile1) * 1.0)
if len(table1) > len(table2):
smallerTable = 2
else:
smallerTable = 1
smallerTableLength = len(eval("table" + str(smallerTable)))
biggerTableLength = len(eval("table" + str(1+(smallerTable%2))))
chunkIndecies = distribute(biggerTableLength,size)
biggerTableName = str("table" + str(1+(smallerTable%2)))
smallerTableName = str("table" + str(smallerTable))
chunkedTable = eval(biggerTableName)[chunkIndecies[0][0]:chunkIndecies[0][1]]
completeTable = eval(smallerTableName)
startTimeSend = time.time()
for index, chunk in enumerate(chunkIndecies):
if index == 0:
continue
req = comm.isend(eval(biggerTableName)[chunk[0]:chunk[1]],dest=index,tag=0)
req.wait()
req1 = comm.isend(eval(smallerTableName),dest=index,tag=1)
req1.wait()
endTimeSend = time.time()
comm.Barrier()
if rank != 0:
req = comm.irecv(source=0,tag=0)
chunkedTable = req.wait()
req1 = comm.irecv(source=0,tag=1)
completeTable = req1.wait()
joinedResults = []
index1 = int(sys.argv[2])
index2 = int(sys.argv[4])
if len(chunkedTable) >= len(completeTable):
endTimeJoinHashJoinFunction = time.time()
joinedResults = hashJoin(completeTable,index1,chunkedTable,index2)
endTimeJoinHashJoinFunction = time.time()
if len(chunkedTable) < len(completeTable):
joinedResults = hashJoin(chunkedTable,index2,completeTable,index1)
if rank !=0:
req = comm.isend(joinedResults,dest=0,tag=rank)
req.wait()
comm.Barrier()
if rank == 0:
finalJoin = []
finalJoin.append(joinedResults)
for index in range(1,size):
req = comm.irecv(source=index,tag=index)
data = req.wait()
finalJoin.append(data)
flattenedJoin = [item for sublist in finalJoin for item in sublist]
outputFileName = sys.argv[5]
flattenedJoin = [item for sublist in finalJoin for item in sublist]
with open(outputFileName, 'w') as output_file:
json.dump(flattenedJoin, output_file)
elapsedTimeFullRun = time.time() - startTimeFullRun
elapsedTimeSend = endTimeSend - startTimeSend
x = PrettyTable()
x.field_names = ["Benchmark", "Time (s)"]
x.align["Benchmark"] = "l"
x.add_row(["Processes", size])
x.add_row(["Read Files", elapsedTimeRead1])
x.add_row(["Send", elapsedTimeSend])
x.add_row(["Total", elapsedTimeFullRun])
benchmarksFileName = sys.argv[6]
with open(benchmarksFileName, 'a+') as benchmarkFile:
benchmarkFile.write('***MPI Benchmark Results*** \n')
benchmarkFile.write(str(x))
benchmarkFile.write('\n\n')
benchmarkFile.close()