跳转到主要内容

Python SIMD加速噪声

项目描述

PyFastNoiseSIMD

PyFastNoiseSIMD是围绕Jordan Peck的合成噪声库https://github.com/Auburns/FastNoise-SIMD的包装,该库已通过SIMD指令集进行了加速。它可以通过pip进行安装

pip install pyfastnoisesimd

通过使用concurrent.futures进行多线程来进一步增强并行性,以生成大型数组中的噪声。线程扩展通常在50-90%之间,这主要取决于使用的矢量化指令集。线程数默认为系统上的虚拟核心数。理想的线程数通常是物理核心数,不受Intel Hyperthreading®影响。

以下是一个简单的示例,用于在3D矩形网格上生成Perlin风格的噪声:

import pyfastnoisesimd as fns
import numpy as np
shape = [512, 512, 512]
seed = np.random.randint(2**31)
N_threads = 4

perlin = fns.Noise(seed=seed, numWorkers=N_threads)
perlin.frequency = 0.02
perlin.noiseType = fns.NoiseType.Perlin
perlin.fractal.octaves = 4
perlin.fractal.lacunarity = 2.1
perlin.fractal.gain = 0.45
perlin.perturb.perturbType = fns.PerturbType.NoPerturb
result = perlin.genAsGrid(shape)

其中 result 是一个dtype为 'float32' 的3D numpy.ndarray。另外,用户可以通过Noise.getFromCoords(coords)提供坐标,这对于自定义凹凸映射细分表面等任务很有帮助。

更详细的示例可以在GitHub存储库上的examples文件夹中找到。

通过使用concurrent.futures进行多线程来进一步增强并行性,以生成大型数组中的噪声。线程扩展通常在50-90%之间,这主要取决于使用的矢量化指令集。线程数默认为系统上的虚拟核心数。理想的线程数通常是物理核心数,不受Intel Hyperthreading®影响。

文档

请查看

http://pyfastnoisesimd.readthedocs.io

安装

pyfastnoisesimd可在PyPI上找到,并且可以通过pip进行安装:

pip install --upgrade pip
pip install --upgrade setuptools
pip install -v pyfastnoisesimd

通过manywheels提供了Windows、Mac OSX和Linux的wheel。

在Windows上从源代码构建需要适合您Python版本的MS Visual Studio版本或MSVC构建工具。

http://landinghub.visualstudio.com/visual-cpp-build-tools

在Linux或OSX上,只提供源代码分发,安装需要gccclang。对于GCC的AVX512支持,需要GCC7.2+,较低版本将仅编译支持AVX2/SSE4.1/SSE2。早于4.7版本的GCC将禁用AVX2。请注意,pip不尊重$CC环境变量,因此要使用gcc-7克隆和从源代码构建

git clone https://github.com/robbmcleod/pyfastnoisesimd.git
alias gcc=gcc-7; alias g++=g++-7
pip install -v ./pyfastnoisesimd

在Ubuntu上安装GCC7.2(使用sudo或作为root用户):

add-apt-repository ppa:ubuntu-toolchain-r/test
apt update
apt install gcc-7 g++-7

基准测试

一般来说,只有SSE4支持的机器上的线程扩展率更高,因为大多数CPU将时钟速度降低以限制AVX2产生的热量。因此,AVX2的速度大约比SSE4快1.5倍,而在纯SIMD指令长度的基础上(4对8),您会期望它快两倍。

第一个测试使用默认模式,一个立方网格,来自examples\gridded_noise.pyNoise.genAsGrid()

数组形状:[8,1024,1024] CPU:Intel i7-7820X Skylake-X(8核心,3.6 GHz),Windows 7 SIMD级别支持:AVX2 & FMA3

单线程模式

Computed 8388608 voxels cellular noise in 0.298 s
    35.5 ns/voxel
Computed 8388608 voxels Perlin noise in 0.054 s
    6.4 ns/voxel

Multi-threaded (8 threads) mode

在0.044秒内计算了8388608个细胞噪声体素,每体素5.2纳秒,线程扩展率685.0%。在0.013秒内计算了8388608个体素Perlin噪声,每体素1.5纳秒,线程扩展率431.3%

另一种模式是Noise.getFromCoords(),用户从examples\GallPeters_projection.py中提供笛卡尔空间中的坐标。

单线程模式

Generated noise from 2666000 coordinates with 1 workers in 1.766e-02 s
    6.6 ns/pixel

Multi-threaded (4 threads) mode

使用4个工作线程在6.161e-03秒内生成来自2666000个坐标的噪声,每像素2.3纳秒,线程扩展率286.6%

