[Fortran] 纯文本查看 复制代码
module inv
contains
! decomposition. Depends on LAPACK.
function inverse(A) result(Ainv)
real(kind=8), dimension(:,:), intent(in) :: A
real(kind=8), dimension(size(A,1),size(A,2)) :: Ainv
real(kind=8), dimension(size(A,1)) :: work ! work array for LAPACK
integer, dimension(size(A,1)) :: ipiv ! pivot indices
integer :: n, info
!external DGETRF
!external DGETRI
Ainv = A
n = size(A,1)
call DGETRF(n, n, Ainv, n, ipiv, info)
if (info /= 0) then
stop 'Matrix is numerically singular!'
end if
call DGETRI(n, Ainv, n, ipiv, work, n, info)
if (info /= 0) then
stop 'Matrix inversion failed!'
end if
end function inverse
end module
program MatrixSpeed
use inv
implicit none
real(kind=8), allocatable :: z(:,:), re(:,:)
integer :: i, j, n, p
real(kind=8) :: u
real(kind=8) :: RealOne = 1.d0, RealZero = 0.d0
real(kind=4) :: t1, t2 ,t3
n = 1000
p = 1000
allocate(z(n,p),re(n,n))
do i=1,n
do j=1,p
call random_number(u)
z(i,j) = u * 3.0
enddo
enddo
! 随机矩阵求逆,再乘以原矩阵,得到单位阵。
print *, "正在计算... ..."
! 1. Matmul 实现乘法
call cpu_time(t1)
re = matmul(z, inverse(z))
call cpu_time(t2)
print *, re(3,3)
do i=1,min(10,n)
write(*,'(10(f10.3,x))') re(i, 1:min(10,n))
enddo
print *, "Matmul Time: ", t2-t1, "秒"
! 2. Lapack-dgemm 实现乘法
!external dgemm
call dgemm('n','t' ,n,n,p,RealOne ,Z,n ,inverse(Z),n ,RealZero,RE,n)
call cpu_time(t3)
print *, re(3,3)
do i=1,min(10,n)
write(*,'(10(f10.3,x))') re(i, 1:min(10,n))
enddo
print *, "Dgemm Time: ", t3-t2, "秒"
end program MatrixSpeed
! gfortran -O3 01_array_inv_mul.f90 -L./lapack -llapack -lrefblas -o array_inv_mul.exe