-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution-process-pool.py
110 lines (103 loc) · 4.22 KB
/
Solution-process-pool.py
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
import random
import time
import threading
from multiprocessing import Pool
NArray = [16, 64, 256, 512, 1024, 2048, 4096 ]
# Writing to a file
f = open('brute-force-4-processes-pool','w')
for N in NArray:
#initialises the matrices
matrix1 = [[0 for x in range(0,N)] for y in range(0,N)]
matrix2 = [[0 for x in range(0,N)] for y in range(0,N)]
matrix3 = [[0 for x in range(0,N)] for y in range(0,N)]
resultMatrix = [[0 for x in range(0,N)] for y in range(0,N)]
M1 = [[0 for x in range(0,N)] for y in range(0,N)]
M2 = [[0 for x in range(0,N)] for y in range(0,N)]
M01 = [[0 for x in range(0,N)] for y in range(0,N)]
M02 = [[0 for x in range(0,N)] for y in range(0,N)]
def generateMatrix():
#Generate matrix 1
for x in range(0,N):
for y in range(0,N):
randomNo = round(random.uniform(0,100),2)
matrix1[x][y] = randomNo
#Generate matrix 2
for x in range(0,N):
for y in range(0,N):
randomNo = round(random.uniform(0,100),2)
matrix2[x][y] = randomNo
# Matrix multiplication in serial
def innerProduct():
for i in range(0,N):
for j in range(0,N):
for k in range(0,N):
matrix3[i][j] = matrix3[i][j]+ matrix1[i][k]*matrix2[k][j]
# Each process does its part of the work in the whole matrix and returns the resultant matrix which it computes
def calculateResult(args):
a = args[0]
b = args[1]
matrix = [[0 for x in range(0,N)] for y in range(0,N)]
for i in range(a,b):
for j in range(0,N):
for k in range(0,N):
matrix[i][j] = matrix[i][j]+ matrix1[i][k]*matrix2[k][j]
return matrix
# After all the processes complete the task, the changes made by each of the process has to be combined
# to get the final answer
def combine(matrix):
resMatrix = []
for j in range(0,N/4):
resMatrix.append(matrix[0][j])
for j in range(N/4,N/2):
resMatrix.append(matrix[1][j])
for j in range(N/2,(3*N)/4):
resMatrix.append(matrix[2][j])
for j in range((3*N)/4,N):
resMatrix.append(matrix[3][j])
return resMatrix
# Creates 4 processes and work is split across each process, each process calls calculateResult.
def innerProductParallel():
resultMatrix = []
try:
# Creates a pool of processes
p = Pool(processes = 4)
# Array of arguments that have to be passed to each of the process's calculateResult
args = [[0,N/4],[N/4,N/2],[N/2,(3*N)/4],[(3*N)/4,N]]
# matrix4 will have the matrices(return value of calculateResult) returned by each of the process
matrix4 = p.map(calculateResult,args)
# combine is called to combine the results computed by all the processes
resultMatrix = combine(matrix4)
except:
print "Error: unable to start thread"
return resultMatrix
#generates the matrix
generateMatrix()
#time taken by the brute force approach
t0 = int(round(time.time() * 1000))
innerProduct()
t1 = int(round(time.time() * 1000))
T3 = t1 - t0
#writes the result into the file and prints it on the console
print "%d elements" %(N)
f.write("%d elements\n" %(N))
print "Inner product method"
f.write("Inner product method\n")
print "The time taken for multiplication is %d milliseconds" %(T3)
f.write("The time taken for multiplication is %d milliseconds\n" %(T3))
print "\n"
f.write('\n')
#time taken by the parallel method
t6 = int(round(time.time() * 1000))
res = innerProductParallel()
t7 = int(round(time.time() * 1000))
T6 = t7 - t6
#writes the result onto the console and into the file
print "Inner product method with processes"
f.write("Inner product method with processes\n")
print "The time taken for multiplication is %d milliseconds" %(T6)
f.write("The time taken for multiplication is %d milliseconds\n" %(T6))
#prints the last element to see if the computation is proper
print matrix3[N-1][N-1]
print res[N-1][N-1]
print "\n"
f.write('\n')