Пример #1
0
double sum_of_squares(T values)
{
  // This is for 4 processors
  // If you have a different number of processors,
  // change the number of arguments to the parallel_invoke function accordingly.

  Concurrency::combinable<double> sums;

  Concurrency::parallel_invoke(
    [&]
  {  for(size_t i = 0 ; i<values.size() ; i += 4)
        sums.local() += values[i]*values[i];
  },
    [&]
  {  for(size_t i = 1 ; i<values.size() ; i += 4)
        sums.local() += values[i]*values[i];
  },
    [&]
  {  for(size_t i = 2 ; i<values.size() ; i += 4)
        sums.local() += values[i]*values[i];
  },
    [&]
  {  for(size_t i = 3 ; i<values.size() ; i += 4)
      sums.local() += values[i]*values[i];
  });
 
  return sums.combine([](double lSum, double rSum){return lSum+rSum; });
}
Пример #2
0
unsigned long long factorial(int n)
{
  Concurrency::combinable<unsigned long long> products
    = Concurrency::combinable<unsigned long long>([]()->unsigned long long{return 1LL;});
  Concurrency::parallel_for(1 , n+1,
      [&products](int i)
      {
        products.local() *= i;
      });
  return products.combine([](unsigned long long lProduct, unsigned long long rProduct)
                             { return lProduct*rProduct;  });
}
Пример #3
0
int main()
{
    Concurrency::combinable<double> piParts;
    Concurrency::parallel_for(1, 1000000, [&piParts](long long n) {
        piParts.local() += 6.0 / (n * n);
    });

    double pi2 = piParts.combine([](double left, double right) {
        return left + right;
    });
    std::cout << "pi squared = " << std::setprecision(10) << pi2 << std::endl;
    std::cout << "pi = " << std::sqrt(pi2) << std::endl;
    return 0;
}