并行模式库 (PPL)

并行模式库 (PPL) 提供命令性编程模型。命令性编程模型可提高并发应用程序开发的可伸缩性和易用性。 PPL 是基于并发运行时的计划和资源管理组件构建的。 它通过提供并行操作数据的泛型、类型安全的算法和容器来提高应用程序代码和基础线程处理机制之间的抽象级别。 通过提供共享状态的替代状态,PPL 还允许您开发可缩放的应用程序。

PPL 提供以下功能:

  • 任务并行:一种并行执行若干工作项(任务)的机制

  • 并行算法:并行作用于数据集合的泛型算法

  • 并行容器和对象:提供对其元素的安全并发访问的泛型容器类型

示例

PPL 提供了类似于标准模板库 (STL) 的编程模型。 下面的示例演示 PPL 的众多功能。 它按顺序并行计算一些 Fibonacci 数列。 这两种计算都将作用于 std::array 对象。 此示例还会将执行两次计算所需的时间输出到控制台。

序列化版本使用 STL std::for_each 算法遍历该数组,并将结果存储在 std::vector 对象中。 并行版本执行相同任务,但使用 PPL Concurrency::parallel_for_each 算法并将结果存储在 Concurrency::concurrent_vector 对象中。 concurrent_vector 类使每个循环迭代都能同时添加元素,而不需要对容器的写访问进行同步。

由于 parallel_for_each 的同时作用,此示例的并行版本必须对 concurrent_vector 对象进行排序以产生与序列化版本相同的结果。

请注意该示例使用 naïve 方法计算斐波纳契数列;但是,此方法阐释了并发运行时如何才能提高冗长计算的性能。

// parallel-fibonacci.cpp
// compile with: /EHsc
#include <windows.h>
#include <ppl.h>
#include <concurrent_vector.h>
#include <array>
#include <vector>
#include <tuple>
#include <algorithm>
#include <iostream>

using namespace Concurrency;
using namespace std;

// Calls the provided work function and returns the number of milliseconds 
// that it takes to call that function.
template <class Function>
__int64 time_call(Function&& f)
{
   __int64 begin = GetTickCount();
   f();
   return GetTickCount() - begin;
}

// Computes the nth Fibonacci number.
int fibonacci(int n)
{
   if(n < 2)
      return n;
   return fibonacci(n-1) + fibonacci(n-2);
}

int wmain()
{
   __int64 elapsed;

   // An array of Fibonacci numbers to compute.
   array<int, 4> a = { 24, 26, 41, 42 };

   // The results of the serial computation.
   vector<tuple<int,int>> results1;

   // The results of the parallel computation.
   concurrent_vector<tuple<int,int>> results2;

   // Use the for_each algorithm to compute the results serially.
   elapsed = time_call([&] 
   {
      for_each (a.begin(), a.end(), [&](int n) {
         results1.push_back(make_tuple(n, fibonacci(n)));
      });
   });   
   wcout << L"serial time: " << elapsed << L" ms" << endl;

   // Use the parallel_for_each algorithm to perform the same task.
   elapsed = time_call([&] 
   {
      parallel_for_each (a.begin(), a.end(), [&](int n) {
         results2.push_back(make_tuple(n, fibonacci(n)));
      });

      // Because parallel_for_each acts concurrently, the results do not 
      // have a pre-determined order. Sort the concurrent_vector object
      // so that the results match the serial version.
      sort(results2.begin(), results2.end());
   });   
   wcout << L"parallel time: " << elapsed << L" ms" << endl << endl;

   // Print the results.
   for_each (results2.begin(), results2.end(), [](tuple<int,int>& pair) {
      wcout << L"fib(" << get<0>(pair) << L"): " << get<1>(pair) << endl;
   });
}

下面的示例输出用于具有四个处理器的计算机。

serial time: 9250 ms
parallel time: 5726 ms

fib(24): 46368
fib(26): 121393
fib(41): 165580141
fib(42): 267914296

每个循环迭代都需要不同的时间量来完成。 parallel_for_each 的性能受最后完成的操作约束。 因此,别指望性能在此示例的序列化版本和并行版本之间会有直线上升。

相关主题

标题

说明

任务并行(并发运行时)

介绍 PPL 中任务和任务组的角色。

并行算法

描述如何使用并行算法(如 parallel_forparallel_for_each)。

并行容器和对象

描述 PPL 提供的各种并行容器和对象。

PPL 中的取消操作

说明如何取消并行算法正在执行的工作。

并发运行时

描述可简化并行编程的并发运行时,并包含指向相关主题的链接。