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; }); }
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; }); }
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; }
Base::BoundBox3d PointKernel::getBoundBox(void)const { Base::BoundBox3d bnd; //FIXME: VS 2015 or later causes a linker error #if defined _WIN32 // Thread-local bounding boxes Concurrency::combinable<Base::BoundBox3d> bbs; // Cannot use a const_point_iterator here as it is *not* a proper iterator (fails the for_each template) Concurrency::parallel_for_each(_Points.begin(), _Points.end(), [this, &bbs](const value_type& value) { Base::Vector3d vertd(value.x, value.y, value.z); bbs.local().Add(this->_Mtrx * vertd); }); // Combine each thread-local bounding box in the final bounding box bbs.combine_each([&bnd](const Base::BoundBox3d& lbb) { bnd.Add(lbb); }); #else for (const_point_iterator it = begin(); it != end(); ++it) bnd.Add(*it); #endif return bnd; }