共用方式為


如何:轉換 OpenMP parallel for 迴圈來使用並行執行階段

這個範例示範如何將使用 OpenMP parallelfor 指示詞的基本迴圈,轉換為使用並行執行階段 concurrency::parallel_for 演算法。

範例

這個範例使用 OpenMP 和並行執行階段,計算隨機值陣列中質數的計數。

// concrt-omp-count-primes.cpp 
// compile with: /EHsc /openmp
#include <ppl.h>
#include <random>
#include <array>
#include <iostream>

using namespace concurrency;
using namespace std;

// Determines whether the input value is prime. 
bool is_prime(int n)
{
   if (n < 2)
      return false;
   for (int i = 2; i < n; ++i)
   {
      if ((n % i) == 0)
         return false;
   }
   return true;
}

// Uses OpenMP to compute the count of prime numbers in an array. 
void omp_count_primes(int* a, size_t size)
{
   if (size == 0)
      return;

   size_t count = 0;
   #pragma omp parallel for 
      for (int i = 0; i < static_cast<int>(size); ++i)
      {
         if (is_prime(a[i])) {
            #pragma omp atomic
               ++count;
         }
      }

   wcout << L"found " << count 
         << L" prime numbers." << endl;
}

// Uses the Concurrency Runtime to compute the count of prime numbers in an array. 
void concrt_count_primes(int* a, size_t size)
{
   if (size == 0)
      return;

   combinable<size_t> counts;
   parallel_for<size_t>(0, size, [&](size_t i) 
   {
      if (is_prime(a[i])) {
         counts.local()++;
      }
   });

   wcout << L"found " << counts.combine(plus<size_t>()) 
         << L" prime numbers." << endl;
}

int wmain()
{
   // The length of the array. 
   const size_t size = 1000000;

   // Create an array and initialize it with random values. 
   int* a = new int[size];

   mt19937 gen(42);
   for (size_t i = 0; i < size; ++i) {
      a[i] = gen();
   }

   // Count prime numbers by using OpenMP and the Concurrency Runtime.

   wcout << L"Using OpenMP..." << endl;
   omp_count_primes(a, size);

   wcout << L"Using the Concurrency Runtime..." << endl;
   concrt_count_primes(a, size);

   delete[] a;
}

這個範例產生下列輸出。

  

parallel_for 演算法和 OpenMP 3.0 允許索引類型為帶正負號的整數類資料型別或不帶正負號的整數類資料型別。 parallel_for 演算法也會確定指定的範圍不溢出帶正負號型別。 OpenMP 2.0 和 2.5 版只允許帶正負號的整數索引類型。 OpenMP 也不會驗證索引範圍。

這個範例中使用並行執行階段的版本也會以 concurrency::combinable 物件代替 atomic 指示詞,遞增計數器值,而不需要同步處理。

如需 parallel_for 和其他平行演算法的詳細資訊,請參閱平行演算法。 如需 combinable 類別的詳細資訊,請參閱 平行容器和物件

這個範例將前述範例修改為作用於 std::array 物件,而不是原生陣列物。 因為 OpenMP 2.0 和 2.5 版在 parallel for 建構中只允許帶正負號的整數索引類型,所以您不可使用 Iterator 平行存取標準樣板程式庫 (STL) 容器中的項目。 平行模式程式庫 (PPL) 提供了 concurrency::parallel_for_each 演算法,這個演算法會以平行方式針對例如 STL 所提供的反覆容器執行工作。 它會使用 parallel_for 演算法所使用的相同分割邏輯。 parallel_for_each 演算法與 STL std::for_each 演算法很相似,不過 parallel_for_each 演算法會執行並行工作。

// Uses OpenMP to compute the count of prime numbers in an  
// array object. 
template<size_t Size>
void omp_count_primes(const array<int, Size>& a)
{
   if (a.size() == 0)
      return;

   size_t count = 0;
   int size = static_cast<int>(a.size());
   #pragma omp parallel for 
      for (int i = 0; i < size; ++i)
      {
         if (is_prime(a[i])) {
            #pragma omp atomic
               ++count;
         }
      }

   wcout << L"found " << count 
         << L" prime numbers." << endl;
}

// Uses the Concurrency Runtime to compute the count of prime numbers in an  
// array object. 
template<size_t Size>
void concrt_count_primes(const array<int, Size>& a)
{
   if (a.size() == 0)
      return;

   combinable<size_t> counts;
   parallel_for_each(begin(a), end(a), [&counts](int n) 
   {
      if (is_prime(n)) {
         counts.local()++;
      }
   });

   wcout << L"found " << counts.combine(plus<size_t>()) 
         << L" prime numbers." << endl;
}

編譯程式碼

請複製範例程式碼並將它貼在 Visual Studio 專案中,或是貼在名為 concrt-omp-count-primes.cpp 的檔案中,然後在 Visual Studio 的 [命令提示字元] 視窗中執行下列命令。

cl.exe /EHsc /openmp concrt-omp-count-primes.cpp

請參閱

概念

從 OpenMP 移轉至並行執行階段

平行演算法

平行容器和物件