parallel_for_each

Bonsoir,

 

I modified the code from Integral with the Parallel Patterns Library so that it uses parallel_for_each() instead of parallel_for():

 


template
<typename FN>
double Integrate(FN fn,double from, double to, double dx) {

   if (from >= to)

      throw invalid_argument("’from’ must be smaller than ‘to’");

 

   if (dx <= 0)

      throw invalid_argument("’dx’ must be greater than 0");

 

   auto intervalsCount = static_cast<size_t>((to – from) / dx);

 

   if (!intervalsCount)

      throw underflow_error("’dx’ is too big or the interval [‘from’, ‘to’] is too small");

 

   // Let’s calculate the x-coordinate values at the center of each interval

   vector<double> xCoordinates(intervalsCount);

   auto it = xCoordinates.begin();

   for(double d = from + dx / 2 ; d < to; d += dx)

      *it++ = d;

 

   combinable<double> integral;

   parallel_for_each(xCoordinates.begin(), xCoordinates.end(), [&fn, &dx, &integral] (double x) {integral.local() += dx * fn(x);});

   return integral.combine(plus<double>());

}


 

¡Hasta pronto!

Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s