scan_test.cc 10.5 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 29 30 31 32 33 34 35 36 37
 *
 * 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 <scan_test.h>
#include <embb/algorithms/scan.h>
#include <vector>
#include <deque>
#include <functional>

/**
 * Functor to compute the square of a number.
 */
struct Square {
  template<typename Type>
38
  Type operator()(Type& l) const {
39 40 41 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    return l * l;
  }
};

static int SquareFunction(int &val) {
  return val * val;
}

static int AddFunction(int lhs, int rhs) {
  return lhs + rhs;
}

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

void ScanTest::TestDataStructures() {
  using embb::algorithms::Scan;

  int array[kCountSize];
  std::vector<double> 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);
  }

  int outputArray[kCountSize];
  std::vector<double> outputVector(kCountSize);
  std::deque<int> outputDeque(kCountSize);

  Scan(array, array + kCountSize, outputArray, 0, std::plus<int>());
  Scan(vector.begin(), vector.end(), outputVector.begin(),
      static_cast<double>(0), std::plus<double>());
  Scan(deque.begin(), deque.end(), outputDeque.begin(), 0, std::plus<int>());

  int expected = 0;
  for (size_t i = 0; i < kCountSize; i++) {
    expected += array[i];

    PT_EXPECT_EQ(expected, outputArray[i]);
    PT_EXPECT_EQ(expected, outputVector[i]);
    PT_EXPECT_EQ(expected, outputDeque[i]);
  }
}

void ScanTest::TestTransform() {
  using embb::algorithms::Scan;

  std::vector<int> vector(kCountSize);
  std::vector<int> outputVector(kCountSize);

  for (size_t i = 0; i < kCountSize; i++) {
    vector[i] = static_cast<int>(i+2);
  }
  Scan(vector.begin(), vector.end(), outputVector.begin(), 0, std::plus<int>(),
       Square());

  int expected = 0;
  for (size_t i = 0; i < kCountSize; i++) {
     expected += vector[i] * vector[i];
     PT_EXPECT_EQ(expected, outputVector[i]);
  }
}

void ScanTest::TestFunctionPointers() {
  using embb::algorithms::Scan;

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

  Scan(vector.begin(), vector.end(), outputVector.begin(), 0, &AddFunction);
  int expected = 0;
  for (size_t i = 0; i < kCountSize; i++) {
    expected += vector[i];
    PT_EXPECT_EQ(expected, outputVector[i]);
  }

  outputVector = init;
  Scan(vector.begin(), vector.end(), outputVector.begin(), 0, &AddFunction,
       &SquareFunction);
  expected = 0;
  for (size_t i = 0; i < kCountSize; i++) {
    expected += vector[i] * vector[i];
    PT_EXPECT_EQ(expected, outputVector[i]);
  }

  outputVector = init;
  Scan(vector.begin(), vector.end(), outputVector.begin(), 0, &AddFunction,
       Square());
  expected = 0;
  for (size_t i = 0; i < kCountSize; i++) {
    expected += vector[i] * vector[i];
    PT_EXPECT_EQ(expected, outputVector[i]);
  }

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

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

  // Ommit first element
  outputVector = init;
  Scan(vector.begin() + 1, vector.end(), outputVector.begin() + 1,
       0, std::plus<int>());
  PT_EXPECT_EQ(outputVector[0], vector[0]);
  int expected = 0;
  for (size_t i = 1; i < count; i++) {
    expected += vector[i];
    PT_EXPECT_EQ(expected, outputVector[i]);
  }

  // Ommit last element
  outputVector = init;
  Scan(vector.begin(), vector.end() - 1, outputVector.begin(), 0,
       std::plus<int>());
  expected = 0;
  for (size_t i = 0; i < count - 1; i++) {
    expected += vector[i];
    PT_EXPECT_EQ(expected, outputVector[i]);
  }
  PT_EXPECT_EQ(outputVector[count - 1], vector[count - 1]);

  // Ommit first and last element
  outputVector = init;
  Scan(vector.begin() + 1, vector.end() - 1, outputVector.begin() + 1, 0,
       std::plus<int>());
  PT_EXPECT_EQ(outputVector[0], vector[0]);
  expected = 0;
  for (size_t i = 1; i < count - 1; i++) {
    expected += vector[i];
    PT_EXPECT_EQ(expected, outputVector[i]);
  }
  PT_EXPECT_EQ(outputVector[count - 1], vector[count - 1]);

  // Only do first element
  outputVector = init;
  Scan(vector.begin(), vector.begin() + 1, outputVector.begin(), 0,
       std::plus<int>());
  for (size_t i = 0; i < count; i++) {
    PT_EXPECT_EQ(outputVector[i], vector[i]);
  }

  // Only do last element
  outputVector = init;
  Scan(vector.end() - 1, vector.end(), outputVector.end() - 1, 0,
       std::plus<int>());
  for (size_t i = 0; i < count; i++) {
    PT_EXPECT_EQ(outputVector[i], vector[i]);
  }

  // Only do second element
  outputVector = init;
  Scan(vector.begin() + 1, vector.begin() + 2, outputVector.begin() + 1, 0,
       std::plus<int>());
  for (size_t i = 0; i < count; i++) {
    PT_EXPECT_EQ(outputVector[i], vector[i]);
  }
}

