Aer*_*ius 5 python arrays fortran numpy f2py
我想在Python中执行多项式计算.该polynomial封装numpy是不是对我不够快.因此,我决定在Fortran中重写几个函数,并用于f2py创建易于导入Python的共享库.目前,我正在对我的例程进行基准测试,以进行单变量和双变量多项式评估numpy.
在单因素例行我用霍纳的方法一样numpy.polynomial.polynomial.polyval.我观察到Fortran例程比numpy对应例程快的因子随着多项式的阶数的增加而增加.
在双变量例程中,我使用Horner的方法两次.首先是y,然后是x.遗憾的是,我观察到,为了增加多项式阶数,numpy对手会赶上并最终超越我的Fortran例程.由于numpy.polynomial.polynomial.polyval2d使用类似于我的方法,我认为这第二个观察是奇怪的.
我希望这个结果源于我对Fortran和f2py.的经验不足.有人可能有任何线索为什么单变量例程总是显得优越,而双变量例程只对低阶多项式优越?
编辑 这是我最新的更新代码,基准脚本和性能图:
polynomial.f95
subroutine polyval(p, x, pval, nx)
implicit none
real(8), dimension(nx), intent(in) :: p
real(8), intent(in) :: x
real(8), intent(out) :: pval
integer, intent(in) :: nx
integer :: i
pval = 0.0d0
do i = nx, 1, -1
pval = pval*x + p(i)
end do
end subroutine polyval
subroutine polyval2(p, x, y, pval, nx, ny)
implicit none
real(8), dimension(nx, ny), intent(in) :: p
real(8), intent(in) :: x, y
real(8), intent(out) :: pval
integer, intent(in) :: nx, ny
real(8) :: tmp
integer :: i, j
pval = 0.0d0
do j = ny, 1, -1
tmp = 0.0d0
do i = nx, 1, -1
tmp = tmp*x + p(i, j)
end do
pval = pval*y + tmp
end do
end subroutine polyval2
subroutine polyval3(p, x, y, z, pval, nx, ny, nz)
implicit none
real(8), dimension(nx, ny, nz), intent(in) :: p
real(8), intent(in) :: x, y, z
real(8), intent(out) :: pval
integer, intent(in) :: nx, ny, nz
real(8) :: tmp, tmp2
integer :: i, j, k
pval = 0.0d0
do k = nz, 1, -1
tmp2 = 0.0d0
do j = ny, 1, -1
tmp = 0.0d0
do i = nx, 1, -1
tmp = tmp*x + p(i, j, k)
end do
tmp2 = tmp2*y + tmp
end do
pval = pval*z + tmp2
end do
end subroutine polyval3
Run Code Online (Sandbox Code Playgroud)
benchmark(py使用此脚本生成图)
import time
import os
import numpy as np
import matplotlib.pyplot as plt
# Compile and import Fortran module
os.system('f2py -c polynomial.f95 --opt="-O3 -ffast-math" \
--f90exec="gfortran-4.8" -m polynomial')
import polynomial
# Create random x and y value
x = np.random.rand()
y = np.random.rand()
z = np.random.rand()
# Number of repetition
repetition = 10
# Number of times to loop over a function
run = 100
# Number of data points
points = 26
# Max number of coefficients for univariate case
n_uni_min = 4
n_uni_max = 100
# Max number of coefficients for bivariate case
n_bi_min = 4
n_bi_max = 100
# Max number of coefficients for trivariate case
n_tri_min = 4
n_tri_max = 100
# Case on/off switch
case_on = [1, 1, 1]
case_1_done = 0
case_2_done = 0
case_3_done = 0
#=================#
# UNIVARIATE CASE #
#=================#
if case_on[0]:
# Array containing the polynomial order + 1 for several univariate polynomials
n_uni = np.array([int(x) for x in np.linspace(n_uni_min, n_uni_max, points)])
# Initialise arrays for storing timing results
time_uni_numpy = np.zeros(n_uni.size)
time_uni_fortran = np.zeros(n_uni.size)
for i in xrange(len(n_uni)):
# Create random univariate polynomial of order n - 1
p = np.random.rand(n_uni[i])
# Time evaluation of polynomial using NumPy
dt = []
for j in xrange(repetition):
t1 = time.time()
for r in xrange(run): np.polynomial.polynomial.polyval(x, p)
t2 = time.time()
dt.append(t2 - t1)
time_uni_numpy[i] = np.average(dt[2::])
# Time evaluation of polynomial using Fortran
dt = []
for j in xrange(repetition):
t1 = time.time()
for r in xrange(run): polynomial.polyval(p, x)
t2 = time.time()
dt.append(t2 - t1)
time_uni_fortran[i] = np.average(dt[2::])
# Speed-up factor
factor_uni = time_uni_numpy / time_uni_fortran
results_uni = np.zeros([len(n_uni), 4])
results_uni[:, 0] = n_uni
results_uni[:, 1] = factor_uni
results_uni[:, 2] = time_uni_numpy
results_uni[:, 3] = time_uni_fortran
print results_uni, '\n'
plt.figure()
plt.plot(n_uni, factor_uni)
plt.title('Univariate comparison')
plt.xlabel('# coefficients')
plt.ylabel('Speed-up factor')
plt.xlim(n_uni[0], n_uni[-1])
plt.ylim(0, max(factor_uni))
plt.grid(aa=True)
case_1_done = 1
#================#
# BIVARIATE CASE #
#================#
if case_on[1]:
# Array containing the polynomial order + 1 for several bivariate polynomials
n_bi = np.array([int(x) for x in np.linspace(n_bi_min, n_bi_max, points)])
# Initialise arrays for storing timing results
time_bi_numpy = np.zeros(n_bi.size)
time_bi_fortran = np.zeros(n_bi.size)
for i in xrange(len(n_bi)):
# Create random bivariate polynomial of order n - 1 in x and in y
p = np.random.rand(n_bi[i], n_bi[i])
# Time evaluation of polynomial using NumPy
dt = []
for j in xrange(repetition):
t1 = time.time()
for r in xrange(run): np.polynomial.polynomial.polyval2d(x, y, p)
t2 = time.time()
dt.append(t2 - t1)
time_bi_numpy[i] = np.average(dt[2::])
# Time evaluation of polynomial using Fortran
p = np.asfortranarray(p)
dt = []
for j in xrange(repetition):
t1 = time.time()
for r in xrange(run): polynomial.polyval2(p, x, y)
t2 = time.time()
dt.append(t2 - t1)
time_bi_fortran[i] = np.average(dt[2::])
# Speed-up factor
factor_bi = time_bi_numpy / time_bi_fortran
results_bi = np.zeros([len(n_bi), 4])
results_bi[:, 0] = n_bi
results_bi[:, 1] = factor_bi
results_bi[:, 2] = time_bi_numpy
results_bi[:, 3] = time_bi_fortran
print results_bi, '\n'
plt.figure()
plt.plot(n_bi, factor_bi)
plt.title('Bivariate comparison')
plt.xlabel('# coefficients')
plt.ylabel('Speed-up factor')
plt.xlim(n_bi[0], n_bi[-1])
plt.ylim(0, max(factor_bi))
plt.grid(aa=True)
case_2_done = 1
#=================#
# TRIVARIATE CASE #
#=================#
if case_on[2]:
# Array containing the polynomial order + 1 for several bivariate polynomials
n_tri = np.array([int(x) for x in np.linspace(n_tri_min, n_tri_max, points)])
# Initialise arrays for storing timing results
time_tri_numpy = np.zeros(n_tri.size)
time_tri_fortran = np.zeros(n_tri.size)
for i in xrange(len(n_tri)):
# Create random bivariate polynomial of order n - 1 in x and in y
p = np.random.rand(n_tri[i], n_tri[i])
# Time evaluation of polynomial using NumPy
dt = []
for j in xrange(repetition):
t1 = time.time()
for r in xrange(run): np.polynomial.polynomial.polyval3d(x, y, z, p)
t2 = time.time()
dt.append(t2 - t1)
time_tri_numpy[i] = np.average(dt[2::])
# Time evaluation of polynomial using Fortran
p = np.asfortranarray(p)
dt = []
for j in xrange(repetition):
t1 = time.time()
for r in xrange(run): polynomial.polyval3(p, x, y, z)
t2 = time.time()
dt.append(t2 - t1)
time_tri_fortran[i] = np.average(dt[2::])
# Speed-up factor
factor_tri = time_tri_numpy / time_tri_fortran
results_tri = np.zeros([len(n_tri), 4])
results_tri[:, 0] = n_tri
results_tri[:, 1] = factor_tri
results_tri[:, 2] = time_tri_numpy
results_tri[:, 3] = time_tri_fortran
print results_tri
plt.figure()
plt.plot(n_bi, factor_bi)
plt.title('Trivariate comparison')
plt.xlabel('# coefficients')
plt.ylabel('Speed-up factor')
plt.xlim(n_tri[0], n_tri[-1])
plt.ylim(0, max(factor_tri))
plt.grid(aa=True)
print '\n'
case_3_done = 1
#==============================================================================
plt.show()
Run Code Online (Sandbox Code Playgroud)
结果

