okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
6.3 KiB149 linesraw
1//==---- lib_common_utils_detail.hpp --------------*- C++ -*----------------==//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __DPCT_LIB_COMMON_UTILS_DETAIL_HPP__
10#define __DPCT_LIB_COMMON_UTILS_DETAIL_HPP__
11
12namespace dpct::detail {
13template <typename T> struct lib_data_traits { using type = T; };
14template <typename T> struct lib_data_traits<sycl::vec<T, 2>> {
15  using type = std::complex<T>;
16};
17template <class T> using lib_data_traits_t = typename lib_data_traits<T>::type;
18
19template <typename T> inline auto get_memory(const void *x) {
20  T *new_x = reinterpret_cast<T *>(const_cast<void *>(x));
21#ifdef DPCT_USM_LEVEL_NONE
22  return dpct::get_buffer<std::remove_cv_t<T>>(new_x);
23#else
24  return new_x;
25#endif
26}
27
28template <typename T>
29inline typename ::dpct::detail::lib_data_traits_t<T> get_value(const T *s,
30                                                               sycl::queue &q) {
31  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
32  Ty s_h;
33  if (::dpct::cs::detail::get_pointer_attribute(q, s) ==
34      ::dpct::cs::detail::pointer_access_attribute::device_only)
35    ::dpct::cs::memcpy(q, (void *)&s_h, (void *)s, sizeof(T),
36                       ::dpct::cs::memcpy_direction::device_to_host)
37        .wait();
38  else
39    s_h = *reinterpret_cast<const Ty *>(s);
40  return s_h;
41}
42
43template <typename ArgT>
44inline constexpr std::uint64_t get_type_combination_id(ArgT Val) {
45  static_assert((unsigned char)library_data_t::library_data_t_size <=
46                    std::numeric_limits<unsigned char>::max() &&
47                "library_data_t size exceeds limit.");
48  static_assert(std::is_same_v<ArgT, library_data_t>, "Unsupported ArgT");
49  return (std::uint64_t)Val;
50}
51
52template <typename FirstT, typename... RestT>
53inline constexpr std::uint64_t get_type_combination_id(FirstT FirstVal,
54                                                       RestT... RestVal) {
55  static_assert((std::uint8_t)library_data_t::library_data_t_size <=
56                    std::numeric_limits<unsigned char>::max() &&
57                "library_data_t size exceeds limit.");
58  static_assert(sizeof...(RestT) <= 8 && "Too many parameters");
59  static_assert(std::is_same_v<FirstT, library_data_t>, "Unsupported FirstT");
60  return get_type_combination_id(RestVal...) << 8 | ((std::uint64_t)FirstVal);
61}
62
63inline constexpr std::size_t library_data_size[] = {
64    8 * sizeof(float),                    // real_float
65    8 * sizeof(std::complex<float>),      // complex_float
66    8 * sizeof(double),                   // real_double
67    8 * sizeof(std::complex<double>),     // complex_double
68    8 * sizeof(sycl::half),               // real_half
69    8 * sizeof(std::complex<sycl::half>), // complex_half
70    16,                                   // real_bfloat16
71    16 * 2,                               // complex_bfloat16
72    4,                                    // real_int4
73    4 * 2,                                // complex_int4
74    4,                                    // real_uint4
75    4 * 2,                                // complex_uint4
76    8,                                    // real_int8
77    8 * 2,                                // complex_int8
78    8,                                    // real_uint8
79    8 * 2,                                // complex_uint8
80    16,                                   // real_int16
81    16 * 2,                               // complex_int16
82    16,                                   // real_uint16
83    16 * 2,                               // complex_uint16
84    32,                                   // real_int32
85    32 * 2,                               // complex_int32
86    32,                                   // real_uint32
87    32 * 2,                               // complex_uint32
88    64,                                   // real_int64
89    64 * 2,                               // complex_int64
90    64,                                   // real_uint64
91    64 * 2,                               // complex_uint64
92    8,                                    // real_int8_4
93    8,                                    // real_int8_32
94    8                                     // real_uint8_4
95};
96
97template <class func_t, typename... args_t>
98std::invoke_result_t<func_t, args_t...>
99catch_batch_error(int *has_execption, const std::string &api_names,
100                  sycl::queue exec_queue, int *info, int *dev_info,
101                  int batch_size, func_t &&f, args_t &&...args) {
102  try {
103    return f(std::forward<args_t>(args)...);
104  } catch (oneapi::mkl::lapack::batch_error const &be) {
105    std::cerr << "Unexpected exception caught during call to API(s): "
106              << api_names << std::endl
107              << "reason: " << be.what() << std::endl
108              << "number: " << be.info() << std::endl;
109    if (be.info() < 0 && info)
110      *info = be.info();
111    int i = 0;
112    auto &ids = be.ids();
113    std::vector<int> info_vec(batch_size, 0);
114    for (auto const &e : be.exceptions()) {
115      try {
116        std::rethrow_exception(e);
117      } catch (oneapi::mkl::lapack::exception &e) {
118        std::cerr << "Exception in problem: " << ids[i] << std::endl
119                  << "reason: " << e.what() << std::endl
120                  << "info: " << e.info() << std::endl;
121        info_vec[ids[i]] = e.info();
122        i++;
123      }
124    }
125    if (dev_info)
126      exec_queue.memcpy(dev_info, info_vec.data(), batch_size * sizeof(int))
127          .wait();
128  } catch (sycl::exception const &e) {
129    std::cerr << "Caught synchronous SYCL exception:" << std::endl
130              << "reason: " << e.what() << std::endl;
131    if (dev_info)
132      exec_queue.memset(dev_info, 0, batch_size * sizeof(int)).wait();
133  }
134  if (has_execption)
135    *has_execption = 1;
136  return {};
137}
138
139template <typename ret_t, typename... args_t>
140ret_t catch_batch_error_f(int *has_execption, const std::string &api_names,
141                          sycl::queue exec_queue, int *info, int *dev_info,
142                          int batch_size, ret_t (*f)(args_t...),
143                          args_t &&...args) {
144  return catch_batch_error(has_execption, api_names, exec_queue, info, dev_info,
145                           batch_size, f, args...);
146}
147} // namespace dpct::detail
148
149#endif // __DPCT_LIB_COMMON_UTILS_DETAIL_HPP__