|
| 1 | +//==---- simd_binop_integer_promotion.cpp - DPC++ ESIMD on-device test ----==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// REQUIRES: gpu |
| 9 | +// UNSUPPORTED: cuda |
| 10 | +// RUN: %clangxx -fsycl %s -o %t.out |
| 11 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 12 | +// |
| 13 | +// The test checks that ESIMD binary operation APIs honor integer promotion |
| 14 | +// rules. E.g.: |
| 15 | +// simd <short, 32> a; |
| 16 | +// simd <short, 32> b; |
| 17 | +// a + b; // yield simd <int, 32> |
| 18 | + |
| 19 | +#include "../esimd_test_utils.hpp" |
| 20 | + |
| 21 | +#include <CL/sycl.hpp> |
| 22 | +#include <sycl/ext/intel/experimental/esimd.hpp> |
| 23 | + |
| 24 | +#include <iostream> |
| 25 | +#include <limits> |
| 26 | + |
| 27 | +using namespace cl::sycl; |
| 28 | +using namespace sycl::ext::intel::experimental::esimd; |
| 29 | + |
| 30 | +template <typename T> struct KernelName; |
| 31 | + |
| 32 | +// The main test routine. |
| 33 | +template <typename T> bool test(queue q) { |
| 34 | + constexpr unsigned VL = 16; |
| 35 | + |
| 36 | + T A[VL]; |
| 37 | + T B[VL]; |
| 38 | + using T_promoted = decltype(T{} + T{}); |
| 39 | + T_promoted C[VL]; |
| 40 | + |
| 41 | + std::cout << "Testing " << typeid(T).name() << " + " << typeid(T).name() |
| 42 | + << " => " << typeid(T_promoted).name() << " ...\n"; |
| 43 | + |
| 44 | + for (unsigned i = 0; i < VL; ++i) { |
| 45 | + A[i] = i; |
| 46 | + B[i] = 1; |
| 47 | + } |
| 48 | + T maxNum = std::numeric_limits<T>::max(); |
| 49 | + // test overflow in one of the lanes |
| 50 | + A[VL / 2] = maxNum; |
| 51 | + B[VL / 2] = maxNum; |
| 52 | + |
| 53 | + try { |
| 54 | + buffer<T, 1> bufA(A, range<1>(VL)); |
| 55 | + buffer<T, 1> bufB(B, range<1>(VL)); |
| 56 | + buffer<T_promoted, 1> bufC(C, range<1>(VL)); |
| 57 | + range<1> glob_range{1}; |
| 58 | + |
| 59 | + auto e = q.submit([&](handler &cgh) { |
| 60 | + auto PA = bufA.template get_access<access::mode::read>(cgh); |
| 61 | + auto PB = bufB.template get_access<access::mode::read>(cgh); |
| 62 | + auto PC = bufC.template get_access<access::mode::write>(cgh); |
| 63 | + cgh.parallel_for<KernelName<T>>( |
| 64 | + glob_range, [=](id<1> i) SYCL_ESIMD_KERNEL { |
| 65 | + using namespace sycl::ext::intel::experimental::esimd; |
| 66 | + unsigned int offset = i * VL * sizeof(T); |
| 67 | + simd<T, VL> va; |
| 68 | + va.copy_from(PA, offset); |
| 69 | + simd<T, VL> vb; |
| 70 | + vb.copy_from(PB, offset); |
| 71 | + auto vc = va + vb; |
| 72 | + unsigned int offsetC = i * VL * sizeof(T_promoted); |
| 73 | + vc.copy_to(PC, offsetC); |
| 74 | + }); |
| 75 | + }); |
| 76 | + q.wait_and_throw(); |
| 77 | + } catch (cl::sycl::exception const &e) { |
| 78 | + std::cout << "SYCL exception caught: " << e.what() << '\n'; |
| 79 | + return e.get_cl_code(); |
| 80 | + } |
| 81 | + |
| 82 | + int err_cnt = 0; |
| 83 | + |
| 84 | + for (unsigned i = 0; i < VL; ++i) { |
| 85 | + T_promoted gold = (T_promoted)(A[i] + B[i]); |
| 86 | + T_promoted val = C[i]; |
| 87 | + |
| 88 | + if (val != gold) { |
| 89 | + if (++err_cnt < 10) { |
| 90 | + std::cout << "failed at index " << i << ": " << val << " != " << gold |
| 91 | + << " (gold)\n"; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + if (err_cnt > 0) { |
| 96 | + std::cout << " pass rate: " << ((float)(VL - err_cnt) / (float)VL) * 100.0f |
| 97 | + << "% (" << (VL - err_cnt) << "/" << VL << ")\n"; |
| 98 | + } |
| 99 | + |
| 100 | + std::cout << (err_cnt > 0 ? " FAILED\n" : " Passed\n"); |
| 101 | + return err_cnt > 0 ? false : true; |
| 102 | +} |
| 103 | + |
| 104 | +int main(int argc, char **argv) { |
| 105 | + queue q(esimd_test::ESIMDSelector{}, esimd_test::createExceptionHandler()); |
| 106 | + |
| 107 | + auto dev = q.get_device(); |
| 108 | + std::cout << "Running on " << dev.get_info<info::device::name>() << "\n"; |
| 109 | + |
| 110 | + bool passed = true; |
| 111 | + passed &= test<unsigned short>(q); |
| 112 | + passed &= test<short>(q); |
| 113 | + passed &= test<unsigned char>(q); |
| 114 | + passed &= test<char>(q); |
| 115 | + |
| 116 | + std::cout << (passed ? "=== Test passed\n" : "=== Test FAILED\n"); |
| 117 | + return passed ? 0 : 1; |
| 118 | +} |
0 commit comments