Skip to content

Commit

Permalink
[SYCL][E2E] Refactor Basic/built-ins/marray_common.cpp (#12875)
Browse files Browse the repository at this point in the history
  • Loading branch information
aelovikov-intel authored Feb 29, 2024
1 parent e151c5c commit 5ec9cce
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 110 deletions.
23 changes: 16 additions & 7 deletions sycl/test-e2e/Basic/built-ins/helpers.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <sycl/sycl.hpp>

template <typename T> bool equal(T x, T y) {
template <typename T> bool equal(T x, T y, double delta) {
// Maybe should be C++20's std::equality_comparable.
if constexpr (std::is_scalar_v<T>) {
return x == y;
return std::abs(x - y) <= delta;
} else {
for (size_t i = 0; i < x.size(); ++i)
if (x[i] != y[i])
if (std::abs(x[i] - y[i]) > delta)
return false;

return true;
Expand All @@ -15,10 +15,10 @@ template <typename T> bool equal(T x, T y) {

template <typename FuncTy, typename ExpectedTy,
typename... ArgTys>
void test(bool CheckDevice, FuncTy F, ExpectedTy Expected, ArgTys... Args) {
void test(bool CheckDevice, double delta, FuncTy F, ExpectedTy Expected, ArgTys... Args) {
auto R = F(Args...);
static_assert(std::is_same_v<decltype(Expected), decltype(R)>);
assert(equal(R, Expected));
assert(equal(R, Expected, delta));

if (!CheckDevice)
return;
Expand All @@ -29,15 +29,24 @@ void test(bool CheckDevice, FuncTy F, ExpectedTy Expected, ArgTys... Args) {
cgh.single_task([=]() {
auto R = F(Args...);
static_assert(std::is_same_v<decltype(Expected), decltype(R)>);
Success[0] = equal(R, Expected);
Success[0] = equal(R, Expected, delta);
});
});
assert(sycl::host_accessor{SuccessBuf}[0]);
}

template <typename FuncTy, typename ExpectedTy, typename... ArgTys>
void test(FuncTy F, ExpectedTy Expected, ArgTys... Args) {
test(true /*CheckDevice*/, F, Expected, Args...);
test(true /*CheckDevice*/, 0.0 /*delta*/, F, Expected, Args...);
}
template <typename FuncTy, typename ExpectedTy,
typename... ArgTys>
void test(bool CheckDevice, FuncTy F, ExpectedTy Expected, ArgTys... Args) {
test(CheckDevice, 0.0 /*delta*/, F, Expected, Args...);
}
template <typename FuncTy, typename ExpectedTy, typename... ArgTys>
void test(double delta, FuncTy F, ExpectedTy Expected, ArgTys... Args) {
test(true /*CheckDevice*/, delta, F, Expected, Args...);
}

// MSVC's STL spoils global namespace with math functions, so use explicit
Expand Down
167 changes: 64 additions & 103 deletions sycl/test-e2e/Basic/built-ins/marray_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,115 +8,76 @@
#endif
#include <cmath>

#include <sycl/sycl.hpp>
#include "helpers.hpp"

#define TEST(FUNC, MARRAY_ELEM_TYPE, DIM, EXPECTED, DELTA, ...) \
{ \
{ \
MARRAY_ELEM_TYPE result[DIM]; \
{ \
sycl::buffer<MARRAY_ELEM_TYPE> b(result, sycl::range{DIM}); \
deviceQueue.submit([&](sycl::handler &cgh) { \
sycl::accessor res_access{b, cgh}; \
cgh.single_task([=]() { \
sycl::marray<MARRAY_ELEM_TYPE, DIM> res = FUNC(__VA_ARGS__); \
for (int i = 0; i < DIM; i++) \
res_access[i] = res[i]; \
}); \
}); \
} \
for (int i = 0; i < DIM; i++) \
assert(abs(result[i] - EXPECTED[i]) <= DELTA); \
} \
}
int main() {
using namespace sycl;

#define EXPECTED(TYPE, ...) ((TYPE[]){__VA_ARGS__})
queue deviceQueue;
device dev = deviceQueue.get_device();

int main() {
sycl::queue deviceQueue;
sycl::device dev = deviceQueue.get_device();
marray<float, 2> ma1{1.0f, 2.0f};
marray<float, 2> ma2{1.0f, 2.0f};
marray<float, 2> ma3{3.0f, 2.0f};
marray<double, 2> ma4{1.0, 2.0};
marray<float, 3> ma5{M_PI, M_PI, M_PI};
marray<double, 3> ma6{M_PI, M_PI, M_PI};
marray<half, 3> ma7{M_PI, M_PI, M_PI};
marray<float, 2> ma8{0.3f, 0.6f};
marray<double, 2> ma9{5.0, 8.0};
marray<float, 3> ma10{180, 180, 180};
marray<double, 3> ma11{180, 180, 180};
marray<half, 3> ma12{180, 180, 180};
marray<half, 3> ma13{181, 179, 181};
marray<float, 2> ma14{+0.0f, -0.6f};
marray<double, 2> ma15{-0.0, 0.6f};

sycl::marray<float, 2> ma1{1.0f, 2.0f};
sycl::marray<float, 2> ma2{1.0f, 2.0f};
sycl::marray<float, 2> ma3{3.0f, 2.0f};
sycl::marray<double, 2> ma4{1.0, 2.0};
sycl::marray<float, 3> ma5{M_PI, M_PI, M_PI};
sycl::marray<double, 3> ma6{M_PI, M_PI, M_PI};
sycl::marray<sycl::half, 3> ma7{M_PI, M_PI, M_PI};
sycl::marray<float, 2> ma8{0.3f, 0.6f};
sycl::marray<double, 2> ma9{5.0, 8.0};
sycl::marray<float, 3> ma10{180, 180, 180};
sycl::marray<double, 3> ma11{180, 180, 180};
sycl::marray<sycl::half, 3> ma12{180, 180, 180};
sycl::marray<sycl::half, 3> ma13{181, 179, 181};
sycl::marray<float, 2> ma14{+0.0f, -0.6f};
sycl::marray<double, 2> ma15{-0.0, 0.6f};
bool has_fp16 = queue{}.get_device().has(sycl::aspect::fp16);
bool has_fp64 = queue{}.get_device().has(sycl::aspect::fp64);

// sycl::clamp
TEST(sycl::clamp, float, 2, EXPECTED(float, 1.0f, 2.0f), 0, ma1, ma2, ma3);
TEST(sycl::clamp, float, 2, EXPECTED(float, 1.0f, 2.0f), 0, ma1, 1.0f, 3.0f);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::clamp, double, 2, EXPECTED(double, 1.0, 2.0), 0, ma4, 1.0, 3.0);
// sycl::degrees
TEST(sycl::degrees, float, 3, EXPECTED(float, 180, 180, 180), 0, ma5);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::degrees, double, 3, EXPECTED(double, 180, 180, 180), 0, ma6);
if (dev.has(sycl::aspect::fp16))
TEST(sycl::degrees, sycl::half, 3, EXPECTED(sycl::half, 180, 180, 180), 0.2,
ma7);
// sycl::max
TEST(sycl::max, float, 2, EXPECTED(float, 3.0f, 2.0f), 0, ma1, ma3);
TEST(sycl::max, float, 2, EXPECTED(float, 1.5f, 2.0f), 0, ma1, 1.5f);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::max, double, 2, EXPECTED(double, 1.5, 2.0), 0, ma4, 1.5);
// sycl::min
TEST(sycl::min, float, 2, EXPECTED(float, 1.0f, 2.0f), 0, ma1, ma3);
TEST(sycl::min, float, 2, EXPECTED(float, 1.0f, 1.5f), 0, ma1, 1.5f);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::min, double, 2, EXPECTED(double, 1.0, 1.5), 0, ma4, 1.5);
// sycl::mix
TEST(sycl::mix, float, 2, EXPECTED(float, 1.6f, 2.0f), 0, ma1, ma3, ma8);
TEST(sycl::mix, float, 2, EXPECTED(float, 1.4f, 2.0f), 0, ma1, ma3, 0.2);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::mix, double, 2, EXPECTED(double, 3.0, 5.0), 0, ma4, ma9, 0.5);
// sycl::radians
TEST(sycl::radians, float, 3, EXPECTED(float, M_PI, M_PI, M_PI), 0, ma10);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::radians, double, 3, EXPECTED(double, M_PI, M_PI, M_PI), 0, ma11);
if (dev.has(sycl::aspect::fp16))
TEST(sycl::radians, sycl::half, 3, EXPECTED(sycl::half, M_PI, M_PI, M_PI),
0.002, ma12);
// sycl::step
TEST(sycl::step, float, 2, EXPECTED(float, 1.0f, 1.0f), 0, ma1, ma3);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::step, double, 2, EXPECTED(double, 1.0, 1.0), 0, ma4, ma9);
if (dev.has(sycl::aspect::fp16))
TEST(sycl::step, sycl::half, 3, EXPECTED(sycl::half, 1.0, 0.0, 1.0), 0,
ma12, ma13);
TEST(sycl::step, float, 2, EXPECTED(float, 1.0f, 0.0f), 0, 2.5f, ma3);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::step, double, 2, EXPECTED(double, 0.0f, 1.0f), 0, 6.0f, ma9);
// sycl::smoothstep
TEST(sycl::smoothstep, float, 2, EXPECTED(float, 1.0f, 1.0f), 0, ma8, ma1,
ma2);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::smoothstep, double, 2, EXPECTED(double, 1.0, 1.0f), 0.00000001,
ma4, ma9, ma9);
if (dev.has(sycl::aspect::fp16))
TEST(sycl::smoothstep, sycl::half, 3, EXPECTED(sycl::half, 1.0, 1.0, 1.0),
0, ma7, ma12, ma13);
TEST(sycl::smoothstep, float, 2, EXPECTED(float, 0.0553936f, 0.0f), 0.0000001,
2.5f, 6.0f, ma3);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::smoothstep, double, 2, EXPECTED(double, 0.0f, 1.0f), 0, 6.0f,
8.0f, ma9);
// clamp
test(F(clamp), marray<float, 2>{1.0f, 2.0f}, ma1, ma2, ma3);
test(F(clamp), marray<float, 2>{1.0f, 2.0f}, ma1, 1.0f, 3.0f);
test(has_fp64, F(clamp), marray<double, 2>{1.0, 2.0}, ma4, 1.0, 3.0);
// degrees
test(F(degrees), marray<float, 3>{180, 180, 180}, ma5);
test(has_fp64, F(degrees), marray<double, 3>{180, 180, 180}, ma6);
test(has_fp16, 0.2, F(degrees), marray<half, 3>{180, 180, 180}, ma7);
// max
test(F(max), marray<float, 2>{3.0f, 2.0f}, ma1, ma3);
test(F(max), marray<float, 2>{1.5f, 2.0f}, ma1, 1.5f);
test(has_fp64, F(max), marray<double, 2>{1.5, 2.0}, ma4, 1.5);
// min
test(F(min), marray<float, 2>{1.0f, 2.0f}, ma1, ma3);
test(F(min), marray<float, 2>{1.0f, 1.5f}, ma1, 1.5f);
test(has_fp64, F(min), marray<double, 2>{1.0, 1.5}, ma4, 1.5);
// mix
test(F(mix), marray<float, 2>{1.6f, 2.0f}, ma1, ma3, ma8);
test(F(mix), marray<float, 2>{1.4f, 2.0f}, ma1, ma3, 0.2);
test(has_fp64, F(mix), marray<double, 2>{3.0, 5.0}, ma4, ma9, 0.5);
// radians
test(F(radians), marray<float, 3>{M_PI, M_PI, M_PI}, ma10);
test(has_fp64, F(radians), marray<double, 3>{M_PI, M_PI, M_PI}, ma11);
test(has_fp16, 0.002, F(radians), marray<half, 3>{M_PI, M_PI, M_PI}, ma12);
// step
test(F(step), marray<float, 2>{1.0f, 1.0f}, ma1, ma3);
test(has_fp64, F(step), marray<double, 2>{1.0, 1.0}, ma4, ma9);
test(has_fp16, F(step), marray<half, 3>{1.0, 0.0, 1.0}, ma12, ma13);
test(F(step), marray<float, 2>{1.0f, 0.0f}, 2.5f, ma3);
test(has_fp64, F(step), marray<double, 2>{0.0f, 1.0f}, 6.0f, ma9);
// smoothstep
test(F(smoothstep), marray<float, 2>{1.0f, 1.0f}, ma8, ma1, ma2);
test(has_fp64, 0.00000001, F(smoothstep), marray<double, 2>{1.0, 1.0f}, ma4,
ma9, ma9);
test(has_fp16, F(smoothstep), marray<half, 3>{1.0, 1.0, 1.0}, ma7, ma12,
ma13);
test(0.0000001, F(smoothstep), marray<float, 2>{0.0553936f, 0.0f}, 2.5f, 6.0f,
ma3);
test(has_fp64, F(smoothstep), marray<double, 2>{0.0f, 1.0f}, 6.0f, 8.0f, ma9);
// sign
TEST(sycl::sign, float, 2, EXPECTED(float, +0.0f, -1.0f), 0, ma14);
if (dev.has(sycl::aspect::fp64))
TEST(sycl::sign, double, 2, EXPECTED(double, -0.0, 1.0), 0, ma15);
if (dev.has(sycl::aspect::fp16))
TEST(sycl::sign, sycl::half, 3, EXPECTED(sycl::half, 1.0, 1.0, 1.0), 0,
ma12);
test(F(sign), marray<float, 2>{+0.0f, -1.0f}, ma14);
test(has_fp64, F(sign), marray<double, 2>{-0.0, 1.0}, ma15);
test(has_fp16, F(sign), marray<half, 3>{1.0, 1.0, 1.0}, ma12);

return 0;
}

0 comments on commit 5ec9cce

Please sign in to comment.