-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmatrix.py
More file actions
executable file
·101 lines (78 loc) · 2.6 KB
/
Copy pathmatrix.py
File metadata and controls
executable file
·101 lines (78 loc) · 2.6 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
#!/usr/bin/env python
# FIXME: Nowhere near PEP8 compatible :(
from csp.cspthread import *
def calculateRowColumnProduct(self, A, row, B, col):
product = 0
for i in range(len(A[row])):
product += A[row][i] * B[i][col]
return product
@process
def ParcalculateRowColumnProduct(cout, A, row, B, col):
"""
readset =
writeset = cout
"""
product = 0
for i in range(len(A[row])):
product += A[row][i] * B[i][col]
cout.write((row,col,product))
class Matrix():
def __init__(self, h, k):
self.matrix = []
for i in range(h):
row = []
for j in range(k):
row.append(0)
self.matrix.append(row)
def Multiply(self, mb):
b = mb.matrix
a = self.matrix
if len(a[0]) != len(b):
raise Exception()
return
mat = Matrix(len(a),len(b[0]))
for i in range(len(a)) :
for j in range(len(b[0])):
mat.matrix[i][j] = calculateRowColumnProduct(self,a,i,b,j)
return mat
def ParMultiply(self, mb):
b = mb.matrix
a = self.matrix
if len(a[0]) != len(b):
raise Exception()
return
procs = []
chnls = []
mat = Matrix(len(a),len(b[0]))
for i in range(len(a)) :
for j in range(len(b[0])):
ch = Channel()
chnls.append(ch);
procs.append(ParcalculateRowColumnProduct(ch,a,i,b,j))
p = Par(*procs);
p.start();
alt = Alt(*chnls)
for i in range(len(chnls)):
a,b,ans = alt.select()
mat.matrix[a][b] = ans
alt.poison()
return mat
def createID(self):
for i in range(len(self.matrix)) :
for j in range(len(self.matrix[0])):
if i == j:
self.matrix[i][j] = 1
else :
self.matrix[i][j] = 0
def printMatrix(self):
print(self.matrix)
if __name__ == '__main__':
i = Matrix(3,3)
g = Matrix(3,3)
i.createID()
g.createID()
j = i.Multiply(g)
j.printMatrix();
j = i.ParMultiply(g)
j.printMatrix()
print("")