並列パターン ライブラリ (PPL)

並列パターン ライブラリ (PPL: Parallel Patterns Library) は、同時実行アプリケーションの開発に不可欠な、スケーラビリティが高く使いやすいプログラミング モデルを提供します。 PPL は、同時実行ランタイムのスケジューリング コンポーネントとリソース管理コンポーネントに基づいています。 PPL には、データを並列的に操作する、タイプ セーフのジェネリックなアルゴリズムとコンテナーが用意されています。これらを使用すると、アプリケーション コードと基になるスレッド処理機構の間で抽象化のレベルを引き上げることができます。 また、PPL には共有状態に代わる手段が用意されているため、規模の変更に対応したアプリケーションの開発にも役立ちます。

PPL には次の機能があります。

  • タスクの並列化: 複数の作業項目 (タスク) を並列的に実行する機構です。

  • 並列アルゴリズム: データのコレクションを並列的に操作するジェネリックなアルゴリズムです。

  • 並列コンテナーと並列オブジェクト: 要素への安全な同時アクセスを提供するジェネリックなコンテナーです。

PPL には、標準テンプレート ライブラリ (STL) に似たプログラミング モデルが用意されています。 次の例では、PPL のさまざまな機能を示します。 複数のフィボナッチの数列を逐次的および並列的に計算します。 どちらの場合も、計算の対象は std::array オブジェクトです。 また、それぞれの計算に要する時間もコンソールに出力します。

逐次バージョンでは、STL の std::for_each アルゴリズムを使用して配列を走査し、結果を std::vector オブジェクトに格納します。 並列バージョンでも実行するタスクは同じですが、PPL の Concurrency::parallel_for_each アルゴリズムを使用し、結果を Concurrency::concurrent_vector オブジェクトに格納します。 concurrent_vector クラスを使用することで、コンテナーへの書き込みアクセスを同期しなくても各ループ反復で要素を同時に追加できます。

parallel_for_each は同時に処理を行うため、この例の並列バージョンでは、concurrent_vector オブジェクトを並べ替え、逐次バージョンと同じ結果を生成する必要があります。

この例では、素朴な方法を使用してフィボナッチの数列を計算しています。ただし、この方法で示しているのは、同時実行ランタイムによって長い計算のパフォーマンスが向上するしくみです。

// 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;
   });
}

4 つのプロセッサを備えたコンピューターを使用したときのサンプル出力を次に示します。

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 における取り消し処理

並列アルゴリズムによって行われている処理を取り消す方法について説明します。

同時実行ランタイム

並列プログラミングを容易にする同時実行ランタイムについて説明します。また、関連トピックへのリンクを示します。