All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tbb_preduce_01_t.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // tbb_preduce_01_t
3 //
4 // Demonstrate trivial use of parallel_reduce over a vector with
5 // functors to ascertain the mean value in a vector, and the location
6 // and value of the minimum value in same.
7 //
8 ////////////////////////////////////////////////////////////////////////
9 
10 #include "tbb/tbb.h"
11 
12 #include <algorithm>
13 #include <cassert>
14 #include <iostream>
15 #include <iterator>
16 #include <numeric>
17 
19 
20 // Calculate the mean.
21 class Meanie {
22 public:
23  Meanie();
24  Meanie(Meanie const&, tbb::split);
25  void operator()(br_t const& r);
26  void join(Meanie const& other);
27 
28  size_t count() const;
29  double result() const;
30 
31 private:
33  double running_sum_;
34 };
35 
36 // Calculate the min.
37 class Minnie {
38 public:
39  Minnie();
41  Minnie(Minnie const& other, tbb::split);
42  void operator()(br_t const& r);
43  void join(Minnie const& other);
44 
45  br_t::const_iterator min() const;
46  bool valid() const;
47 
48 private:
50  bool valid_;
51 };
52 
53 // Main program.
54 int
56 {
57  // Setup.
58  size_t const n = 500000;
59  double const val = 27.125; // Exactly representable as a double in IEEE.
60  std::vector<double> v(n, val);
61  // Mean.
62  Meanie m;
63  tbb::parallel_reduce(br_t(v.cbegin(), v.cend()), m);
64  assert(m.count() == n);
65  assert(m.result() == val);
66  // Setup for min.
67  std::vector<double>::difference_type const loc = 47856;
68  double const minval = 22.3;
69  v[loc] = minval;
70  // Min.
71  Minnie mincalc;
72  tbb::parallel_reduce(br_t(v.cbegin(), v.cend()), mincalc);
73  assert(mincalc.valid() && std::distance(v.cbegin(), mincalc.min()) == loc);
74  assert(*mincalc.min() == minval);
75 }
76 
77 ////////////////////////////////////////////////////////////////////////
78 // Member function implementations.
79 
80 ////////////////////////////////////
81 // Meanie
82 
84 
85 inline Meanie::Meanie(Meanie const&, tbb::split) : Meanie() {}
86 
87 inline void
89 {
90  running_count_ += r.size();
91  running_sum_ = std::accumulate(r.begin(), r.end(), running_sum_);
92 }
93 
94 inline void
96 {
98  running_sum_ += other.running_sum_;
99 }
100 
101 inline size_t
103 {
104  return running_count_;
105 }
106 
107 inline double
109 {
110  return running_count_ ? (running_sum_ / running_count_) : 0.0;
111 }
112 
113 ////////////////////////////////////
114 // Minnie
115 
116 inline Minnie::Minnie() : min_(), valid_(false) {}
117 
119 
121  : min_(other.min_), valid_(other.valid_)
122 {}
123 
124 inline void
126 {
127  for (br_t::const_iterator i = r.begin(), e = r.end(); i != e; ++i) {
128  if (!valid_ || *i < *min_) {
129  min_ = i;
130  valid_ = true;
131  }
132  }
133 }
134 
135 inline void
137 {
138  if (other.valid_ && (!valid_ || (*other.min_ < *min_))) {
139  min_ = other.min_;
140  valid_ = true;
141  }
142 }
143 
145 Minnie::min() const
146 {
147  return min_;
148 }
149 
150 inline bool
152 {
153  return valid_;
154 }
static const double m
Definition: Units.h:79
tbb::blocked_range< typename std::vector< double >::const_iterator > br_t
void operator()(br_t const &r)
size_t count() const
size_t running_count_
int main()
void operator()(br_t const &r)
const double e
intermediate_table::const_iterator const_iterator
br_t::const_iterator min_
void join(Meanie const &other)
double result() const
br_t::const_iterator min() const
void join(Minnie const &other)
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:57
double running_sum_
void split(std::string const &s, char c, OutIter dest)
Definition: split.h:35
bool valid() const