C++
C#
VB
JScript
All

External Function parallel_reduce


Copyright (C) 2005 IENT-RWTH Aachen

template<class RanIt,class F> void parallel_reduce(RanIt begin,RanIt end,const F &f,int grain)
template<class G,class F> void parallel_reduce(const Vector<G> &X, const F &f,int grain)
template<class G,class F> void parallel_reduce(const Vector<G> &X, const F &f)
template<class G,class F> void parallel_reduce(const Matrix<G> &X, const F &f,int grain)
template<class G,class F> void parallel_reduce(const Matrix<G> &X, const F &f)

Computes parallel reduction over a range of values

Parameters

begin

Integer or random access iterator addressing the position of the first element in the range to be operated on.

end

Integer or random access iterator addressing the position one past the final element in the range operated on.

grain

Number of iteration for a reasonable chunk to deal out to a thread. If no value given, the range is subdivided in num_threads() chunks.

X

Range to be operated on.

f

User-defined function object that is applied on subranges.

Remarks

The function object must model the following requirements:

Copy constructor F::F(const F &)
Destructor F::~F(const F &)
Merge the results of 2 threads F::join(const F &) const
Apply on subranges template<class RanIt> F::operator()(RanIt,RanIt) const

Any modified variable, that is used to store intermediate results, has to be declared as mutable.

Example

template<class V>
struct accumulate_function
{
  mutable V val;
  accumulate_function() : val(0) {}
  template<class It> void operator()(It begin, It end) const { val=accumulate(begin,end,val); }
  void join(const accumulate_function &x) const { val+=x.val; }
};

template<class RanIt,class T>
T parallel_accumulate(RanIt begin, RanIt end, const T &val, int grain=10000)
{
  accumulate_function<T> func;
  gmt::parallel_reduce(begin,end,func,grain);
  return val+func.val;
}

See Also

parallel_copy, parallel_for