几个numba函数的并行编译

2024-05-01 21:51:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用numba在我的代码库中编译几个函数以加速执行。我有几千个独立函数,我正试图用jitdecorator编译它们。在编译函数时,我注意到所有代码都是在一个python进程中编译的,并且只在我的机器中使用一个内核。这导致编译时间通常超过一小时。我想知道是否可以在类似于make -j <number of processors>的编译步骤中启用并行化

我的函数彼此完全独立,使用它们的后续调用位于单独的子模块中。下面是我希望能够并行编译的函数类型的示例:

import numpy as np
from numba import jit, float64
@jit(float64[:](float64[:]),nopython=True,cache=True,fastmath=True,parallel=True,nogil=True)
def fun_0(x):
  return np.power(x,0)

@jit(float64[:](float64[:]),nopython=True,cache=True,fastmath=True,parallel=True,nogil=True)
def fun_1(x):
  return np.power(x,1)

@jit(float64[:](float64[:]),nopython=True,cache=True,fastmath=True,parallel=True,nogil=True)
def fun_2(x):
  return np.power(x,2)

@jit(float64[:](float64[:]),nopython=True,cache=True,fastmath=True,parallel=True,nogil=True)
def fun_3(x):
  return np.power(x,3)

代码只是我的用例的说明。构建我的功能/模块的替代方法也可能是我可以接受的解决方案


Tags: 函数代码truecachereturnparalleldefnp
1条回答
网友
1楼 · 发布于 2024-05-01 21:51:55

我们的目标是提高运行时性能吗?几千个独立函数的@decorator-“内联”JIT编译会降低运行时性能吗

解决方案:
可以使用“AoT编译:即提前编译代码”

While Numba’s main use case is Just-in-Time compilation, it also provides a facility for Ahead-of-Time compilation (AOT).

Limitations :
1) AOT compilation only allows for regular functions, not ufuncs.
2) You have to specify function signatures explicitly.
3) Each exported function can have only one signature (but you can export several different signatures under different names).
4) AOT compilation produces generic code for your CPU’s architectural family (for example “x86-64”), while JIT compilation produces code optimized for your particular CPU model.

警告:
您的代码按原样似乎容易陷入Amdahl's Law Trap

您将很容易支付更多的附加组件开销,这比从(仅仅)潜在的处理加速中得到的任何回报都要多,因为与更密集的内存I/O和CPU核心缓存重用机制相关的约束(绝对I/O上限和重用效率的相对损失)做并且仍然会违背您提高绩效的愿望)

At least, you have been warned :o)

相关问题 更多 >