From b7aa4c97f06048b14eb31dd76f83808652f9ca7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ciglari=C4=8D?= Date: Wed, 2 Mar 2022 08:45:54 +0000 Subject: [PATCH 1/8] [SYCL] added tests for some half builtins --- SYCL/Basic/half_builtins.cpp | 227 +++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100755 SYCL/Basic/half_builtins.cpp diff --git a/SYCL/Basic/half_builtins.cpp b/SYCL/Basic/half_builtins.cpp new file mode 100755 index 000000000000..9bab82dd3192 --- /dev/null +++ b/SYCL/Basic/half_builtins.cpp @@ -0,0 +1,227 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: %HOST_RUN_PLACEHOLDER %t.out +// RUN: %CPU_RUN_PLACEHOLDER %t.out +// RUN: %GPU_RUN_PLACEHOLDER %t.out +// RUN: %ACC_RUN_PLACEHOLDER %t.out + +#include + +#include +#include + +using namespace cl::sycl; + +constexpr int N = 16 * 3; // divisible by all vector sizes + +#define TEST_BUILTIN_1_VEC_IMPL(NAME, SZ) \ + { \ + buffer a_buf((half##SZ *)&a[0], N / SZ); \ + buffer d_buf((half##SZ *)&d[0], N / SZ); \ + q.submit([&](handler &cgh) { \ + auto A = a_buf.get_access(cgh); \ + auto D = d_buf.get_access(cgh); \ + cgh.parallel_for(N / SZ, \ + [=](id<1> index) { D[index] = NAME(A[index]); }); \ + }); \ + } \ + for (int i = 0; i < N; i++) { \ + assert(fabs(d[i] - NAME(a[i])) < \ + std::numeric_limits::epsilon()); \ + } + +// vectors of size 3 need separate test, as they actually have the size of 4 +// halfs +#define TEST_BUILTIN_1_VEC3_IMPL(NAME) \ + { \ + buffer a_buf((half3 *)&a[0], N / 4); \ + buffer d_buf((half3 *)&d[0], N / 4); \ + q.submit([&](handler &cgh) { \ + auto A = a_buf.get_access(cgh); \ + auto D = d_buf.get_access(cgh); \ + cgh.parallel_for(N / 4, \ + [=](id<1> index) { D[index] = NAME(A[index]); }); \ + }); \ + } \ + for (int i = 0; i < N; i++) { \ + if (i % 4 != 3) { \ + assert(fabs(d[i] - NAME(a[i])) < \ + std::numeric_limits::epsilon()); \ + } \ + } + +#define TEST_BUILTIN_1_SCAL_IMPL(NAME) \ + { \ + buffer a_buf(&a[0], N); \ + buffer d_buf(&d[0], N); \ + q.submit([&](handler &cgh) { \ + auto A = a_buf.get_access(cgh); \ + auto D = d_buf.get_access(cgh); \ + cgh.parallel_for(N, [=](id<1> index) { D[index] = NAME(A[index]); }); \ + }); \ + } \ + for (int i = 0; i < N; i++) { \ + assert(fabs(d[i] - NAME(a[i])) < \ + std::numeric_limits::epsilon()); \ + } + +#define TEST_BUILTIN_1(NAME) \ + TEST_BUILTIN_1_SCAL_IMPL(NAME) \ + TEST_BUILTIN_1_VEC_IMPL(NAME, 2) \ + TEST_BUILTIN_1_VEC3_IMPL(NAME) \ + TEST_BUILTIN_1_VEC_IMPL(NAME, 4) \ + TEST_BUILTIN_1_VEC_IMPL(NAME, 8) \ + TEST_BUILTIN_1_VEC_IMPL(NAME, 16) + +#define TEST_BUILTIN_2_VEC_IMPL(NAME, SZ) \ + { \ + buffer a_buf((half##SZ *)&a[0], N / SZ); \ + buffer b_buf((half##SZ *)&b[0], N / SZ); \ + buffer d_buf((half##SZ *)&d[0], N / SZ); \ + q.submit([&](handler &cgh) { \ + auto A = a_buf.get_access(cgh); \ + auto B = b_buf.get_access(cgh); \ + auto D = d_buf.get_access(cgh); \ + cgh.parallel_for( \ + N / SZ, [=](id<1> index) { D[index] = NAME(A[index], B[index]); }); \ + }); \ + } \ + for (int i = 0; i < N; i++) { \ + assert(fabs(d[i] - NAME(a[i], b[i])) < \ + std::numeric_limits::epsilon()); \ + } + +#define TEST_BUILTIN_2_VEC3_IMPL(NAME) \ + { \ + buffer a_buf((half3 *)&a[0], N / 4); \ + buffer b_buf((half3 *)&b[0], N / 4); \ + buffer d_buf((half3 *)&d[0], N / 4); \ + q.submit([&](handler &cgh) { \ + auto A = a_buf.get_access(cgh); \ + auto B = b_buf.get_access(cgh); \ + auto D = d_buf.get_access(cgh); \ + cgh.parallel_for( \ + N / 4, [=](id<1> index) { D[index] = NAME(A[index], B[index]); }); \ + }); \ + } \ + for (int i = 0; i < N; i++) { \ + if (i % 4 != 3) { \ + assert(fabs(d[i] - NAME(a[i], b[i])) < \ + std::numeric_limits::epsilon()); \ + } \ + } + +#define TEST_BUILTIN_2_SCAL_IMPL(NAME) \ + { \ + buffer a_buf(&a[0], N); \ + buffer b_buf(&b[0], N); \ + buffer d_buf(&d[0], N); \ + q.submit([&](handler &cgh) { \ + auto A = a_buf.get_access(cgh); \ + auto B = b_buf.get_access(cgh); \ + auto D = d_buf.get_access(cgh); \ + cgh.parallel_for( \ + N, [=](id<1> index) { D[index] = NAME(A[index], B[index]); }); \ + }); \ + } \ + for (int i = 0; i < N; i++) { \ + assert(fabs(d[i] - NAME(a[i], b[i])) < \ + std::numeric_limits::epsilon()); \ + } + +#define TEST_BUILTIN_2(NAME) \ + TEST_BUILTIN_2_SCAL_IMPL(NAME) \ + TEST_BUILTIN_2_VEC_IMPL(NAME, 2) \ + TEST_BUILTIN_2_VEC3_IMPL(NAME) \ + TEST_BUILTIN_2_VEC_IMPL(NAME, 4) \ + TEST_BUILTIN_2_VEC_IMPL(NAME, 8) \ + TEST_BUILTIN_2_VEC_IMPL(NAME, 16) + +#define TEST_BUILTIN_3_VEC_IMPL(NAME, SZ) \ + { \ + buffer a_buf((half##SZ *)&a[0], N / SZ); \ + buffer b_buf((half##SZ *)&b[0], N / SZ); \ + buffer c_buf((half##SZ *)&c[0], N / SZ); \ + buffer d_buf((half##SZ *)&d[0], N / SZ); \ + q.submit([&](handler &cgh) { \ + auto A = a_buf.get_access(cgh); \ + auto B = b_buf.get_access(cgh); \ + auto C = c_buf.get_access(cgh); \ + auto D = d_buf.get_access(cgh); \ + cgh.parallel_for(N / SZ, [=](id<1> index) { \ + D[index] = NAME(A[index], B[index], C[index]); \ + }); \ + }); \ + } \ + for (int i = 0; i < N; i++) { \ + assert(fabs(d[i] - NAME(a[i], b[i], c[i])) < \ + std::numeric_limits::epsilon()); \ + } + +#define TEST_BUILTIN_3_VEC3_IMPL(NAME) \ + { \ + buffer a_buf((half3 *)&a[0], N / 4); \ + buffer b_buf((half3 *)&b[0], N / 4); \ + buffer c_buf((half3 *)&c[0], N / 4); \ + buffer d_buf((half3 *)&d[0], N / 4); \ + q.submit([&](handler &cgh) { \ + auto A = a_buf.get_access(cgh); \ + auto B = b_buf.get_access(cgh); \ + auto C = c_buf.get_access(cgh); \ + auto D = d_buf.get_access(cgh); \ + cgh.parallel_for(N / 4, [=](id<1> index) { \ + D[index] = NAME(A[index], B[index], C[index]); \ + }); \ + }); \ + } \ + for (int i = 0; i < N; i++) { \ + if (i % 4 != 3) { \ + assert(fabs(d[i] - NAME(a[i], b[i], c[i])) < \ + std::numeric_limits::epsilon()); \ + } \ + } + +#define TEST_BUILTIN_3_SCAL_IMPL(NAME) \ + { \ + buffer a_buf(&a[0], N); \ + buffer b_buf(&b[0], N); \ + buffer c_buf(&c[0], N); \ + buffer d_buf(&d[0], N); \ + q.submit([&](handler &cgh) { \ + auto A = a_buf.get_access(cgh); \ + auto B = b_buf.get_access(cgh); \ + auto C = c_buf.get_access(cgh); \ + auto D = d_buf.get_access(cgh); \ + cgh.parallel_for(N, [=](id<1> index) { \ + D[index] = NAME(A[index], B[index], C[index]); \ + }); \ + }); \ + } \ + for (int i = 0; i < N; i++) { \ + assert(fabs(d[i] - NAME(a[i], b[i], c[i])) < \ + std::numeric_limits::epsilon()); \ + } + +#define TEST_BUILTIN_3(NAME) \ + TEST_BUILTIN_3_SCAL_IMPL(NAME) \ + TEST_BUILTIN_3_VEC_IMPL(NAME, 2) \ + TEST_BUILTIN_3_VEC3_IMPL(NAME) \ + TEST_BUILTIN_3_VEC_IMPL(NAME, 4) \ + TEST_BUILTIN_3_VEC_IMPL(NAME, 8) \ + TEST_BUILTIN_3_VEC_IMPL(NAME, 16) + +int main() { + queue q; + std::vector a(N), b(N), c(N), d(N); + for (int i = 0; i < N; i++) { + a[i] = i / (half)N; + b[i] = (N - i) / (half)N; + c[i] = (half)(3 * i); + } + + TEST_BUILTIN_1(fabs); + TEST_BUILTIN_2(fmin); + TEST_BUILTIN_2(fmax); + TEST_BUILTIN_3(fma); + + return 0; +} From 2e797c108a56f041dd9637ee6daf0367a40ea0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ciglari=C4=8D?= Date: Thu, 3 Mar 2022 08:41:05 +0000 Subject: [PATCH 2/8] disabled for OpenCL backend on CPU --- SYCL/Basic/half_builtins.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/SYCL/Basic/half_builtins.cpp b/SYCL/Basic/half_builtins.cpp index 9bab82dd3192..be7232adaa1b 100755 --- a/SYCL/Basic/half_builtins.cpp +++ b/SYCL/Basic/half_builtins.cpp @@ -4,6 +4,9 @@ // RUN: %GPU_RUN_PLACEHOLDER %t.out // RUN: %ACC_RUN_PLACEHOLDER %t.out +// OpenCL CPU driver does not support cl_khr_fp16 extension +// UNSUPPORTED: cpu && opencl + #include #include @@ -210,6 +213,8 @@ constexpr int N = 16 * 3; // divisible by all vector sizes TEST_BUILTIN_3_VEC_IMPL(NAME, 16) int main() { + + queue q; std::vector a(N), b(N), c(N), d(N); for (int i = 0; i < N; i++) { From 02f653c779181a996c3c48e75e3809ae5875bd4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ciglari=C4=8D?= Date: Thu, 3 Mar 2022 08:49:59 +0000 Subject: [PATCH 3/8] format --- SYCL/Basic/half_builtins.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/SYCL/Basic/half_builtins.cpp b/SYCL/Basic/half_builtins.cpp index be7232adaa1b..fe74f406a4db 100755 --- a/SYCL/Basic/half_builtins.cpp +++ b/SYCL/Basic/half_builtins.cpp @@ -213,8 +213,6 @@ constexpr int N = 16 * 3; // divisible by all vector sizes TEST_BUILTIN_3_VEC_IMPL(NAME, 16) int main() { - - queue q; std::vector a(N), b(N), c(N), d(N); for (int i = 0; i < N; i++) { From ac29a61a6cb03a65fa90c9a672666caa39aa4ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ciglari=C4=8D?= Date: Mon, 7 Mar 2022 11:47:07 +0000 Subject: [PATCH 4/8] actually check relative error --- SYCL/Basic/half_builtins.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/SYCL/Basic/half_builtins.cpp b/SYCL/Basic/half_builtins.cpp index fe74f406a4db..ca568fe77919 100755 --- a/SYCL/Basic/half_builtins.cpp +++ b/SYCL/Basic/half_builtins.cpp @@ -16,6 +16,11 @@ using namespace cl::sycl; constexpr int N = 16 * 3; // divisible by all vector sizes +bool check(half a, half b) { + return fabs(2 * (a - b) / (a + b)) < + std::numeric_limits::epsilon() || a < std::numeric_limits::min(); +} + #define TEST_BUILTIN_1_VEC_IMPL(NAME, SZ) \ { \ buffer a_buf((half##SZ *)&a[0], N / SZ); \ @@ -28,8 +33,7 @@ constexpr int N = 16 * 3; // divisible by all vector sizes }); \ } \ for (int i = 0; i < N; i++) { \ - assert(fabs(d[i] - NAME(a[i])) < \ - std::numeric_limits::epsilon()); \ + assert(check(d[i], NAME(a[i]))); \ } // vectors of size 3 need separate test, as they actually have the size of 4 @@ -47,8 +51,7 @@ constexpr int N = 16 * 3; // divisible by all vector sizes } \ for (int i = 0; i < N; i++) { \ if (i % 4 != 3) { \ - assert(fabs(d[i] - NAME(a[i])) < \ - std::numeric_limits::epsilon()); \ + assert(check(d[i], NAME(a[i]))); \ } \ } @@ -63,8 +66,7 @@ constexpr int N = 16 * 3; // divisible by all vector sizes }); \ } \ for (int i = 0; i < N; i++) { \ - assert(fabs(d[i] - NAME(a[i])) < \ - std::numeric_limits::epsilon()); \ + assert(check(d[i], NAME(a[i]))); \ } #define TEST_BUILTIN_1(NAME) \ @@ -89,8 +91,7 @@ constexpr int N = 16 * 3; // divisible by all vector sizes }); \ } \ for (int i = 0; i < N; i++) { \ - assert(fabs(d[i] - NAME(a[i], b[i])) < \ - std::numeric_limits::epsilon()); \ + assert(check(d[i], NAME(a[i], b[i]))); \ } #define TEST_BUILTIN_2_VEC3_IMPL(NAME) \ @@ -108,8 +109,7 @@ constexpr int N = 16 * 3; // divisible by all vector sizes } \ for (int i = 0; i < N; i++) { \ if (i % 4 != 3) { \ - assert(fabs(d[i] - NAME(a[i], b[i])) < \ - std::numeric_limits::epsilon()); \ + assert(check(d[i], NAME(a[i], b[i]))); \ } \ } @@ -127,8 +127,7 @@ constexpr int N = 16 * 3; // divisible by all vector sizes }); \ } \ for (int i = 0; i < N; i++) { \ - assert(fabs(d[i] - NAME(a[i], b[i])) < \ - std::numeric_limits::epsilon()); \ + assert(check(d[i], NAME(a[i], b[i]))); \ } #define TEST_BUILTIN_2(NAME) \ @@ -156,8 +155,7 @@ constexpr int N = 16 * 3; // divisible by all vector sizes }); \ } \ for (int i = 0; i < N; i++) { \ - assert(fabs(d[i] - NAME(a[i], b[i], c[i])) < \ - std::numeric_limits::epsilon()); \ + assert(check(d[i], NAME(a[i], b[i], c[i]))); \ } #define TEST_BUILTIN_3_VEC3_IMPL(NAME) \ @@ -178,8 +176,7 @@ constexpr int N = 16 * 3; // divisible by all vector sizes } \ for (int i = 0; i < N; i++) { \ if (i % 4 != 3) { \ - assert(fabs(d[i] - NAME(a[i], b[i], c[i])) < \ - std::numeric_limits::epsilon()); \ + assert(check(d[i], NAME(a[i], b[i], c[i]))); \ } \ } @@ -200,8 +197,7 @@ constexpr int N = 16 * 3; // divisible by all vector sizes }); \ } \ for (int i = 0; i < N; i++) { \ - assert(fabs(d[i] - NAME(a[i], b[i], c[i])) < \ - std::numeric_limits::epsilon()); \ + assert(check(d[i], NAME(a[i], b[i], c[i]))); \ } #define TEST_BUILTIN_3(NAME) \ From 2f7fc03e23718e5bd31f4c385a6ad6db7c5405e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ciglari=C4=8D?= Date: Mon, 7 Mar 2022 11:54:41 +0000 Subject: [PATCH 5/8] format --- SYCL/Basic/half_builtins.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SYCL/Basic/half_builtins.cpp b/SYCL/Basic/half_builtins.cpp index ca568fe77919..83ee9f1eea62 100755 --- a/SYCL/Basic/half_builtins.cpp +++ b/SYCL/Basic/half_builtins.cpp @@ -18,7 +18,8 @@ constexpr int N = 16 * 3; // divisible by all vector sizes bool check(half a, half b) { return fabs(2 * (a - b) / (a + b)) < - std::numeric_limits::epsilon() || a < std::numeric_limits::min(); + std::numeric_limits::epsilon() || + a < std::numeric_limits::min(); } #define TEST_BUILTIN_1_VEC_IMPL(NAME, SZ) \ From 20dd1119e1ef482ea3c8f33838734774ba539885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ciglari=C4=8D?= Date: Mon, 21 Mar 2022 12:45:03 +0000 Subject: [PATCH 6/8] addressed review comments and refactored into a single kernel --- SYCL/Basic/half_builtins.cpp | 250 +++++++++++++++-------------------- 1 file changed, 110 insertions(+), 140 deletions(-) diff --git a/SYCL/Basic/half_builtins.cpp b/SYCL/Basic/half_builtins.cpp index 83ee9f1eea62..2bc8e2d5cf51 100755 --- a/SYCL/Basic/half_builtins.cpp +++ b/SYCL/Basic/half_builtins.cpp @@ -10,64 +10,57 @@ #include #include -#include +#include -using namespace cl::sycl; +using namespace sycl; -constexpr int N = 16 * 3; // divisible by all vector sizes +constexpr int SZ_max = 16; -bool check(half a, half b) { - return fabs(2 * (a - b) / (a + b)) < - std::numeric_limits::epsilon() || - a < std::numeric_limits::min(); +bool check(float a, float b) { + return fabs(2 * (a - b) / (a + b)) < std::numeric_limits::epsilon() || + a < std::numeric_limits::min(); +} + +template bool check(vec a, vec b) { + for (int i = 0; i < N; i++) { + if (!check(a[i], b[i])) { + return false; + } + } + return true; } #define TEST_BUILTIN_1_VEC_IMPL(NAME, SZ) \ { \ - buffer a_buf((half##SZ *)&a[0], N / SZ); \ - buffer d_buf((half##SZ *)&d[0], N / SZ); \ - q.submit([&](handler &cgh) { \ - auto A = a_buf.get_access(cgh); \ - auto D = d_buf.get_access(cgh); \ - cgh.parallel_for(N / SZ, \ - [=](id<1> index) { D[index] = NAME(A[index]); }); \ - }); \ - } \ - for (int i = 0; i < N; i++) { \ - assert(check(d[i], NAME(a[i]))); \ + float##SZ *a = (float##SZ *)&A[0]; \ + float##SZ *b = (float##SZ *)&B[0]; \ + if (i < SZ_max / SZ) { \ + if (!check(NAME(a[i]), NAME(a[i].convert()).convert())) { \ + err[0] = 1; \ + } \ + } \ } // vectors of size 3 need separate test, as they actually have the size of 4 -// halfs +// elements #define TEST_BUILTIN_1_VEC3_IMPL(NAME) \ { \ - buffer a_buf((half3 *)&a[0], N / 4); \ - buffer d_buf((half3 *)&d[0], N / 4); \ - q.submit([&](handler &cgh) { \ - auto A = a_buf.get_access(cgh); \ - auto D = d_buf.get_access(cgh); \ - cgh.parallel_for(N / 4, \ - [=](id<1> index) { D[index] = NAME(A[index]); }); \ - }); \ - } \ - for (int i = 0; i < N; i++) { \ - if (i % 4 != 3) { \ - assert(check(d[i], NAME(a[i]))); \ + float3 *a = (float3 *)&A[0]; \ + float3 *b = (float3 *)&B[0]; \ + if (i < SZ_max / 4) { \ + if (!check(NAME(a[i]), NAME(a[i].convert()).convert())) { \ + err[0] = 1; \ + } \ } \ } #define TEST_BUILTIN_1_SCAL_IMPL(NAME) \ { \ - buffer a_buf(&a[0], N); \ - buffer d_buf(&d[0], N); \ - q.submit([&](handler &cgh) { \ - auto A = a_buf.get_access(cgh); \ - auto D = d_buf.get_access(cgh); \ - cgh.parallel_for(N, [=](id<1> index) { D[index] = NAME(A[index]); }); \ - }); \ - } \ - for (int i = 0; i < N; i++) { \ - assert(check(d[i], NAME(a[i]))); \ + float *a = (float *)&A[0]; \ + float *b = (float *)&B[0]; \ + if (!check(NAME(a[i]), (float)NAME((half)a[i]))) { \ + err[0] = 1; \ + } \ } #define TEST_BUILTIN_1(NAME) \ @@ -80,55 +73,37 @@ bool check(half a, half b) { #define TEST_BUILTIN_2_VEC_IMPL(NAME, SZ) \ { \ - buffer a_buf((half##SZ *)&a[0], N / SZ); \ - buffer b_buf((half##SZ *)&b[0], N / SZ); \ - buffer d_buf((half##SZ *)&d[0], N / SZ); \ - q.submit([&](handler &cgh) { \ - auto A = a_buf.get_access(cgh); \ - auto B = b_buf.get_access(cgh); \ - auto D = d_buf.get_access(cgh); \ - cgh.parallel_for( \ - N / SZ, [=](id<1> index) { D[index] = NAME(A[index], B[index]); }); \ - }); \ - } \ - for (int i = 0; i < N; i++) { \ - assert(check(d[i], NAME(a[i], b[i]))); \ + float##SZ *a = (float##SZ *)&A[0]; \ + float##SZ *b = (float##SZ *)&B[0]; \ + if (i < SZ_max / SZ) { \ + if (!check(NAME(a[i], b[i]), \ + NAME(a[i].convert(), b[i].convert()) \ + .convert())) { \ + err[0] = 1; \ + } \ + } \ } #define TEST_BUILTIN_2_VEC3_IMPL(NAME) \ { \ - buffer a_buf((half3 *)&a[0], N / 4); \ - buffer b_buf((half3 *)&b[0], N / 4); \ - buffer d_buf((half3 *)&d[0], N / 4); \ - q.submit([&](handler &cgh) { \ - auto A = a_buf.get_access(cgh); \ - auto B = b_buf.get_access(cgh); \ - auto D = d_buf.get_access(cgh); \ - cgh.parallel_for( \ - N / 4, [=](id<1> index) { D[index] = NAME(A[index], B[index]); }); \ - }); \ - } \ - for (int i = 0; i < N; i++) { \ - if (i % 4 != 3) { \ - assert(check(d[i], NAME(a[i], b[i]))); \ + float3 *a = (float3 *)&A[0]; \ + float3 *b = (float3 *)&B[0]; \ + if (i < SZ_max / 4) { \ + if (!check(NAME(a[i], b[i]), \ + NAME(a[i].convert(), b[i].convert()) \ + .convert())) { \ + err[0] = 1; \ + } \ } \ } #define TEST_BUILTIN_2_SCAL_IMPL(NAME) \ { \ - buffer a_buf(&a[0], N); \ - buffer b_buf(&b[0], N); \ - buffer d_buf(&d[0], N); \ - q.submit([&](handler &cgh) { \ - auto A = a_buf.get_access(cgh); \ - auto B = b_buf.get_access(cgh); \ - auto D = d_buf.get_access(cgh); \ - cgh.parallel_for( \ - N, [=](id<1> index) { D[index] = NAME(A[index], B[index]); }); \ - }); \ - } \ - for (int i = 0; i < N; i++) { \ - assert(check(d[i], NAME(a[i], b[i]))); \ + float *a = (float *)&A[0]; \ + float *b = (float *)&B[0]; \ + if (!check(NAME(a[i], b[i]), (float)NAME((half)a[i], (half)b[i]))) { \ + err[0] = 1; \ + } \ } #define TEST_BUILTIN_2(NAME) \ @@ -141,64 +116,43 @@ bool check(half a, half b) { #define TEST_BUILTIN_3_VEC_IMPL(NAME, SZ) \ { \ - buffer a_buf((half##SZ *)&a[0], N / SZ); \ - buffer b_buf((half##SZ *)&b[0], N / SZ); \ - buffer c_buf((half##SZ *)&c[0], N / SZ); \ - buffer d_buf((half##SZ *)&d[0], N / SZ); \ - q.submit([&](handler &cgh) { \ - auto A = a_buf.get_access(cgh); \ - auto B = b_buf.get_access(cgh); \ - auto C = c_buf.get_access(cgh); \ - auto D = d_buf.get_access(cgh); \ - cgh.parallel_for(N / SZ, [=](id<1> index) { \ - D[index] = NAME(A[index], B[index], C[index]); \ - }); \ - }); \ - } \ - for (int i = 0; i < N; i++) { \ - assert(check(d[i], NAME(a[i], b[i], c[i]))); \ + float##SZ *a = (float##SZ *)&A[0]; \ + float##SZ *b = (float##SZ *)&B[0]; \ + float##SZ *c = (float##SZ *)&C[0]; \ + if (i < SZ_max / SZ) { \ + if (!check(NAME(a[i], b[i], c[i]), \ + NAME(a[i].convert(), b[i].convert(), \ + c[i].convert()) \ + .convert())) { \ + err[0] = 1; \ + } \ + } \ } #define TEST_BUILTIN_3_VEC3_IMPL(NAME) \ { \ - buffer a_buf((half3 *)&a[0], N / 4); \ - buffer b_buf((half3 *)&b[0], N / 4); \ - buffer c_buf((half3 *)&c[0], N / 4); \ - buffer d_buf((half3 *)&d[0], N / 4); \ - q.submit([&](handler &cgh) { \ - auto A = a_buf.get_access(cgh); \ - auto B = b_buf.get_access(cgh); \ - auto C = c_buf.get_access(cgh); \ - auto D = d_buf.get_access(cgh); \ - cgh.parallel_for(N / 4, [=](id<1> index) { \ - D[index] = NAME(A[index], B[index], C[index]); \ - }); \ - }); \ - } \ - for (int i = 0; i < N; i++) { \ - if (i % 4 != 3) { \ - assert(check(d[i], NAME(a[i], b[i], c[i]))); \ + float3 *a = (float3 *)&A[0]; \ + float3 *b = (float3 *)&B[0]; \ + float3 *c = (float3 *)&C[0]; \ + if (i < SZ_max / 4) { \ + if (!check(NAME(a[i], b[i], c[i]), \ + NAME(a[i].convert(), b[i].convert(), \ + c[i].convert()) \ + .convert())) { \ + err[0] = 1; \ + } \ } \ } #define TEST_BUILTIN_3_SCAL_IMPL(NAME) \ { \ - buffer a_buf(&a[0], N); \ - buffer b_buf(&b[0], N); \ - buffer c_buf(&c[0], N); \ - buffer d_buf(&d[0], N); \ - q.submit([&](handler &cgh) { \ - auto A = a_buf.get_access(cgh); \ - auto B = b_buf.get_access(cgh); \ - auto C = c_buf.get_access(cgh); \ - auto D = d_buf.get_access(cgh); \ - cgh.parallel_for(N, [=](id<1> index) { \ - D[index] = NAME(A[index], B[index], C[index]); \ - }); \ - }); \ - } \ - for (int i = 0; i < N; i++) { \ - assert(check(d[i], NAME(a[i], b[i], c[i]))); \ + float *a = (float *)&A[0]; \ + float *b = (float *)&B[0]; \ + float *c = (float *)&C[0]; \ + if (!check(NAME(a[i], b[i], c[i]), \ + (float)NAME((half)a[i], (half)b[i], (half)c[i]))) { \ + err[0] = 1; \ + } \ } #define TEST_BUILTIN_3(NAME) \ @@ -211,17 +165,33 @@ bool check(half a, half b) { int main() { queue q; - std::vector a(N), b(N), c(N), d(N); - for (int i = 0; i < N; i++) { - a[i] = i / (half)N; - b[i] = (N - i) / (half)N; - c[i] = (half)(3 * i); + float16 a, b, c, d; + for (int i = 0; i < SZ_max; i++) { + a[i] = i / (float)SZ_max; + b[i] = (SZ_max - i) / (float)SZ_max; + c[i] = (float)(3 * i); } - - TEST_BUILTIN_1(fabs); - TEST_BUILTIN_2(fmin); - TEST_BUILTIN_2(fmax); - TEST_BUILTIN_3(fma); + int err = 0; + { + buffer a_buf(&a, 1); + buffer b_buf(&b, 1); + buffer c_buf(&c, 1); + buffer err_buf(&err, 1); + q.submit([&](handler &cgh) { + auto A = a_buf.get_access(cgh); + auto B = b_buf.get_access(cgh); + auto C = c_buf.get_access(cgh); + auto err = err_buf.get_access(cgh); + cgh.parallel_for(SZ_max, [=](item<1> index) { + size_t i = index.get_id(0); + TEST_BUILTIN_1(fabs); + TEST_BUILTIN_2(fmin); + TEST_BUILTIN_2(fmax); + TEST_BUILTIN_3(fma); + }); + }); + } + assert(err == 0); return 0; } From f0dbb4a494a3d1ee898ef444ccf9e81e0fb490b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ciglari=C4=8D?= Date: Mon, 21 Mar 2022 13:58:28 +0100 Subject: [PATCH 7/8] Update SYCL/Basic/half_builtins.cpp Co-authored-by: Alexey Bader --- SYCL/Basic/half_builtins.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SYCL/Basic/half_builtins.cpp b/SYCL/Basic/half_builtins.cpp index 2bc8e2d5cf51..91c9dd917fe8 100755 --- a/SYCL/Basic/half_builtins.cpp +++ b/SYCL/Basic/half_builtins.cpp @@ -7,7 +7,8 @@ // OpenCL CPU driver does not support cl_khr_fp16 extension // UNSUPPORTED: cpu && opencl -#include +#include + #include #include From bf1912dfe674e6366be0da97336de69c048efc80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Ciglari=C4=8D?= Date: Mon, 21 Mar 2022 13:06:24 +0000 Subject: [PATCH 8/8] format --- SYCL/Basic/half_builtins.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/SYCL/Basic/half_builtins.cpp b/SYCL/Basic/half_builtins.cpp index 91c9dd917fe8..0d49bd689163 100755 --- a/SYCL/Basic/half_builtins.cpp +++ b/SYCL/Basic/half_builtins.cpp @@ -9,7 +9,6 @@ #include - #include #include