for_each_test.cc 7.08 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2015, 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
 *
 * 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.
 */

#include <for_each_test.h>
#include <embb/algorithms/for_each.h>
29
#include <embb/mtapi/execution_policy.h>
30 31 32 33 34 35 36 37 38 39 40
#include <vector>
#include <deque>
#include <sstream>

/**
 * Functor to compute the square of a number.
 *
 * The result overwrites the original number.
 */
struct Square {
  template<typename Type>
41
  void operator()(Type& l) const {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    l = l * l;
  }
};

/**
 * Free function to compute the square of a number.
 *
 * The result overwrites the original number.
 */
static void SquareFunction(int &val) {
  val = val * val;
}

ForEachTest::ForEachTest() {
  CreateUnit("Different data structures")
    .Add(&ForEachTest::TestDataStructures, this);
  CreateUnit("Function Pointers").Add(&ForEachTest::TestFunctionPointers, this);
  CreateUnit("Ranges").Add(&ForEachTest::TestRanges, this);
  CreateUnit("Block sizes").Add(&ForEachTest::TestBlockSizes, this);
  CreateUnit("Policies").Add(&ForEachTest::TestPolicy, this);
  CreateUnit("Stress test").Add(&ForEachTest::StressTest, this);
}

void ForEachTest::TestDataStructures() {
  using embb::algorithms::ForEach;

  int array[kCountSize];
  std::vector<int> vector(kCountSize);
  std::deque<int> deque(kCountSize);
  for (size_t i = 0; i < kCountSize; i++) {
    array[i] = static_cast<int>(i+2);
    vector[i] = static_cast<int>(i+2);
    deque[i] = static_cast<int>(i+2);
  }

  ForEach(array, array + kCountSize, Square());
  ForEach(vector.begin(), vector.end(), Square());
  ForEach(deque.begin(), deque.end(), Square());

  for (size_t i = 0; i < kCountSize; i++) {
    int expected = static_cast<int>(i+2);
    expected = expected * expected;
    PT_EXPECT_EQ(expected, array[i]);
    PT_EXPECT_EQ(expected, vector[i]);
    PT_EXPECT_EQ(expected, deque[i]);
  }
}

void ForEachTest::TestFunctionPointers() {
  using embb::algorithms::ForEach;

  std::vector<int> vector(kCountSize);
  for (size_t i = 0; i < kCountSize; i++) {
    vector[i] = static_cast<int>(i+2);
  }
  ForEach(vector.begin(), vector.end(), &SquareFunction);
  for (size_t i = 0; i < kCountSize; i++) {
    int expected = static_cast<int>(i+2);
    expected = expected * expected;
    PT_EXPECT_EQ(expected, vector[i]);
  }
}

void ForEachTest::TestRanges() {
  using embb::algorithms::ForEach;
  size_t count = 4;
  std::vector<int> init(count);
  std::vector<int> vector(count);
  for (size_t i = 0; i < count; i++) {
    init[i] = static_cast<int>(i+2);
  }

  // Ommit first element
  vector = init;
  ForEach(vector.begin() + 1, vector.end(), Square());
  PT_EXPECT_EQ(vector[0], init[0]);
  for (size_t i = 1; i < count; i++) {
    PT_EXPECT_EQ(vector[i], init[i]*init[i]);
  }

  // Ommit last element
  vector = init;
  ForEach(vector.begin(), vector.end() - 1, Square());
  for (size_t i = 0; i < count - 1; i++) {
    PT_EXPECT_EQ(vector[i], init[i]*init[i]);
  }
  PT_EXPECT_EQ(vector[count - 1], init[count - 1]);

  // Ommit first and last element
  vector = init;
  ForEach(vector.begin() + 1, vector.end() - 1, Square());
  PT_EXPECT_EQ(vector[0], init[0]);
  for (size_t i = 1; i < count - 1; i++) {
    PT_EXPECT_EQ(vector[i], init[i]*init[i]);
  }
  PT_EXPECT_EQ(vector[count - 1], init[count - 1]);

  // Only do first element
  vector = init;
  ForEach(vector.begin(), vector.begin() + 1, Square());
  PT_EXPECT_EQ(vector[0], init[0] * init[0]);
  for (size_t i = 1; i < count; i++) {
    PT_EXPECT_EQ(vector[i], init[i]);
  }

  // Only do last element
  vector = init;
  ForEach(vector.end() - 1, vector.end(), Square());
  for (size_t i = 0; i < count - 1; i++) {
    PT_EXPECT_EQ(vector[i], init[i]);
  }
  PT_EXPECT_EQ(vector[count - 1], init[count - 1] * init[count - 1]);

  // Only do second element
  vector = init;
  ForEach(vector.begin() + 1, vector.begin() + 2, Square());
  for (size_t i = 1; i < count; i++) {
    if (i != 1) {
      PT_EXPECT_EQ(vector[i], init[i]);
    } else {
      PT_EXPECT_EQ(vector[i], init[i] * init[i]);
    }
  }
}

void ForEachTest::TestBlockSizes() {
  using embb::algorithms::ForEach;
169
  using embb::mtapi::ExecutionPolicy;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  size_t count = 4;
  std::vector<int> init(count);
  std::vector<int> vector(count);
  for (size_t i = 0; i < count; i++) {
    init[i] = static_cast<int>(i+2);
  }

  for (size_t block_size = 1; block_size < count + 2; block_size++) {
    vector = init;
    ForEach(vector.begin(), vector.end(), Square(), ExecutionPolicy(),
            block_size);
    for (size_t i = 0; i < count; i++) {
      PT_EXPECT_EQ(vector[i], init[i]*init[i]);
    }
  }
}

void ForEachTest::TestPolicy() {
  using embb::algorithms::ForEach;
189
  using embb::mtapi::ExecutionPolicy;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  size_t count = 4;
  std::vector<int> init(count);
  std::vector<int> vector(count);
  for (size_t i = 0; i < count; i++) {
    init[i] = static_cast<int>(i+2);
  }

  vector = init;
  ForEach(vector.begin(), vector.end(), Square(), ExecutionPolicy());
  for (size_t i = 0; i < count; i++) {
    PT_EXPECT_EQ(vector[i], init[i]*init[i]);
  }

  vector = init;
  ForEach(vector.begin(), vector.end(), Square(), ExecutionPolicy(true));
  for (size_t i = 0; i < count; i++) {
    PT_EXPECT_EQ(vector[i], init[i]*init[i]);
  }
Marcus Winter committed
208

209 210 211 212 213 214 215 216 217
  vector = init;
  ForEach(vector.begin(), vector.end(), Square(), ExecutionPolicy(true, 1));
  for (size_t i = 0; i < count; i++) {
    PT_EXPECT_EQ(vector[i], init[i]*init[i]);
  }
}

void ForEachTest::StressTest() {
  using embb::algorithms::ForEach;
218
  using embb::mtapi::ExecutionPolicy;
219 220 221 222 223 224 225 226 227 228 229 230 231
  size_t count = embb::mtapi::Node::GetInstance().GetCoreCount() *10;
  std::vector<int> large_vector(count);
  for (size_t i = 0; i < count; i++) {
    large_vector[i] = static_cast<int>((i + 2) % 1000);
  }
  ForEach(large_vector.begin(), large_vector.end(), Square(), ExecutionPolicy(),
          2000);
  for (size_t i = 0; i < count; i++) {
    int expected = static_cast<int>((i + 2) % 1000);
    expected = expected * expected;
    PT_EXPECT_EQ(large_vector[i], expected);
  }
}