scan-inl.h 8.87 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 30 31
 *
 * 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_SCAN_INL_H_
#define EMBB_ALGORITHMS_INTERNAL_SCAN_INL_H_

#include <cassert>
#include <embb/base/exceptions.h>
32 33
#include <embb/base/function.h>
#include <embb/mtapi/mtapi.h>
34 35 36 37 38 39 40 41 42 43
#include <embb/algorithms/internal/partition.h>

namespace embb {
namespace algorithms {
namespace internal {

template<typename RAIIn, typename RAIOut, typename ReturnType,
typename ScanFunction, typename TransformationFunction>
class ScanFunctor {
 public:
44
  ScanFunctor(size_t chunk_first, size_t chunk_last, RAIOut output_iterator,
45 46
              ReturnType neutral, ScanFunction scan,
              TransformationFunction transformation,
47
              const embb::mtapi::ExecutionPolicy& policy,
48
              const BlockSizePartitioner<RAIIn>& partitioner,
49
              ReturnType* tree_values, size_t node_id,
50
              bool going_down)
51
    : policy_(policy), chunk_first_(chunk_first), chunk_last_(chunk_last),
52 53
      output_iterator_(output_iterator), scan_(scan),
      transformation_(transformation),
54
      neutral_(neutral), partitioner_(partitioner), tree_values_(tree_values),
55 56 57
      node_id_(node_id), parent_value_(neutral), is_first_pass_(going_down)  {
  }

58
  void Action(embb::mtapi::TaskContext&) {
59
    if (chunk_first_ == chunk_last_) {
60 61 62 63 64
      ChunkDescriptor<RAIIn> chunk = partitioner_[chunk_first_];
      RAIIn iter_in = chunk.GetFirst();
      RAIIn last_in = chunk.GetLast();
      RAIOut iter_out = output_iterator_;
      // leaf case -> do work
65
      if (is_first_pass_) {
66
        ReturnType result = transformation_(*iter_in);
67 68 69
        *iter_out = result;
        ++iter_in;
        ++iter_out;
70
        for (; iter_in != last_in; ++iter_in, ++iter_out) {
71 72 73 74
          result = scan_(result, transformation_(*iter_in));
          *iter_out = result;
        }
        SetTreeValue(result);
75
      } else {
76 77
        // Second pass
        for (; iter_in != last_in; ++iter_in, ++iter_out) {
78 79 80
          *iter_out = scan_(parent_value_, *iter_out);
        }
      }
81
    } else {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      // recurse further
      size_t chunk_split_index = (chunk_first_ + chunk_last_) / 2;
      // Split chunks into left / right branches:
      ScanFunctor functor_l(
        chunk_first_, chunk_split_index,
        output_iterator_, neutral_, scan_, transformation_,
        policy_, partitioner_, tree_values_, node_id_,
        is_first_pass_);
      ScanFunctor functor_r(
        chunk_split_index + 1, chunk_last_,
        output_iterator_, neutral_, scan_, transformation_,
        policy_, partitioner_, tree_values_, node_id_,
        is_first_pass_);
      functor_l.SetID(LEFT);
      functor_r.SetID(RIGHT);
      // Advance output iterator of right branch:
      ChunkDescriptor<RAIIn> chunk_left  = partitioner_[chunk_first_];
      ChunkDescriptor<RAIIn> chunk_right = partitioner_[chunk_split_index + 1];
100 101
      std::advance(functor_r.output_iterator_,
          std::distance(chunk_left.GetFirst(), chunk_right.GetFirst()));
102 103 104 105
      if (!is_first_pass_) {
        functor_l.parent_value_ = parent_value_;
        functor_r.parent_value_ = functor_l.GetTreeValue() + parent_value_;
      }
106
      // Spawn tasks to recurse:
107 108 109 110 111 112 113
      embb::mtapi::Node& node = embb::mtapi::Node::GetInstance();
      embb::mtapi::Task task_l = node.Start(
        base::MakeFunction(functor_l, &ScanFunctor::Action),
        policy_);
      embb::mtapi::Task task_r = node.Start(
        base::MakeFunction(functor_r, &ScanFunctor::Action),
        policy_);
114 115
      // Wait for tasks to complete:
      task_l.Wait(MTAPI_INFINITE);
116 117 118 119 120 121 122 123 124 125 126 127 128 129
      task_r.Wait(MTAPI_INFINITE);
      SetTreeValue(scan_(functor_l.GetTreeValue(), functor_r.GetTreeValue()));
    }
  }

  ReturnType GetTreeValue() {
    return tree_values_[node_id_];
  }

  void SetTreeValue(ReturnType value) {
    tree_values_[node_id_] = value;
  }

