C++学习系列(13):C++ 并行算法(Parallel Algorithms)

部署运行你感兴趣的模型镜像

C++学习系列(13):C++ 并行算法(Parallel Algorithms)

1. 引言

在现代计算机上,多核 CPU 和 GPU 使并行计算成为提升程序性能的重要手段。C++17 引入了 并行算法(Parallel Algorithms),大大简化了并行编程,使得 标准算法(如 sortreducefor_each)能够并行执行,充分利用多线程能力。

本篇博客将介绍:

  • C++17 并行算法的基本概念
  • 并行执行策略 std::execution::parpar_unseq
  • 如何使用并行版本的 std::sortstd::reducestd::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 并行处理数据
合理选择执行策略 seqparpar_unseq


📢 下一篇 C++学习系列(14):C++ 文件操作(文件读写、流操作),敬请期待!🚀

💡 如果你喜欢这篇文章,欢迎点赞、收藏,并关注本系列!

您可能感兴趣的与本文相关的镜像

Wan2.2-I2V-A14B

Wan2.2-I2V-A14B

图生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

Author(s) Henri Casanova (Author), Arnaud Legrand (Author), Yves Robert (Author) Publisher: Chapman and Hall/CRC (July 17, 2008) ISBN-10: 1584889454 ISBN-13: 978-1584889458 Book Description Focusing on algorithms for distributed-memory parallel architectures, Parallel Algorithms presents a rigorous yet accessible treatment of theoretical models of parallel computation, parallel algorithm design for homogeneous and heterogeneous platforms, complexity and performance analysis, and essential notions of scheduling. The book extracts fundamental ideas and algorithmic principles from the mass of parallel algorithm expertise and practical implementations developed over the last few decades. In the first section of the text, the authors cover two classical theoretical models of parallel computation (PRAMs and sorting networks), describe network models for topology and performance, and define several classical communication primitives. The next part deals with parallel algorithms on ring and grid logical topologies as well as the issue of load balancing on heterogeneous computing platforms. The final section presents basic results and approaches for common scheduling problems that arise when developing parallel algorithms. It also discusses advanced scheduling topics, such as divisible load scheduling and steady-state scheduling. With numerous examples and exercises in each chapter, this text encompasses both the theoretical foundations of parallel algorithms and practical parallel algorithm design.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值