reduce-inl.h 7.67 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
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
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_ALGORITHMS_INTERNAL_REDUCE_INL_H_
#define EMBB_ALGORITHMS_INTERNAL_REDUCE_INL_H_

30
#include <embb/mtapi/mtapi.h>
31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <embb/algorithms/internal/partition.h>

#include <functional>
#include <embb/base/exceptions.h>
#include <cassert>

namespace embb {
namespace algorithms {
namespace internal {

template<typename RAI, typename ReturnType, typename ReductionFunction,
         typename TransformationFunction>
class ReduceFunctor {
 public:
45 46
  ReduceFunctor(size_t chunk_first, size_t chunk_last,
                ReturnType neutral,
47 48
                ReductionFunction reduction,
                TransformationFunction transformation,
49
                const embb::mtapi::ExecutionPolicy& policy,
50
                const BlockSizePartitioner<RAI>& partitioner,
51
                ReturnType& result)
52
  : chunk_first_(chunk_first), chunk_last_(chunk_last), neutral_(neutral),
53 54
    reduction_(reduction), transformation_(transformation), policy_(policy),
    partitioner_(partitioner), result_(result) {
55 56
  }

57
  void Action(embb::mtapi::TaskContext&) {
58 59 60 61 62
    if (chunk_first_ == chunk_last_) {
      // Leaf case, recursed to single chunk. Do work on chunk:
      ChunkDescriptor<RAI> chunk = partitioner_[chunk_first_];
      RAI first = chunk.GetFirst();
      RAI last  = chunk.GetLast();
63
      ReturnType result(neutral_);
64 65
      for (RAI it = first; it != last; ++it) {
        result = reduction_(result, transformation_(*it));
66 67
      }
      result_ = result;
68
    } else {
69 70 71
      // Recurse further:
      size_t chunk_split_index = (chunk_first_ + chunk_last_) / 2;
      // Split chunks into left / right branches:
72 73
      ReturnType result_l(neutral_);
      ReturnType result_r(neutral_);
74 75 76 77 78 79 80 81 82 83
      self_t functor_l(chunk_first_,
                       chunk_split_index,
                       neutral_, reduction_, transformation_, policy_,
                       partitioner_,
                       result_l);
      self_t functor_r(chunk_split_index + 1,
                       chunk_last_,
                       neutral_, reduction_, transformation_, policy_,
                       partitioner_,
                       result_r);
84 85 86 87 88 89
      embb::mtapi::Task task_l = embb::mtapi::Node::GetInstance().Start(
        base::MakeFunction(functor_l, &self_t::Action),
        policy_);
      embb::mtapi::Task task_r = embb::mtapi::Node::GetInstance().Start(
        base::MakeFunction(functor_r, &self_t::Action),
        policy_);
90
      task_l.Wait(MTAPI_INFINITE);
91 92 93 94 95 96
      task_r.Wait(MTAPI_INFINITE);
      result_ = reduction_(result_l, result_r);
    }
  }

 private:
97 98 99 100 101
  typedef ReduceFunctor<RAI, ReturnType,
                        ReductionFunction,
                        TransformationFunction> self_t;

 private:
102 103
  size_t chunk_first_;
  size_t chunk_last_;
104 105 106
  ReturnType neutral_;
  ReductionFunction reduction_;
  TransformationFunction transformation_;
107
  const embb::mtapi::ExecutionPolicy& policy_;
108
  const BlockSizePartitioner<RAI>& partitioner_;
109 110
  ReturnType& result_;

111 112 113
  /**
   * Disables assignment and copy-construction.
   */
114 115 116 117 118 119
  ReduceFunctor& operator=(const ReduceFunctor&);
  ReduceFunctor(const ReduceFunctor&);
};

template<typename RAI, typename ReturnType, typename ReductionFunction,
         typename TransformationFunction>
120
ReturnType ReduceRecursive(RAI first, RAI last, ReturnType neutral,
121 122
                           ReductionFunction reduction,
                           TransformationFunction transformation,
123
                           const embb::mtapi::ExecutionPolicy& policy,
124
                           size_t block_size) {
125 126
  typedef typename std::iterator_traits<RAI>::difference_type difference_type;
  difference_type distance = std::distance(first, last);
127
  if (distance == 0) {
128 129 130
    return neutral;
  } else if (distance < 0) {
    EMBB_THROW(embb::base::ErrorException, "Negative range for Reduce");
131
  }
132 133 134 135
  unsigned int num_cores = policy.GetCoreCount();
  if (num_cores == 0) {
    EMBB_THROW(embb::base::ErrorException, "No cores in execution policy");
  }
136
  embb::mtapi::Node& node = embb::mtapi::Node::GetInstance();
137 138
  // Determine actually used block size
  if (block_size == 0) {
139
    block_size = (static_cast<size_t>(distance) / num_cores);
140 141 142
    if (block_size == 0) {
      block_size = 1;
    }
143
  }
144
  // Perform check of task number sufficiency
145
  if (((distance / block_size) * 2) + 1 > MTAPI_NODE_MAX_TASKS_DEFAULT) {
146 147 148 149 150 151
    EMBB_THROW(embb::base::ErrorException,
               "Number of computation tasks required in reduction would "
               "exceed MTAPI maximum number of tasks");
  }
  typedef ReduceFunctor<RAI, ReturnType, ReductionFunction,
                        TransformationFunction> Functor;
152 153 154 155
  BlockSizePartitioner<RAI> partitioner(first, last, block_size);
  ReturnType result = neutral;
  Functor functor(0,
                  partitioner.Size() - 1,
156 157
                  neutral,
                  reduction, transformation,
158
                  policy,
159
                  partitioner,
160
                  result);
161 162 163
  embb::mtapi::Task task = node.Start(
    base::MakeFunction(functor, &Functor::Action),
    policy);
164 165 166 167 168 169 170 171 172
  task.Wait(MTAPI_INFINITE);
  return result;
}

template<typename RAI, typename TransformationFunction,
  typename ReductionFunction, typename ReturnType>
ReturnType ReduceIteratorCheck(RAI first, RAI last, ReductionFunction reduction,
                               TransformationFunction transformation,
                               ReturnType neutral,
173
                               const embb::mtapi::ExecutionPolicy& policy,
174
                               size_t block_size,
175
                               std::random_access_iterator_tag) {
176
    return ReduceRecursive(first, last, neutral, reduction, transformation,
177 178 179 180 181 182 183 184 185 186
                           policy, block_size);
}

}  // namespace internal

template<typename RAI, typename ReturnType, typename ReductionFunction,
         typename TransformationFunction >
ReturnType Reduce(RAI first, RAI last, ReturnType neutral,
                  ReductionFunction reduction,
                  TransformationFunction transformation,
187
                  const embb::mtapi::ExecutionPolicy& policy,
188
                  size_t block_size) {
189 190 191 192 193 194 195 196 197
  typename std::iterator_traits<RAI>::iterator_category category;
  return internal::ReduceIteratorCheck(first, last, reduction, transformation,
                                       neutral, policy, block_size, category);
}

}  // namespace algorithms
}  // namespace embb

#endif  // EMBB_ALGORITHMS_INTERNAL_REDUCE_INL_H_