 private:
130 131
  static const int LEFT  = 1;
  static const int RIGHT = 2;
132
  const embb::mtapi::ExecutionPolicy& policy_;
133 134
  size_t chunk_first_;
  size_t chunk_last_;
135 136 137 138
  RAIOut output_iterator_;
  ScanFunction scan_;
  TransformationFunction transformation_;
  ReturnType neutral_;
139
  const BlockSizePartitioner<RAIIn>& partitioner_;
140 141 142 143 144
  ReturnType* tree_values_;
  size_t node_id_;
  ReturnType parent_value_;
  bool is_first_pass_;

145 146
  void SetID(int branch) {
    if (branch == LEFT) {
147
      node_id_ = 2 * node_id_ + 1;
148
    } else if (branch == RIGHT) {
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      node_id_ = 2 * node_id_ + 2;
    }
  }

  /**
   * Disables assignment.
   */
  ScanFunctor& operator=(const ScanFunctor&);

  /**
   * Disables copying.
   */
  ScanFunctor(const ScanFunctor&);
};

template<typename RAIIn, typename RAIOut, typename ReturnType,
typename ScanFunction, typename TransformationFunction>
void ScanIteratorCheck(RAIIn first, RAIIn last, RAIOut output_iterator,
                       ReturnType neutral, ScanFunction scan,
                       TransformationFunction transformation,
169
                       const embb::mtapi::ExecutionPolicy& policy,
170
                       size_t block_size,
171 172 173
                       std::random_access_iterator_tag) {
  typedef typename std::iterator_traits<RAIIn>::difference_type difference_type;
  difference_type distance = std::distance(first, last);
174
  if (distance == 0) {
175
    return;
176 177
  } else if (distance < 0) {
    EMBB_THROW(embb::base::ErrorException, "Negative range for Scan");
178
  }
179 180 181 182
  unsigned int num_cores = policy.GetCoreCount();
  if (num_cores == 0) {
    EMBB_THROW(embb::base::ErrorException, "No cores in execution policy");
  }
Marcus Winter committed
183

184 185
  ReturnType values[MTAPI_NODE_MAX_TASKS_DEFAULT];
  if (block_size == 0) {
186
    block_size = static_cast<size_t>(distance) / num_cores;
187 188 189
    if (block_size == 0) {
      block_size = 1;
    }
190
  }
191
  if (((distance / block_size) * 2) + 1 > MTAPI_NODE_MAX_TASKS_DEFAULT) {
192
    EMBB_THROW(embb::base::ErrorException,
193
               "Not enough MTAPI tasks available for parallel scan");
194 195 196 197 198 199
  }

  // first pass. Calculates prefix sums for leaves and when recursion returns
  // it creates the tree.
  typedef ScanFunctor<RAIIn, RAIOut, ReturnType, ScanFunction,
                      TransformationFunction> Functor;
200
  embb::mtapi::Node& node = embb::mtapi::Node::GetInstance();
201 202

  BlockSizePartitioner<RAIIn> partitioner_down(first, last, block_size);
203 204 205
  Functor functor_down(0, partitioner_down.Size() - 1, output_iterator,
                       neutral, scan, transformation, policy, partitioner_down,
                       values, 0, true);
206 207 208
  embb::mtapi::Task task_down = node.Start(
    base::MakeFunction(functor_down, &Functor::Action),
    policy);
209 210 211
  task_down.Wait(MTAPI_INFINITE);

  // Second pass. Gives to each leaf the part of the prefix missing
212
  BlockSizePartitioner<RAIIn> partitioner_up(first, last, block_size);
213 214 215
  Functor functor_up(0, partitioner_up.Size() - 1, output_iterator,
                     neutral, scan, transformation, policy, partitioner_up,
                     values, 0, false);
216 217 218
  embb::mtapi::Task task_up = node.Start(
    base::MakeFunction(functor_up, &Functor::Action),
    policy);
219 220 221 222 223 224 225 226 227
  task_up.Wait(MTAPI_INFINITE);
}

}  // namespace internal

template<typename RAIIn, typename RAIOut, typename ReturnType,
         typename ScanFunction, typename TransformationFunction>
void Scan(RAIIn first, RAIIn last, RAIOut output_iterator, ReturnType neutral,
          ScanFunction scan, TransformationFunction transformation,
228
          const embb::mtapi::ExecutionPolicy& policy, size_t block_size) {
229 230 231 232 233 234 235 236 237
  typedef typename std::iterator_traits<RAIIn>::iterator_category category;
  internal::ScanIteratorCheck(first, last, output_iterator, neutral,
      scan, transformation, policy, block_size, category());
}

}  // namespace algorithms
}  // namespace embb

#endif  // EMBB_ALGORITHMS_INTERNAL_SCAN_INL_H_