Compare commits
2 Commits
8d726fe9e6
...
5327c384ee
Author | SHA1 | Date | |
---|---|---|---|
5327c384ee | |||
02a3f2b279 |
@ -1,7 +1,7 @@
|
||||
import guan # https://py.guanjihuan.com | install: pip install --upgrade guan
|
||||
import numpy as np
|
||||
|
||||
cpu_num_array = np.arange(1, 17)
|
||||
cpu_num_array = np.arange(1, 9)
|
||||
|
||||
sh_filename = 'task'
|
||||
task_name = 'test'
|
||||
|
@ -6,22 +6,14 @@ The newest version of this code is on the web page: https://www.guanjihuan.com/a
|
||||
import numpy as np
|
||||
import time
|
||||
|
||||
n = 5000
|
||||
A = np.random.rand(n, n)
|
||||
B = np.random.rand(n, n)
|
||||
|
||||
n = 1000
|
||||
test_times = 20
|
||||
|
||||
# 矩阵行列式
|
||||
start_time = time.time()
|
||||
for _ in range(test_times):
|
||||
det_A = np.linalg.det(A)
|
||||
det_time = (time.time() - start_time)/test_times
|
||||
print(f"矩阵行列式时间: {det_time:.3f} 秒")
|
||||
|
||||
# 矩阵乘法
|
||||
start_time = time.time()
|
||||
for _ in range(test_times):
|
||||
A = np.random.rand(n, n)
|
||||
B = np.random.rand(n, n)
|
||||
C = np.dot(A, B)
|
||||
multiply_time = (time.time() - start_time)/test_times
|
||||
print(f"矩阵乘法时间: {multiply_time:.3f} 秒")
|
||||
@ -29,27 +21,15 @@ print(f"矩阵乘法时间: {multiply_time:.3f} 秒")
|
||||
# 矩阵求逆
|
||||
start_time = time.time()
|
||||
for _ in range(test_times):
|
||||
A = np.random.rand(n, n)
|
||||
inv_A = np.linalg.inv(A)
|
||||
inv_time = (time.time() - start_time)/test_times
|
||||
print(f"矩阵求逆时间: {inv_time:.3f} 秒")
|
||||
|
||||
# 矩阵的秩
|
||||
start_time = time.time()
|
||||
for _ in range(test_times):
|
||||
rank_A = np.linalg.matrix_rank(A)
|
||||
rank_time = (time.time() - start_time)/test_times
|
||||
print(f"矩阵的秩时间: {rank_time:.3f} 秒")
|
||||
|
||||
# 矩阵的特征值
|
||||
start_time = time.time()
|
||||
for _ in range(test_times):
|
||||
eigenvalues_A = np.linalg.eigvals(A)
|
||||
eigen_time = (time.time() - start_time)/test_times
|
||||
print(f"矩阵特征值时间: {eigen_time:.3f} 秒")
|
||||
|
||||
# 矩阵的特征值和特征向量
|
||||
start_time = time.time()
|
||||
for _ in range(test_times):
|
||||
A = np.random.rand(n, n)
|
||||
eigenvalues_A, eigenvector_A = np.linalg.eig(A)
|
||||
eigen_time = (time.time() - start_time)/test_times
|
||||
print(f"矩阵特征值和特征向量时间: {eigen_time:.3f} 秒")
|
@ -1,74 +0,0 @@
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.ticker import MultipleLocator
|
||||
import numpy as np
|
||||
|
||||
cpu_num_array = np.arange(1, 17)
|
||||
|
||||
# n = 5000
|
||||
|
||||
time_array_1 = [3.999, 1.679, 1.257, 0.985, 0.699, 0.562, 0.534, 0.525, 0.510, 0.424, 0.409, 0.380, 0.372, 0.339, 0.334, 0.293]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Time (s)')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.show()
|
||||
|
||||
time_0 = time_array_1[0]
|
||||
for i0 in range(len(time_array_1)):
|
||||
time_array_1[i0] = time_0/time_array_1[i0]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Ratio')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.plot(cpu_num_array, cpu_num_array, '--r')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
time_array_2 = [6.059, 2.723, 2.190, 1.577, 1.169, 0.939, 1.231, 0.958, 1.070, 0.793, 1.208, 0.760, 0.709, 0.677, 0.671, 0.906]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.linalg.inv()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Time (s)')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_2, '-o', )
|
||||
plt.show()
|
||||
|
||||
time_0 = time_array_2[0]
|
||||
for i0 in range(len(time_array_2)):
|
||||
time_array_2[i0] = time_0/time_array_2[i0]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.linalg.inv()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Ratio')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_2, '-o', )
|
||||
plt.plot(cpu_num_array, cpu_num_array, '--r')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
time_array_3 = [95.896, 49.107, 41.703, 38.915, 32.468, 25.804, 40.716, 31.072, 40.045, 26.397, 38.557, 28.684, 28.267, 26.840, 27.381, 34.494]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.linalg.eig()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Time (s)')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_3, '-o', )
|
||||
plt.show()
|
||||
|
||||
time_0 = time_array_3[0]
|
||||
for i0 in range(len(time_array_3)):
|
||||
time_array_3[i0] = time_0/time_array_3[i0]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.linalg.eig()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Ratio')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_3, '-o', )
|
||||
plt.plot(cpu_num_array, cpu_num_array, '--r')
|
||||
plt.show()
|
@ -1,160 +0,0 @@
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.ticker import MultipleLocator
|
||||
import numpy as np
|
||||
|
||||
cpu_num_array = np.arange(1, 17)
|
||||
|
||||
|
||||
|
||||
# n = 30000
|
||||
|
||||
time_array_1 = [1164.522, 648.587, 444.488, 647.176, 298.652, 256.809, 239.630, 231.987, 475.781, 383.823, 410.388, 172.702, 163.727, 144.307, 121.530, 109.636]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Time (s)')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.show()
|
||||
|
||||
time_0 = time_array_1[0]
|
||||
for i0 in range(len(time_array_1)):
|
||||
time_array_1[i0] = time_0/time_array_1[i0]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Ratio')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.plot(cpu_num_array, cpu_num_array, '--r')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
# n = 20000
|
||||
|
||||
time_array_1 = [365.730, 209.475, 151.805, 120.739, 104.102, 88.944, 80.475, 70.486, 67.500, 61.404, 53.837, 51.219, 51.542, 84.641, 80.378, 30.629]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Time (s)')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.show()
|
||||
|
||||
time_0 = time_array_1[0]
|
||||
for i0 in range(len(time_array_1)):
|
||||
time_array_1[i0] = time_0/time_array_1[i0]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Ratio')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.plot(cpu_num_array, cpu_num_array, '--r')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
# n = 10000
|
||||
|
||||
time_array_1 = [62.485, 36.402, 25.316, 21.785, 17.619, 15.412, 16.718, 10.874, 9.588, 7.004, 6.733, 7.251, 7.248, 4.979, 4.889, 7.037]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Time (s)')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.show()
|
||||
|
||||
time_0 = time_array_1[0]
|
||||
for i0 in range(len(time_array_1)):
|
||||
time_array_1[i0] = time_0/time_array_1[i0]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Ratio')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.plot(cpu_num_array, cpu_num_array, '--r')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
# n = 500
|
||||
|
||||
time_array_1 = [0.004053801, 0.002605676, 0.001590664, 0.001103345, 0.001266495, 0.000954730, 0.000930616, 0.000779754, 0.000970088, 0.000651353, 0.000918634, 0.000538209, 0.000716934, 0.000521487, 0.001165715, 0.000764804 ]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Time (s)')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.show()
|
||||
|
||||
time_0 = time_array_1[0]
|
||||
for i0 in range(len(time_array_1)):
|
||||
time_array_1[i0] = time_0/time_array_1[i0]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Ratio')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.plot(cpu_num_array, cpu_num_array, '--r')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
# n = 300
|
||||
|
||||
time_array_1 = [0.001144045, 0.000554059, 0.000413136, 0.000385368, 0.000287431, 0.000270178, 0.000268842, 0.000535453, 0.000219275, 0.000219676, 0.000186116, 0.000522058, 0.000146788, 0.000134041, 0.000558532, 0.000135554]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Time (s)')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.show()
|
||||
|
||||
time_0 = time_array_1[0]
|
||||
for i0 in range(len(time_array_1)):
|
||||
time_array_1[i0] = time_0/time_array_1[i0]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Ratio')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.plot(cpu_num_array, cpu_num_array, '--r')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
# n = 100
|
||||
|
||||
time_array_1 = [0.000041899, 0.000104283, 0.000138595, 0.000038628, 0.000039540, 0.000029726, 0.000101140, 0.000023468, 0.000061134, 0.000225034, 0.000033439, 0.000075225, 0.000057184, 0.000040229, 0.000104894, 0.000041510]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Time (s)')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.show()
|
||||
|
||||
time_0 = time_array_1[0]
|
||||
for i0 in range(len(time_array_1)):
|
||||
time_array_1[i0] = time_0/time_array_1[i0]
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title('np.dot()')
|
||||
ax.set_xlabel('Number of CPU cores')
|
||||
ax.set_ylabel('Ratio')
|
||||
ax.xaxis.set_major_locator(MultipleLocator(1))
|
||||
plt.plot(cpu_num_array, time_array_1, '-o', )
|
||||
plt.plot(cpu_num_array, cpu_num_array, '--r')
|
||||
plt.show()
|
@ -1,7 +1,7 @@
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
cpu_num_array = np.arange(1, 17)
|
||||
cpu_num_array = np.arange(1, 9)
|
||||
|
||||
for cpu_num in cpu_num_array:
|
||||
os.system(f'qsub task_{cpu_num}.sh')
|
@ -8,15 +8,13 @@ import time
|
||||
import pickle
|
||||
|
||||
n = 1000
|
||||
|
||||
A = np.random.rand(n, n)
|
||||
B = np.random.rand(n, n)
|
||||
|
||||
test_times = 20
|
||||
|
||||
# 矩阵乘法
|
||||
start_time = time.time()
|
||||
for _ in range(test_times):
|
||||
A = np.random.rand(n, n)
|
||||
B = np.random.rand(n, n)
|
||||
C = np.dot(A, B)
|
||||
multiply_time = (time.time() - start_time)/test_times
|
||||
with open(f'multiply_time_n={n}.pkl', 'wb') as f:
|
||||
@ -25,6 +23,7 @@ with open(f'multiply_time_n={n}.pkl', 'wb') as f:
|
||||
# 矩阵求逆
|
||||
start_time = time.time()
|
||||
for _ in range(test_times):
|
||||
A = np.random.rand(n, n)
|
||||
inv_A = np.linalg.inv(A)
|
||||
inv_time = (time.time() - start_time)/test_times
|
||||
with open(f'inv_time_n={n}.pkl', 'wb') as f:
|
||||
@ -33,6 +32,7 @@ with open(f'inv_time_n={n}.pkl', 'wb') as f:
|
||||
# 矩阵的特征值和特征向量
|
||||
start_time = time.time()
|
||||
for _ in range(test_times):
|
||||
A = np.random.rand(n, n)
|
||||
eigenvalues_A, eigenvector_A = np.linalg.eig(A)
|
||||
eigen_time = (time.time() - start_time)/test_times
|
||||
with open(f'eigen_time_n={n}.pkl', 'wb') as f:
|
||||
|
@ -40,10 +40,12 @@ program main
|
||||
implicit none
|
||||
|
||||
integer, allocatable :: index1(:)
|
||||
integer n, i, j, info, ierr, stage, start, end_val, step, count_start, count_end, count_rate
|
||||
integer n, i, j, info, ierr, stage, start, end_val, step, count_start, count_end, count_rate, test_0, test_times
|
||||
double precision, allocatable :: A(:,:)
|
||||
double precision time_used
|
||||
|
||||
test_times = 20
|
||||
|
||||
! 定义不同阶段的参数
|
||||
do stage = 1, 3
|
||||
select case(stage)
|
||||
@ -55,9 +57,9 @@ program main
|
||||
start = 2000
|
||||
end_val = 10000
|
||||
step = 1000
|
||||
case(3) ! 第三阶段:20000-50000,步长10000
|
||||
case(3) ! 第三阶段:20000-30000,步长10000
|
||||
start = 20000
|
||||
end_val = 50000
|
||||
end_val = 30000
|
||||
step = 10000
|
||||
end select
|
||||
|
||||
@ -65,21 +67,25 @@ program main
|
||||
do while (n <= end_val)
|
||||
|
||||
allocate(index1(n), stat=ierr)
|
||||
call generate_random_matrix(n, A)
|
||||
|
||||
|
||||
call system_clock(count_start, count_rate)
|
||||
call getrf(A, index1, info); call getri(A, index1, info) ! 使用 getrf 和 getri 对矩阵求逆。这时候 A 不再是原来的矩阵了,而是求逆后的矩阵。
|
||||
test_0 = 1
|
||||
do while (test_0 <= test_times)
|
||||
call generate_random_matrix(n, A)
|
||||
call getrf(A, index1, info); call getri(A, index1, info) ! 使用 getrf 和 getri 对矩阵求逆。这时候 A 不再是原来的矩阵了,而是求逆后的矩阵。
|
||||
deallocate(A, stat=ierr)
|
||||
test_0 = test_0 + 1
|
||||
end do
|
||||
call system_clock(count_end)
|
||||
|
||||
! 打印计算时间
|
||||
if (count_rate > 0) then
|
||||
time_used = real(count_end - count_start) / real(count_rate)
|
||||
time_used = real(count_end - count_start) / real(count_rate) / test_times
|
||||
write(*, '(a, I6, a, f12.6, a)') 'n = ', n, ' 的计算时间: ', time_used, ' 秒'
|
||||
else
|
||||
write(*,*) "无法获取计算时间"
|
||||
endif
|
||||
|
||||
deallocate(A, stat=ierr)
|
||||
deallocate(index1, stat=ierr)
|
||||
|
||||
n = n + step
|
||||
|
@ -8,11 +8,13 @@ import time
|
||||
|
||||
n_array = np.concatenate((np.arange(100, 1000, 100),
|
||||
np.arange(1000, 10000, 1000),
|
||||
np.arange(10000, 60000, 10000)))
|
||||
np.arange(10000, 40000, 10000)))
|
||||
|
||||
for n in n_array:
|
||||
A = np.random.rand(n, n)
|
||||
test_times = 20
|
||||
start_time = time.time()
|
||||
inv_A = np.linalg.inv(A)
|
||||
inv_time = time.time() - start_time
|
||||
for _ in range(test_times):
|
||||
A = np.random.rand(n, n)
|
||||
inv_A = np.linalg.inv(A)
|
||||
inv_time = (time.time() - start_time)/test_times
|
||||
print(f"n = {n} 的计算时间: {inv_time:.6f} 秒")
|
@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
#PBS -N fortran
|
||||
#PBS -l nodes=1:ppn=24
|
||||
./a.exe
|
@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
#PBS -N python
|
||||
#PBS -l nodes=1:ppn=24
|
||||
python a.py
|
Loading…
x
Reference in New Issue
Block a user