共用方式為


平行模式程式庫 (PPL)

平行模式程式庫 (PPL) 提供了重要的程式設計模型來提升開發並行應用程式時的延展性和易用性。 PPL 建置於並行執行階段的排程及資源管理元件上。 它透過提供型別安全的泛型演算法和容器來平行處理資料,提高了應用程式程式碼與基礎執行緒機制之間的抽象層級。 PPL 也提供替代共用狀態的另一種方式,讓您開發可延展的應用程式。

PPL 提供下列功能:

  • 工作平行處理原則 (Task Parallelism):以平行方式執行數個工作項目 (工作) 的機制

  • 平行演算法 (Parallel Algorithm):以平行方式處理資料集合的泛型演算法

  • 平行容器和物件 (Parallel Container And Object):泛型容器型別,提供對其項目的安全並行存取

範例

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 物件排序,才能產生與循序版本相同的結果。

請注意,這個範例使用直覺法來計算 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 中的取消

說明如何取消平行演算法正在執行的工作。

並行執行階段

描述「並行執行階段」,它可以簡化平行程式設計工作,而且包含相關主題的連結。