编辑纠正了steabert的提议
subroutine polyval(p, x, pval, nx)
implicit none
real*8, dimension(nx), intent(in) :: p
real*8, intent(in) :: x
real*8, intent(out) :: pval
integer, intent(in) :: nx
integer, parameter :: simd = 8
real*8 :: tmp(simd), xpower(simd), maxpower
integer :: i, j, k
xpower(1) = x
do i = 2, simd
xpower(i) = xpower(i-1)*x
end do
maxpower = xpower(simd)
tmp = 0.0d0
do i = nx+1, simd+2, -simd
do j = 1, simd
tmp(j) = tmp(j)*maxpower + p(i-j)*xpower(simd-j+1)
end do
end do
k = mod(nx-1, simd)
if (k == 0) then
pval = sum(tmp) + p(1)
else
pval = sum(tmp) + p(k+1)
do i = k, 1, -1
pval = pval*x + p(i)
end do
end if
end subroutine polyval
Run Code Online (Sandbox Code Playgroud)
编辑测试代码以验证上面的代码为x> 1提供了不良结果
import polynomial as P
import numpy.polynomial.polynomial as PP
import numpy as np
for n in xrange(2,100):
poly1n = np.random.rand(n)
poly1f = np.asfortranarray(poly1n)
x = 2
print np.linalg.norm(P.polyval(poly1f, x) - PP.polyval(x, poly1n)), '\n'
Run Code Online (Sandbox Code Playgroud)
在双变量情况下,p是一个二维数组.这意味着C vs fortran对数组的排序是不同的.默认情况下,numpy函数提供C排序,显然fortran例程使用fortran排序.
f2py非常聪明,可以处理这个问题,并自动在C和fortran格式数组之间进行转换.但是,这会导致一些开销,这是性能降低的可能原因之一.您可以p通过numpy.asfortranarray在计时例程之外手动转换为fortran类型来检查这是否是原因.当然,为了使其有意义,在您的实际用例中,您需要确保输入数组符合fortran顺序.
f2py有一个选项-DF2PY_REPORT_ON_ARRAY_COPY,可以在任何时候复制数组时发出警告.
如果这不是原因,那么您需要考虑更深入的细节,例如您正在使用哪个fortran编译器,以及它正在应用哪种优化.可能会降低速度的一些例子包括在堆上而不是堆栈上分配数组(使用昂贵的调用malloc),尽管我希望这种效果对于较大的数组变得不那么重要.
最后,你应该考虑这样一种可能性,即对于大型的双变量拟合,Nnumpy例程已经基本上处于最佳效率.在这种情况下,numpy例程可能花费大部分时间来运行优化的C例程,并且python代码的开销相比之下变得可以忽略不计.在这种情况下,您不希望您的fortran代码显示任何显着的加速.
遵循其他建议,p=np.asfortranarray(p)在我测试它时,在计时器之前使用确实可以使性能与 numpy 相当。我将双变量工作台的范围扩展为n_bi = np.array([2**i for i in xrange(1, 15)]),以便 p 矩阵将大于我的 L3 缓存大小。
为了进一步优化这一点,我认为自动编译器选项不会有太大帮助,因为内部循环具有依赖性。仅当您手动展开它时,才会ifort对最内层的循环进行矢量化。需要和。gfortran 对于受主存带宽限制的矩阵大小,与 numpy 相比,这将性能优势提高了 1 到 3 倍。-O3-ffast-math
更新:将其也应用于单变量代码并使用 进行编译后f2py --opt='-O3 -ffast-math' -c -m polynomial polynomial.f90,我得到 benchmark.py 的源代码和结果:
subroutine polyval(p, x, pval, nx)
implicit none
real*8, dimension(nx), intent(in) :: p
real*8, intent(in) :: x
real*8, intent(out) :: pval
integer, intent(in) :: nx
integer, parameter :: simd = 8
real*8 :: tmp(simd), vecx(simd), xfactor
integer :: i, j, k
! precompute factors
do i = 1, simd
vecx(i)=x**(i-1)
end do
xfactor = x**simd
tmp = 0.0d0
do i = 1, nx, simd
do k = 1, simd
tmp(k) = tmp(k)*xfactor + p(nx-(i+k-1)+1)*vecx(simd-k+1)
end do
end do
pval = sum(tmp)
end subroutine polyval
subroutine polyval2(p, x, y, pval, nx, ny)
implicit none
real*8, dimension(nx, ny), intent(in) :: p
real*8, intent(in) :: x, y
real*8, intent(out) :: pval
integer, intent(in) :: nx, ny
integer, parameter :: simd = 8
real*8 :: tmp(simd), vecx(simd), xfactor
integer :: i, j, k
! precompute factors
do i = 1, simd
vecx(i)=x**(i-1)
end do
xfactor = x**simd
! horner
pval=0.0d0
do i = 1, ny
tmp = 0.0d0
do j = 1, nx, simd
! inner vectorizable loop
do k = 1, simd
tmp(k) = tmp(k)*xfactor + p(nx-(j+k-1)+1,ny-i+1)*vecx(simd-k+1)
end do
end do
pval = pval*y + sum(tmp)
end do
end subroutine polyval2
Run Code Online (Sandbox Code Playgroud)
更新 2:正如所指出的,此代码不正确,至少当大小不能被 整除时simd。它只是展示了手动帮助编译器的概念,所以不要只是这样使用它。如果大小不是 2 的幂,则必须使用一个小的余数循环来处理悬空索引。做到这一点并不难,这是单变量情况的正确过程,应该很容易将其扩展到双变量:
subroutine polyval(p, x, pval, nx)
implicit none
real*8, dimension(nx), intent(in) :: p
real*8, intent(in) :: x
real*8, intent(out) :: pval
integer, intent(in) :: nx
integer, parameter :: simd = 4
real*8 :: tmp(simd), vecx(simd), xfactor
integer :: i, j, k, nr
! precompute factors
do i = 1, simd
vecx(i)=x**(i-1)
end do
xfactor = x**simd
! check remainder
nr = mod(nx, simd)
! horner
tmp = 0.0d0
do i = 1, nx-nr, simd
do k = 1, simd
tmp(k) = tmp(k)*xfactor + p(nx-(i+k-1)+1)*vecx(simd-k+1)
end do
end do
pval = sum(tmp)
! do remainder
pval = pval * x**nr
do i = 1, nr
pval = pval + p(i) * vecx(i)
end do
end subroutine polyval
Run Code Online (Sandbox Code Playgroud)