发布说明

0.4.2

  • 现在通过GitHub Actions构建Wheels,并在PyPi上提供。
  • 增加了对Python 3.8和3.9的支持。已删除对Python 3.5的支持。

0.4.1

  • Python 3.7现在为官方支持。在Windows上,AVX512仍然被禁用,因为即使在MSVC2017.3中,一些必需的SIMD指令也不可用。

0.4.0

  • 在Windows上修复了对齐内存位置,并为两个生成器启用了多线程处理。
  • emptyCoords函数重命名为empty_coords

0.3.2

  • 由于在Windows上导致段错误,禁用了对齐内存分配。
  • 感谢Luke H-W发现并修复了genAsGrid中的内存泄漏。
  • 感谢Enderlook报告说,在多线程模式下调用genAsGrid时,start参数不起作用。

0.3.1

  • 更改调用约定以避免64位和32位操作系统之间指针大小混淆。

0.3.0

  • Elliott Sales de Andrade修复了与安装相关的一些问题,以便干净地构建并更好地处理CPU SIMD功能。
  • Noise.genFromCoords()添加了多线程操作。
  • orthographic_projection.py添加到examples/
  • 更新文档字符串以适应sphinx.napoleon格式。
  • doc目录中添加了Sphinx文档。
  • 纠正了拼写错误PerturbType.NoPetrub -> PerturbType.NoPerturb
  • 停止释放fastnoisesimdNoise.genFromCoords(coords)coords参数分配的内存。现在应该可以在不发生段错误的情况下重用坐标。

0.2.1

  • 删除对Python 3.4的显式支持,因为我们无法在MSVC2010上为Windows进行测试,而且无论如何它也不会有AVX2指令支持。
  • 开始打标签,请参阅RELEASING_GUIDE.txt以获取说明。

0.2.0

  • 添加了提供坐标的功能
  • examples/projection.py添加到演示如何通过提供的坐标生成噪声,应用于球体(即世界地图)的Gall-Peters圆柱投影。
  • 添加了面向对象的接口Noise。使用Python属性在FastNoiseSIMD中公开NoiseSet/Get函数。
  • 添加了unittest支持。
  • 弃用了'kitchen sink' pyfastnoisesimd.generate()函数。
  • 将README从markdown更改为丰富结构化文本。
  • 修复了弃用的pyfastnoisesimd.generate()中的错误,该错误始终将种子设置为42。
  • 修复了拼写错误:axisScales -> axesScalesindicies -> indices

0.1.5

  • 为*nix使用所有小写目录。

0.1.4

  • 修复了多线程问题;当前方法将数组拆分至 min(threads, array.shape[0])

0.1.2

  • 为 PyPI 的源分发添加了 MANIFEST.in 文件

FastNoiseSIMD 库

如果您想与底层库有更直接的接口,可以使用 pyfastsimd._ext 模块,该模块将 C++ 代码逐函数映射。

FastNoiseSIMD 由 Jordan Peck 实现,可在以下地址找到

https://github.com/Auburns/FastNoiseSIMD

它旨在通过使用内建(SIMD)CPU 函数提供更快的性能。代码的矢量化允许噪声函数以 4/8/16 的数据集处理数据,在某些情况下(Simplex)性能提高 700%。

请参阅 Wiki 了解噪声类型的用法信息

https://github.com/Auburns/FastNoiseSIMD/wiki

GUI 基于的参考噪声生成器的下载链接可在以下位置找到

https://github.com/Auburns/FastNoiseSIMD/releases

作者

Robert A. McLeod 编写了 Python 包装器,实现了多线程,并编写了文档。

Elliott Sales de Andrade 贡献了许多修复,使许多平台上的构建成功。

Jordan Peck 编写了底层库 FastNoiseSIMD

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

pyfastnoisesimd-0.4.2.tar.gz (52.4 kB 查看哈希值)

上传时间

构建分发

pyfastnoisesimd-0.4.2-cp39-cp39-win_amd64.whl (249.0 kB 查看哈希值)

上传时间 CPython 3.9 Windows x86-64

pyfastnoisesimd-0.4.2-cp39-cp39-win32.whl (283.4 kB 查看哈希值)

上传时间 CPython 3.9 Windows x86

pyfastnoisesimd-0.4.2-cp39-cp39-manylinux2010_x86_64.whl (3.5 MB 查看哈希值)

上传时间 CPython 3.9 manylinux: glibc 2.12+ x86-64

pyfastnoisesimd-0.4.2-cp39-cp39-manylinux2010_i686.whl (3.8 MB 查看哈希值)

