如何:使用 combinable 提高性能

此示例演示如何使用 concurrency::combinable 类计算 std::array 对象中质数的数字之和。 combinable 类通过消除共享状态来提高性能。

提示

在某些情况下,并行映射 (concurrency::parallel_transform) 和化简 (concurrency:: parallel_reduce) 可以通过 combinable 实现性能改进。 有关使用映射和化简运算生成与此示例相同结果的示例,请参阅并行算法

示例 - accumulate

下面的示例使用 std::accumulate 函数来计算数组中质数元素的总和。 在此示例中,aarray 对象,并且 is_prime 函数确定其输入值是否是质数。

prime_sum = accumulate(begin(a), end(a), 0, [&](int acc, int i) {
   return acc + (is_prime(i) ? i : 0);
});

示例 - parallel_for_each

下面的示例演示了并行化上一个示例的朴素方法。 此示例使用 concurrency::parallel_for_each 算法并行处理数组,并使用 concurrency::critical_section 对象同步对 prime_sum 变量的访问。 此示例不进行缩放,因为每个线程必须等待共享资源可用。

critical_section cs;
prime_sum = 0;
parallel_for_each(begin(a), end(a), [&](int i) {
   cs.lock();
   prime_sum += (is_prime(i) ? i : 0);
   cs.unlock();
});

示例 - combinable

下面的示例使用 combinable 对象来提高上一个示例的性能。 此示例无需同步对象;该示例将缩放,因为 combinable 对象使每个线程能够独立执行其任务。

combinable 对象通常用于两个步骤。 首先,通过并行执行工作来生成一系列精细计算。 接下来,将计算合并(或化简)成最终结果。 此示例使用 concurrency::combinable::local 方法获取对本地求和计算的引用。 然后,它使用 concurrency::combinable::combine 方法和 std::plus 对象将本地计算合并成最终结果。

combinable<int> sum;
parallel_for_each(begin(a), end(a), [&](int i) {
   sum.local() += (is_prime(i) ? i : 0);
});
prime_sum = sum.combine(plus<int>());

示例 - 串行和并行

下面的完整示例以串行和并行方式计算质数之和。 该示例向控制台输出了进行两种计算所需的时间。

// parallel-sum-of-primes.cpp
// compile with: /EHsc
#include <windows.h>
#include <ppl.h>
#include <array>
#include <numeric>
#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;
}

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

int wmain()
{   
   // Create an array object that contains 200000 integers.
   array<int, 200000> a;

   // Initialize the array such that a[i] == i.
   iota(begin(a), end(a), 0);

   int prime_sum;
   __int64 elapsed;

   // Compute the sum of the numbers in the array that are prime.
   elapsed = time_call([&] {
      prime_sum = accumulate(begin(a), end(a), 0, [&](int acc, int i) {
         return acc + (is_prime(i) ? i : 0);
      });
   });   
   wcout << prime_sum << endl;   
   wcout << L"serial time: " << elapsed << L" ms" << endl << endl;

   // Now perform the same task in parallel.
   elapsed = time_call([&] {
      combinable<int> sum;
      parallel_for_each(begin(a), end(a), [&](int i) {
         sum.local() += (is_prime(i) ? i : 0);
      });
      prime_sum = sum.combine(plus<int>());
   });
   wcout << prime_sum << endl;
   wcout << L"parallel time: " << elapsed << L" ms" << endl << endl;
}

以下是具有四个处理器的计算机的输出示例。

1709600813
serial time: 6178 ms

1709600813
parallel time: 1638 ms

编译代码

若要编译代码,请复制代码并将其粘贴到 Visual Studio 项目中,或粘贴到一个名为 parallel-sum-of-primes.cpp 的文件中,然后在 Visual Studio 命令提示符窗口中运行以下命令。

cl.exe /EHsc parallel-sum-of-primes.cpp

可靠编程

有关使用映射和化简运算生成相同结果的示例,请参阅并行算法

另请参阅

并行容器和对象
combinable 类
critical_section 类