1. oneDNN(Intel® oneAPI Deep Neural Network Library)
-
简介
oneDNN 是 Intel 开源的深度学习神经网络加速库,专为 CPU 和 GPU 上的深度学习推理和训练优化。它提供高效的底层算子(如卷积、池化、矩阵乘法等),支持多种深度学习框架(如 TensorFlow、PyTorch)作为后端。 -
核心功能
- 高性能的神经网络算子(如卷积、RNN、BatchNorm)。
- 支持 INT8 量化推理。
- 自动内存管理和线程调度优化。
-
使用示例(C++)
以下是一个简单的卷积操作示例:#include <oneapi/dnnl/dnnl.hpp> using namespace dnnl; void demo_convolution() { engine eng(engine::kind::cpu, 0); stream s(eng); // 定义输入、权重、输出的维度 memory::dims input_dims = {1, 3, 224, 224}; // NCHW格式 memory::dims weight_dims = {64, 3, 3, 3}; // OIHW格式 memory::dims output_dims = {1, 64, 222, 222}; // 创建内存对象 auto input_mem = memory({{input_dims}, memory::data_type::f32, memory::format_tag::nchw}, eng); auto weight_mem = memory({{weight_dims}, memory::data_type::f32, memory::format_tag::oihw}, eng); auto output_mem = memory({{output_dims}, memory::data_type::f32, memory::format_tag::nchw}, eng); // 定义卷积算子 auto conv_desc = convolution_forward::desc( prop_kind::forward_inference, algorithm::convolution_auto, input_mem.get_desc(), weight_mem.get_desc(), output_mem.get_desc(), {1, 1}, // Stride {0, 0} // Padding ); auto conv_prim = convolution_forward(conv_desc, eng); // 执行卷积 conv_prim.execute(s, { {DNNL_ARG_SRC, input_mem}, {DNNL_ARG_WEIGHTS, weight_mem}, {DNNL_ARG_DST, output_mem} }); s.wait(); }
-
编译命令
g++ -O2 demo.cpp -o demo -ldnnl
2. oneMKL(Intel® oneAPI Math Kernel Library)
-
简介
oneMKL 是高性能数学库,提供优化的数学函数(如 BLAS、LAPACK、FFT、随机数生成等),支持 CPU、GPU 和 FPGA,适用于科学计算、金融分析等领域。 -
核心功能
- 线性代数(矩阵乘法、分解)。
- 快速傅里叶变换(FFT)。
- 统计函数(随机数生成、统计分布)。
-
使用示例(矩阵乘法)
#include <CL/sycl.hpp> #include <oneapi/mkl.hpp> using namespace sycl; using namespace oneapi::mkl; void demo_gemm() { queue q; const int m = 1024, n = 1024, k = 1024; float *A = malloc_shared<float>(m * k, q); float *B = malloc_shared<float>(k * n, q); float *C = malloc_shared<float>(m * n, q); // 填充数据(略) blas::gemm(q, transpose::N, transpose::N, m, n, k, 1.0f, A, k, B, n, 0.0f, C, n); q.wait(); }
-
编译命令
g++ -O2 demo.cpp -o demo -lmkl_sycl -lmkl_intel_ilp64 -lmkl_core
3. oneTBB(Intel® oneAPI Threading Building Blocks)
-
简介
oneTBB 是 C++ 并行编程库,提供任务调度、并行算法(如parallel_for
)和并发容器,简化多线程开发,支持动态负载均衡。 -
核心功能
- 并行循环(
parallel_for
、parallel_reduce
)。 - 任务调度器(自动管理线程池)。
- 并发容器(如
concurrent_queue
)。
- 并行循环(
-
使用示例(并行求和)
#include <tbb/parallel_reduce.h> #include <tbb/blocked_range.h> #include <vector> using namespace tbb; float parallel_sum(const std::vector<float>& data) { return parallel_reduce( blocked_range<size_t>(0, data.size()), 0.0f, [&](const blocked_range<size_t>& r, float init) { for (size_t i = r.begin(); i < r.end(); ++i) init += data[i]; return init; }, [](float a, float b) { return a + b; } ); }
-
编译命令
g++ -O2 demo.cpp -o demo -ltbb
总结
库名 | 应用场景 | 优势 |
---|---|---|
oneDNN | 深度学习推理/训练 | 低延迟、高吞吐量,支持量化 |
oneMKL | 科学计算、数值分析 | 数学函数高度优化,跨硬件支持 |
oneTBB | 多线程任务并行 | 简化并行编程,动态负载均衡 |
- 协作示例:在深度学习训练中,可用 oneTBB 管理线程池,oneMKL 加速矩阵运算,oneDNN 实现卷积层。
- 共同点:均属于 oneAPI 生态,支持跨 CPU/GPU 异构计算。
如需进一步优化,建议参考官方文档调整参数(如内存布局、线程数)。