partition-inl.h 4.52 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 33
 *
 * 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_PARTITION_INL_H_
#define EMBB_ALGORITHMS_INTERNAL_PARTITION_INL_H_

namespace embb {
namespace algorithms {
namespace internal {

34 35 36 37
template<typename RAI>
ChunkDescriptor<RAI>::ChunkDescriptor(
  RAI first, RAI last) :
  first_(first), last_(last) {
38 39
}

40 41 42
template<typename RAI>
RAI ChunkDescriptor<RAI>::GetFirst() const {
  return first_;
43 44
}

45 46 47
template<typename RAI>
RAI ChunkDescriptor<RAI>::GetLast() const {
  return last_;
48 49
}

50 51 52 53 54 55 56 57 58
template<typename RAI>
BlockSizePartitioner<RAI>::BlockSizePartitioner(
    RAI first, RAI last, size_t chunkSize) :
    first_(first), last_(last), chunk_size_(chunkSize) {
  elements_count_ = static_cast<size_t>(std::distance(first_, last_));
  chunks_ = elements_count_ / chunk_size_;
  if (elements_count_ % chunk_size_ != 0) {
    chunks_++;
  }
59 60
}

61 62 63
template<typename RAI>
size_t BlockSizePartitioner<RAI>::Size() {
  return chunks_;
64 65
}

66 67 68 69
template<typename RAI>
const ChunkDescriptor<RAI>
  BlockSizePartitioner<RAI>::operator[](
    size_t const & index) const {
70 71
  typedef typename std::iterator_traits<RAI>::difference_type
    difference_type;
72 73 74 75 76
  RAI first_new(first_);
  first_new += static_cast<difference_type>(chunk_size_ * index);
  RAI last_new(first_new);
  if (index >= chunks_ - 1) {
    last_new = last_;
77
  } else {
78
    last_new += static_cast<difference_type>(chunk_size_);
79
  }
80
  return ChunkDescriptor<RAI>(first_new, last_new);
81 82
}

83 84 85
template<typename RAI>
size_t ChunkPartitioner<RAI>::Size() {
  return size_;
86 87
}

88 89 90 91
template<typename RAI>
ChunkPartitioner<RAI>::ChunkPartitioner(
  RAI first, RAI last, size_t amountChunks) :
  first_(first), last_(last) {
92
  if (amountChunks > 0) {
93
    size_ = amountChunks;
94
  } else {
95
    // if no concrete chunk size was given, use number of cores
96
    embb::mtapi::Node& node = embb::mtapi::Node::GetInstance();
97
    size_ = node.GetWorkerThreadCount();
98
  }
99 100
  elements_count_ = static_cast<size_t>(std::distance(first_, last_));
  if (size_ > elements_count_) {
101 102
    // if we want to make more chunks than we have elements, correct
    // the number of chunks
103
    size_ = elements_count_;
104
  }
105 106
  standard_chunk_size_ = elements_count_ / size_;
  bigger_chunk_count_  = elements_count_ % size_;
107 108
}

109 110 111
template<typename RAI>
const ChunkDescriptor<RAI>
  ChunkPartitioner<RAI>::operator[](
112
    size_t const& index) const {
113
  typedef typename std::iterator_traits<RAI>::difference_type
114
      difference_type;
115
  // Number of element preceding elements in the given chunk
116
  size_t prec_elements_count = 0;
117 118
  if (index <= bigger_chunk_count_) {
    prec_elements_count = index * (standard_chunk_size_ + 1);
119
  } else {
120 121
    prec_elements_count =
      (standard_chunk_size_ + 1) * bigger_chunk_count_ +
122
      (standard_chunk_size_ * (index - bigger_chunk_count_));
123
  }
124 125 126 127 128 129 130 131
  size_t cur_elements_count = (index < bigger_chunk_count_)
           ? (standard_chunk_size_ + 1)
           : standard_chunk_size_;
  RAI first_new(first_);
  first_new += static_cast<difference_type>(prec_elements_count);
  RAI last_new(first_new);
  last_new += static_cast<difference_type>(cur_elements_count);
  return ChunkDescriptor<RAI>(first_new, last_new);
132 133 134 135 136 137 138
}

}  // namespace internal
}  // namespace algorithms
}  // namespace embb

#endif  // EMBB_ALGORITHMS_INTERNAL_PARTITION_INL_H_