本文介绍了@jit减速功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为复杂的油藏操作问题开发优化代码.这部分需要我为大量潜在解决方案计算目标函数.我正在测试Rosenbrock函数上的优化器,并试图提高其速度.我注意到,在分析代码时,在for循环中计算目标函数是代码瓶颈之一,因此我开发了一种针对多组决策变量并行执行此操作的方法.我有两个目标函数计算器:FO用于一组决策变量,P_FO用于多组决策变量.目标函数的计算是代码中最慢的部分之一,因此我想使用@jit进一步加快处理速度.我使用@jit测试了这两个函数,发现使用@jit的P_FO函数比没有使用它的速度要慢.代码如下:

I'm developing an optimization code for a complex reservoir operations problem. Part of this requires me to calculate the objective function for a large number of potential solutions. I'm testing the optimizer on the Rosenbrock function and trying to improve its speed. I noticed when I profiled the code that calculating the objective function within a for loop was one of the code bottlenecks so I developed a way to do this in parallel for multiple sets of decision variables. I have two objective function calculators: FO for one set of decision variables and P_FO for multiple sets of decision variables. Calculation of the objective function is one of the slowest parts of my code so I would like to speed things up further using @jit. I tested both functions using @jit and am finding the P_FO function is slower with @jit than without it. The code is below:

import time
import numpy as np
from numba import jit

def FO(X):
    #Rosenbrock function
    ObjV=0
    for i in range(65-1):
        F=100*((X[i+1]-X[i]**2)+(X[i]-1)**2)
        ObjV+=F
    return ObjV

t0=time.time()
X=10+np.zeros(65)
for i in range(5000):
    FO(X)
t1 = time.time()
total = t1-t0
print("time FO="+str(total))

@jit
def FO(X):
    #Rosenbrock function
    ObjV=0
    for i in range(65-1):
        F=100*((X[i+1]-X[i]**2)+(X[i]-1)**2)
        ObjV+=F
    return ObjV

t0=time.time()
X=10+np.zeros(65)
for i in range(5000):
    FO(X)
t1 = time.time()
total = t1-t0
print("time FO with @jit="+str(total))



def P_FO(X):
    ObjV=np.zeros(X.shape[0])
    for i in range(X.shape[1]-1):
        F=100*((X[:,i+1]-X[:,i]**2)+(X[:,i]-1)**2)
        ObjV+=F
    return ObjV

t0=time.time()
X=10+np.zeros((65, 5000))
P_FO(X)
t1 = time.time()
total = t1-t0
print("time P_FO="+str(total))


@jit
def P_FO(X):
    ObjV=np.zeros(X.shape[0])
    for i in range(X.shape[1]-1):
        F=100*((X[:,i+1]-X[:,i]**2)+(X[:,i]-1)**2)
        ObjV+=F
    return ObjV

t0=time.time()
X=10+np.zeros((65, 5000))
P_FO(X)
t1 = time.time()
total = t1-t0
print("time P_FO with @jit="+str(total))

结果是:

time FO=0.523999929428
time FO with @jit=0.0720000267029
time P_FO=0.0380001068115
time P_FO with @jit=0.229000091553

有人可以指出@jit减慢并行目标函数P_FO的原因吗?是因为使用了np.zeros还是也许array.shape()?

Can anyone point me to a reason why @jit slows down the parallel objective function P_FO? Is it because of the use of np.zeros or perhaps array.shape()?

推荐答案

numba函数是延迟编译的,即直到第一次调用时才进行编译,因此您的时间占用了一次性编译开销.如果我在运行计时部分之前调用每个函数一次,我将得到:

numba functions are compiled lazily, i.e., not until called the first time, so your timings are a capturing the one-time compilation overhead. If I call each function once before running the timing sections, I get:

time FO=0.4103426933288574
time FO with @jit=0.0020008087158203125
time P_FO=0.04154801368713379
time P_FO with @jit=0.004002809524536133

这篇关于@jit减速功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:25