Skip to content

Commit dfac52d

Browse files
Apply Black code formatting
1 parent 5525e70 commit dfac52d

File tree

3 files changed

+93
-105
lines changed

3 files changed

+93
-105
lines changed

scripts/mpi_running.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#from pyclassify.parallel_tridiag_eigen import parallel_eigen
1+
# from pyclassify.parallel_tridiag_eigen import parallel_eigen
22
from pyclassify import parallel_tridiag_eigen
33
from time import time
44
import numpy as np
@@ -10,41 +10,36 @@ def parallel_eig(d, off_d, nprocs):
1010

1111
print("inside parallel_eig")
1212
comm = MPI.COMM_SELF.Spawn(
13-
sys.executable,
14-
args=['parallel_tridiag_eigen.py'],
15-
maxprocs=nprocs
13+
sys.executable, args=["parallel_tridiag_eigen.py"], maxprocs=nprocs
1614
)
1715
print("sending")
1816
comm.send(d, dest=0, tag=11)
1917
comm.send(off_d, dest=0, tag=12)
20-
print("Sent data to child, waiting for results..."); sys.stdout.flush()
21-
22-
18+
print("Sent data to child, waiting for results...")
19+
sys.stdout.flush()
2320

2421
# Receive result from rank 0 of child group
2522
eigvals = comm.recv(source=0, tag=22)
26-
eigvecs= comm.recv(source=0, tag=23)
27-
delta_t= comm.recv(source=0, tag=24)
23+
eigvecs = comm.recv(source=0, tag=23)
24+
delta_t = comm.recv(source=0, tag=24)
2825

2926
comm.Disconnect()
3027
return eigvals, eigvecs, delta_t
3128

3229

33-
34-
n=1000
35-
nprocs=4
36-
#np.random.seed(42)
37-
d = np.random.rand(n)*2
38-
off_d=np.random.rand(n-1)/2
30+
n = 1000
31+
nprocs = 4
32+
# np.random.seed(42)
33+
d = np.random.rand(n) * 2
34+
off_d = np.random.rand(n - 1) / 2
3935
print("Starting")
40-
file_name="Profiling_numba.txt"
41-
t_s=time()
42-
eigvals, eigvecs, delta_t=parallel_eig(d, off_d, nprocs)
43-
t_e=time()
44-
45-
T=np.diag(d) + np.diag(off_d, 1) + np.diag(off_d, -1)
46-
npEig_val, _ =np.linalg.eigh(T)
47-
print(np.linalg.norm(np.abs(npEig_val-eigvals), np.inf))
36+
file_name = "Profiling_numba.txt"
37+
t_s = time()
38+
eigvals, eigvecs, delta_t = parallel_eig(d, off_d, nprocs)
39+
t_e = time()
40+
41+
T = np.diag(d) + np.diag(off_d, 1) + np.diag(off_d, -1)
42+
npEig_val, _ = np.linalg.eigh(T)
43+
print(np.linalg.norm(np.abs(npEig_val - eigvals), np.inf))
4844
with open(file_name, "a") as f:
4945
f.write(f"{delta_t: .4e}")
50-

scripts/profiling_solver.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,21 @@
4040
# QREig_val=np.array(QREig_val)
4141
# QREig_vec=QREig_vec[:, index_sort]
4242
# QREig_val=QREig_val[index_sort]
43-
# print(f"The maximum of the difference between the eigenvalue computed by numpy on A and the developed QR algorithm computed"
43+
# print(f"The maximum of the difference between the eigenvalue computed by numpy on A and the developed QR algorithm computed"
4444
# f"on the Matrix T is {np.linalg.norm(npEig_val-QREig_val, np.inf)}")
4545

4646

47-
4847
# def check_column_directions(A, B):
4948
# n = A.shape[1]
5049
# for i in range(n):
5150
# # Normalize the vectors
52-
# a = A[:, i]
53-
# b = B[:, i]
51+
# a = A[:, i]
52+
# b = B[:, i]
5453
# dot = np.dot(a, b)
5554
# # Should be close to 1 or -1
5655
# if dot<0:
57-
# B[:, i] = -B[:, i]
58-
56+
# B[:, i] = -B[:, i]
57+
5958

6059
# check_column_directions(QREig_vec, LaEig_vec )
6160
# print(f"The maximum absoulute error among the components of all the eigenvectors of T is {np.max(np.abs(QREig_vec - LaEig_vec))}")
@@ -142,33 +141,35 @@
142141

143142
# #Time scaling QR
144143

145-
i_max=12
144+
i_max = 12
146145

147-
time_vector=np.array([])
148-
error_eigenvalue=np.array([])
146+
time_vector = np.array([])
147+
error_eigenvalue = np.array([])
149148
for i in range(3, i_max):
150-
n=2**i
151-
#np.random.seed(42)
149+
n = 2**i
150+
# np.random.seed(42)
152151
d = np.random.rand(n)
153-
off_d=np.random.rand(n-1)
154-
T=np.diag(d) + np.diag(off_d, 1) + np.diag(off_d, -1)
155-
npEig_val, _ =np.linalg.eigh(T)
156-
t_s=time()
157-
QREig_val, QREig_vec= QR_algorithm(d, off_d)
158-
t_e=time()
159-
index_sort=np.argsort(QREig_val)
160-
QREig_vec=np.array(QREig_vec)
161-
QREig_val=np.array(QREig_val)
162-
QREig_vec=QREig_vec[:, index_sort]
163-
QREig_val=QREig_val[index_sort]
164-
error_eigenvalue=np.append(error_eigenvalue, np.linalg.norm(np.abs(npEig_val-QREig_val), np.inf))
165-
time_vector=np.append(time_vector, t_e - t_s)
152+
off_d = np.random.rand(n - 1)
153+
T = np.diag(d) + np.diag(off_d, 1) + np.diag(off_d, -1)
154+
npEig_val, _ = np.linalg.eigh(T)
155+
t_s = time()
156+
QREig_val, QREig_vec = QR_algorithm(d, off_d)
157+
t_e = time()
158+
index_sort = np.argsort(QREig_val)
159+
QREig_vec = np.array(QREig_vec)
160+
QREig_val = np.array(QREig_val)
161+
QREig_vec = QREig_vec[:, index_sort]
162+
QREig_val = QREig_val[index_sort]
163+
error_eigenvalue = np.append(
164+
error_eigenvalue, np.linalg.norm(np.abs(npEig_val - QREig_val), np.inf)
165+
)
166+
time_vector = np.append(time_vector, t_e - t_s)
166167

167168
print(error_eigenvalue)
168-
p=np.polyfit([2**i for i in range(3, i_max)], time_vector, 3)
169+
p = np.polyfit([2**i for i in range(3, i_max)], time_vector, 3)
169170
print(p)
170-
plt.plot([2**i for i in range(3, i_max)], time_vector, 'ko')
171-
x=np.linspace(2**3, 2**(i_max-1))
171+
plt.plot([2**i for i in range(3, i_max)], time_vector, "ko")
172+
x = np.linspace(2**3, 2 ** (i_max - 1))
172173
plt.plot(x, np.polyval(p, x))
173174
plt.show()
174175

@@ -211,4 +212,4 @@
211212
# axs[1].grid(True, which="both")
212213
# axs[1].set_xlabel('Tolerance ($\epsilon$)', fontsize=15)
213214
# plt.tight_layout()
214-
# plt.show()
215+
# plt.show()

0 commit comments

Comments
 (0)