首页 > 系统相关 >windows C++ 并行编程-转换使用取消的 OpenMP 循环以使用并发运行时

windows C++ 并行编程-转换使用取消的 OpenMP 循环以使用并发运行时

时间:2024-09-06 23:21:03浏览次数:6  
标签:task 迭代 示例 windows C++ OpenMP parallel condition

某些并行循环不需要执行所有迭代。 例如,搜索值的算法可以在找到值后终止。 OpenMP 不提供中断并行循环的机制。 但是,可以使用布尔值或标志来启用循环迭代,以指示已找到解决方案。 并发运行时提供允许一个任务取消其他尚未启动的任务的功能。

此示例演示如何将一个不需要运行所有迭代的 OpenMP parallelfor 循环转换为使用并发运行时取消机制。

示例

此示例同时使用 OpenMP 和并发运行时来实现 std::any_of 算法的并行版本。 此示例的 OpenMP 版本使用标志来协调所有满足条件的并行循环迭代。 使用并发运行时的版本使用 concurrency::structured_task_group::cancel 方法在满足条件时停止整个操作。

// concrt-omp-parallel-any-of.cpp
// compile with: /EHsc /openmp
#include <ppl.h>
#include <array>
#include <random>
#include <iostream>

using namespace concurrency;
using namespace std;

// Uses OpenMP to determine whether a condition exists in 
// the specified range of elements.
template <class InIt, class Predicate>
bool omp_parallel_any_of(InIt first, InIt last, const Predicate& pr)
{
   typedef typename std::iterator_traits<InIt>::value_type item_type;

   // A flag that indicates that the condition exists.
   bool found = false;

   #pragma omp parallel for
      for (int i = 0; i < static_cast<int>(last-first); ++i)
      {
         if (!found)
         {
            item_type& cur = *(first + i);

            // If the element satisfies the condition, set the flag to 
            // cancel the operation.
            if (pr(cur)) {
               found = true;
            }
         }
      }

   return found;
}

// Uses the Concurrency Runtime to determine whether a condition exists in 
// the specified range of elements.
template <class InIt, class Predicate>
bool concrt_parallel_any_of(InIt first, InIt last, const Predicate& pr)
{
   typedef typename std::iterator_traits<InIt>::value_type item_type;

   structured_task_group tasks;

   // Create a predicate function that cancels the task group when
   // an element satisfies the condition.
   auto for_each_predicate = [&pr, &tasks](const item_type& cur) {
      if (pr(cur)) {
         tasks.cancel();
      }
   };

   // Create a task that calls the predicate function in parallel on each
   // element in the range.
   auto task = make_task([&]() {
       parallel_for_each(first, last, for_each_predicate);
   });

   // The condition is satisfied if the task group is in the cancelled state.
   return tasks.run_and_wait(task) == canceled;
}

int wmain()
{
   // The length of the array.
   const size_t size = 100000;
   
   // Create an array and initialize it with random values.
   array<int, size> a;   
   generate(begin(a), end(a), mt19937(42));

   // Search for a value in the array by using OpenMP and the Concurrency Runtime.

   const int what = 9114046;
   auto predicate = [what](int n) -> bool { 
      return (n == what);
   };

   wcout << L"Using OpenMP..." << endl;
   if (omp_parallel_any_of(begin(a), end(a), predicate))
   {
      wcout << what << L" is in the array." << endl;
   }
   else
   {
      wcout << what << L" is not in the array." << endl;
   }

   wcout << L"Using the Concurrency Runtime..." << endl;
   if (concrt_parallel_any_of(begin(a), end(a), predicate))
   {
      wcout << what << L" is in the array." << endl;
   }
   else
   {
      wcout << what << L" is not in the array." << endl;
   }
}

本示例生成以下输出。

Using OpenMP...
9114046 is in the array.
Using the Concurrency Runtime...
9114046 is in the array.

在使用 OpenMP 的版本中,将执行循环的所有迭代,即使设置了标志。 此外,如果任务具有任何子任务,则标志还必须可供这些子任务用来传达取消信息。 在并发运行时中,当任务组被取消时,运行时会取消整个工作树,包括子任务。 concurrency::parallel_for_each 算法使用任务来执行工作。 因此,当循环的一次迭代取消根任务时,也会取消整个计算树。 取消工作树后,运行时不会启动新任务。 但是,运行时允许已经开始的任务完成。 因此,对于 parallel_for_each 算法,活动循环迭代可以清理其资源。

