如何：轉換使用削減變數的 OpenMP 迴圈來使用並行執行階段

Visual Studio 2015

OpenMP `reduction` 子句可讓您在平行區域末端指定一個或多個執行削減作業的執行緒私用變數。 OpenMP 預先定義一組削減運算子。 每個削減變數都必須是純量 (例如 `int``long``float`)。 OpenMP 也定義平行區域中削減變數用法的數個限制。

範例

```// concrt-omp-fibonacci-reduction.cpp
// compile with: /EHsc /openmp
#include <ppl.h>
#include <iostream>

using namespace concurrency;
using namespace std;

// Computes the nth Fibonacci number.
// This function illustrates a lengthy operation and is therefore
// not optimized for performance.
int fibonacci(int n)
{
if (n < 2)
return n;

// Compute the components in parallel.
int n1, n2;
parallel_invoke(
[n,&n1] { n1 = fibonacci(n-1); },
[n,&n2] { n2 = fibonacci(n-2); }
);

return n1 + n2;
}

// Uses OpenMP to compute the sum of Fibonacci numbers in parallel.
void omp_parallel_fibonacci_sum(int count)
{
int sum = 0;
#pragma omp parallel for reduction(+ : sum)
for (int i = 0; i < count; ++i)
{
sum += fibonacci(i);
}

wcout << L"The sum of the first " << count << L" Fibonacci numbers is "
<< sum << L'.' << endl;
}

// Uses the Concurrency Runtime to compute the sum of Fibonacci numbers in parallel.
void concrt_parallel_fibonacci_sum(int count)
{
combinable<int> sums;
parallel_for(0, count, [&sums](int i)
{
sums.local() += fibonacci(i);
});

wcout << L"The sum of the first " << count << L" Fibonacci numbers is "
<< sums.combine(plus<int>()) << L'.' << endl;
}

int wmain()
{
const int count = 35;

wcout << L"Using OpenMP..." << endl;
omp_parallel_fibonacci_sum(count);

wcout << L"Using the Concurrency Runtime..." << endl;
concrt_parallel_fibonacci_sum(count);
}
```

Using OpenMP...

編譯程式碼

cl.exe /EHsc /openmp concrt-omp-fibonacci-reduction.cpp