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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值