From 02a3f2b279bd53660347319b6a1324d9ae9ee6d7 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Thu, 3 Apr 2025 23:46:51 +0800 Subject: [PATCH] update --- .../a.f90 | 22 ++++++++++++------- .../a.py | 10 +++++---- .../task_fotran.sh | 4 ++++ .../task_python.sh | 4 ++++ 4 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 2025.04.03_time_of_fotran_mkl_and_python_numpy/task_fotran.sh create mode 100644 2025.04.03_time_of_fotran_mkl_and_python_numpy/task_python.sh diff --git a/2025.04.03_time_of_fotran_mkl_and_python_numpy/a.f90 b/2025.04.03_time_of_fotran_mkl_and_python_numpy/a.f90 index 414239e..2644d9d 100644 --- a/2025.04.03_time_of_fotran_mkl_and_python_numpy/a.f90 +++ b/2025.04.03_time_of_fotran_mkl_and_python_numpy/a.f90 @@ -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 diff --git a/2025.04.03_time_of_fotran_mkl_and_python_numpy/a.py b/2025.04.03_time_of_fotran_mkl_and_python_numpy/a.py index 9b52e40..fc2cc43 100644 --- a/2025.04.03_time_of_fotran_mkl_and_python_numpy/a.py +++ b/2025.04.03_time_of_fotran_mkl_and_python_numpy/a.py @@ -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} 秒") \ No newline at end of file diff --git a/2025.04.03_time_of_fotran_mkl_and_python_numpy/task_fotran.sh b/2025.04.03_time_of_fotran_mkl_and_python_numpy/task_fotran.sh new file mode 100644 index 0000000..7f1603d --- /dev/null +++ b/2025.04.03_time_of_fotran_mkl_and_python_numpy/task_fotran.sh @@ -0,0 +1,4 @@ +#!/bin/sh +#PBS -N fortran +#PBS -l nodes=1:ppn=24 +./a.exe \ No newline at end of file diff --git a/2025.04.03_time_of_fotran_mkl_and_python_numpy/task_python.sh b/2025.04.03_time_of_fotran_mkl_and_python_numpy/task_python.sh new file mode 100644 index 0000000..b6a9763 --- /dev/null +++ b/2025.04.03_time_of_fotran_mkl_and_python_numpy/task_python.sh @@ -0,0 +1,4 @@ +#!/bin/sh +#PBS -N python +#PBS -l nodes=1:ppn=24 +python a.py \ No newline at end of file