utils.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_DECISION_TREE_UTILS_HPP
13 #define MLPACK_METHODS_DECISION_TREE_UTILS_HPP
14 
18 template<typename VecType, typename WeightVecType>
19 inline void WeightedSum(const VecType& values,
20  const WeightVecType& weights,
21  const size_t begin,
22  const size_t end,
23  double& accWeights,
24  double& weightedMean)
25 {
26  typedef typename VecType::elem_type VType;
27  typedef typename WeightVecType::elem_type WType;
28 
29  WType totalWeights[4] = { 0.0, 0.0, 0.0, 0.0 };
30  VType weightedSum[4] = { 0.0, 0.0, 0.0, 0.0 };
31 
32  // SIMD loop: sums four elements simultaneously (if the compiler manages
33  // to vectorize the loop).
34  for (size_t i = begin + 3; i < end; i += 4)
35  {
36  const WType weight1 = weights[i - 3];
37  const WType weight2 = weights[i - 2];
38  const WType weight3 = weights[i - 1];
39  const WType weight4 = weights[i];
40 
41  weightedSum[0] += weight1 * values[i - 3];
42  weightedSum[1] += weight2 * values[i - 2];
43  weightedSum[2] += weight3 * values[i - 1];
44  weightedSum[3] += weight4 * values[i];
45 
46  totalWeights[0] += weight1;
47  totalWeights[1] += weight2;
48  totalWeights[2] += weight3;
49  totalWeights[3] += weight4;
50  }
51 
52  // Handle leftovers.
53  if ((end - begin) % 4 == 1)
54  {
55  const WType weight1 = weights[end - 1];
56  weightedSum[0] += weight1 * values[end - 1];
57  totalWeights[0] += weight1;
58  }
59  else if ((end - begin) % 4 == 2)
60  {
61  const WType weight1 = weights[end - 2];
62  const WType weight2 = weights[end - 1];
63 
64  weightedSum[0] += weight1 * values[end - 2];
65  weightedSum[1] += weight2 * values[end - 1];
66 
67  totalWeights[0] += weight1;
68  totalWeights[1] += weight2;
69  }
70  else if ((end - begin) % 4 == 3)
71  {
72  const WType weight1 = weights[end - 3];
73  const WType weight2 = weights[end - 2];
74  const WType weight3 = weights[end - 1];
75 
76  weightedSum[0] += weight1 * values[end - 3];
77  weightedSum[1] += weight2 * values[end - 2];
78  weightedSum[2] += weight1 * values[end - 1];
79 
80  totalWeights[0] += weight1;
81  totalWeights[1] += weight2;
82  totalWeights[2] += weight3;
83  }
84 
85  totalWeights[0] += totalWeights[1] + totalWeights[2] + totalWeights[3];
86  weightedSum[0] += weightedSum[1] + weightedSum[2] + weightedSum[3];
87 
88  accWeights = totalWeights[0];
89  weightedMean = weightedSum[0];
90 }
91 
95 template<typename VecType>
96 inline void Sum(const VecType& values,
97  const size_t begin,
98  const size_t end,
99  double& mean)
100 {
101  typename VecType::elem_type total[4] = { 0.0, 0.0, 0.0, 0.0 };
102 
103  // SIMD loop: add counts for four elements simultaneously (if the compiler
104  // manages to vectorize the loop).
105  for (size_t i = begin + 3; i < end; i += 4)
106  {
107  total[0] += values[i - 3];
108  total[1] += values[i - 2];
109  total[2] += values[i - 1];
110  total[3] += values[i];
111  }
112 
113  // Handle leftovers.
114  if ((end - begin) % 4 == 1)
115  {
116  total[0] += values[end - 1];
117  }
118  else if ((end - begin) % 4 == 2)
119  {
120  total[0] += values[end - 2];
121  total[1] += values[end - 1];
122  }
123  else if ((end - begin) % 4 == 3)
124  {
125  total[0] += values[end - 3];
126  total[1] += values[end - 2];
127  total[2] += values[end - 1];
128  }
129 
130  total[0] += total[1] + total[2] + total[3];
131 
132  mean = total[0];
133 }
134 
135 #endif
void WeightedSum(const VecType &values, const WeightVecType &weights, const size_t begin, const size_t end, double &accWeights, double &weightedMean)
Calculates the weighted sum and total weight of labels.
Definition: utils.hpp:19
void Sum(const VecType &values, const size_t begin, const size_t end, double &mean)
Sums up the labels vector.
Definition: utils.hpp:96