1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
   | #include <vector>
#include <numeric>
#include <ctime>
 
#define MAX_THREADS 2
#include "threads.h"
 
using namespace std;
 
inline double get_time() { return double(clock())/CLOCKS_PER_SEC; }
 
template<class V>
struct fill_function
{
  V val;
  fill_function(const V &v) : val(v) {}
  template<class RanIt> void operator()(RanIt begin, RanIt end) const { fill(begin,end,val); }
};
 
template<class V>
struct accumulate_function
{
  mutable V val;
  accumulate_function() : val(0) {}
  void join(const accumulate_function &x) const { val+=x.val; }
  template<class It> void operator()(It begin, It end) const { val=accumulate(begin,end,val); }
};
 
template<class RanIt,class T>
T parallel_accumulate(RanIt begin, RanIt end, const T &val)
{
  accumulate_function<T> func;
  gmt::parallel_reduce(begin,end,func,10000);
  return val+func.val;
}
 
int main()
{
  typedef int value_type;
  vector<value_type> X(10000000,1);
 
  //fill(X.begin(),X.end(),1);
  //gmt::parallel_for(X.begin(),X.end(), fill_function<value_type>(1), 100);
 
  double t0=0;
  value_type *psum = new value_type; //variable allouée dynamiquement pour bluffer l'optimisation d'ICL qui peut sinon carrément zapper les itérations des boucles
 
  //t0=get_time();
  //for (int i=0; i<500; ++i)
  //  *psum=accumulate(X.begin(),X.end(),0);
  //cout << *psum << " " << get_time()-t0 << "s" << endl;
 
  t0=get_time();
  for (int i=0; i<500; ++i)
    *psum=parallel_accumulate(X.begin(),X.end(),0);
  cout << *psum << " " << get_time()-t0 << "s" << endl;
} |