当前位置: 首页 > article >正文

windows C++-并行编程-将使用缩减变量的 OpenMP 循环转换为使用并发运行时

此示例介绍如何将使用 reduction 子句的 OpenMP parallelfor loop 转换为使用并发运行时。

OpenMP reduction 子句允许指定一个或多个线程专用变量,这些变量受并行区域末尾的缩减操作的约束。 OpenMP 预定义一组缩减运算符。 每个减量变量必须是标量(例如 int、long 和 float)。 OpenMP 还定义了一些限制,说明如何在并行区域中使用缩减变量。

并行模式库 (PPL) 提供 concurrency::combinable 类,此类提供了可重用的线程本地存储,该存储允许你执行细化的计算,然后将这些计算合并为最终的结果。 类 combinable 是对标量和复杂类型执行操作的模板。 若要使用 combinable 类,请在并行构造的正文中执行子计算,然后调用 concurrency::combinable::combine 或 concurrency::combinable::combine_each 方法生成最终结果。 combine 和 combine_each 方法都采用组合函数来指定如何组合每对元素。 因此,类 combinable 不限于一组固定的缩减运算符。

示例

此示例使用 OpenMP 和并发运行时来计算前 35 个 Fibonacci 数字的总和。

// concrt-omp-fibonacci-reduction.cpp
// compile with: /EHsc /openmp
#include <ppl.h>
#include <iostream>

using namespace concurrency;
using namespace std;

// Computes the nth Fibonacci number.
// This function illustrates a lengthy operation and is therefore
// not optimized for performance.
int fibonacci(int n)
{
   if (n < 2)
      return n;

   // Compute the components in parallel.
   int n1, n2;
   parallel_invoke(
      [n,&n1] { n1 = fibonacci(n-1); },
      [n,&n2] { n2 = fibonacci(n-2); }
   );

   return n1 + n2;
}

// Uses OpenMP to compute the sum of Fibonacci numbers in parallel.
void omp_parallel_fibonacci_sum(int count)
{
   int sum = 0;
   #pragma omp parallel for reduction(+ : sum)
      for (int i = 0; i < count; ++i)
      {
         sum += fibonacci(i);
      }

   wcout << L"The sum of the first " << count << L" Fibonacci numbers is " 
         << sum << L'.' << endl;
}

// Uses the Concurrency Runtime to compute the sum of Fibonacci numbers in parallel.
void concrt_parallel_fibonacci_sum(int count) 
{
   combinable<int> sums;
   parallel_for(0, count, [&sums](int i)
      {
         sums.local() += fibonacci(i);
      });

   wcout << L"The sum of the first " << count << L" Fibonacci numbers is " 
         << sums.combine(plus<int>()) << L'.' << endl;
}

int wmain()
{
   const int count = 35;

   wcout << L"Using OpenMP..." << endl;
   omp_parallel_fibonacci_sum(count);

   wcout << L"Using the Concurrency Runtime..." << endl;
   concrt_parallel_fibonacci_sum(count);
}

本示例生成以下输出。

Using OpenMP...
The sum of the first 35 Fibonacci numbers is 14930351.
Using the Concurrency Runtime...
The sum of the first 35 Fibonacci numbers is 14930351.
编译代码

复制示例代码,并将它粘贴到 Visual Studio 项目中,或粘贴到名为 concrt-omp-fibonacci-reduction.cpp 的文件中,再在 Visual Studio 命令提示符窗口中运行以下命令。

cl.exe /EHsc /openmp concrt-omp-fibonacci-reduction.cpp


http://www.kler.cn/news/295185.html

相关文章:

  • 经验笔记:负载均衡
  • Hive的优势与使用场景
  • WebTopo 组态软件+ARM 工业计算机:重塑污水处理
  • macos系统内置php文件列表 系统自带php卸载方法
  • 周报2024、9、8
  • 消息认证码(MAC)
  • HTTP与HTTPS:网络通信的安全之旅
  • 通信工程学习:什么是AB地址总线、DB数据总线、CD控制总线
  • 今日早报 每日精选15条新闻简报 每天一分钟 知晓天下事 9月8日,星期日
  • [动态规划] 删除并获得点数
  • el-table 封装表格(完整代码-实时更新)
  • 【技术调研】三维(0)-webGL、三维基础知识、前置知识、数学知识以及简单示例
  • 【Linux】服务器上在VSCode中运行JupyterNotebook
  • Exchange 服务器地址列表的配置方法与注意事项
  • 物联网之MQTT
  • 计算机视觉中,如何理解自适应和注意力机制的关系?
  • 云手机怎样简化海外社媒平台运营
  • 网关功能介绍
  • ffmpeg命令(详解)
  • 什么是GPT-3的自回归架构?为什么GPT-3无需梯度更新和微调
  • 数学基础 -- 统计学之零均值化
  • 小米Vela:端侧AI推理框架
  • 域名证书,泛域名证书,sni
  • 测试一些概念
  • Flutter集成Firebase框架
  • unity 实现吸血鬼幸存者的随机奖励
  • 基于stm32的河流检测系统-单片机毕业设计
  • u盘显示需要格式化才能用预警下的数据拯救恢复指南
  • CNC数控加工如何开启个性化制造新时代?
  • C++数据结构重要知识点(5)(哈希表、unordered_map和unordered_set封装)