问题描述
我正在处理的子例程在数组大小变大时效率很低,例如NN = 1000,KK = 200,MM =200.但是,我无法提出优化它的想法.
I am dealing with subroutine that is very inefficient when the array size becomes large, for example, NN=1000, KK=200, MM = 200. But, I can not come up with ideas to optimize it.
program main
implicit none
integer :: NN, KK, MM
integer, allocatable, dimension(:,:) :: id
complex*16, allocatable, dimension(:) :: phase
complex*16 :: phase_base(3)
real*8, allocatable, dimension(:,:) :: wave_base
complex*16, allocatable, dimension(:,:) :: wave
integer :: i, j, k, n
NN = 1000
KK = 200
MM = 200
allocate(id(MM,3))
allocate(phase(KK))
allocate(wave_base(KK, NN*(NN+1)/2 ))
allocate(wave(NN, NN))
id(:,:) = 2
phase_base(:) = (1.0d0,1.0d0)
wave_base(:,:) = 1.0d0
phase(:) = (1.0d0,1.0d0)
call noise_wave(NN, KK, MM, id, phase, phase_base, wave_base, wave)
deallocate(id)
deallocate(phase)
deallocate(wave_base)
deallocate(wave)
end program main
subroutine noise_wave(NN, KK, MM, id, phase_1, phase_base, wave_base, wave)
implicit none
integer, intent(in) :: NN, KK, MM
integer, intent(in), dimension(MM, 3) :: id
complex*16, intent(in) :: phase_1(KK)
complex*16, intent(in) :: phase_base(3)
real*8, intent(in) :: wave_base(KK, NN*(NN+1)/2 )
complex*16, intent(out) :: wave(NN, NN)
integer :: i, j, k, p, n
integer :: x, y, z
real :: start, finish
complex*16 :: phase_2, phase_2_conjg
do p = 1, MM
x = id(p, 1)
y = id(p, 2)
z = id(p, 3)
phase_2 = (phase_base(1) ** x) * (phase_base(2) ** y) * (phase_base(3) ** z)
phase_2_conjg = conjg(phase_2)
n = 0
do j = 1, NN
do i = 1, j ! upper triangle
n = n + 1
do k = 1, KK
wave(i,j) = wave(i,j) + wave_base(k,n) * phase_1(k) * phase_2_conjg
enddo
wave(j,i) = conjg(wave(i,j) )
enddo
enddo
enddo
end subroutin
有人可以给我一些提示吗? (我已经完成了建议的优化.此外,按照Ian的建议,我添加了一个小测试.因此您可以直接对其进行测试.)
Could someone give me some hint? (I have fulfill the suggested optimizations. Also, following Ian's suggestion, I have added a small test. Thus you can test it directly.)
推荐答案
以下是我的解决方案,遵循上面的好主意.在使用OpenMP之前,仍有一些提高效率的空间.例如,可以通过求和函数消除子例程中的第一个k循环.
Here are my solution following the nice ideas above. There is still some room for efficiency gain before OpenMP. For example, the first k loop in the subroutine can be eliminated by sum function.
program main
implicit none
integer :: NN, KK, MM
integer, allocatable, dimension(:,:) :: id
complex*16, allocatable, dimension(:) :: phase
complex*16 :: phase_base(3)
real*8, allocatable, dimension(:,:) :: wave_base
complex*16, allocatable, dimension(:,:) :: wave
integer :: i, j, k, n
NN = 1000
KK = 200
MM = 200
allocate(id(MM,3))
allocate(phase(KK))
allocate(wave_base(KK, NN*(NN+1)/2 ))
allocate(wave(NN, NN))
id(:,:) = 2
phase_base(:) = (1.0d0,1.0d0)
wave_base(:,:) = 1.0d0
phase(:) = (1.0d0,1.0d0)
call noise_wave(NN, KK, MM, id, phase, phase_base, wave_base, wave)
deallocate(id)
deallocate(phase)
deallocate(wave_base)
deallocate(wave)
end program main
subroutine noise_wave(NN, KK, MM, id, phase_1, phase_base, wave_base, wave)
implicit none
integer, intent(in) :: NN, KK, MM
integer, intent(in), dimension(MM, 3) :: id
complex*16, intent(in) :: phase_1(KK)
complex*16, intent(in) :: phase_base(3)
real*8, intent(in) :: wave_base(KK, NN*(NN+1)/2 )
complex*16, intent(out):: wave(NN, NN)
integer :: i, j, k, p, n
integer :: x, y, z
real :: start, finish
complex*16 :: phase_2, phase_2_conjg
complex*16 :: wave_tmp(NN*(NN+1)/2)
complex*16 :: wave_tmp_2(NN*(NN+1)/2)
do k = 1, KK
wave_tmp(:) = wave_tmp(:) + wave_base(k,:) * phase_1(k)
enddo
do p = 1, MM
phase_2 = product(phase_base(:)**id(p,:) )
phase_2_conjg = conjg(phase_2)
wave_tmp2(:) = wave_tmp2(:) + wave_tmp(n) * phase_2_conjg
enddo
n = 0
do j = 1, NN
do i = 1, j
n = n + 1
wave(i,j) = wave_tmp2(n)
wave(j,i) = conjg(wave_tmp2(n) )
enddo
enddo
end subroutine
这篇关于如何使用多个循环优化此Fortran子例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!