-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmpi_sor.py
210 lines (172 loc) · 5.21 KB
/
mpi_sor.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
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
import time, sys
import numpy as np
from scipy.sparse import csc_matrix
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = MPI.COMM_WORLD.Get_rank()
size = MPI.COMM_WORLD.Get_size()
if rank ==0:
start = time.time()
def run_exact(A, b):
A = A.todense()
x = np.dot(np.linalg.inv(A), b)
print "x=", x
def my_SOR(D, L, U, colsL, colsU, b, A, rank, size, error):
"""
solving Ax = b to find x
input parameters:
error - maximum acceptable error
A - csr matrix for fast norm calculation
"""
n = 0 #all rows
privateN = 0 #n-rows for each process
w = 1.6 #omega
myValues = [] # list for private process ranges
if rank == 0:
n = len(D)
#-----------------------------------------------------
n = comm.bcast(n, root = 0)
colsL = comm.bcast(colsL, root = 0)
colsU = comm.bcast(colsU, root = 0)
L = comm.bcast(L, root = 0)
U = comm.bcast(U, root = 0)
b = comm.bcast(b, root = 0)
A = comm.bcast(A, root = 0)
#------------------------------------------------------
if rank == 0:
ranges = compute_range(n, size)
for i in xrange(1, size):
comm.send(ranges[i:i+2], dest = i, tag = i)
myValues = [0]
myValues.append(ranges[1])
else:
myValues = comm.recv(source = 0, tag = rank)
#sending diagonal
if rank == 0:
for i in xrange(1, size):
comm.send(D[ranges[i]:ranges[i+1]], dest = i, tag = i)
else:
D = comm.recv(0, tag = rank)
#---------------------------------------------------------
x = np.zeros(n)
oldX = np.copy(x)
f = int(myValues[0])
l = int(myValues[1])
privateN = l-f
for iteration in xrange(100):
s = np.zeros(privateN)
for row in xrange(privateN):
for j in xrange(len(L[row+f])):
s[row] += L[row+f][j] * x[colsL[row+f][j]]
for j in xrange(len(U[row+f])):
s[row] += U[row+f][j] * oldX[colsU[row+f][j]]
x[row+f] += w * ((b[row+f]-s[row]) / D[row] - x[row+f])
if rank != 0:
comm.send(x[f:l], dest = 0, tag = rank)
else:
for i in xrange(1, size):
tmpx = comm.recv(source = i, tag = i)
x[ ranges[i] : ranges[i+1]] = tmpx[0:len(tmpx)]
x = comm.bcast(x, root = 0)
if my_residual(A, x, b) < error:
break
oldX = np.copy(x)
if rank == 0:
print "Error %f" % my_residual(A, x, b)
return x
else:
exit(0)
def my_residual(A,x,b):
return np.linalg.norm(b-A.dot(x))
#returns non-zero elements and diagonal(1 row in values = 1 row in full matrix)
def organize_values(A, col, rows):
n = len(col)
L = []
U = []
colsL = []
colsU = []
D = np.zeros(n-1)
for i in xrange(n-1):
L.append([])
U.append([])
colsL.append([])
colsU.append([])
for i in xrange(n-1):
for j in xrange(col[i], col[i+1]):
if i < rows[j]:
L[rows[j]].append(A[j])
colsL[rows[j]].append(i)
elif i > rows[j]:
U[rows[j]].append(A[j])
colsU[rows[j]].append(i)
else:
D[rows[j]] = A[j]
return (D,L,U,colsL,colsU)
#sets matrix ranges for each process
def compute_range(n, size):
rangeList = np.zeros(size+1)
elems = n/size
rest = n%size
j = 0
for i in xrange(0,size):
if i<rest:
rangeList[i+1] = int((i+1)*elems+1+j)
j += 1
else:
rangeList[i+1] = int((i+1)*elems+j)
return rangeList
#-----------------------------------------------------------
#parsing input
#------------------------------------------------------------
if len(sys.argv) < 4:
if rank == 0:
print """Usage:
mpiexec -np 2 python mpi_sor.py <matrix_filename> <vector_filename> <max_error>"""
exit(0)
#-------------------------------------------------------------
#reading data from files
#-------------------------------------------------------------
if rank == 0:
with open(sys.argv[1], 'r') as f:
f.readline()
val_line = f.readline()
ind_line = f.readline()
ptr_line = f.readline()
dataA = np.fromstring(val_line[6:-2], sep = " ", dtype=float)
indicesA = np.fromstring(ind_line[9:-2], sep = " ", dtype=int)
indicesA -=1
indptrA = np.fromstring(ptr_line[9:-2], sep = " ", dtype=int)
indptrA -= 1
#required for scipy csc_matrix format
indptrA = np.append(indptrA, len(dataA))
with open(sys.argv[2], 'r') as f:
f.readline()
val_line = f.readline()
ind_line = f.readline()
dataB = np.fromstring(val_line[6:-3], sep = " ", dtype=float)
indicesB = np.fromstring(ind_line[9:-2], sep = " ", dtype=int)
indicesB -=1
A = csc_matrix((dataA, indicesA, indptrA))
A = A.tocsr() # used to check norm
b = np.zeros(A.shape[0])
b[indicesB] = dataB
(D, L, U, colsL, colsU) = organize_values(dataA, indptrA, indicesA)
#initializing values for ranks!=0
else:
D = []
L = []
U = []
colsL = []
colsU = []
A = []
b = []
#--------------------------------
#finally running SOR
#--------------------------------
x = my_SOR(D, L, U, colsL, colsU, b, A,rank, size, error=float(sys.argv[3]))
end = time.time()
print "Czas %f s" % (end-start)
if rank == 0:
with open('Xsolutions', 'w') as solX:
for item in x:
print >> solX, item