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

windows C++-并行编程-转换使用异常处理的 OpenMP 循环以使用并发运行时

此示例演示如何将执行异常处理的 OpenMP 并行 for 循环转换为使用并发运行时异常处理机制。

在 OpenMP 中,在并行区域中引发的异常必须由同一线程在同一区域中捕获和处理。 未处理的异常处理程序会捕获逃离并行区域的异常,默认情况下会终止进程。

在并发运行时中,在传递给任务组(例如 concurrency::task_group 或 concurrency::structured_task_group 对象)的工作函数主体中引发异常,或向并行算法(例如 concurrency::parallel_for)引发异常时,运行时会存储该异常并将其封送到等待任务组或算法完成的上下文。 对于任务组,等待上下文是调用 concurrency::task_group::wait、concurrency::structured_task_group::wait、concurrency::task_group::run_and_wait 或 concurrency::structured_task_group::run_and_wait 的上下文。 对于并行算法,等待上下文是调用该算法的上下文。 运行时还会停止任务组中的所有活动任务(包括子任务组中的任务),并丢弃任何尚未启动的任务。

示例

此示例演示如何在 OpenMP parallel 区域和对 parallel_for 的调用中处理异常。 do_work 函数执行未成功的内存分配请求,因此引发 std::bad_alloc 类型的异常。 在使用 OpenMP 的版本中,引发异常的线程也必须捕获它。 换句话说,OpenMP 并行循环的每次迭代都必须处理异常。 在使用并发运行时的版本中,主线程捕获另一个线程引发的异常。

// concrt-omp-exceptions.cpp
// compile with: /EHsc /openmp
#include <ppl.h>
#include <new>
#include <iostream>
#include <sstream>using namespace concurrency;
using namespace std;// Demonstrates a function that performs a memory allocation request 
// that does not succeed.
void do_work(int)
{// The following memory allocation causes this function to // throw std::bad_alloc.char* ptr = new char[(~unsigned int((int)0)/2) - 1];// TODO: Assuming that the allocation succeeds, perform some work // and free the allocated memory.delete[] ptr;
}// Demonstrates an OpenMP parallel loop that performs exception handling.
void omp_exception_handling()
{#pragma omp parallel for for(int i = 0; i < 10; i++) {try {// Perform a unit of work.do_work(i);}catch (exception const& e) {// Print the error to the console.wstringstream ss;ss << L"An error of type '" << typeid(e).name() << L"' occurred." << endl;wcout << ss.str();}}
}// Demonstrates an Concurrency Runtime parallel loop that performs exception handling.
void concrt_exception_handling()
{try {parallel_for(0, 10, [](int i) {// Perform a unit of work.do_work(i);});}catch (exception const& e) {// Print the error to the console.wcout << L"An error of type '" << typeid(e).name() << L"' occurred." << endl;}
}int wmain()
{wcout << L"Using OpenMP..." << endl;omp_exception_handling();wcout << L"Using the Concurrency Runtime..." << endl;concrt_exception_handling();
}

本示例生成以下输出。 

Using OpenMP...
An error of type 'class std::bad_alloc' occurred.
An error of type 'class std::bad_alloc' occurred.
An error of type 'class std::bad_alloc' occurred.
An error of type 'class std::bad_alloc' occurred.
An error of type 'class std::bad_alloc' occurred.
An error of type 'class std::bad_alloc' occurred.
An error of type 'class std::bad_alloc' occurred.
An error of type 'class std::bad_alloc' occurred.
An error of type 'class std::bad_alloc' occurred.
An error of type 'class std::bad_alloc' occurred.
Using the Concurrency Runtime...
An error of type 'class std::bad_alloc' occurred.

 在使用 OpenMP 的此示例版本中,异常发生在每个循环迭代中并进行处理。 在使用并发运行时的版本中,运行时存储异常,停止所有活动任务,丢弃任何尚未启动的任务,并将异常封送到调用 parallel_for 的上下文中。

如果要求使用 OpenMP 的版本在发生异常后终止,可以使用布尔标记向其他循环迭代发出信号,指出错误的发生。 如主题如何:将使用取消的 OpenMP 循环转换为使用并发运行时中的示例,如果设置了该标记,后续循环迭代将不会执行任何操作。 相反,如果要求使用并发运行时的循环在发生异常后继续,则在并行循环体本身中处理异常。

并发运行时的其他组件,例如异步代理和轻量级任务,不传输异常。 相反,未处理的异常处理程序会捕获未处理的异常,默认情况下会终止进程。

编译代码

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

cl.exe /EHsc /openmp concrt-omp-exceptions.cpp

http://www.mrgr.cn/news/21260.html

相关文章:

  • 【机器学习】自然语言处理中的Transformer模型:深度解析与前沿发展
  • 使用seq_file
  • 无法让杀毒软件杀的木马
  • Linux shell脚本 (十二)case语句_linux awk case语句
  • 【微处理器系统原理与应用设计第七讲】片上微处理器系统典型外设包括输入输出设备、定时计数设备、通信设备
  • (五十七)第 9 章 查找(动态查找表(二叉排序树))
  • 结合Python与GUI实现比赛预测与游戏数据分析
  • k8s API资源对象
  • 评估代码的可维护性,是否容易扩展
  • ASP.NET Core 入门教学十六 防止常见的Web攻击
  • python数值误差
  • CNN-LSTM模型中应用贝叶斯推断进行时间序列预测
  • AFSim仿真系统---向导参考指南 (1)
  • MySQL 触发器
  • WEB渗透Linux提权篇-MYSQL漏洞提权
  • 第十三届山东省ICPC
  • 关于STM32 使用 LVGL 及 DMA2D 必须知道的事
  • 【C语言】---- for循环函数
  • java工程师成功转型大数据
  • 会议《测试团队过程改进实践分享》记录