平行模式程式庫 (PPL)

平行模式程式庫 (PPL) 提供命令式程式設計模型,可提升開發並存應用程式時的延展性和方便性。 PPL 建置在並行執行階段的排程和資源管理元件上。 其提供在資料上平行作用的泛型、類型安全演算法和容器,可提升應用程式程式碼與基礎執行緒機制之間的抽象層級。 PPL 有提供共用狀態的替代方案,也可以讓您開發可調整大小的應用程式。

PPL 提供下列功能:

  • 工作平行處理原則 :可在 Windows ThreadPool 之上運作的機制,以平行方式執行數個工作專案(工作)

  • 平行演算法 :在並行執行時間上運作的泛型演算法,以平行方式處理資料收集

  • 平行容器和物件 :提供安全平行存取其元素的泛型容器類型

範例

PPL 提供類似 C++ 標準程式庫的程式設計模型。 下列範例示範 PPL 的許多功能。 它會循序和平行計算數個 Fibonacci 數字。 這兩個 計算都會在 std::array 物件上執行。 這個範例也會將執行這兩項計算所需的時間列印到主控台。

序列版本會使用 C++ 標準程式庫 std::for_each 演算法來周遊陣列,並將結果儲存在 std::vector 物件中 。 平行版本會執行相同的工作,但會使用 PPL 並行::p arallel_for_each 演算法,並將結果儲存在 並行::concurrent_vector 物件中。 concurrent_vector 類別可讓每個迴圈反覆項目並行加入項目,而不需要同步處理對容器的寫入權限。

因為 parallel_for_each 會並行作用,所以此範例的平行版本必須將 concurrent_vector 物件排序,以產生與序列版相同的結果。

請注意,此範例使用 naïve 方法來計算 Fibonacci 數字;不過,這個方法會說明並行執行階段如何改善長時間運算的效能。

// 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 (begin(a), end(a), [&](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 (begin(a), end(a), [&](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(begin(results2), end(results2));
   });   
   wcout << L"parallel time: " << elapsed << L" ms" << endl << endl;

   // Print the results.
   for_each (begin(results2), end(results2), [](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 中的取消 說明如何取消平行演算法正在執行的工作。
並行執行階段 說明並行執行階段,它可簡化平行程式設計,並包含相關主題的連結。