|
| 1 | +//===-- ctor_broadcast.hpp - Functions for tests on simd broadcast constructor |
| 2 | +// definition. -------------------------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +/// |
| 10 | +/// \file |
| 11 | +/// This file provides functions for tests on simd broadcast constructor. |
| 12 | +/// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include "../value_conv.hpp" |
| 18 | +#include "common.hpp" |
| 19 | + |
| 20 | +namespace esimd = sycl::ext::intel::experimental::esimd; |
| 21 | + |
| 22 | +namespace esimd_test::api::functional::ctors { |
| 23 | + |
| 24 | +// Descriptor class for the case of calling constructor in initializer context. |
| 25 | +struct initializer { |
| 26 | + static std::string get_description() { return "initializer"; } |
| 27 | + |
| 28 | + template <typename SrcT, typename DstT, int NumElems> |
| 29 | + static void call_simd_ctor(SrcT ref_value, DstT *const out) { |
| 30 | + const auto simd_by_init = esimd::simd<DstT, NumElems>(ref_value); |
| 31 | + simd_by_init.copy_to(out); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +// Descriptor class for the case of calling constructor in variable declaration |
| 36 | +// context. |
| 37 | +struct var_decl { |
| 38 | + static std::string get_description() { return "variable declaration"; } |
| 39 | + |
| 40 | + template <typename SrcT, typename DstT, int NumElems> |
| 41 | + static void call_simd_ctor(SrcT ref_value, DstT *const out) { |
| 42 | + const esimd::simd<DstT, NumElems> simd_by_var_decl(ref_value); |
| 43 | + simd_by_var_decl.copy_to(out); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +// Descriptor class for the case of calling constructor in rvalue in an |
| 48 | +// expression context. |
| 49 | +struct rval_in_expr { |
| 50 | + static std::string get_description() { return "rvalue in an expression"; } |
| 51 | + |
| 52 | + template <typename SrcT, typename DstT, int NumElems> |
| 53 | + static void call_simd_ctor(SrcT ref_value, DstT *const out) { |
| 54 | + esimd::simd<DstT, NumElems> simd_by_rval; |
| 55 | + simd_by_rval = esimd::simd<DstT, NumElems>(ref_value); |
| 56 | + simd_by_rval.copy_to(out); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +// Descriptor class for the case of calling constructor in const reference |
| 61 | +// context. |
| 62 | +class const_ref { |
| 63 | +public: |
| 64 | + static std::string get_description() { return "const reference"; } |
| 65 | + |
| 66 | + template <typename SrcT, typename DstT, int NumElems> |
| 67 | + static void call_simd_ctor(SrcT ref_value, DstT *const out) { |
| 68 | + call_simd_by_const_ref<DstT, NumElems>( |
| 69 | + esimd::simd<DstT, NumElems>(ref_value), out); |
| 70 | + } |
| 71 | + |
| 72 | +private: |
| 73 | + template <typename DstT, int NumElems> |
| 74 | + static void |
| 75 | + call_simd_by_const_ref(const esimd::simd<DstT, NumElems> &simd_by_const_ref, |
| 76 | + DstT *const out) { |
| 77 | + simd_by_const_ref.copy_to(out); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +template <typename SrcT, typename DstT, int NumElems, typename ContextT> |
| 82 | +class BroadcastCtorTestDescription : public ITestDescription { |
| 83 | +public: |
| 84 | + BroadcastCtorTestDescription(size_t index, DstT retrieved_val, |
| 85 | + DstT expected_val, SrcT ref_value, |
| 86 | + const std::string &src_data_type, |
| 87 | + const std::string &dst_data_type) |
| 88 | + : m_src_data_type(src_data_type), m_dst_data_type(dst_data_type), |
| 89 | + m_retrieved_val(retrieved_val), m_expected_val(expected_val), |
| 90 | + m_ref_val(ref_value), m_index(index) {} |
| 91 | + |
| 92 | + std::string to_string() const override { |
| 93 | + // TODO: Make strings for fp values more short during failure output, may be |
| 94 | + // by using hex representation |
| 95 | + std::string log_msg("Failed for simd<"); |
| 96 | + |
| 97 | + log_msg += m_dst_data_type + ", " + std::to_string(NumElems) + ">"; |
| 98 | + log_msg += ", with context: " + ContextT::get_description(); |
| 99 | + log_msg += ", source type: " + m_src_data_type; |
| 100 | + log_msg += ", destination type: " + m_dst_data_type; |
| 101 | + log_msg += ", retrieved: " + std::to_string(m_retrieved_val); |
| 102 | + log_msg += ", expected: " + std::to_string(m_expected_val); |
| 103 | + log_msg += ", input: " + std::to_string(m_ref_val); |
| 104 | + log_msg += ", at index: " + std::to_string(m_index); |
| 105 | + |
| 106 | + return log_msg; |
| 107 | + } |
| 108 | + |
| 109 | +private: |
| 110 | + const std::string m_src_data_type; |
| 111 | + const std::string m_dst_data_type; |
| 112 | + const DstT m_retrieved_val; |
| 113 | + const DstT m_ref_val; |
| 114 | + const DstT m_expected_val; |
| 115 | + const size_t m_index; |
| 116 | +}; |
| 117 | + |
| 118 | +// The main test routine. |
| 119 | +// Using functor class to be able to iterate over the pre-defined data types. |
| 120 | +template <typename UsePositiveValueOnly, typename SrcT, typename SizeT, |
| 121 | + typename DstT, typename TestCaseT> |
| 122 | +class run_test { |
| 123 | + static constexpr int NumElems = SizeT::value; |
| 124 | + |
| 125 | +public: |
| 126 | + bool operator()(sycl::queue &queue, const std::string &src_data_type, |
| 127 | + const std::string &dst_data_type) { |
| 128 | + bool passed = true; |
| 129 | + std::vector<SrcT> ref_data; |
| 130 | + |
| 131 | + if constexpr (UsePositiveValueOnly::value) { |
| 132 | + ref_data.push_back(static_cast<SrcT>(126.75)); |
| 133 | + } else { |
| 134 | + ref_data = generate_ref_conv_data<SrcT, DstT, 1>(); |
| 135 | + } |
| 136 | + |
| 137 | + for (size_t i = 0; i < ref_data.size(); ++i) { |
| 138 | + passed &= |
| 139 | + run_verification(queue, ref_data[i], src_data_type, dst_data_type); |
| 140 | + } |
| 141 | + |
| 142 | + return passed; |
| 143 | + } |
| 144 | + |
| 145 | +private: |
| 146 | + bool run_verification(sycl::queue &queue, SrcT ref_value, |
| 147 | + const std::string &src_data_type, |
| 148 | + const std::string &dst_data_type) { |
| 149 | + shared_vector<DstT> result(NumElems, shared_allocator<DstT>(queue)); |
| 150 | + shared_vector<SrcT> shared_ref_data(1, shared_allocator<SrcT>(queue)); |
| 151 | + shared_ref_data.push_back(ref_value); |
| 152 | + |
| 153 | + queue.submit([&](sycl::handler &cgh) { |
| 154 | + const SrcT *const ref = shared_ref_data.data(); |
| 155 | + DstT *const out = result.data(); |
| 156 | + |
| 157 | + cgh.single_task< |
| 158 | + Kernel<SrcT, NumElems, DstT, TestCaseT, UsePositiveValueOnly>>( |
| 159 | + [=]() SYCL_ESIMD_KERNEL { |
| 160 | + TestCaseT::template call_simd_ctor<SrcT, DstT, NumElems>(ref[0], |
| 161 | + out); |
| 162 | + }); |
| 163 | + }); |
| 164 | + queue.wait_and_throw(); |
| 165 | + |
| 166 | + const DstT expected = static_cast<DstT>(ref_value); |
| 167 | + bool passed = true; |
| 168 | + for (size_t i = 0; i < result.size(); ++i) { |
| 169 | + const DstT &retrieved = result[i]; |
| 170 | + |
| 171 | + if constexpr (std::is_same_v<SrcT, DstT>) { |
| 172 | + if (!are_bitwise_equal(ref_value, retrieved)) { |
| 173 | + fail_test(i, retrieved, expected, ref_value, src_data_type, |
| 174 | + dst_data_type); |
| 175 | + } |
| 176 | + } else if constexpr (type_traits::is_sycl_floating_point_v<DstT>) { |
| 177 | + // std::isnan() couldn't be called for integral types because it call is |
| 178 | + // ambiguous GitHub issue for that case: |
| 179 | + // https://github.com/microsoft/STL/issues/519 |
| 180 | + if (!std::isnan(expected) && !std::isnan(retrieved)) { |
| 181 | + if (expected != retrieved) { |
| 182 | + passed = fail_test(i, retrieved, expected, ref_value, src_data_type, |
| 183 | + dst_data_type); |
| 184 | + } |
| 185 | + } |
| 186 | + } else { |
| 187 | + if (expected != retrieved) { |
| 188 | + passed = fail_test(i, retrieved, expected, ref_value, src_data_type, |
| 189 | + dst_data_type); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + return passed; |
| 195 | + } |
| 196 | + |
| 197 | + bool fail_test(size_t index, DstT retrieved, DstT expected, SrcT ref_value, |
| 198 | + const std::string &src_data_type, |
| 199 | + const std::string &dst_data_type) { |
| 200 | + const auto description = |
| 201 | + BroadcastCtorTestDescription<SrcT, DstT, NumElems, TestCaseT>( |
| 202 | + index, retrieved, expected, ref_value, src_data_type, |
| 203 | + dst_data_type); |
| 204 | + log::fail(description); |
| 205 | + |
| 206 | + return false; |
| 207 | + } |
| 208 | +}; |
| 209 | + |
| 210 | +template <typename SrcT, typename SizeT, typename DstT, typename TestCaseT> |
| 211 | +using run_test_with_all_values = |
| 212 | + run_test<std::false_type, SrcT, SizeT, DstT, TestCaseT>; |
| 213 | + |
| 214 | +template <typename SrcT, typename SizeT, typename DstT, typename TestCaseT> |
| 215 | +using run_test_with_positive_value_only = |
| 216 | + run_test<std::true_type, SrcT, SizeT, DstT, TestCaseT>; |
| 217 | + |
| 218 | +} // namespace esimd_test::api::functional::ctors |
0 commit comments