okami.mom/strata

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

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

main
26.5 KiB649 linesraw
1//==---- blas_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_BLAS_UTILS_DETAIL_HPP__
10#define __DPCT_BLAS_UTILS_DETAIL_HPP__
11
12#include <thread>
13#include <utility>
14#include <vector>
15
16namespace dpct {
17namespace blas {
18namespace detail {
19template <typename target_t, typename source_t> class parameter_wrapper_base_t {
20public:
21  parameter_wrapper_base_t(sycl::queue q, source_t *source, size_t ele_num)
22      : _source_attribute(::dpct::cs::detail::get_pointer_attribute(q, source)),
23        _q(q), _source(source), _ele_num(ele_num),
24        _target(construct_member_variable_target()) {}
25
26  ~parameter_wrapper_base_t() {
27    if (_need_free) {
28      _q.submit([&](sycl::handler &cgh) {
29        cgh.host_task([t = _target, q = _q] { ::dpct::cs::free(t, q); });
30      });
31    }
32  }
33
34protected:
35  ::dpct::cs::detail::pointer_access_attribute _source_attribute;
36  sycl::queue _q;
37  source_t *_source = nullptr;
38  size_t _ele_num;
39  bool _need_free = true;
40  target_t *_target;
41
42private:
43  target_t *construct_member_variable_target() {
44    if constexpr (std::is_same_v<target_t, source_t>) {
45      if (_source_attribute ==
46          ::dpct::cs::detail::pointer_access_attribute::host_only)
47        return (target_t *)::dpct::cs::malloc(sizeof(target_t) * _ele_num, _q);
48#ifdef DPCT_USM_LEVEL_NONE
49      auto alloc = dpct::detail::mem_mgr::instance().translate_ptr(_source);
50      size_t offset = (byte_t *)_source - alloc.alloc_ptr;
51      if (offset)
52        return (target_t *)::dpct::cs::malloc(sizeof(target_t) * _ele_num, _q);
53#endif
54      // If (data type is same && it is device pointer && (USM || buffer offset
55      // is 0)), it can be used directly.
56      _need_free = false;
57      return _source;
58    } else {
59      return (target_t *)::dpct::cs::malloc(sizeof(target_t) * _ele_num, _q);
60    }
61  }
62};
63
64inline library_data_t compute_type_to_library_data_t(compute_type ct) {
65  switch (ct) {
66  case compute_type::f16:
67  case compute_type::f16_standard:
68    return library_data_t::real_half;
69  case compute_type::f32:
70  case compute_type::f32_standard:
71  case compute_type::f32_fast_bf16:
72  case compute_type::f32_fast_tf32:
73    return library_data_t::real_float;
74  case compute_type::f64:
75  case compute_type::f64_standard:
76    return library_data_t::real_double;
77  case compute_type::i32:
78  case compute_type::i32_standard:
79    return library_data_t::real_int32;
80  default:
81    throw std::runtime_error("conversion is not supported.");
82  }
83}
84} // namespace detail
85} // namespace blas
86
87namespace detail {
88
89inline void mem_free(sycl::queue *exec_queue,
90                     std::vector<void *> pointers_array, sycl::event e) {
91  e.wait();
92  for (auto p : pointers_array)
93    sycl::free(p, *exec_queue);
94}
95
96inline int stride_for(int num_elems, int mem_align_in_elems) {
97  return ((num_elems - 1) / mem_align_in_elems + 1) * mem_align_in_elems;
98}
99
100#ifndef DPCT_USM_LEVEL_NONE
101template <typename T> class working_memory {
102  T *_input_ptr;
103  T *_temp_ptr;
104  bool _is_sycl_malloced = false;
105  bool _is_scalar_value = false;
106  sycl::queue _q;
107  sycl::event _e;
108
109public:
110  working_memory(size_t size, sycl::queue q) : _q(q) {
111    _is_scalar_value = false;
112    _temp_ptr = (T *)sycl::malloc_device(size, q);
113  }
114  working_memory(T *result_ptr, sycl::queue q) : _input_ptr(result_ptr), _q(q) {
115    _is_scalar_value = true;
116    _is_sycl_malloced = sycl::get_pointer_type(_input_ptr, _q.get_context()) !=
117                        sycl::usm::alloc::unknown;
118    if (!_is_sycl_malloced)
119      _temp_ptr = sycl::malloc_shared<T>(1, _q);
120  }
121  auto get_ptr() {
122    if (_is_scalar_value && _is_sycl_malloced)
123      return _input_ptr;
124    return _temp_ptr;
125  }
126  void set_event(sycl::event e) { _e = e; }
127  ~working_memory() {
128    if (_is_scalar_value) {
129      if (!_is_sycl_malloced) {
130        _q.memcpy(_input_ptr, _temp_ptr, sizeof(T)).wait();
131        sycl::free(_temp_ptr, _q);
132      }
133    } else {
134      std::vector<void *> ptrs{_temp_ptr};
135      ::dpct::cs::enqueue_free(ptrs, {_e});
136    }
137  }
138};
139#endif
140
141template <typename Tx, typename Tr>
142inline void nrm2_impl(sycl::queue &q, std::int64_t n, const void *x,
143                      std::int64_t incx, void *result) {
144#ifdef DPCT_USM_LEVEL_NONE
145  auto x_buffer = dpct::get_buffer<Tx>(x);
146  auto r_buffer =
147      sycl::buffer<Tr, 1>(reinterpret_cast<Tr *>(result), sycl::range<1>(1));
148  if (dpct::is_device_ptr(result))
149    r_buffer = dpct::get_buffer<Tr>(result);
150  oneapi::mkl::blas::column_major::nrm2(q, n, x_buffer, incx, r_buffer);
151#else
152  working_memory<Tr> res_mem(reinterpret_cast<Tr *>(result), q);
153  oneapi::mkl::blas::column_major::nrm2(q, n, reinterpret_cast<const Tx *>(x),
154                                        incx, res_mem.get_ptr());
155#endif
156}
157
158template <bool is_conjugate, class Txy, class Tr>
159inline void dotuc_impl(sycl::queue &q, std::int64_t n, const Txy *x,
160                       std::int64_t incx, const Txy *y, std::int64_t incy,
161                       Tr *result) {
162#ifdef DPCT_USM_LEVEL_NONE
163  auto x_buffer = dpct::get_buffer<Txy>(x);
164  auto y_buffer = dpct::get_buffer<Txy>(y);
165  auto r_buffer = sycl::buffer<Tr, 1>((Tr *)result, sycl::range<1>(1));
166  if (dpct::is_device_ptr(result))
167    r_buffer = dpct::get_buffer<Tr>(result);
168  if constexpr (std::is_same_v<Txy, std::complex<float>> ||
169                std::is_same_v<Txy, std::complex<double>>) {
170    if constexpr (is_conjugate)
171      oneapi::mkl::blas::column_major::dotc(q, n, x_buffer, incx, y_buffer,
172                                            incy, r_buffer);
173    else
174      oneapi::mkl::blas::column_major::dotu(q, n, x_buffer, incx, y_buffer,
175                                            incy, r_buffer);
176  } else
177    oneapi::mkl::blas::column_major::dot(q, n, x_buffer, incx, y_buffer, incy,
178                                         r_buffer);
179#else
180  working_memory<Tr> res_mem(result, q);
181  if constexpr (std::is_same_v<Txy, std::complex<float>> ||
182                std::is_same_v<Txy, std::complex<double>>) {
183    if constexpr (is_conjugate)
184      oneapi::mkl::blas::column_major::dotc(q, n, x, incx, y, incy,
185                                            res_mem.get_ptr());
186    else
187      oneapi::mkl::blas::column_major::dotu(q, n, x, incx, y, incy,
188                                            res_mem.get_ptr());
189  } else
190    oneapi::mkl::blas::column_major::dot(q, n, x, incx, y, incy,
191                                         res_mem.get_ptr());
192#endif
193}
194
195template <bool is_conjugate>
196inline void dotuc(sycl::queue &q, std::int64_t n, const void *x,
197                  library_data_t x_type, std::int64_t incx, const void *y,
198                  library_data_t y_type, std::int64_t incy, void *result,
199                  library_data_t result_type) {
200  std::uint64_t key =
201      detail::get_type_combination_id(x_type, y_type, result_type);
202  switch (key) {
203  case detail::get_type_combination_id(library_data_t::real_float,
204                                       library_data_t::real_float,
205                                       library_data_t::real_float): {
206    detail::dotuc_impl<is_conjugate>(q, n, reinterpret_cast<const float *>(x),
207                                     incx, reinterpret_cast<const float *>(y),
208                                     incy, reinterpret_cast<float *>(result));
209    break;
210  }
211  case detail::get_type_combination_id(library_data_t::real_double,
212                                       library_data_t::real_double,
213                                       library_data_t::real_double): {
214    detail::dotuc_impl<is_conjugate>(q, n, reinterpret_cast<const double *>(x),
215                                     incx, reinterpret_cast<const double *>(y),
216                                     incy, reinterpret_cast<double *>(result));
217    break;
218  }
219  case detail::get_type_combination_id(library_data_t::complex_float,
220                                       library_data_t::complex_float,
221                                       library_data_t::complex_float): {
222    detail::dotuc_impl<is_conjugate>(
223        q, n, reinterpret_cast<const std::complex<float> *>(x), incx,
224        reinterpret_cast<const std::complex<float> *>(y), incy,
225        reinterpret_cast<std::complex<float> *>(result));
226    break;
227  }
228  case detail::get_type_combination_id(library_data_t::complex_double,
229                                       library_data_t::complex_double,
230                                       library_data_t::complex_double): {
231    detail::dotuc_impl<is_conjugate>(
232        q, n, reinterpret_cast<const std::complex<double> *>(x), incx,
233        reinterpret_cast<const std::complex<double> *>(y), incy,
234        reinterpret_cast<std::complex<double> *>(result));
235    break;
236  }
237#ifdef __INTEL_MKL__
238  case detail::get_type_combination_id(library_data_t::real_half,
239                                       library_data_t::real_half,
240                                       library_data_t::real_half): {
241    detail::dotuc_impl<is_conjugate>(
242        q, n, reinterpret_cast<const sycl::half *>(x), incx,
243        reinterpret_cast<const sycl::half *>(y), incy,
244        reinterpret_cast<sycl::half *>(result));
245    break;
246  }
247#endif
248  default:
249    throw std::runtime_error("the combination of data type is unsupported");
250  }
251}
252
253template <class Tx, class Te>
254inline void scal_impl(sycl::queue &q, std::int64_t n, const void *alpha,
255                      void *x, std::int64_t incx) {
256  Te alpha_val = get_value(reinterpret_cast<const Te *>(alpha), q);
257  auto data_x = get_memory<Tx>(x);
258  oneapi::mkl::blas::column_major::scal(q, n, alpha_val, data_x, incx);
259}
260
261template <class Txy, class Te>
262inline void axpy_impl(sycl::queue &q, std::int64_t n, const void *alpha,
263                      const void *x, std::int64_t incx, void *y,
264                      std::int64_t incy) {
265  Te alpha_val = get_value(reinterpret_cast<const Te *>(alpha), q);
266  auto data_x = get_memory<const Txy>(x);
267  auto data_y = get_memory<Txy>(y);
268  oneapi::mkl::blas::column_major::axpy(q, n, alpha_val, data_x, incx, data_y,
269                                        incy);
270}
271
272template <class Txy, class Tc, class Ts>
273inline void rot_impl(sycl::queue &q, std::int64_t n, void *x, std::int64_t incx,
274                     void *y, std::int64_t incy, const void *c, const void *s) {
275  Tc c_value = get_value(reinterpret_cast<const Tc *>(c), q);
276  Ts s_value = get_value(reinterpret_cast<const Ts *>(s), q);
277  auto data_x = get_memory<Txy>(x);
278  auto data_y = get_memory<Txy>(y);
279  oneapi::mkl::blas::column_major::rot(q, n, data_x, incx, data_y, incy,
280                                       c_value, s_value);
281}
282
283template <class Tx, class Ty, class Tparam>
284inline void rotm_impl(sycl::queue &q, std::int64_t n, void *x, int64_t incx,
285                      void *y, int64_t incy, const void *param) {
286  auto data_x = get_memory<Tx>(x);
287  auto data_y = get_memory<Ty>(y);
288  auto data_param = get_memory<Tparam>(const_cast<void *>(param));
289  oneapi::mkl::blas::column_major::rotm(q, n, data_x, incx, data_y, incy,
290                                        data_param);
291}
292
293template <class Tx, class Ty>
294inline void copy_impl(sycl::queue &q, std::int64_t n, const void *x,
295                      std::int64_t incx, void *y, std::int64_t incy) {
296  auto data_x = get_memory<const Tx>(x);
297  auto data_y = get_memory<Ty>(y);
298  oneapi::mkl::blas::column_major::copy(q, n, data_x, incx, data_y, incy);
299}
300
301template <class Tx, class Ty>
302inline void swap_impl(sycl::queue &q, std::int64_t n, void *x,
303                      std::int64_t incx, void *y, std::int64_t incy) {
304  auto data_x = get_memory<Tx>(x);
305  auto data_y = get_memory<Ty>(y);
306  oneapi::mkl::blas::column_major::swap(q, n, data_x, incx, data_y, incy);
307}
308
309template <class Tx, class Tres>
310inline void asum_impl(sycl::queue &q, std::int64_t n, const void *x,
311                      std::int64_t incx, void *res) {
312  auto data_x = get_memory<Tx>(x);
313  auto data_res = get_memory<Tres>(res);
314  oneapi::mkl::blas::column_major::asum(q, n, data_x, incx, data_res);
315}
316
317template <class T>
318inline void iamax_impl(sycl::queue &q, std::int64_t n, const void *x,
319                       std::int64_t incx, std::int64_t *res) {
320#ifndef __INTEL_MKL__
321  throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
322                           "Project does not support this API.");
323#else
324  auto data_x = get_memory<T>(x);
325  auto data_res = get_memory<std::int64_t>(res);
326  oneapi::mkl::blas::column_major::iamax(q, n, data_x, incx, data_res,
327                                         oneapi::mkl::index_base::one);
328#endif
329}
330
331template <class T>
332inline void iamin_impl(sycl::queue &q, std::int64_t n, const void *x,
333                       std::int64_t incx, std::int64_t *res) {
334#ifndef __INTEL_MKL__
335  throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
336                           "Project does not support this API.");
337#else
338  auto data_x = get_memory<T>(x);
339  auto data_res = get_memory<std::int64_t>(res);
340  oneapi::mkl::blas::column_major::iamin(q, n, data_x, incx, data_res,
341                                         oneapi::mkl::index_base::one);
342#endif
343}
344
345#ifdef __INTEL_MKL__
346template <class Ta, class Tb, class Tc, class Ts>
347inline void gemm_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
348                      oneapi::mkl::transpose b_trans, int m, int n, int k,
349                      const void *alpha, const void *a, int lda, const void *b,
350                      int ldb, const void *beta, void *c, int ldc,
351                      oneapi::mkl::blas::compute_mode cm) {
352  Ts alpha_value = get_value(reinterpret_cast<const Ts *>(alpha), q);
353  Ts beta_value = get_value(reinterpret_cast<const Ts *>(beta), q);
354  auto data_a = get_memory<const Ta>(a);
355  auto data_b = get_memory<const Tb>(b);
356  auto data_c = get_memory<Tc>(c);
357  oneapi::mkl::blas::column_major::gemm(q, a_trans, b_trans, m, n, k,
358                                        alpha_value, data_a, lda, data_b, ldb,
359                                        beta_value, data_c, ldc, cm);
360}
361#endif
362
363#ifdef __INTEL_MKL__
364#define DPCT_COMPUTE_MODE_PARAM , oneapi::mkl::blas::compute_mode cm
365#define DPCT_COMPUTE_MODE_ARG , cm
366#else
367#define DPCT_COMPUTE_MODE_PARAM
368#define DPCT_COMPUTE_MODE_ARG
369#endif
370
371template <class Ta, class Tb, class Tc, class Ts>
372inline void gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
373                            oneapi::mkl::transpose b_trans, int m, int n, int k,
374                            const void *alpha, const void **a, int lda,
375                            const void **b, int ldb, const void *beta, void **c,
376                            int ldc, int batch_size DPCT_COMPUTE_MODE_PARAM) {
377  struct matrix_info_t {
378    oneapi::mkl::transpose transpose_info[2];
379    Ts value_info[2];
380    std::int64_t size_info[3];
381    std::int64_t ld_info[3];
382    std::int64_t groupsize_info;
383  };
384
385  Ts alpha_value = get_value(reinterpret_cast<const Ts *>(alpha), q);
386  Ts beta_value = get_value(reinterpret_cast<const Ts *>(beta), q);
387
388  matrix_info_t *matrix_info =
389      (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
390  matrix_info->transpose_info[0] = a_trans;
391  matrix_info->transpose_info[1] = b_trans;
392  matrix_info->value_info[0] = alpha_value;
393  matrix_info->value_info[1] = beta_value;
394  matrix_info->size_info[0] = m;
395  matrix_info->size_info[1] = n;
396  matrix_info->size_info[2] = k;
397  matrix_info->ld_info[0] = lda;
398  matrix_info->ld_info[1] = ldb;
399  matrix_info->ld_info[2] = ldc;
400  matrix_info->groupsize_info = batch_size;
401
402  sycl::event e = oneapi::mkl::blas::column_major::gemm_batch(
403      q, matrix_info->transpose_info, matrix_info->transpose_info + 1,
404      matrix_info->size_info, matrix_info->size_info + 1,
405      matrix_info->size_info + 2, matrix_info->value_info,
406      reinterpret_cast<const Ta **>(a), matrix_info->ld_info,
407      reinterpret_cast<const Tb **>(b), matrix_info->ld_info + 1,
408      matrix_info->value_info + 1, reinterpret_cast<Tc **>(c),
409      matrix_info->ld_info + 2, 1,
410      &(matrix_info->groupsize_info)DPCT_COMPUTE_MODE_ARG);
411
412  q.submit([&](sycl::handler &cgh) {
413    cgh.depends_on(e);
414    cgh.host_task([=] { std::free(matrix_info); });
415  });
416}
417
418template <class Ta, class Tb, class Tc, class Ts>
419inline void gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
420                            oneapi::mkl::transpose b_trans, int m, int n, int k,
421                            const void *alpha, const void *a, int lda,
422                            long long int stride_a, const void *b, int ldb,
423                            long long int stride_b, const void *beta, void *c,
424                            int ldc, long long int stride_c,
425                            int batch_size DPCT_COMPUTE_MODE_PARAM) {
426  Ts alpha_value = get_value(reinterpret_cast<const Ts *>(alpha), q);
427  Ts beta_value = get_value(reinterpret_cast<const Ts *>(beta), q);
428  auto data_a = get_memory<const Ta>(a);
429  auto data_b = get_memory<const Tb>(b);
430  auto data_c = get_memory<Tc>(c);
431  oneapi::mkl::blas::column_major::gemm_batch(
432      q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda, stride_a, data_b,
433      ldb, stride_b, beta_value, data_c, ldc, stride_c,
434      batch_size DPCT_COMPUTE_MODE_ARG);
435}
436
437template <bool is_hermitian, class T>
438inline void syherk_impl(sycl::queue &q, oneapi::mkl::uplo uplo,
439                        oneapi::mkl::transpose trans, int n, int k,
440                        const void *alpha, const void *a, int lda,
441                        const void *beta, void *c,
442                        int ldc DPCT_COMPUTE_MODE_PARAM) {
443  auto data_a = get_memory<const T>(a);
444  auto data_c = get_memory<T>(c);
445  if constexpr (is_hermitian) {
446    auto alpha_value =
447        get_value(reinterpret_cast<const typename T::value_type *>(alpha), q);
448    auto beta_value =
449        get_value(reinterpret_cast<const typename T::value_type *>(beta), q);
450    oneapi::mkl::blas::column_major::herk(q, uplo, trans, n, k, alpha_value,
451                                          data_a, lda, beta_value, data_c,
452                                          ldc DPCT_COMPUTE_MODE_ARG);
453  } else {
454    T alpha_value = get_value(reinterpret_cast<const T *>(alpha), q);
455    T beta_value = get_value(reinterpret_cast<const T *>(beta), q);
456    oneapi::mkl::blas::column_major::syrk(q, uplo, trans, n, k, alpha_value,
457                                          data_a, lda, beta_value, data_c,
458                                          ldc DPCT_COMPUTE_MODE_ARG);
459  }
460}
461
462template <bool is_hermitian, class T, class Tbeta>
463inline void rk_impl(sycl::queue &q, oneapi::mkl::uplo uplo,
464                    oneapi::mkl::transpose trans, int n, int k, const T *alpha,
465                    const T *a, int lda, const T *b, int ldb, const Tbeta *beta,
466                    T *c, int ldc DPCT_COMPUTE_MODE_PARAM) {
467  // For symmetric matrix, this function performs: C = alpha*OP(A)*(OP(B))^T +
468  // beta*C For Hermitian matrix, this function performs: C =
469  // alpha*OP(A)*(OP(B))^H + beta*C The gemmt() function performs: C =
470  // alpha*OPA(A)*OPB(B) + beta*C So the OPB need be updated before we call
471  // gemmt().
472  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
473  using Ts = typename ::dpct::detail::lib_data_traits_t<Tbeta>;
474  Ty alpha_value = get_value(reinterpret_cast<const Ty *>(alpha), q);
475  Ts beta_value = get_value(reinterpret_cast<const Ts *>(beta), q);
476  oneapi::mkl::transpose trans_A = trans, trans_B = trans;
477  int origin_b_rows = trans == oneapi::mkl::transpose::nontrans ? n : k;
478  int origin_b_cols = trans == oneapi::mkl::transpose::nontrans ? k : n;
479
480  if ((is_hermitian && trans == oneapi::mkl::transpose::trans) ||
481      (!is_hermitian && !std::is_floating_point_v<Ty> &&
482       trans == oneapi::mkl::transpose::conjtrans)) {
483    // In this case, OPB need be a conjugate operation,
484    // but only notrans, conjtrans and trans are available.
485    // So we need do a conjtrans operation first, then do a trans operation.
486    trans_B = oneapi::mkl::transpose::trans;
487    auto data_a = get_memory<const Ty>(a);
488    auto data_c = get_memory<Ty>(c);
489#ifdef DPCT_USM_LEVEL_NONE
490    auto new_B_buffer =
491        sycl::buffer<Ty, 1>(sycl::range<1>(origin_b_rows * origin_b_cols));
492    auto from_buffer = dpct::get_buffer<Ty>(b);
493    oneapi::mkl::blas::column_major::omatcopy_batch(
494        q, oneapi::mkl::transpose::conjtrans, origin_b_rows, origin_b_cols,
495        Ts(1.0), from_buffer, ldb, origin_b_rows * ldb, new_B_buffer,
496        origin_b_cols, origin_b_rows * origin_b_cols, 1);
497    oneapi::mkl::blas::column_major::gemmt(
498        q, uplo, trans_A, trans_B, n, k, alpha_value, data_a, lda, new_B_buffer,
499        origin_b_cols, beta_value, data_c, ldc DPCT_COMPUTE_MODE_ARG);
500#else
501    working_memory<T> new_B(origin_b_rows * origin_b_cols * sizeof(T), q);
502    oneapi::mkl::blas::column_major::omatcopy_batch(
503        q, oneapi::mkl::transpose::conjtrans, origin_b_rows, origin_b_cols,
504        Ts(1.0), reinterpret_cast<const Ty *>(b), ldb, origin_b_rows * ldb,
505        reinterpret_cast<Ty *>(new_B.get_ptr()), origin_b_cols,
506        origin_b_rows * origin_b_cols, 1);
507    sycl::event e = oneapi::mkl::blas::column_major::gemmt(
508        q, uplo, trans_A, trans_B, n, k, alpha_value, data_a, lda,
509        reinterpret_cast<Ty *>(new_B.get_ptr()), origin_b_cols, beta_value,
510        data_c, ldc DPCT_COMPUTE_MODE_ARG);
511    new_B.set_event(e);
512#endif
513  } else {
514    if constexpr (is_hermitian) {
515      trans_B = trans == oneapi::mkl::transpose::nontrans
516                    ? oneapi::mkl::transpose::conjtrans
517                    : oneapi::mkl::transpose::nontrans;
518    } else {
519      trans_B = trans == oneapi::mkl::transpose::nontrans
520                    ? oneapi::mkl::transpose::trans
521                    : oneapi::mkl::transpose::nontrans;
522    }
523    auto data_a = get_memory<const Ty>(a);
524    auto data_b = get_memory<const Ty>(b);
525    auto data_c = get_memory<Ty>(c);
526    oneapi::mkl::blas::column_major::gemmt(
527        q, uplo, trans_A, trans_B, n, k, alpha_value, data_a, lda, data_b, ldb,
528        beta_value, data_c, ldc DPCT_COMPUTE_MODE_ARG);
529  }
530}
531
532template <class Ta, class Tb, class Ts>
533inline void
534trsm_batch_impl(sycl::queue &q, oneapi::mkl::side left_right,
535                oneapi::mkl::uplo upper_lower, oneapi::mkl::transpose trans,
536                oneapi::mkl::diag unit_diag, int m, int n, const void *alpha,
537                const void **a, int lda, void **b, int ldb,
538                int batch_size DPCT_COMPUTE_MODE_PARAM) {
539  struct matrix_info_t {
540    matrix_info_t(oneapi::mkl::side side_info, oneapi::mkl::uplo uplo_info,
541                  oneapi::mkl::transpose transpose_info,
542                  oneapi::mkl::diag diag_info, Ts value_info, std::int64_t m,
543                  std::int64_t n, std::int64_t lda, std::int64_t ldb,
544                  std::int64_t groupsize_info)
545        : side_info(side_info), uplo_info(uplo_info),
546          transpose_info(transpose_info), diag_info(diag_info),
547          value_info(value_info), groupsize_info(groupsize_info) {
548      size_info[0] = m;
549      size_info[1] = n;
550      ld_info[0] = lda;
551      ld_info[1] = ldb;
552    }
553    oneapi::mkl::side side_info;
554    oneapi::mkl::uplo uplo_info;
555    oneapi::mkl::transpose transpose_info;
556    oneapi::mkl::diag diag_info;
557    Ts value_info;
558    std::int64_t size_info[2];
559    std::int64_t ld_info[2];
560    std::int64_t groupsize_info;
561  };
562
563  Ts alpha_value = get_value(reinterpret_cast<const Ts *>(alpha), q);
564
565  matrix_info_t *matrix_info =
566      new matrix_info_t(left_right, upper_lower, trans, unit_diag, alpha_value,
567                        m, n, lda, ldb, batch_size);
568
569  sycl::event e = oneapi::mkl::blas::column_major::trsm_batch(
570      q, &(matrix_info->side_info), &(matrix_info->uplo_info),
571      &(matrix_info->transpose_info), &(matrix_info->diag_info),
572      matrix_info->size_info, matrix_info->size_info + 1,
573      &(matrix_info->value_info), reinterpret_cast<const Ta **>(a),
574      matrix_info->ld_info, reinterpret_cast<Tb **>(b),
575      matrix_info->ld_info + 1, 1,
576      &(matrix_info->groupsize_info)DPCT_COMPUTE_MODE_ARG);
577
578  q.submit([&](sycl::handler &cgh) {
579    cgh.depends_on(e);
580    cgh.host_task([=] { delete matrix_info; });
581  });
582}
583
584template <typename T>
585inline void getrfnp_batch_wrapper(sycl::queue &exec_queue, int n, T *a[],
586                                  int lda, int *info, int batch_size) {
587#ifndef __INTEL_MKL__
588  throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
589                           "Project does not support this API.");
590#else
591  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
592  // Set the info array value to 0
593  ::dpct::cs::fill<unsigned char>(exec_queue, info, 0,
594                                  sizeof(int) * batch_size);
595  std::int64_t stride_a = n * lda;
596  std::int64_t scratchpad_size =
597      oneapi::mkl::lapack::getrfnp_batch_scratchpad_size<Ty>(
598          exec_queue, n, n, lda, stride_a, batch_size);
599
600  Ty *a_strided_mem =
601      (Ty *)::dpct::cs::malloc(stride_a * batch_size * sizeof(Ty), exec_queue);
602  T **host_a = (T **)std::malloc(batch_size * sizeof(T *));
603  ::dpct::cs::memcpy(::dpct::cs::get_default_queue(), host_a, a,
604                     batch_size * sizeof(T *))
605      .wait();
606  for (std::int64_t i = 0; i < batch_size; ++i)
607    ::dpct::cs::memcpy(::dpct::cs::get_default_queue(),
608                       a_strided_mem + i * stride_a, host_a[i],
609                       n * lda * sizeof(T))
610        .wait();
611
612#ifdef DPCT_USM_LEVEL_NONE
613  {
614    sycl::buffer<Ty, 1> scratchpad{sycl::range<1>(scratchpad_size)};
615    auto a_buffer = get_buffer<Ty>(a_strided_mem);
616    oneapi::mkl::lapack::getrfnp_batch(exec_queue, n, n, a_buffer, lda,
617                                       stride_a, batch_size, scratchpad,
618                                       scratchpad_size);
619  }
620  std::vector<sycl::event> events;
621  for (std::int64_t i = 0; i < batch_size; ++i)
622    events.push_back(::dpct::cs::memcpy(
623        exec_queue, host_a[i], a_strided_mem + i * stride_a,
624        n * lda * sizeof(T), ::dpct::cs::memcpy_direction::automatic));
625#else
626  Ty *scratchpad = sycl::malloc_device<Ty>(scratchpad_size, exec_queue);
627  sycl::event e = oneapi::mkl::lapack::getrfnp_batch(
628      exec_queue, n, n, a_strided_mem, lda, stride_a, batch_size, scratchpad,
629      scratchpad_size);
630  std::vector<sycl::event> events;
631  for (std::int64_t i = 0; i < batch_size; ++i)
632    events.push_back(::dpct::cs::memcpy(
633        exec_queue, host_a[i], a_strided_mem + i * stride_a,
634        n * lda * sizeof(T), ::dpct::cs::memcpy_direction::automatic, {e}));
635
636  std::vector<void *> ptrs{scratchpad, a_strided_mem};
637  ::dpct::cs::enqueue_free(ptrs, events, exec_queue);
638#endif
639
640  exec_queue.submit([&](sycl::handler &cgh) {
641    cgh.depends_on(events);
642    cgh.host_task([=] { std::free(host_a); });
643  });
644#endif
645}
646} // namespace detail
647} // namespace dpct
648
649#endif // __DPCT_BLAS_UTILS_DETAIL_HPP__