天天看点

python边写边总结(三)Numba概要

Numba is an open source JIT Complier that translates a subset of Python and NumPy code into fast machine code.

Numba是一个开源的JIT编译器,它将Python和NumPy代码的子集转换为机器代码

  • JIT(Just in time)

首先需要认识一下python是一种解释性的语言,它会把每一句都解释一遍,如果语句出现在循环体中,循环十次就会被解释10次,不产生目标代码,但是如果加入了编译compilation,就会把文件一次性编译成目标代码,目标代码十可执行的机器语言,或是已存在相应翻译器的不可执行语言

这里就来解释一下Just in time compilation,首先仍然是compilation,有caching,不像interpretation只是执行到某句之后再解释,也不会像static compilation,它是在编译过程对代码进行优化,选择性的编译某个函数或任意某段代码。

Numba translates Python functions to optiized machine code at runtime using the industry-standard LLVM compiler library. Numba-compiled numerical algorithms in Python can approach the speeds of C or FORTRAN.

Numba使用行业标准的LLVM编译器库在运行时将Python函数转换为优化的机器代码。 Python中的Numba编译数值算法可以接近C或FORTRAN的速度。

  • LLVM

LLVM是架构编译器(compiler)的框架系统,以C++编译而成,用于优化任意程序编写的程序的编译时间(compile-time)、链接时间(link-time)、运行时间(runtime)以及空闲时间(idle-time),对开发者保持开放,并兼容已有脚本

You don't need to replace the Python interpreter,ru a separate compilation step,or even have a C/C++ compiler installed.Just apply one of the Numba decorators to your Python function,and Numba does the rest.

您不需要替换Python解释器,单独编译步骤,甚至安装C / C ++编译器。只需将其中一个Numba装饰器应用于您的Python函数,Numba将完成剩下的工作。

Numba is designed to be used with NumPy arrays and functions. Numba generates specialized code for different array data types and layouts to optimize performance. Special decorators can create universal functions that broadcast over NumPy arrays just like NumPy functions do.

Numba旨在与NumPy阵列和功能一起使用。 Numba为不同的阵列数据类型和布局生成专门的代码,以优化性能。 特殊装饰器可以创建通用功能,可以像NumPy函数那样在NumPy数组上进行广播。

这里主要是利用了python的装饰器

  • 并行加速部分

 Numba offers a range of options for parallelizing your code for CPUs and GPUs, often with only minor code changes.

 首先是Simplified THredading

Numba can automatically execute NumPy array expressions on multiple CPU cores and makes it easy to write parallel loops.

python边写边总结(三)Numba概要

然后是SIMD Vectorization

Numba can automatically translate some loops into vector instructions for 2-4x speed improvements. Numba adapts to your CPU capabilities, whether your CPU supports SSE, AVX, or AVX-512.

python边写边总结(三)Numba概要

 最后就是使用GPU Acceleration

With support for both NVIDIA's CUDA and AMD's ROCm drivers, Numba lets you write parallel GPU algorithms entirely from Python.

Numba支持NVIDIA的CUDA和AMD的ROCm驱动程序,可让您完全使用Python编写并行GPU算法。

遗憾的是这里面并没有提供高级GPU的封装函数,只是简单的封装