| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
|
| 34 |
#ifndef UNITTEST_SIMDFIXTURE_HPP |
| 35 |
#define UNITTEST_SIMDFIXTURE_HPP |
| 36 |
|
| 37 |
#include <gtest/gtest.h> |
| 38 |
#include <stk_simd/Simd.hpp> |
| 39 |
#include <Kokkos_Timer.hpp> |
| 40 |
#include <algorithm> |
| 41 |
#include <functional> |
| 42 |
|
| 43 |
namespace stk |
| 44 |
{ |
| 45 |
namespace unit_test_simd |
| 46 |
{ |
| 47 |
|
| 48 |
typedef std::vector<double, non_std::AlignedAllocator<double,64> > DoubleVector; |
| 49 |
typedef std::vector<stk::simd::Double> SimdDoubleVector; |
| 50 |
|
| 51 |
typedef std::vector<bool> BoolVector; |
| 52 |
typedef std::vector<stk::simd::Bool> SimdBoolVector; |
| 53 |
|
| 54 |
typedef stk::simd::Double* DoublesPtr; |
| 55 |
|
| 56 |
struct Range { |
| 57 |
double left; |
| 58 |
double right; |
| 59 |
}; |
| 60 |
|
| 61 |
inline |
| 62 |
double rand_in(Range range) { |
| 63 |
return range.left + (range.right-range.left) * (double(rand()) / (double)RAND_MAX); |
| 64 |
} |
| 65 |
|
| 66 |
// #define TIME_SIMD_OPERATIONS |
| 67 |
|
| 68 |
inline |
| 69 |
int size() { |
| 70 |
#ifdef TIME_SIMD_OPERATIONS |
| 71 |
return 8192; |
| 72 |
#else |
| 73 |
return 32; |
| 74 |
#endif |
| 75 |
} |
| 76 |
inline |
| 77 |
int simd_size() { return size() / stk::simd::ndoubles; } |
| 78 |
|
| 79 |
|
| 80 |
class TestSimdMathFunction : public ::testing::Test |
| 81 |
{ |
| 82 |
protected: |
| 83 |
TestSimdMathFunction() { } |
| 84 |
|
| 85 |
template <typename F1, typename F2> |
| 86 |
void test_operator(F1 scalarOperation, F2 simdOperation) |
| 87 |
{ |
| 88 |
DoubleVector scalarSolution = apply_generic_operator_on_scalar_array( scalarOperation ); |
| 89 |
SimdDoubleVector simdSolution = apply_generic_operator_on_simd_array( simdOperation ); |
| 90 |
#ifndef TIME_SIMD_OPERATIONS |
| 91 |
verify_arrays_equal(scalarSolution, simdSolution); |
| 92 |
#endif |
| 93 |
} |
| 94 |
|
| 95 |
template <typename ArgFunc> |
| 96 |
DoubleVector apply_generic_operator_on_scalar_array(const ArgFunc& argFunc) const { |
| 97 |
DoubleVector solution(size()); |
| 98 |
const int sz = size(); |
| 99 |
#ifdef TIME_SIMD_OPERATIONS |
| 100 |
Kokkos::Timer scalarTimer; |
| 101 |
#endif |
| 102 |
#if defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER) |
| 103 |
#pragma novector |
| 104 |
#endif |
| 105 |
for (int i=0; i < sz; ++i) |
| 106 |
solution[i] = argFunc(i); |
| 107 |
#ifdef TIME_SIMD_OPERATIONS |
| 108 |
printf("Sclr loop took: %g seconds.\n", scalarTimer.seconds()); |
| 109 |
#endif |
| 110 |
return solution; |
| 111 |
} |
| 112 |
|
| 113 |
template <typename ArgFunc> |
| 114 |
SimdDoubleVector apply_generic_operator_on_simd_array(const ArgFunc& argFunc) const { |
| 115 |
SimdDoubleVector solution(simd_size()); |
| 116 |
const int sz = simd_size(); |
| 117 |
#ifdef TIME_SIMD_OPERATIONS |
| 118 |
Kokkos::Timer simdTimer; |
| 119 |
#endif |
| 120 |
for (int i=0; i < sz; ++i) |
| 121 |
solution[i] = argFunc(i); |
| 122 |
#ifdef TIME_SIMD_OPERATIONS |
| 123 |
printf("Simd loop took: %g seconds.\n", simdTimer.seconds()); |
| 124 |
#endif |
| 125 |
return solution; |
| 126 |
} |
| 127 |
|
| 128 |
virtual std::string get_input_vals(int i) const = 0; |
| 129 |
|
| 130 |
void verify_arrays_equal(const DoubleVector& scalarVec, const SimdDoubleVector& simdVec) const { |
| 131 |
const double* simdSol = reinterpret_cast<const double*>(simdVec.data()); |
| 132 |
for (int i=0; i < size(); ++i) { |
| 133 |
EXPECT_DOUBLE_EQ(scalarVec[i], simdSol[i]) << "input values = " + get_input_vals(i); |
| 134 |
} |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
class SimdScalarDoubleHybrid |
| 140 |
{ |
| 141 |
public: |
| 142 |
|
| 143 |
SimdScalarDoubleHybrid() |
| 144 |
{ |
| 145 |
initialize_array(); |
| 146 |
} |
| 147 |
|
| 148 |
inline double scalar(int i) const { return array[i]; } |
| 149 |
inline stk::simd::Double simd(int i) const { return arraySimd[i]; } |
| 150 |
|
| 151 |
void set_values_in(Range range) { |
| 152 |
for (int i=0; i<size(); ++i) |
| 153 |
array[i] = rand_in(range); |
| 154 |
} |
| 155 |
|
| 156 |
private: |
| 157 |
|
| 158 |
void initialize_array() { |
| 159 |
array.resize(size()); |
| 160 |
set_values_in(Range{0.0, 0.5}); |
| 161 |
arraySimd = stk::simd::simd_ptr_cast(array.data()); |
| 162 |
} |
| 163 |
|
| 164 |
DoubleVector array; |
| 165 |
stk::simd::Double* arraySimd; |
| 166 |
}; |
| 167 |
|
| 168 |
class SimdScalarBoolHybrid |
| 169 |
{ |
| 170 |
public: |
| 171 |
|
| 172 |
SimdScalarBoolHybrid() |
| 173 |
{ |
| 174 |
initialize_array(); |
| 175 |
} |
| 176 |
|
| 177 |
inline bool scalar(int i) const { return array[i]; } |
| 178 |
inline stk::simd::Bool simd(int i) const { return arraySimd[i]; } |
| 179 |
|
| 180 |
void set_alternating_bools() { |
| 181 |
for (int i=0; i < size(); ++i) |
| 182 |
array[i] = i%2==0 ? true : false; |
| 183 |
|
| 184 |
DoubleVector onesAndZeros(size()); |
| 185 |
for (int i=0; i < size(); ++i) |
| 186 |
onesAndZeros[i] = array[i] ? 1.0 : 0.0; |
| 187 |
|
| 188 |
stk::simd::Double* onesAndZerosSimd = stk::simd::simd_ptr_cast(onesAndZeros.data()); |
| 189 |
|
| 190 |
for (int i=0; i < simd_size(); ++i) { |
| 191 |
arraySimd[i] = onesAndZerosSimd[i] > 0.5; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private: |
| 196 |
|
| 197 |
void initialize_array() { |
| 198 |
array.resize(size()); |
| 199 |
arraySimd.resize(simd_size()); |
| 200 |
set_alternating_bools(); |
| 201 |
} |
| 202 |
|
| 203 |
BoolVector array; |
| 204 |
std::vector<stk::simd::Bool> arraySimd; |
| 205 |
}; |
| 206 |
|
| 207 |
|
| 208 |
class MathFunctionWithOneDoubleArg : public TestSimdMathFunction |
| 209 |
{ |
| 210 |
protected: |
| 211 |
SimdScalarDoubleHybrid x; |
| 212 |
|
| 213 |
template <typename ScalarFunc, typename SimdFunc> |
| 214 |
void test_simd_operator(ScalarFunc scalarFunc, SimdFunc simdFunc, |
| 215 |
Range range) { |
| 216 |
x.set_values_in(range); |
| 217 |
test_operator([&](int i) { return scalarFunc(x.scalar(i)); }, |
| 218 |
[&](int i) { return simdFunc(x.simd(i)); }); |
| 219 |
} |
| 220 |
|
| 221 |
virtual std::string get_input_vals(int i) const override { |
| 222 |
return std::to_string(x.scalar(i)); |
| 223 |
} |
| 224 |
}; |
| 225 |
|
| 226 |
class MathFunctionWithTwoDoubleArg : public TestSimdMathFunction |
| 227 |
{ |
| 228 |
protected: |
| 229 |
SimdScalarDoubleHybrid x; |
| 230 |
SimdScalarDoubleHybrid y; |
| 231 |
|
| 232 |
template <typename ScalarFunc, typename SimdFunc> |
| 233 |
void test_simd_operator(ScalarFunc scalarFunc, SimdFunc simdFunc, |
| 234 |
Range rangeX, Range rangeY) { |
| 235 |
x.set_values_in(rangeX); |
| 236 |
y.set_values_in(rangeY); |
| 237 |
test_operator([&](int i) { return scalarFunc(x.scalar(i), y.scalar(i)); }, |
| 238 |
[&](int i) { return simdFunc(x.simd(i), y.simd(i)); }); |
| 239 |
} |
| 240 |
|
| 241 |
virtual std::string get_input_vals(int i) const override { |
| 242 |
return std::to_string(x.scalar(i))+" "+std::to_string(y.scalar(i)); |
| 243 |
} |
| 244 |
}; |
| 245 |
|
| 246 |
class MathFunctionWithThreeDoubleArg : public TestSimdMathFunction |
| 247 |
{ |
| 248 |
protected: |
| 249 |
SimdScalarDoubleHybrid x; |
| 250 |
SimdScalarDoubleHybrid y; |
| 251 |
SimdScalarDoubleHybrid z; |
| 252 |
|
| 253 |
template <typename ScalarFunc, typename SimdFunc> |
| 254 |
void test_simd_operator(ScalarFunc scalarFunc, SimdFunc simdFunc, |
| 255 |
Range rangeX, Range rangeY, Range rangeZ) { |
| 256 |
x.set_values_in(rangeX); |
| 257 |
y.set_values_in(rangeY); |
| 258 |
z.set_values_in(rangeZ); |
| 259 |
test_operator([&](int i) { return scalarFunc(x.scalar(i), y.scalar(i), z.scalar(i)); }, |
| 260 |
[&](int i) { return simdFunc(x.simd(i), y.simd(i), z.simd(i)); }); |
| 261 |
} |
| 262 |
|
| 263 |
virtual std::string get_input_vals(int i) const override { |
| 264 |
return std::to_string(x.scalar(i))+" "+std::to_string(y.scalar(i))+" "+std::to_string(z.scalar(i)); |
| 265 |
} |
| 266 |
}; |
| 267 |
|
| 268 |
class MathFunctionWithBoolAndDoubleArg : public TestSimdMathFunction |
| 269 |
{ |
| 270 |
protected: |
| 271 |
SimdScalarBoolHybrid condition; |
| 272 |
SimdScalarDoubleHybrid x; |
| 273 |
|
| 274 |
template <typename ScalarFunc, typename SimdFunc> |
| 275 |
void test_simd_operator(ScalarFunc scalarFunc, SimdFunc simdFunc, |
| 276 |
Range rangeX) { |
| 277 |
x.set_values_in(rangeX); |
| 278 |
test_operator([&](int i) { return scalarFunc(condition.scalar(i), x.scalar(i)); }, |
| 279 |
[&](int i) { return simdFunc(condition.simd(i), x.simd(i)); }); |
| 280 |
} |
| 281 |
|
| 282 |
virtual std::string get_input_vals(int i) const override { |
| 283 |
return std::to_string(x.scalar(i)); |
| 284 |
} |
| 285 |
}; |
| 286 |
|
| 287 |
class MathFunctionWithBoolAndTwoDoubleArg : public TestSimdMathFunction |
| 288 |
{ |
| 289 |
protected: |
| 290 |
SimdScalarBoolHybrid condition; |
| 291 |
SimdScalarDoubleHybrid x; |
| 292 |
SimdScalarDoubleHybrid y; |
| 293 |
|
| 294 |
template <typename ScalarFunc, typename SimdFunc> |
| 295 |
void test_simd_operator(ScalarFunc scalarFunc, SimdFunc simdFunc, |
| 296 |
Range rangeX, Range rangeY) { |
| 297 |
x.set_values_in(rangeX); |
| 298 |
y.set_values_in(rangeY); |
| 299 |
test_operator([&](int i) { return scalarFunc(condition.scalar(i), x.scalar(i), y.scalar(i)); }, |
| 300 |
[&](int i) { return simdFunc(condition.simd(i), x.simd(i), y.simd(i)); }); |
| 301 |
} |
| 302 |
|
| 303 |
virtual std::string get_input_vals(int i) const override { |
| 304 |
return std::to_string(x.scalar(i))+" "+std::to_string(y.scalar(i)); |
| 305 |
} |
| 306 |
}; |
| 307 |
|
| 308 |
} |
| 309 |
} |
| 310 |
// |
| 311 |
// helper functions. |
| 312 |
// |
| 313 |
template<typename VECTOR> |
| 314 |
typename VECTOR::value_type max_error(VECTOR& out1, VECTOR& out2) |
| 315 |
{ |
| 316 |
assert(out1.size() == out2.size()); |
| 317 |
typename VECTOR::value_type maxerr = 0.0; |
| 318 |
for (std::size_t n=0; n < out1.size(); ++n) { |
| 319 |
auto err = stk::math::abs(out1[n]-out2[n]); |
| 320 |
maxerr = stk::math::max(err, maxerr); |
| 321 |
} |
| 322 |
return maxerr; |
| 323 |
} |
| 324 |
|
| 325 |
template<typename VECTOR> |
| 326 |
typename VECTOR::value_type max_error(VECTOR& out1, VECTOR& out2, VECTOR& out3) |
| 327 |
{ |
| 328 |
assert(out1.size() == out2.size()); |
| 329 |
assert(out1.size() == out3.size()); |
| 330 |
typename VECTOR::value_type maxerr = 0.0; |
| 331 |
for (std::size_t n=0; n < out1.size(); ++n) { |
| 332 |
auto err = stk::math::abs(out1[n]-out2[n]); |
| 333 |
maxerr = stk::math::max(err, maxerr); |
| 334 |
err = stk::math::abs(out1[n]-out3[n]); |
| 335 |
maxerr = stk::math::max(err, maxerr); |
| 336 |
} |
| 337 |
return maxerr; |
| 338 |
} |
| 339 |
|
| 340 |
template<typename VECTOR> |
| 341 |
typename VECTOR::value_type max_error(VECTOR& out1, VECTOR& out2, VECTOR& out3, VECTOR& out4) |
| 342 |
{ |
| 343 |
assert(out1.size() == out2.size()); |
| 344 |
assert(out1.size() == out3.size()); |
| 345 |
assert(out1.size() == out4.size()); |
| 346 |
typename VECTOR::value_type maxerr = 0.0; |
| 347 |
for (std::size_t n=0; n < out1.size(); ++n) { |
| 348 |
auto err = stk::math::abs(out1[n]-out2[n]); |
| 349 |
maxerr = stk::math::max(err, maxerr); |
| 350 |
err = stk::math::abs(out1[n]-out3[n]); |
| 351 |
maxerr = stk::math::max(err, maxerr); |
| 352 |
err = stk::math::abs(out1[n]-out4[n]); |
| 353 |
maxerr = stk::math::max(err, maxerr); |
| 354 |
} |
| 355 |
return maxerr; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
#endif |