在此示例的两个版本中,如果数组包含要搜索的值的多个副本,则多个循环迭代可以同时设置结果并取消整个操作。 如果问题要求在满足条件时只有一个任务执行工作,则可以使用同步基元,如关键部分。

编译代码

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

cl.exe /EHsc /openmp concrt-omp-parallel-any-of.cpp

标签:task,迭代,示例,windows,C++,OpenMP,parallel,condition
From: https://blog.csdn.net/m0_72813396/article/details/141575706

相关文章

  • windows C++ 并行编程-使用 加速器 对象(下)
    并发运行时支持各种编程模型。这些模型可能会与其他库的模型重叠或对其进行补充。本部分中的文档将OpenMP与并发运行时进行比较,并提供有关如何迁移现有OpenMP代码以使用并发运行时的示例。OpenMP编程模型由开放标准定义,具有与Fortran和C/C++编程语言定义完善的绑定......
  • 【Windows系列】at(计划任务)
    ps:WindowsServer2012以后的版本没有at命令,只有schtasks命令nettime\\192.168.1.131#查看时间at\\192.168.1.13110:30cmd.exe/c"whoami>c:\result.txt"#结果输出到result下type\\192.168.1.131\c$\result.txt#查看执行结果at\\192.168.1.1311/delet......
  • C++中的 new 与 delete
    我们今天来学习C++中的new与delete。它们2个是C++中的关键字,作用是在freestore(C语言中的堆区)中申请空间来存放数据。存在的意义为什么我们要在freestore中去存放数据呢?——因为freestore中的生命周期是由我们程序员所控制的。在某些时刻,我们只需要暂时性地使用一些数据。在这......
  • c++一个数因子和(快速求解)
    void一个数因子和(int整数){//缘由https://ask.csdn.net/questions/1054457#answer_1251715 inthe=0,j=0;stringa=""; while(++j<整数)if(!(整数%j))he+=j,a+=to_string(j)+"+"; cout<<a<<"的因子和:"<<he......
  • Oracle 19c数据库:Windows详细安装与配置指南
    Oracle19c的安装和配置是一个相对复杂但系统化的过程,本文演示如何在Windows系统下安装Oracle数据库,安装足够的磁盘空间(一般需要5~6个G,所以选剩余空间大的盘)。以下是一个详细的步骤指南,包括准备工作、安装过程、配置监听器和数据库测试等关键步骤:一、下载Oracle19c安装包访问Or......
  • C++初学(19)
    19.1、文本IO如果你需要写入一千份以上的数据,如果手打那可太浪费时间了。这种情况下,应该让程序直接读取文件;同样的,让程序将输入写入到文件也是更加方便。文本I/O的概念:使用cin进行输入时,程序将输入视为一系列的字节,其中每个字节都被解释为字符编码。无论目标数据类型是什么,输......
  • C++入门基础(类和对象)
    7.类和对象1.面向对象和面向过程的区别面向对象:是将事务抽象成类和对象,以对象为中心,通过对象的交互实现程序的功能,可维护性强面向过程:是将复杂问题一步步拆分,通过依次执行来解决问题,可维护性比较弱2.类大小的计算1.内存对齐考虑内存对齐的默认大小vs默认是8字......
  • C++入门基础
    6.C++入门基础1.函数重载1.什么是函数重载函数重载是实现C++多态的一个重要技术,是在同一作用域内相同函数名因为参数的类型不同或者个数不同或者都不同构成重载2.C++是如何支持函数重载的1.命名改变根据对应的参数类名改成比如voidfoo(int)voidfoo(double)_fo......
  • Modern C++——使用分支预测优化代码性能
    大纲[[likely]][[unlikely]]样例应用场景题外参考代码参考资料在C++20中,新引入了一对属性关键字[[likely]]和[[unlikely]],它们用于为编译器提供关于代码分支执行概率的额外信息,以帮助编译器进行更好的优化。这对属性是基于长期实践中开发人员对程序执行路径的深入理解......
  • 枚举: C++和Python实现鸡兔同笼问题
    作者制作不易,关注、点赞、收藏一下吧!目录1.Python实现2.C++实现1.Python实现首先,我们需要输入头和脚的数量:head=int(input("请输入头的数量:"))feet=int(input("请输入脚的数量:"))input()实现输入,int()实现把字符串型(str)换为整型(int)。然后,进行循环......