另外,对于非常小的尺寸应该小心,因为时间太短而无法获得准确的性能概况。此外,相对时间numpy可能具有欺骗性,因为 numpy 的绝对时间可能非常糟糕。以下是最大案例的时间安排:
对于 nx=2 20 的单变量,numpy 的时间为 1.21 s,自定义 fortran 版本的时间为 1.69e-3 s。对于 nx ny=2 20 的双变量,numpy 的时间为 8e-3 s,自定义版本的时间为 1.68e-3 s。当总 nx ny 大小相同时,单变量和双变量的时间相同这一事实非常重要,因为它支持代码在内存带宽限制附近执行的事实。
更新 3:使用较小尺寸的新 python 脚本,simd=4我得到以下性能:


更新 4:至于正确性,结果在双精度精度内是相同的,如果您针对单变量示例运行此 python 代码,您可以看到这一点:
import polynomial as P
import numpy.polynomial.polynomial as PP
import numpy as np
for n in xrange(2,100):
poly1n = np.random.rand(n)
poly1f = np.asfortranarray(poly1n)
x = 2
print "%18.14e" % P.polyval(poly1f, x)
print "%18.14e" % PP.polyval(x, poly1n)
print (P.polyval(poly1f, x) - PP.polyval(x, poly1n))/PP.polyval(x,poly1n), '\n'
Run Code Online (Sandbox Code Playgroud)