这是之前的两篇:
由于矩阵运算在底层通常采用了自动并行化的算法,因此在选择多个 CPU 核心时,矩阵计算的速度会显著提升。这种加速主要得益于矩阵运算本身的高度可并行性,例如矩阵乘法、求逆、特征值分解等操作可以被分解为多个独立的子任务,从而分配到不同的 CPU 核心上同时执行。本篇测试和记录不同 CPU 核数对矩阵运算的加速效果。
通过初步的测试,可以得到以下大致结论(仅供参考,可自行测试):
- 对于矩阵乘法,如果矩阵维度较大,那么选择 1~12 核,计算加速的效果比较明显,这时候资源的利用率和性价比也相对比较高;如果矩阵维度较小,那么选择 1~8 核。
- 对于矩阵求逆,如果矩阵维度较大,那么选择 1~8 核,计算加速的效果比较明显,这时候资源的利用率和性价比也相对比较高;如果矩阵维度较小,那么选择 1~4 核。
- 对于求解矩阵特征值和特征向量,如果矩阵维度较大,那么选择 1~4 核,计算加速的效果比较明显,这时候资源的利用率和性价比也相对比较高;如果矩阵维度较小,那么选择 1~2 核。
更多说明:
- 在实际的程序中,除了矩阵运算,还有其他一些串行的代码,加速效果不一定那么理想,另外,如果是极小的矩阵,这时候多核也基本上起不到太多的加速作用,因此在以机时为计算成本的情况下,且不追求计算速度,那么使用单核计算的性价比最高。
- 如果希望以尽快速度完成计算,且不占用过多资源,那么推荐以上的核数选择方案。
- 如果资源比较充足,希望以尽快速度完成计算,且不追求资源占用的性价比,那么应该尽量选择最多核数,这样计算速度最快,虽然速度的提升可能会趋于饱和。对于可以大量并行的情况,速度的提升效果会更加明显。
一、测试结果
说明:以下结果为 20 个样本的平均计算时间;n 为矩阵的维度;Ratio 为多核计算时间和单核计算时间相比减少的倍数,具体计算方法见后面的数据处理代码。
1. 矩阵乘积:
2. 矩阵求逆:
3. 矩阵特征值和特征向量:
二、测试代码(手动记录时间,并统计画图)【可用于简单测试,对于大范围的测试不推荐】
以下是批量提交任务的脚本代码。
1. 批量生产 PBS 文件的 Python 代码:
import guan # https://py.guanjihuan.com | install: pip install --upgrade guan
import numpy as np
cpu_num_array = np.arange(1, 17)
sh_filename = 'task'
task_name = 'test'
py_filename='matrix_running_time_for_different_num_of_cpu_cores'
for cpu_num in cpu_num_array:
guan.make_sh_file_for_qsub(sh_filename=sh_filename+'_'+str(cpu_num), command_line=f'python {py_filename}.py', cpu_num=cpu_num, task_name=task_name+'_'+str(cpu_num), cd_dir=0)
2. 提交任务的 PBS 文件示例 task_2.sh(核数为 2):
#!/bin/sh
#PBS -N test_2
#PBS -l nodes=1:ppn=2
python matrix_running_time_for_different_num_of_cpu_cores.py
3. 批量提交任务 Python 代码:
import numpy as np
import os
cpu_num_array = np.arange(1, 17)
for cpu_num in cpu_num_array:
os.system(f'qsub task_{cpu_num}.sh')
4. 以下是矩阵运行测试的代码:
"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/45324
"""
import numpy as np
import time
n = 5000
A = np.random.rand(n, n)
B = np.random.rand(n, n)
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):
C = np.dot(A, B)
multiply_time = (time.time() - start_time)/test_times
print(f"矩阵乘法时间: {multiply_time:.3f} 秒")
# 矩阵求逆
start_time = time.time()
for _ in range(test_times):
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):
eigenvalues_A, eigenvector_A = np.linalg.eig(A)
eigen_time = (time.time() - start_time)/test_times
print(f"矩阵特征值和特征向量时间: {eigen_time:.3f} 秒")
5. 矩阵维度为 5000 使用 1~16 核运行结果的数据:点击展开
运行结果(1核):
矩阵行列式时间: 1.750 秒
矩阵乘法时间: 3.999 秒
矩阵求逆时间: 6.059 秒
矩阵的秩时间: 17.196 秒
矩阵特征值时间: 61.901 秒
矩阵特征值和特征向量时间: 95.896 秒
运行结果(2核):
矩阵行列式时间: 0.831 秒
矩阵乘法时间: 1.679 秒
矩阵求逆时间: 2.723 秒
矩阵的秩时间: 7.136 秒
矩阵特征值时间: 29.890 秒
矩阵特征值和特征向量时间: 49.107 秒
运行结果(3核):
矩阵行列式时间: 0.807 秒
矩阵乘法时间: 1.257 秒
矩阵求逆时间: 2.190 秒
矩阵的秩时间: 5.901 秒
矩阵特征值时间: 24.575 秒
矩阵特征值和特征向量时间: 41.703 秒
运行结果(4核):
矩阵行列式时间: 0.470 秒
矩阵乘法时间: 0.985 秒
矩阵求逆时间: 1.577 秒
矩阵的秩时间: 4.676 秒
矩阵特征值时间: 21.824 秒
矩阵特征值和特征向量时间: 38.915 秒
运行结果(5核):
矩阵行列式时间: 0.404 秒
矩阵乘法时间: 0.699 秒
矩阵求逆时间: 1.169 秒
矩阵的秩时间: 4.294 秒
矩阵特征值时间: 17.642 秒
矩阵特征值和特征向量时间: 32.468 秒
运行结果(6核):
矩阵行列式时间: 0.266 秒
矩阵乘法时间: 0.562 秒
矩阵求逆时间: 0.939 秒
矩阵的秩时间: 3.147 秒
矩阵特征值时间: 14.322 秒
矩阵特征值和特征向量时间: 25.804 秒
运行结果(7核):
矩阵行列式时间: 0.557 秒
矩阵乘法时间: 0.534 秒
矩阵求逆时间: 1.231 秒
矩阵的秩时间: 5.389 秒
矩阵特征值时间: 21.276 秒
矩阵特征值和特征向量时间: 40.716 秒
运行结果(8核):
矩阵行列式时间: 0.303 秒
矩阵乘法时间: 0.525 秒
矩阵求逆时间: 0.958 秒
矩阵的秩时间: 3.438 秒
矩阵特征值时间: 16.942 秒
矩阵特征值和特征向量时间: 31.072 秒
运行结果(9核):
矩阵行列式时间: 0.395 秒
矩阵乘法时间: 0.510 秒
矩阵求逆时间: 1.070 秒
矩阵的秩时间: 5.832 秒
矩阵特征值时间: 21.368 秒
矩阵特征值和特征向量时间: 40.045 秒
运行结果(10核):
矩阵行列式时间: 0.263 秒
矩阵乘法时间: 0.424 秒
矩阵求逆时间: 0.793 秒
矩阵的秩时间: 2.915 秒
矩阵特征值时间: 14.077 秒
矩阵特征值和特征向量时间: 26.397 秒
运行结果(11核):
矩阵行列式时间: 0.403 秒
矩阵乘法时间: 0.409 秒
矩阵求逆时间: 1.208 秒
矩阵的秩时间: 4.633 秒
矩阵特征值时间: 20.758 秒
矩阵特征值和特征向量时间: 38.557 秒
运行结果(12核):
矩阵行列式时间: 0.227 秒
矩阵乘法时间: 0.380 秒
矩阵求逆时间: 0.760 秒
矩阵的秩时间: 2.863 秒
矩阵特征值时间: 15.104 秒
矩阵特征值和特征向量时间: 28.684 秒
运行结果(13核):
矩阵行列式时间: 0.223 秒
矩阵乘法时间: 0.372 秒
矩阵求逆时间: 0.709 秒
矩阵的秩时间: 3.690 秒
矩阵特征值时间: 14.505 秒
矩阵特征值和特征向量时间: 28.267 秒
运行结果(14核):
矩阵行列式时间: 0.219 秒
矩阵乘法时间: 0.339 秒
矩阵求逆时间: 0.677 秒
矩阵的秩时间: 2.672 秒
矩阵特征值时间: 14.159 秒
矩阵特征值和特征向量时间: 26.840 秒
运行结果(15核):
矩阵行列式时间: 0.253 秒
矩阵乘法时间: 0.334 秒
矩阵求逆时间: 0.671 秒
矩阵的秩时间: 2.787 秒
矩阵特征值时间: 14.552 秒
矩阵特征值和特征向量时间: 27.381 秒
运行结果(16核):
矩阵行列式时间: 0.253 秒
矩阵乘法时间: 0.293 秒
矩阵求逆时间: 0.906 秒
矩阵的秩时间: 3.186 秒
矩阵特征值时间: 17.547 秒
矩阵特征值和特征向量时间: 34.494 秒
6. 矩阵维度为 5000 使用 1~16 核运行结果画图的 Python 代码:点击展开
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()
7. 其他矩阵维度使用 1~16 核运行结果画图的 Python 代码:点击展开
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. 批量生产 PBS 文件的 Python 代码:
import guan # https://py.guanjihuan.com | install: pip install --upgrade guan
import numpy as np
import os
cpu_num_array = np.arange(1, 33)
py_filename='matrix_running_time_for_different_num_of_cpu_cores_writing_into_files'
current_directory = os.getcwd()
for cpu_num in cpu_num_array:
guan.make_directory(f'./task_{cpu_num}')
os.system(f'cp ./{py_filename}.py ./task_{cpu_num}/{py_filename}.py')
os.system(f'cd {current_directory}/task_{cpu_num}')
guan.make_sh_file_for_qsub(sh_filename=f'./task_{cpu_num}/task_{cpu_num}', command_line=f'python {py_filename}.py', cpu_num=cpu_num, task_name=f'test_{cpu_num}', cd_dir=0)
2. 批量提交任务 Python 代码:
import numpy as np
import os
cpu_num_array = np.arange(1, 33)
current_directory = os.getcwd()
for cpu_num in cpu_num_array:
os.system(f'cd {current_directory}/task_{cpu_num} && qsub {current_directory}/task_{cpu_num}/task_{cpu_num}.sh')
3. 以下是矩阵运行测试的代码:
"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/45324
"""
import numpy as np
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):
C = np.dot(A, B)
multiply_time = (time.time() - start_time)/test_times
with open(f'multiply_time_n={n}.pkl', 'wb') as f:
pickle.dump(multiply_time, f)
# 矩阵求逆
start_time = time.time()
for _ in range(test_times):
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:
pickle.dump(inv_time, f)
# 矩阵的特征值和特征向量
start_time = time.time()
for _ in range(test_times):
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:
pickle.dump(eigen_time, f)
4. 获取数据并画图的 Python 代码:
import matplotlib.pyplot as plt
# from matplotlib.ticker import MultipleLocator
import numpy as np
import pickle
cpu_num_array = np.arange(1, 33)
n = 1000
time_array_1 = []
for cpu_num in cpu_num_array:
with open(f'./task_{cpu_num}/multiply_time_n={n}.pkl', 'rb') as f:
data = pickle.load(f)
time_array_1.append(data)
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.savefig(f'multiply_time_n={n}.jpg')
# 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.savefig(f'multiply_time_ratio_n={n}.jpg')
# plt.show()
time_array_2 = []
for cpu_num in cpu_num_array:
with open(f'./task_{cpu_num}/inv_time_n={n}.pkl', 'rb') as f:
data = pickle.load(f)
time_array_2.append(data)
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.savefig(f'inv_time_n={n}.jpg')
# 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.savefig(f'inv_time_ratio_n={n}.jpg')
# plt.show()
time_array_3 = []
for cpu_num in cpu_num_array:
with open(f'./task_{cpu_num}/eigen_time_n={n}.pkl', 'rb') as f:
data = pickle.load(f)
time_array_3.append(data)
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.savefig(f'eigen_time_n={n}.jpg')
# 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.savefig(f'eigen_time_ratio_n={n}.jpg')
# plt.show()
5. 提交任务画图:
#!/bin/sh
#PBS -N plot
#PBS -l nodes=1:ppn=1
python plot_result_of_running_time_by_reading_files.py
74 次浏览
【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com】