【发布时间】:2013-12-17 15:02:51
【问题描述】:
当测试具有不同内部表示(种类)的计算机性能时,代码或多或少保持相同,除了测试参数的定义(种类=1;种类=2)。我尝试构建不同的模块。
module var_1
implicit none
real(8), allocatable :: x(:,:),xi(:,:),xt(:,:)
integer(kind=1), allocatable :: z(:,:)
end module var_1
module var_2
implicit none
real(8), allocatable :: x(:,:),xi(:,:),xt(:,:)
integer(kind=2), allocatable :: z(:,:)
end module var_2
还有一个全局模块,定义了不改变的参数:
module global
integer :: i,j,n,p,nProcessors,s,v,w,infodpotrf,infodpotri,mkl_get_max_threads
integer, dimension(3) :: ni = [100, 1000, 10000], pi = [100, 1000, 10000]
integer, dimension(5) :: nProcessorsi = [1, 2, 4, 6, 12]
real(8):: u,myone= 1.d0,t11,t22
real:: t2,t1
include 'omp_lib.h'
end module global
比在程序部分我们调用稍后定义的子程序:
program test
call matrix_multi_inv_1
call matrix_multi_inv_2
end program test
子程序:
subroutine matrix_multi_inv_1
use global
use var_1
open (unit=100,file="results.txt",status="unknown")
do s=1,5
nProcessors = nProcessorsi(s)
CALL OMP_SET_NUM_THREADS(nProcessors)
do v=1,3
n=ni(v)
do w=1,3
p=pi(w)
allocate(x(n,n),z(n,p),xi(n,n),xt(n,n))
do i=1,n
do j=1,p
call random_number(u)
z(i,j)=real(floor(u*3),8)
enddo
enddo
1000 format(3(a20),2(i10),2(f15.3),i10)
t11=omp_get_wtime()
call cpu_time(t1)
x=matmul(z,transpose(z))
t22=omp_get_wtime()
call cpu_time(t2)
write(100,1000) 'x_integer_kind_1', 'G_real_8', 'matmul', n, p, t22-t11,t2-t1, mkl_get_max_threads()
deallocate(x,z,xi,xt)
enddo
enddo
enddo
end subroutine matrix_multi_inv_1
subroutine matrix_multi_inv_2
use global
use var_1
open (unit=100,file="results.txt",status="unknown")
do s=1,5
nProcessors = nProcessorsi(s)
CALL OMP_SET_NUM_THREADS(nProcessors)
do v=1,3
n=ni(v)
do w=1,3
p=pi(w)
allocate(x(n,n),z(n,p),xi(n,n),xt(n,n))
do i=1,n
do j=1,p
call random_number(u)
z(i,j)=real(floor(u*3),8)
enddo
enddo
1000 format(3(a20),2(i10),2(f15.3),i10)
t11=omp_get_wtime()
call cpu_time(t1)
x=matmul(z,transpose(z))
t22=omp_get_wtime()
call cpu_time(t2)
write(100,1000) 'x_integer_kind_2', 'G_real_8', 'matmul', n, p, t22-t11,t2-t1, mkl_get_max_threads()
deallocate(x,z,xi,xt)
enddo
enddo
enddo
end subroutine matrix_multi_inv_2
问题来了。除了调用模块部分之外,子程序完全相同。我尝试在子例程中使用包含语句,但如果调用内部子例程,这将不起作用。我也尝试使用带有属性的子程序,但我的编译器报告错误:
种类类型参数必须是编译时常量。
有谁知道如何优化代码的好解决方案。当测试 10 种不同内部表示的不同变体时,这段代码就变得太大了。
【问题讨论】:
标签: fortran