void ScanTest::TestBlockSizes() {
  using embb::algorithms::Scan;
231
  using embb::mtapi::ExecutionPolicy;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  using embb::algorithms::Identity;
  size_t count = 4;
  std::vector<int> init(count);
  std::vector<int> vector(count);
  std::vector<int> outputVector(count);
  for (size_t i = 0; i < count; i++) {
    init[i] = static_cast<int>(i+2);
  }
  vector = init;

  for (size_t block_size = 1; block_size < count + 2; block_size++) {
    outputVector = init;
    Scan(vector.begin(), vector.end(), outputVector.begin(), 0,
         std::plus<int>(), Identity(), ExecutionPolicy(), block_size);
    int expected = 0;
    for (size_t i = 0; i < count; i++) {
      expected += vector[i];
      PT_EXPECT_EQ(expected, outputVector[i]);
    }
  }
}

void ScanTest::TestPolicy() {
  using embb::algorithms::Scan;
256
  using embb::mtapi::ExecutionPolicy;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
  using embb::algorithms::Identity;
  size_t count = 4;
  std::vector<int> init(count);
  std::vector<int> vector(count);
  std::vector<int> outputVector(count);
  for (size_t i = 0; i < count; i++) {
    init[i] = static_cast<int>(i+2);
  }
  vector = init;

  outputVector = init;
  Scan(vector.begin(), vector.end(), outputVector.begin(), 0, std::plus<int>(),
    Identity(), ExecutionPolicy());
  int expected = 0;
  for (size_t i = 0; i < count; i++) {
    expected += vector[i];
    PT_EXPECT_EQ(expected, outputVector[i]);
  }

  outputVector = init;
  Scan(vector.begin(), vector.end(), outputVector.begin(), 0, std::plus<int>(),
    Identity(), ExecutionPolicy(true));
  expected = 0;
  for (size_t i = 0; i < count; i++) {
    expected += vector[i];
    PT_EXPECT_EQ(expected, outputVector[i]);
  }

  outputVector = init;
  Scan(vector.begin(), vector.end(), outputVector.begin(), 0, std::plus<int>(),
    Identity(), ExecutionPolicy(true, 1));
  expected = 0;
  for (size_t i = 0; i < count; i++) {
    expected += vector[i];
    PT_EXPECT_EQ(expected, outputVector[i]);
  }
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  // Empty list should not throw and not change output:
  outputVector = init;
  std::vector<int>::iterator out_it = outputVector.begin();
  Scan(vector.begin(), vector.begin(), out_it, 0, std::plus<int>());
  PT_EXPECT(out_it == outputVector.begin());

#ifdef EMBB_USE_EXCEPTIONS
  bool empty_core_set_thrown = false;
  try {
    Scan(vector.begin(), vector.end(), outputVector.begin(),
         0, std::plus<int>(), Identity(),
         ExecutionPolicy(false));
  }
  catch (embb::base::ErrorException &) {
    empty_core_set_thrown = true;
  }
  PT_EXPECT_MSG(empty_core_set_thrown,
    "Empty core set should throw ErrorException");
  bool negative_range_thrown = false;
  try {
    std::vector<int>::iterator second = vector.begin() + 1;
    Scan(second, vector.begin(), outputVector.begin(), 0, std::plus<int>());
  }
  catch (embb::base::ErrorException &) {
    negative_range_thrown = true;
  }
  PT_EXPECT_MSG(negative_range_thrown,
    "Negative range should throw ErrorException");
#endif
322 323 324 325 326
}

void ScanTest::StressTest() {
  using embb::algorithms::Scan;
  using embb::algorithms::Identity;
327
  using embb::mtapi::ExecutionPolicy;
328 329 330 331 332 333 334 335 336 337 338 339 340 341
  size_t count = embb::mtapi::Node::GetInstance().GetCoreCount() *10;
  std::vector<int> large_vector(count);
  std::vector<int> large_vector_output(count);
  for (size_t i = 0; i < count; i++) {
    large_vector[i] = static_cast<int>((i + 2) % 1000);
  }
  Scan(large_vector.begin(), large_vector.end(), large_vector_output.begin(), 0,
       std::plus<int>(), Identity(), ExecutionPolicy(), 2000);
  int expected = 0;
  for (size_t i = 0; i < count; i++) {
    expected += large_vector[i];
    PT_EXPECT_EQ(expected, large_vector_output[i]);
  }
}