update
This commit is contained in:
		| @@ -0,0 +1,14 @@ | |||||||
|  | import guan  # https://py.guanjihuan.com | install: pip install --upgrade guan | ||||||
|  | import numpy as np | ||||||
|  | import os | ||||||
|  |  | ||||||
|  | cpu_num_array = np.arange(1, 65) | ||||||
|  |  | ||||||
|  | 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) | ||||||
| @@ -0,0 +1,39 @@ | |||||||
|  | """ | ||||||
|  | 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) | ||||||
| @@ -0,0 +1,91 @@ | |||||||
|  | import matplotlib.pyplot as plt | ||||||
|  | # from matplotlib.ticker import MultipleLocator | ||||||
|  | import numpy as np | ||||||
|  | import pickle | ||||||
|  |  | ||||||
|  | cpu_num_array = np.arange(1, 65) | ||||||
|  |  | ||||||
|  | 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() | ||||||
| @@ -0,0 +1,4 @@ | |||||||
|  | #!/bin/sh | ||||||
|  | #PBS -N plot | ||||||
|  | #PBS -l nodes=1:ppn=1 | ||||||
|  | python plot_result_of_running_time_by_reading_files.py | ||||||
| @@ -0,0 +1,9 @@ | |||||||
|  | import numpy as np | ||||||
|  | import os | ||||||
|  |  | ||||||
|  | cpu_num_array = np.arange(1, 65) | ||||||
|  |  | ||||||
|  | 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') | ||||||
		Reference in New Issue
	
	Block a user