C++学习系列(13):C++ 并行算法(Parallel Algorithms)
1. 引言
在现代计算机上,多核 CPU 和 GPU 使并行计算成为提升程序性能的重要手段。C++17 引入了 并行算法(Parallel Algorithms),大大简化了并行编程,使得 标准算法(如 sort、reduce、for_each)能够并行执行,充分利用多线程能力。
本篇博客将介绍:
- C++17 并行算法的基本概念
- 并行执行策略
std::execution::par、par_unseq - 如何使用并行版本的
std::sort、std::reduce、std::for_each - 并行算法的性能对比与应用场景
2. C++17 并行算法概述
C++17 通过 <execution> 头文件引入了 并行执行策略,用于加速标准算法:
| 执行策略 | 说明 |
|---|---|
std::execution::seq | 默认顺序执行(单线程) |
std::execution::par | 并行执行(多线程) |
std::execution::par_unseq | 并行 & 向量化执行(可能使用 SIMD 指令) |
📌 示例
#include <algorithm>
#include <execution>
#include <vector>
#include <iostream>
int main() {
std::vector<int> data = {5, 2, 8, 4, 1};
std::sort(std::execution::par, data.begin(), data.end()); // 并行排序
for (int n : data) std::cout << n << " ";
}
✅ std::execution::par 并行执行 std::sort,提升排序速度
3. std::sort 并行排序
3.1 顺序排序
std::sort(std::execution::seq, data.begin(), data.end()); // 单线程
📌 默认使用单线程执行
3.2 并行排序
std::sort(std::execution::par, data.begin(), data.end()); // 并行排序
📌 多线程加速排序
3.3 par_unseq 并行 + 向量化
std::sort(std::execution::par_unseq, data.begin(), data.end()); // 可能使用 SIMD 加速
📌 适用于 CPU 支持 SIMD(如 AVX 指令集)
4. std::reduce 并行累加
C++17 提供 std::reduce() 用于并行累加,比 std::accumulate() 更快。
4.1 传统 std::accumulate
#include <numeric>
#include <vector>
#include <iostream>
int main() {
std::vector<int> data(1000000, 1);
int sum = std::accumulate(data.begin(), data.end(), 0); // 单线程
std::cout << "Sum: " << sum << std::endl;
}
📌 单线程计算,速度较慢
4.2 std::reduce 并行加速
#include <numeric>
#include <execution>
#include <vector>
#include <iostream>
int main() {
std::vector<int> data(1000000, 1);
int sum = std::reduce(std::execution::par, data.begin(), data.end()); // 并行计算
std::cout << "Sum: " << sum << std::endl;
}
✅ 相比 std::accumulate(),std::reduce() 采用 无锁并行执行,更高效
5. std::for_each 并行遍历
std::for_each() 可用于 并行处理数组元素。
5.1 顺序执行
std::for_each(std::execution::seq, data.begin(), data.end(), [](int &x) {
x *= 2;
});
📌 单线程执行
5.2 并行执行
std::for_each(std::execution::par, data.begin(), data.end(), [](int &x) {
x *= 2;
});
✅ 多线程加速数据处理
6. 并行算法性能对比
| 算法 | 单线程(seq) | 并行(par) | 向量化(par_unseq) |
|---|---|---|---|
std::sort | ❌ 较慢 | ✅ 提升 2~4 倍 | ✅ 进一步优化 |
std::reduce | ❌ 较慢 | ✅ 大幅加速 | ✅ 适用于 SIMD |
std::for_each | ❌ 较慢 | ✅ 并行计算 | ✅ 适合大数据 |
📌 建议
- 数据量 小(<10000):单线程(seq)
- 数据量 大(>1000000):并行(par)
- 计算 向量化(如浮点运算):par_unseq
7. 总结
✅ C++17 并行算法加速计算
✅ std::sort 支持并行排序
✅ std::reduce 替代 std::accumulate,提升性能
✅ std::for_each 并行处理数据
✅ 合理选择执行策略 seq、par、par_unseq
📢 下一篇 C++学习系列(14):C++ 文件操作(文件读写、流操作),敬请期待!🚀
💡 如果你喜欢这篇文章,欢迎点赞、收藏,并关注本系列!
978

被折叠的 条评论
为什么被折叠?



