|
| 1 | +//==------- ctor_default.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, level_zero |
| 9 | +// XREQUIRES: gpu |
| 10 | +// TODO gpu and level_zero in REQUIRES due to only this platforms supported yet. |
| 11 | +// The current "REQUIRES" should be replaced with "gpu" only as mentioned in |
| 12 | +// "XREQUIRES". |
| 13 | +// UNSUPPORTED: cuda, hip |
| 14 | +// RUN: %clangxx -fsycl %s -fsycl-device-code-split=per_kernel -o %t.out |
| 15 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 16 | +// |
| 17 | +// Test for esimd default constructor. |
| 18 | +// |
| 19 | +// The simd can't be constructed with sycl::half data type. The issue was |
| 20 | +// created (https://github.com/intel/llvm/issues/5077) and the TEST_HALF macros |
| 21 | +// must be enabled when it is resolved. |
| 22 | + |
| 23 | +#include "../common.hpp" |
| 24 | + |
| 25 | +using namespace cl::sycl; |
| 26 | +using namespace sycl::ext::intel::experimental::esimd; |
| 27 | +using namespace esimd_test::api::functional; |
| 28 | + |
| 29 | +// Dummy kernel for submitting some code into device side. |
| 30 | +template <typename DataT, int NumElems, typename T> struct Kernel; |
| 31 | + |
| 32 | +// Descriptor class for the case of calling constructor in initializer context |
| 33 | +struct initializer { |
| 34 | + static std::string get_description() { return "initializer"; } |
| 35 | + |
| 36 | + template <typename DataT, int NumElems> |
| 37 | + static void call_simd_ctor(DataT *output_data) { |
| 38 | + simd<DataT, NumElems> simd_by_init = simd<DataT, NumElems>(); |
| 39 | + simd_by_init.copy_to(output_data); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +// Descriptor class for the case of calling constructor in variable declaration |
| 44 | +// context |
| 45 | +struct var_declaration { |
| 46 | + static std::string get_description() { return "variable declaration"; } |
| 47 | + |
| 48 | + template <typename DataT, int NumElems> |
| 49 | + static void call_simd_ctor(DataT *output_data) { |
| 50 | + simd<DataT, NumElems> simd_by_var_decl; |
| 51 | + simd_by_var_decl.copy_to(output_data); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +// Descriptor class for the case of calling constructor in rvalue in an |
| 56 | +// expression context |
| 57 | +struct rval_in_expression { |
| 58 | + static std::string get_description() { return "rvalue in an expression"; } |
| 59 | + |
| 60 | + template <typename DataT, int NumElems> |
| 61 | + static void call_simd_ctor(DataT *output_data) { |
| 62 | + simd<DataT, NumElems> simd_by_rval; |
| 63 | + simd_by_rval = simd<DataT, NumElems>(); |
| 64 | + simd_by_rval.copy_to(output_data); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +// Descriptor class for the case of calling constructor in const reference |
| 69 | +// context |
| 70 | +struct const_ref { |
| 71 | + static std::string get_description() { return "const reference"; } |
| 72 | + |
| 73 | + template <typename DataT, int NumElems> |
| 74 | + static void |
| 75 | + call_simd_by_const_ref(const simd<DataT, NumElems> &simd_by_const_ref, |
| 76 | + DataT *output_data) { |
| 77 | + simd_by_const_ref.copy_to(output_data); |
| 78 | + } |
| 79 | + |
| 80 | + template <typename DataT, int NumElems> |
| 81 | + static void call_simd_ctor(DataT *output_data) { |
| 82 | + call_simd_by_const_ref<DataT, NumElems>(simd<DataT, NumElems>(), |
| 83 | + output_data); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +// Functor with the main test routine to iterate over the pre-determined |
| 88 | +// datatypes |
| 89 | +template <typename DataT, int NumElems, typename TestCaseT> struct test { |
| 90 | + bool operator()(sycl::queue &queue, const std::string &data_type) { |
| 91 | + bool passed{true}; |
| 92 | + |
| 93 | + DataT default_val{}; |
| 94 | + using AllocatorT = sycl::usm_allocator<DataT, sycl::usm::alloc::shared>; |
| 95 | + |
| 96 | + std::vector<DataT, AllocatorT> result{NumElems, AllocatorT{queue}}; |
| 97 | + queue.submit([&](sycl::handler &cgh) { |
| 98 | + auto out = result.data(); |
| 99 | + cgh.single_task<Kernel<DataT, NumElems, TestCaseT>>( |
| 100 | + [=]() SYCL_ESIMD_KERNEL { |
| 101 | + TestCaseT::template call_simd_ctor<DataT, NumElems>(out); |
| 102 | + }); |
| 103 | + }); |
| 104 | + for (const auto &it : result) { |
| 105 | + if (it != default_val) { |
| 106 | + passed = false; |
| 107 | + log::fail<NumElems>( |
| 108 | + "In simd by " + TestCaseT::get_description() + |
| 109 | + " elem value is not equal to default value, retrieved: " + |
| 110 | + std::to_string(it) + |
| 111 | + ", expected: " + std::to_string(default_val), |
| 112 | + data_type); |
| 113 | + } |
| 114 | + } |
| 115 | + return passed; |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +template <typename DataT, typename TestT> |
| 120 | +using run_test_with_one_elem = test<DataT, 1, TestT>; |
| 121 | + |
| 122 | +template <typename DataT, typename TestT> |
| 123 | +using run_test_with_eight_elems = test<DataT, 8, TestT>; |
| 124 | + |
| 125 | +template <typename DataT, typename TestT> |
| 126 | +using run_test_with_sixteen_elems = test<DataT, 16, TestT>; |
| 127 | + |
| 128 | +template <typename DataT, typename TestT> |
| 129 | +using run_test_with_thirty_two_elems = test<DataT, 32, TestT>; |
| 130 | + |
| 131 | +template <typename TestT, typename... T> |
| 132 | +bool run_verification_with_chosen_test_type( |
| 133 | + sycl::queue &queue, const named_type_pack<T...> &types) { |
| 134 | + bool passed{true}; |
| 135 | + |
| 136 | + passed &= for_all_types<run_test_with_one_elem, TestT>(types, queue); |
| 137 | + passed &= for_all_types<run_test_with_eight_elems, TestT>(types, queue); |
| 138 | + passed &= for_all_types<run_test_with_sixteen_elems, TestT>(types, queue); |
| 139 | + passed &= for_all_types<run_test_with_thirty_two_elems, TestT>(types, queue); |
| 140 | + return passed; |
| 141 | +} |
| 142 | + |
| 143 | +int main(int argc, char **argv) { |
| 144 | + sycl::queue queue{esimd_test::ESIMDSelector{}, |
| 145 | + esimd_test::createExceptionHandler()}; |
| 146 | + |
| 147 | + bool passed{true}; |
| 148 | + |
| 149 | + const auto types{get_tested_types<tested_types::all>()}; |
| 150 | + |
| 151 | + passed &= run_verification_with_chosen_test_type<initializer>(queue, types); |
| 152 | + passed &= |
| 153 | + run_verification_with_chosen_test_type<var_declaration>(queue, types); |
| 154 | + passed &= |
| 155 | + run_verification_with_chosen_test_type<rval_in_expression>(queue, types); |
| 156 | + passed &= run_verification_with_chosen_test_type<const_ref>(queue, types); |
| 157 | + |
| 158 | + std::cout << (passed ? "=== Test passed\n" : "=== Test FAILED\n"); |
| 159 | + return passed ? 0 : 1; |
| 160 | +} |
0 commit comments