上传时间 CPython 3.9 manylinux: glibc 2.12+ i686

pyfastnoisesimd-0.4.2-cp39-cp39-manylinux1_x86_64.whl (3.5 MB 查看哈希值)

上传时间 CPython 3.9

pyfastnoisesimd-0.4.2-cp39-cp39-manylinux1_i686.whl (3.8 MB 查看哈希值)

上传时间 CPython 3.9

pyfastnoisesimd-0.4.2-cp39-cp39-macosx_10_9_x86_64.whl (322.5 kB 查看哈希值)

上传时间 CPython 3.9 macOS 10.9+ x86-64

pyfastnoisesimd-0.4.2-cp38-cp38-win_amd64.whl (248.9 kB 查看哈希值)

上传时间 CPython 3.8 Windows x86-64

pyfastnoisesimd-0.4.2-cp38-cp38-win32.whl (283.5 kB 查看哈希值)

上传时间 CPython 3.8 Windows x86

pyfastnoisesimd-0.4.2-cp38-cp38-manylinux2010_x86_64.whl (3.5 MB 查看哈希值)

上传时间 CPython 3.8 manylinux: glibc 2.12+ x86-64

pyfastnoisesimd-0.4.2-cp38-cp38-manylinux2010_i686.whl (3.8 MB 查看哈希值)

上传时间 CPython 3.8 manylinux: glibc 2.12+ i686

pyfastnoisesimd-0.4.2-cp38-cp38-manylinux1_x86_64.whl (3.5 MB 查看哈希值)

上传时间 CPython 3.8

pyfastnoisesimd-0.4.2-cp38-cp38-manylinux1_i686.whl (3.8 MB 查看哈希值)

上传时间 CPython 3.8

pyfastnoisesimd-0.4.2-cp38-cp38-macosx_10_9_x86_64.whl (322.5 kB 查看哈希值)

上传时间 CPython 3.8 macOS 10.9+ x86-64

pyfastnoisesimd-0.4.2-cp37-cp37m-win_amd64.whl (248.9 kB 查看哈希值)

上传于 CPython 3.7m Windows x86-64

pyfastnoisesimd-0.4.2-cp37-cp37m-win32.whl (283.3 kB 查看哈希值)

上传于 CPython 3.7m Windows x86

pyfastnoisesimd-0.4.2-cp37-cp37m-manylinux2010_x86_64.whl (3.5 MB 查看哈希值)

上传于 CPython 3.7m manylinux: glibc 2.12+ x86-64

pyfastnoisesimd-0.4.2-cp37-cp37m-manylinux2010_i686.whl (3.8 MB 查看哈希值)

上传于 CPython 3.7m manylinux: glibc 2.12+ i686

pyfastnoisesimd-0.4.2-cp37-cp37m-manylinux1_x86_64.whl (3.5 MB 查看哈希值)

上传于 CPython 3.7m

pyfastnoisesimd-0.4.2-cp37-cp37m-manylinux1_i686.whl (3.8 MB 查看哈希值)

上传于 CPython 3.7m

pyfastnoisesimd-0.4.2-cp37-cp37m-macosx_10_9_x86_64.whl (322.3 kB 查看哈希值)

上传于 CPython 3.7m macOS 10.9+ x86-64

pyfastnoisesimd-0.4.2-cp36-cp36m-win_amd64.whl (248.8 kB 查看哈希值)

上传于 CPython 3.6m Windows x86-64

pyfastnoisesimd-0.4.2-cp36-cp36m-win32.whl (283.2 kB 查看哈希值)

上传于 CPython 3.6m Windows x86

pyfastnoisesimd-0.4.2-cp36-cp36m-manylinux2010_x86_64.whl (3.5 MB 查看哈希值)

上传于 CPython 3.6m manylinux: glibc 2.12+ x86-64

pyfastnoisesimd-0.4.2-cp36-cp36m-manylinux2010_i686.whl (3.8 MB 查看哈希值)

上传于 CPython 3.6m manylinux: glibc 2.12+ i686

pyfastnoisesimd-0.4.2-cp36-cp36m-manylinux1_x86_64.whl (3.5 MB 查看哈希值)

上传于 CPython 3.6m

pyfastnoisesimd-0.4.2-cp36-cp36m-manylinux1_i686.whl (3.8 MB 查看哈希值)

上传于 CPython 3.6m

pyfastnoisesimd-0.4.2-cp36-cp36m-macosx_10_9_x86_64.whl (322.3 kB 查看哈希值)

上传于 CPython 3.6m macOS 10.9+ x86-64

支持者