1 2 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 38 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
|
/*
* GridTools
*
* Copyright (c) 2014-2023, ETH Zurich
* All rights reserved.
*
* Please, refer to the LICENSE file in the root directory.
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <gridtools/common/atomic_functions.hpp>
#include <cstdlib>
#include <gtest/gtest.h>
#include <verifier.hpp>
template <typename T>
void TestAtomicAdd() {
constexpr int size = 360;
T field[size];
T sum = 0;
T sumRef = 0;
for (int cnt = 0; cnt < size; ++cnt) {
field[cnt] = static_cast<T>(std::rand() % 100 + (std::rand() % 100) * 0.005);
sumRef += field[cnt];
}
#pragma omp for nowait
for (int cnt = 0; cnt < size; ++cnt) {
gridtools::atomic_add(sum, field[cnt]);
}
EXPECT_TRUE(gridtools::expect_with_threshold(sumRef, sum));
}
template <typename T>
void TestAtomicSub() {
constexpr int size = 360;
T field[size];
T sum = 0;
T sumRef = 0;
for (int cnt = 0; cnt < size; ++cnt) {
field[cnt] = static_cast<T>(std::rand() % 100 + (std::rand() % 100) * 0.005);
sumRef -= field[cnt];
}
#pragma omp for nowait
for (int cnt = 0; cnt < size; ++cnt) {
gridtools::atomic_sub(sum, field[cnt]);
}
EXPECT_TRUE(gridtools::expect_with_threshold(sumRef, sum));
}
template <typename T>
void TestAtomicMin() {
constexpr int size = 360;
T field[size];
T min = 99999;
T minRef = 99999;
for (int cnt = 0; cnt < size; ++cnt) {
field[cnt] = static_cast<T>(std::rand() % 100 + (std::rand() % 100) * 0.005);
minRef = std::min(minRef, field[cnt]);
}
#pragma omp for nowait
for (int cnt = 0; cnt < size; ++cnt) {
gridtools::atomic_min(min, field[cnt]);
}
EXPECT_EQ(minRef, min);
}
template <typename T>
void TestAtomicMax() {
constexpr int size = 360;
T field[size];
T max = 0;
T maxRef = 0;
for (int cnt = 0; cnt < size; ++cnt) {
field[cnt] = static_cast<T>(std::rand() % 100 + (std::rand() % 100) * 0.005);
maxRef = std::max(maxRef, field[cnt]);
}
#pragma omp for nowait
for (int cnt = 0; cnt < size; ++cnt) {
gridtools::atomic_max(max, field[cnt]);
}
EXPECT_EQ(maxRef, max);
}
TEST(AtomicFunctionsUnittest, add) {
TestAtomicAdd<int>();
TestAtomicAdd<double>();
TestAtomicAdd<float>();
}
TEST(AtomicFunctionsUnittest, sub) {
TestAtomicSub<int>();
TestAtomicSub<double>();
TestAtomicSub<float>();
}
TEST(AtomicFunctionsUnittest, min) {
TestAtomicMin<int>();
TestAtomicMin<double>();
TestAtomicMin<float>();
}
TEST(AtomicFunctionsUnittest, max) {
TestAtomicMax<int>();
TestAtomicMax<double>();
TestAtomicMax<float>();
}
|