- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含 3 个部分的分段函数,我正尝试使用 Numba @jit 指令在 Python 中编写这些函数。该函数是在数组上计算的。该函数定义为:
@njit(parallel=True)
def f(x_vec):
N=len(x_vec)
y_vec=np.zeros(N)
for i in prange(N):
x=x_vec[i]
if x<=2000:
y=64/x
elif x>=4000:
y=np.log(x)
else:
y=np.log(1.2*x)
y_vec[i]=y
return y_vec
我正在使用 Numba 使这段代码非常快,并在我的 CPU 的所有 8 个线程上运行它。
现在,我的问题是,如果我想将函数的每个部分分别定义为 f1
、f2
和 f3
,然后将if 语句中的那些(并且仍然受益于 Numba 速度),我该怎么做?原因是子函数可以更复杂,我不想让我的代码难以阅读。我希望它和这个一样快(或稍慢但不是很多)。
为了测试功能,我们可以使用这个数组:
Np=10000000
x_vec=100*np.power(1e8/100,np.random.rand(Np))
%timeit f(x_vec) #0.06sec on intel core i7 3610
对于完成主义,调用了以下库:
import numpy as np
from numba import njit, prange
所以在这种情况下,函数将是:
def f1(x):
return 64/x
def f2(x):
return np.log(x)
def f3(x):
return np.log(1.2*x)
实际函数是这些,它们用于层流、过渡和湍流状态的光滑管道摩擦系数:
@njit
def f1(x):
return 64/x
@njit
def f2(x):
#x is the Reynolds number(Re), y is the Darcy friction(f)
#for transition, we can assume Re=4000 (max possible friction)
y=0.02
y=(-2/np.log(10))*np.log(2.51/(4000*np.sqrt(y)))
return 1/(y*y)
@njit
def f3(x): #colebrook-white approximation
#x is the Reynolds number(Re), y is the Darcy friction(f)
y=0.02
y=(-2/np.log(10))*np.log(2.51/(x*np.sqrt(y)))
return 1/(y*y)
感谢大家的贡献。这是 numpy 解决方案(由于某种原因,最后的树行很慢,但不需要预热):
y = np.empty_like(x_vec)
a1=np.where(x_vec<=2000,True,False)
a3=np.where(x_vec>=4000,True,False)
a2=~(a1 | a3)
y[a1] = f1(x_vec[a1])
y[a2] = f2(x_vec[a2])
y[a3] = f3(x_vec[a3])
最快的 Numba 解决方案,允许传递函数名称并利用 prange(但受到 jit 预热的阻碍)是这个,它可以和第一个解决方案一样快(问题的顶部):
@njit(parallel=True)
def f(x_vec,f1,f2,f3):
N = len(x_vec)
y_vec = np.zeros(N)
for i in prange(N):
x=x_vec[i]
if x<=2000:
y=f1(x)
elif x>=4000:
y=f3(x)
else:
y=f2(x)
y_vec[i]=y
return y_vec
最佳答案
你可以写f()
来接受函数参数,例如:
@njit
def f(arr, f1, f2, f3):
N = len(arr)
y_vec = np.zeros(N)
for i in range(N):
x = x_vec[i]
if x <= 2000:
y = f1(x)
elif x >= 4000:
y = f2(x)
else:
y = f3(x)
y_vec[i] = y
return y_vec
确保您传递的函数与 Numba 兼容。
关于带有 if 语句的 Python Numba jit 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64576042/
我想将一个类对象传递给一个函数。我可以让它工作,但我想知道是否有我可以分配的类型?我有一个我正在尝试做的“最小”示例。 spec = [("a", float64),("b",float64)] @j
我有一个简单的函数来对扑克手牌进行排序(手牌是字符串)。 我用 rA,rB = rank(a),rank(b) 调用它,这是我的实现。没有 @jit(nopython=True) 也能很好地工作,但是
我在这里有一个简单的例子来帮助我理解使用 numba 和 cython。我是 numba 和 cython 的新手。我已经尽力结合所有技巧来使 numba 更快,并且在某种程度上,cython 也是如
我正在使用 numbas @jit 装饰器在 python 中添加两个 numpy 数组。如果我使用 @jit 与 python 相比,性能是如此之高。 然而,即使我传入 @numba.jit(nop
我需要为通用指标构建相异矩阵。由于我需要算法快速运行,所以我在 nopython 模式下使用了 numba 0.35。这是我的代码 import numpy as np from numba impo
Numba Cuda 有 syncthreads() 来同步一个 block 中的所有线程。如何在不退出当前内核的情况下同步网格中的所有 block ? 在 C-Cuda 中有一个 cooperati
有人尝试在Google合作伙伴中使用numba吗?我只是不知道如何在此环境中进行设置。 此刻,我陷入了错误library nvvm not found。 最佳答案 将此代码复制到单元格中。这个对我有用
我想编写一个函数,它既可以作为 jitted 函数运行,也可以作为普通 python 或对象模式 numba 运行,具体取决于 numba 是否能够进行类型推断。我实际上更喜欢普通的 python,但
我有一个非常简单的问题我无法解决。 我正在使用 Numba 和 Cuda。我有一个列表 T=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0] 我想要一个包含列表元素的元组,如
我正在测试一些采用 numpy 数组的函数的 numba 性能,并比较: import numpy as np from numba import jit, vectorize, float64 im
我正在使用 Scipy 的 interpolate.interp1d 在 Python3 中插入一维数组。我想将它与 numba 一起使用,但不支持 scipy 和此功能。是否有 numba 支持
我是 Numba 的新手,我正在尝试使用 Numba(版本 0.54.1)在 Python 中实现旧的 Fortran 代码,但是当我添加 parallel = True 时,程序实际上变慢了.我的程
我需要在 Python 中创建一个位数组。到目前为止,我发现可以使用 bitarray 生成非常节省内存的数组。模块。 然而,我的最终目的是使用来自Numba 的@vectorize 装饰器。 . N
我认为这是一个简单的问题,但我发现 numba 文档缺乏关于如何将字符串类型与 numpy 数组和字典一起使用的信息。我有一个我想使用 numba 的函数,它需要一个邮政编码列表,然后是一个映射邮政编
假设我有两个功能 def my_sub1(a): return a + 2 def my_main(a): a += 1 b = mysub1(a) return b
在以下用于逻辑比较的 numba 编译函数中,性能下降的原因可能是什么: from numba import njit t = (True, 'and_', False) #@njit(boolean
我的代码使用如下列表的笛卡尔积: import itertools cartesian_product = itertools.product(list('ABCDEF'), repeat=n) n可
我正在使用 Numba(版本 0.37.0)来优化 GPU 代码。我想使用组合矢量化函数(使用 Numba 的 @vectorize 装饰器)。 导入和数据: import numpy as np f
我想知道在 numba 函数中计算两个列表的交集的最快方法。只是为了澄清:两个列表的交集示例: Input : lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] lst2
我正在使用 Numba 非 python 模式和一些 NumPy 函数。 @njit def invert(W, copy=True): ''' Inverts elementwise
我是一名优秀的程序员,十分优秀!