一、软件介绍
文末提供程序和源码下载
Simd 库是一个免费的开源图像处理和机器学习库,专为 C 和 C++ 程序员设计。它为图像处理提供了许多有用的高性能算法,例如:像素格式转换、图像缩放和过滤、从图像中提取统计信息、运动检测、对象检测和分类、神经网络。使用不同的 SIMD CPU 扩展对算法进行了优化。特别是,该库支持以下 CPU 扩展:用于 x86/x64 的 SSE、AVX、AVX-512 和 AMX,以及用于 ARM 的 NEON。Simd 库具有 C API,还包含有用的 C++ 类和函数,以方便访问 C API。该库支持动态和静态链接、32 位和 64 位 Windows 和 Linux、MSVS、G++ 和 Clang 编译器、MSVS 项目和 CMake 构建系统。
二、Library 文件夹的结构
Simd 库具有 next 文件夹的结构:
simd/src/Simd/
- contains source codes of the library.
simd/src/Simd/
- 包含库的源代码。simd/src/Test/
- contains test framework of the library.
simd/src/Test/
- 包含库的测试框架。simd/src/Use/
- contains the use examples of the library.
simd/src/Use/
- 包含库的使用示例。simd/py/SimdPy/
- contains Python wrapper of the library.
simd/py/SimdPy/
- 包含库的 Python 包装器。simd/prj/vs2015/
- contains project files of Microsoft Visual Studio 2015.
simd/prj/vs2015/
- 包含 Microsoft Visual Studio 2015 的项目文件。simd/prj/vs2017/
- contains project files of Microsoft Visual Studio 2017.
simd/prj/vs2017/
- 包含 Microsoft Visual Studio 2017 的项目文件。simd/prj/vs2019/
- contains project files of Microsoft Visual Studio 2019.
simd/prj/vs2019/
- 包含 Microsoft Visual Studio 2019 的项目文件。simd/prj/vs2022/
- contains project files of Microsoft Visual Studio 2022.
simd/prj/vs2022/
- 包含 Microsoft Visual Studio 2022 的项目文件。simd/prj/cmd/
- contains additional scripts needed for building of the library in Windows.
simd/prj/cmd/
- 包含在 Windows 中构建库所需的其他脚本。simd/prj/cmake/
- contains files of CMake build systems.
simd/prj/cmake/
- 包含 CMake 构建系统的文件。simd/prj/sh/
- contains additional scripts needed for building of the library in Linux.
simd/prj/sh/
- 包含在 Linux 中构建库所需的其他脚本。simd/prj/txt/
- contains text files needed for building of the library.
simd/prj/txt/
- 包含构建库所需的文本文件。simd/data/cascade/
- contains OpenCV cascades (HAAR and LBP).
simd/data/cascade/
- 包含 OpenCV 级联(HAAR 和 LBP)。simd/data/image/
- contains image samples.
simd/data/image/
- 包含图像样本。simd/data/network/
- contains examples of trained networks.
simd/data/network/
- 包含经过训练的网络示例。simd/docs/
- contains documentation of the library.
simd/docs/
- 包含库的文档。
三、构建适用于 Windows 的库
o build the library and test application for Windows 32/64 you need to use Microsoft Visual Studio 2022 (or 2015/2017/2019). The project files are in the directory:
要为 Windows 32/64 构建库和测试应用程序,您需要使用 Microsoft Visual Studio 2022(或 2015/2017/2019)。项目文件位于以下目录中:
simd/prj/vs2022/
By default the library is built as a DLL (Dynamic Linked Library). You also may build it as a static library. To do this you must change appropriate property (Configuration Type) of Simd project and also uncomment #define SIMD_STATIC
in file:
默认情况下,该库构建为 DLL(动态链接库)。您也可以将其构建为静态库。为此,您必须更改 Simd 项目的相应属性(配置类型),并取消注释文件中 #define SIMD_STATIC
:
simd/src/Simd/SimdConfig.h
Also in order to build the library you can use CMake and MinGW:
此外,为了构建库,您可以使用 CMake 和 MinGW:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>mkdir build
cd build
cmake ..\prj\cmake -DSIMD_TOOLCHAIN="your_toolchain\bin\g++" -DSIMD_TARGET="x86_64" -DCMAKE_BUILD_TYPE="Release" -G "MinGW Makefiles"
mingw32-make
</code></span></span></span></span>
四、Building the library for Linux构建适用于 Linux 的库
To build the library and test application for Linux 32/64 you need to use CMake build systems. Files of CMake build systems are placed in the directory:
要为 Linux 32/64 构建库和测试应用程序,您需要使用 CMake 构建系统。CMake 构建系统的文件放在目录中:
simd/prj/cmake/
The library can be built for x86/x64, ARM(32/64) platforms using the G++ or Clang compilers. Using the native compiler (g++) for the current platform is simple:
该库可以使用 G++ 或 Clang 编译器为 x86/x64、ARM(32/64) 平台构建。使用当前平台的本机编译器 (g++) 很简单:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>mkdir build
cd build
cmake ../prj/cmake -DSIMD_TOOLCHAIN="" -DSIMD_TARGET=""
make
</code></span></span></span></span>
To build the library for ARM(32/64) platform you can also use a toolchain for cross compilation. There is an example of using for ARM (32 bit):
要为 ARM(32/64) 平台构建库,您还可以使用工具链进行交叉编译。有一个用于 ARM(32 位)的示例:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>mkdir build
cd build
cmake ../prj/cmake -DSIMD_TOOLCHAIN="/your_toolchain/usr/bin/arm-linux-gnueabihf-g++" -DSIMD_TARGET="arm" -DCMAKE_BUILD_TYPE="Release"
make
</code></span></span></span></span>
And for ARM (64 bit):
对于 ARM(64 位):
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>mkdir build
cd build
cmake ../prj/cmake -DSIMD_TOOLCHAIN="/your_toolchain/usr/bin/aarch64-linux-gnu-g++" -DSIMD_TARGET="aarch64" -DCMAKE_BUILD_TYPE="Release"
make
</code></span></span></span></span>
As result the library and the test application will be built in the current directory.
因此,将在当前目录中构建库和测试应用程序。
There are addition build parameters:
还有其他 build 参数:
SIMD_AVX512
- Enable of AVX-512 (AVX-512F, AVX-512CD, AVX-512VL, AVX-512DQ, AVX-512BW) CPU extensions. It is switched on by default.
SIMD_AVX512
- 启用 AVX-512(AVX-512F、AVX-512CD、AVX-512VL、AVX-512DQ、AVX-512BW)CPU 扩展。默认情况下,它是打开的。SIMD_AVX512VNNI
- Enable of AVX-512-VNNI CPU extensions. It is switched on by default.
SIMD_AVX512VNNI
- 启用 AVX-512-VNNI CPU 扩展。默认情况下,它是打开的。SIMD_AMXBF16
- Enable of AMX-BF16, AMX-INT8 and AVX-512-BF16 CPU extensions. It is switched off by default.
SIMD_AMXBF16
- 启用 AMX-BF16、AMX-INT8 和 AVX-512-BF16 CPU 扩展。默认情况下,它是关闭的。SIMD_TEST
- Build test framework. It is switched on by default.
SIMD_TEST
- 构建测试框架。默认情况下,它是打开的。SIMD_INFO
- Print build information. It is switched on by default.
SIMD_INFO
- 打印生成信息。默认情况下,它是打开的。SIMD_PERF
- Enable of internal performance statistic. It is switched off by default.
SIMD_PERF
- 启用内部绩效统计。默认情况下,它是关闭的。SIMD_SHARED
- Build as SHARED library. It is switched off by default.
SIMD_SHARED
- 构建为 SHARED 库。默认情况下,它是关闭的。SIMD_GET_VERSION
- Call scipt to get Simd Library version. It is switched on by default.
SIMD_GET_VERSION
- 调用 scipt 以获取 Simd 库版本。默认情况下,它是打开的。SIMD_SYNET
- Enable optimizations for Synet framework. It is switched on by default.
SIMD_SYNET
- 启用 Synet 框架的优化。默认情况下,它是打开的。SIMD_INT8_DEBUG
- Enable debug INT8 capabilities for Synet framework. It is switched off by default.
SIMD_INT8_DEBUG
- 为 Synet 框架启用调试 INT8 功能。默认情况下,它是关闭的。SIMD_HIDE
- Hide internal functions of Simd Library. It is switched off by default.
SIMD_HIDE
- 隐藏 Simd 库的内部功能。默认情况下,它是关闭的。SIMD_RUNTIME
- Enable of runtime faster algorithm choise. It is switched on by default.
SIMD_RUNTIME
- 启用运行时更快的算法选择。默认情况下,它是打开的。SIMD_TEST_FLAGS
- Addition compiler flags to build test framework.
SIMD_TEST_FLAGS
- 添加了编译器标志以构建测试框架。SIMD_OPENCV
- Use OpenCV in test framework. It is switched off by default.
SIMD_OPENCV
- 在测试框架中使用 OpenCV。默认情况下,它是关闭的。SIMD_INSTALL
- Enabling of install target. It is switched on by default.
SIMD_INSTALL
- 启用安装目标。默认情况下,它是打开的。SIMD_UNINSTALL
- Enabling of uninstall target. It is switched on by default.
SIMD_UNINSTALL
- 启用卸载目标。默认情况下,它是打开的。SIMD_PYTHON
- Enabling of Simd Python wrapper. It is switched on by default.
SIMD_PYTHON
- 启用 Simd Python 包装器。默认情况下,它是打开的。
五、Using the library 使用库
If you use the library from C code you must include:
如果使用 C 代码中的库,则必须包含:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>#include "Simd/SimdLib.h"
</code></span></span></span></span>
And to use the library from C++ code you must include:
要从 C++ 代码中使用该库,您必须包括:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>#include "Simd/SimdLib.hpp"
</code></span></span></span></span>
In order to use Simd::Detection you must include:
要使用 Simd::D etection,您必须包括:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>#include "Simd/SimdDetection.hpp"
</code></span></span></span></span>
In order to use Simd::Neural you must include:
为了使用 Simd::Neural,您必须包括:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>#include "Simd/SimdNeural.hpp"
</code></span></span></span></span>
In order to use Simd::Motion you must include:
为了使用 Simd::Motion,您必须包括:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>#include "Simd/SimdMotion.hpp"
</code></span></span></span></span>
Package Managers 包管理器
You can download and install simd using the vcpkg dependency manager:
您可以使用 vcpkg 依赖项管理器下载并安装 simd:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install simd
</code></span></span></span></span>
The simd port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
vcpkg 中的 simd 端口由 Microsoft 团队成员和社区参与者保持最新状态。如果版本已过期,请在 vcpkg 存储库上创建议题或拉取请求。
Interaction with OpenCV 与 OpenCV 交互
If you need to use mutual conversion between Simd and OpenCV types you just have to define macro SIMD_OPENCV_ENABLE
before including of Simd headers:
如果你需要在 Simd 和 OpenCV 类型之间使用相互转换,你只需要 SIMD_OPENCV_ENABLE
在包含 Simd 头文件之前定义宏:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>#include <opencv2/core/core.hpp>
#define SIMD_OPENCV_ENABLE
#include "Simd/SimdLib.hpp"
</code></span></span></span></span>
And you can convert next types:
您可以转换 next 类型:
cv::Point
,cv::Size
<-->Simd::Point
.
cv::Point
,cv::Size
<-->Simd::Point
.cv::Rect
<-->Simd::Rectangle
.cv::Mat
<-->Simd::View
.
Test Framework 测试框架
The test suite is needed for testing of correctness of work of the library and also for its performance testing. There is a set of tests for every function from API of the library. There is an example of test application using:
测试套件用于测试库工作的正确性以及其性能测试。库的 API 中的每个函数都有一组测试。有一个测试应用程序使用以下示例:
<span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><span style="color:#1f2328"><span style="color:var(--fgColor-default, var(--color-fg-default))"><span style="background-color:var(--bgColor-muted, var(--color-canvas-subtle))"><code>./Test -m=a -tt=1 -f=Sobel -ot=log.txt
</code></span></span></span></span>
Where next parameters were used:
使用 next 参数的位置:
-m=a
- a auto checking mode which includes performance testing (only for library built in Release mode). In this case different implementations of each functions will be compared between themselves (for example a scalar implementation and implementations with using of different SIMD instructions such as SSE2, AVX2, and other). Also it can be-m=s
(running of special tests).
-m=a
- 自动检查模式,包括性能测试(仅适用于在 Release 模式下构建的库)。在这种情况下,将比较每个函数的不同实现(例如,标量实现和使用不同 SIMD 指令的实现,如 SSE2、AVX2 等)。也可以是-m=s
(运行特殊测试)。-tt=1
- a number of test threads. Use -1 to set maximum parallelization.
-tt=1
- 多个测试线程。使用 -1 设置最大并行化。-fi=Sobel
- an include filter. In current case will be tested only functions which contain word 'Sobel' in their names. If you miss this parameter then full testing will be performed. You can use several filters - function name has to satisfy at least one of them.
-fi=Sobel
- 一个 include 过滤器。在当前情况下,将仅测试名称中包含单词 'Sobel' 的函数。如果您错过了此参数,则将执行完整测试。您可以使用多个过滤器 - function name 必须至少满足其中一个过滤器。-ot=log.txt
- a file name with test report (in TEXT file format). The test's report also will be output to console.
-ot=log.txt
- 带有测试报告的文件名(TEXT 文件格式)。测试的报告也将输出到控制台。
Also you can use parameters:
您也可以使用参数:
--help
or-?
in order to print help message.
--help
或-?
为了打印帮助消息。-r=../..
to set project root directory.
-r=../..
设置项目根目录。-pa=1
to print alignment statistics.
-pa=1
打印对齐统计信息。-pi=1
to print internal statistics (Cmake parameter SIMD_PERF must be ON).
-pi=1
打印内部统计信息(Cmake 参数SIMD_PERF必须为 ON)。-c=512
a number of channels in test image for performance testing.
-c=512
测试映像中的多个通道,用于性能测试。-h=1080
a height of test image for performance testing.
-h=1080
用于性能测试的测试图像的高度。-w=1920
a width of test image for performance testing.
-w=1920
用于性能测试的测试图像的宽度。-oh=log.html
- a file name with test report (in HTML file format).
-oh=log.html
- 带有测试报告的文件名(HTML 文件格式)。-s=sample.avi
a video source (SeeSimd::Motion
test).
-s=sample.avi
视频源(请参阅Simd::Motion
test)。-o=output.avi
an annotated video output (SeeSimd::Motion
test).
-o=output.avi
带注释的视频输出(参见Simd::Motion
测试)。-wt=1
a thread number used to parallelize algorithms. Use -1 to set maximum parallelization.
-wt=1
用于并行化算法的线程号。使用 -1 设置最大并行化。-fe=Abs
an exclude filter to exclude some tests.
-fe=Abs
用于排除某些测试的 Exclude 筛选器。-mt=100
a minimal test execution time (in milliseconds).
-mt=100
最短测试执行时间(以毫秒为单位)。-lc=1
to litter CPU cache between test runs.
-lc=1
在测试运行之间丢弃 CPU 缓存。-ri=city.jpg
a name of real image used in some tests. The image have to be placed in./data/image
directory.
-ri=city.jpg
某些测试中使用的真实图像的名称。图像必须放在./data/image
目录中。-tr=2
a number of test execution repeats.
-tr=2
多次测试执行重复。-ts=1
to print statistics of time of tests execution.
-ts=1
打印测试执行时间的统计信息。-cc=1
to check c++ API.
-cc=1
检查 C++ API。-de=2
a flags of SIMD extensions which testing are disabled. Base - 1, 2 - SSE4.1/NEON, 4 - AVX2, 8 - AVX-512BW, 16 - AVX-512VNNI, 32 - AMX-BF16.
-de=2
禁用测试的 SIMD 扩展的标志。底座 - 1、2 - SSE4.1/NEON、4 - AVX2、8 - AVX-512BW、16 - AVX-512VNNI、32 - AMX-BF16。-wu=100
a time to warm up CPU before testing (in milliseconds).
-wu=100
测试前预热 CPU 的时间(以毫秒为单位)。-pt=1
a boolean flag to pin threads to cpu cores.
-pt=1
一个布尔标志,用于将线程固定到 CPU 内核。
六、软件下载
本文信息来源于GitHub作者地址:GitHub - ermig1979/Simd: C++ image processing and machine learning library with using of SIMD: SSE, AVX, AVX-512, AMX for x86/x64, NEON for ARM.