for_each-inl.h 5.76 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 32
 *
 * 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_FOR_EACH_INL_H_
#define EMBB_ALGORITHMS_INTERNAL_FOR_EACH_INL_H_

#include <cassert>

#include <embb/base/exceptions.h>
33
#include <embb/mtapi/mtapi.h>
34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <embb/algorithms/internal/partition.h>
#include <embb/algorithms/zip_iterator.h>

namespace embb {
namespace algorithms {

namespace internal {

template<typename RAI, typename Function>
class ForEachFunctor {
 public:
  /**
   * Constructs a for-each functor with arguments.
   */
48
  ForEachFunctor(size_t chunk_first, size_t chunk_last, Function unary,
49
                 const embb::mtapi::ExecutionPolicy& policy,
50
                 const BlockSizePartitioner<RAI>& partitioner)
51
  : chunk_first_(chunk_first), chunk_last_(chunk_last),
52
    unary_(unary), policy_(policy), partitioner_(partitioner) {
53 54
  }

55
  void Action(embb::mtapi::TaskContext&) {
56 57 58 59 60 61
    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();
      for (RAI it = first; it != last; ++it) {
62
        unary_(*it);
63
      }
64
    } else {
65 66 67
      // Recurse further:
      size_t chunk_split_index = (chunk_first_ + chunk_last_) / 2;
      // Split chunks into left / right branches:
68
      self_t functor_l(chunk_first_,
69 70
                       chunk_split_index,
                       unary_, policy_, partitioner_);
71
      self_t functor_r(chunk_split_index + 1,
72 73
                       chunk_last_,
                       unary_, policy_, partitioner_);
74 75 76 77 78 79 80
      embb::mtapi::Node& node = embb::mtapi::Node::GetInstance();
      embb::mtapi::Task task_l = node.Start(
        embb::base::MakeFunction(functor_l, &self_t::Action),
        policy_);
      embb::mtapi::Task task_r = node.Start(
        embb::base::MakeFunction(functor_r, &self_t::Action),
        policy_);
81
      task_l.Wait(MTAPI_INFINITE);
82
      task_r.Wait(MTAPI_INFINITE);
83 84 85 86
    }
  }

 private:
87 88 89
  typedef ForEachFunctor<RAI, Function> self_t;

 private:
90 91
  size_t chunk_first_;
  size_t chunk_last_;
92
  Function unary_;
93
  const embb::mtapi::ExecutionPolicy& policy_;
94
  const BlockSizePartitioner<RAI>& partitioner_;
95 96 97 98 99 100 101 102

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

template<typename RAI, typename Function>
103
void ForEachRecursive(RAI first, RAI last, Function unary,
104
  const embb::mtapi::ExecutionPolicy& policy, size_t block_size) {
105 106
  typedef typename std::iterator_traits<RAI>::difference_type difference_type;
  difference_type distance = std::distance(first, last);
107
  if (distance == 0) {
108
    return;
109 110
  } else if (distance < 0) {
    EMBB_THROW(embb::base::ErrorException, "Negative range for ForEach");
111 112 113 114
  }
  unsigned int num_cores = policy.GetCoreCount();
  if (num_cores == 0) {
    EMBB_THROW(embb::base::ErrorException, "No cores in execution policy");
115
  }
116
  embb::mtapi::Node& node = embb::mtapi::Node::GetInstance();
117 118
  // Determine actually used block size
  if (block_size == 0) {
119
    block_size = (static_cast<size_t>(distance) / num_cores);
120 121 122 123
    if (block_size == 0) {
      block_size = 1;
    }
  }
124
  // Check task number sufficiency
125
  if (((distance / block_size) * 2) + 1 > MTAPI_NODE_MAX_TASKS_DEFAULT) {
126 127
    EMBB_THROW(embb::base::ErrorException,
               "Not enough MTAPI tasks available for parallel foreach");
128
  }
129 130

  BlockSizePartitioner<RAI> partitioner(first, last, block_size);
131 132 133 134 135 136 137
  typedef ForEachFunctor<RAI, Function> functor_t;
  functor_t functor(0,
                    partitioner.Size() - 1,
                    unary, policy, partitioner);
  embb::mtapi::Task task = node.Start(
    embb::base::MakeFunction(functor, &functor_t::Action),
    policy);
138 139 140 141 142
  task.Wait(MTAPI_INFINITE);
}

template<typename RAI, typename Function>
void ForEachIteratorCheck(RAI first, RAI last, Function unary,
143
  const embb::mtapi::ExecutionPolicy& policy, size_t block_size,
144
                          std::random_access_iterator_tag) {
145
  return ForEachRecursive(first, last, unary, policy, block_size);
146 147 148 149 150
}

}  // namespace internal

template<typename RAI, typename Function>
151
void ForEach(RAI first, const RAI last, Function unary,
152
  const embb::mtapi::ExecutionPolicy& policy, size_t block_size) {
153 154 155 156 157 158 159 160 161
  typename std::iterator_traits<RAI>::iterator_category category;
  internal::ForEachIteratorCheck(first, last, unary, policy, block_size,
                                 category);
}

}  // namespace algorithms
}  // namespace embb

#endif  // EMBB_ALGORITHMS_INTERNAL_FOR_EACH_INL_H_