okami.mom/strata

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

Niko1221Merge f-0221-sycl: a Linux SYCL (Intel Arc) engine package recipe and feed entry, --version without GPU access, RUNPATH $ORIGIN; Arc PCs get an engine from the feed23b3dfc2c

main
116.5 KiB2599 linesraw
1//==---- blas_utils.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_HPP__
10#define __DPCT_BLAS_UTILS_HPP__
11
12#include "compat_service.hpp"
13#include "lib_common_utils.hpp"
14
15#include "detail/blas_utils_detail.hpp"
16
17namespace dpct {
18namespace blas {
19
20/// Parameter input/output properties are:
21/// in: input parameter
22/// out: output parameter
23/// in_out: input and output parameter
24enum class parameter_inout_prop { in, out, in_out };
25
26/// parameter_wrapper_t is a class to wrap the parameter to fit the oneMKL
27/// interface.
28/// E.g.,
29/// \code
30/// void foo(sycl::queue q, int *res) {
31///   // Declare the wrapper.
32///   parameter_wrapper_t<std::int64_t, int, parameter_inout_prop::out>
33///       res_wrapper(q, res);
34///   // parameter_wrapper_t::get_ptr() is passed as a parameter. The result is
35///   // saved into the wrapper.
36///   oneapi::mkl::...(q, ..., res_wrapper.get_ptr());
37///   // The action to copy the result in the wrapper to the original variable
38///   // is submitted when destructing the wrapper automatically.
39/// }
40/// \endcode
41/// \tparam target_t Data type to fit oneMKL parameter data type. E.g.,
42/// get_ptr() returns \tparam target_t data type and can be used as oneMKL
43/// parameter with USM memory configuration.
44/// \tparam source_t Data type of the original parameter.
45/// \tparam inout_prop The input/output property of the parameter.
46template <typename target_t, typename source_t, parameter_inout_prop inout_prop>
47class parameter_wrapper_t
48    : public detail::parameter_wrapper_base_t<target_t, source_t> {
49  static_assert(inout_prop == parameter_inout_prop::out &&
50                "Only parameter_inout_prop::out is supported if "
51                "target_t and source_t are not same.");
52  using base_t = detail::parameter_wrapper_base_t<target_t, source_t>;
53  using base_t::_q;
54  using base_t::_source;
55  using base_t::_source_attribute;
56  using base_t::_target;
57
58public:
59  /// Constructor. Malloc the wrapper memory.
60  /// \param q The queue used for internal malloc and memcpy
61  /// \param source The original parameter
62  parameter_wrapper_t(sycl::queue q, source_t *source) : base_t(q, source, 1) {}
63  /// Destructor. Copy back content from wrapper memory to original memory
64  ~parameter_wrapper_t() {
65#ifdef DPCT_USM_LEVEL_NONE
66    if (_source_attribute ==
67        ::dpct::cs::detail::pointer_access_attribute::device_only) {
68      _q.submit([&](sycl::handler &cgh) {
69        auto from_acc = dpct::get_buffer<target_t>(_target)
70                            .template get_access<sycl::access_mode::read>(cgh);
71        auto to_acc = dpct::get_buffer<source_t>(_source)
72                          .template get_access<sycl::access_mode::write>(cgh);
73        cgh.single_task<::dpct::cs::kernel_name<
74            class parameter_wrapper_copyback, target_t, source_t>>(
75            [=]() { to_acc[0] = static_cast<source_t>(from_acc[0]); });
76      });
77    } else {
78      source_t temp =
79          static_cast<source_t>(dpct::get_host_ptr<target_t>(_target)[0]);
80      *_source = temp;
81    }
82#else
83    if (_source_attribute ==
84        ::dpct::cs::detail::pointer_access_attribute::device_only) {
85      _q.template single_task<::dpct::cs::kernel_name<
86          class parameter_wrapper_copyback, target_t, source_t>>(
87          [t = _target, s = _source]() { *s = static_cast<source_t>(*t); });
88    } else {
89      target_t temp;
90      _q.memcpy(&temp, _target, sizeof(target_t)).wait();
91      *_source = static_cast<source_t>(temp);
92    }
93#endif
94  }
95  /// Get the target memory of the wrapper to represent the wrapped variable
96  target_t *get_ptr() { return _target; }
97};
98
99/// parameter_wrapper_t is a class to wrap the parameter to fit the oneMKL
100/// interface.
101/// E.g.,
102/// \code
103/// void foo(sycl::queue q, int *params, int ele_num) {
104///   // Declare the wrapper.
105///   parameter_wrapper_t<float, parameter_inout_prop::in_out> params_wrapper(
106///       q, params, ele_num);
107///   // parameter_wrapper_t::get_ptr() is passed as a parameter. The result is
108///   // saved into the wrapper.
109///   oneapi::mkl::...(q, ..., params_wrapper.get_ptr());
110///   // The action to copy the result in the wrapper to the original variable
111///   // is submitted when destructing the wrapper automatically.
112/// }
113/// \endcode
114/// \tparam target_t Data type to fit oneMKL parameter data type. E.g.,
115/// get_ptr() returns \tparam target_t data type and can be used as oneMKL
116/// parameter with USM memory configuration.
117/// \tparam inout_prop The input/output property of the parameter.
118template <typename target_t, parameter_inout_prop inout_prop>
119class parameter_wrapper_t<target_t, target_t, inout_prop>
120    : public detail::parameter_wrapper_base_t<target_t, target_t> {
121  using base_t = detail::parameter_wrapper_base_t<target_t, target_t>;
122  using base_t::_ele_num;
123  using base_t::_need_free;
124  using base_t::_q;
125  using base_t::_source;
126  using base_t::_source_attribute;
127  using base_t::_target;
128
129public:
130  /// Constructor. Malloc the wrapper memory.
131  /// \param q The queue used for internal malloc and memcpy
132  /// \param source The original parameter
133  /// \param ele_num Element number in \p source
134  parameter_wrapper_t(sycl::queue q, target_t *source, size_t ele_num = 1)
135      : base_t(q, source, ele_num) {
136    if constexpr (inout_prop != parameter_inout_prop::out) {
137      if (_need_free) {
138        ::dpct::cs::memcpy(_q, _target, _source, sizeof(target_t) * _ele_num,
139                           ::dpct::cs::memcpy_direction::automatic);
140      }
141    }
142  }
143
144  /// Constructor. Only vaild for parameter_inout_prop::in.
145  /// \param q The queue used for internal malloc and memcpy
146  /// \param source The original parameter
147  /// \param ele_num Element number in \p source
148  template <parameter_inout_prop prop = inout_prop>
149  parameter_wrapper_t(
150      sycl::queue q, const target_t *source, size_t ele_num = 1,
151      typename std::enable_if<prop == parameter_inout_prop::in>::type * = 0)
152      : parameter_wrapper_t(q, const_cast<target_t *>(source), ele_num) {}
153
154  /// Destructor. Copy back content from wrapper memory to original memory
155  ~parameter_wrapper_t() {
156    if constexpr (inout_prop != parameter_inout_prop::in) {
157      if (_need_free) {
158        sycl::event e = ::dpct::cs::memcpy(
159            _q, _source, _target, sizeof(target_t) * _ele_num,
160            ::dpct::cs::memcpy_direction::automatic);
161        (void)e;
162        if (_source_attribute ==
163            ::dpct::cs::detail::pointer_access_attribute::host_only)
164          e.wait();
165      }
166    }
167  }
168  /// Get the target memory of the wrapper to represent the wrapped variable
169  target_t *get_ptr() { return _target; }
170};
171
172using wrapper_int_to_int64_out =
173    parameter_wrapper_t<std::int64_t, int, parameter_inout_prop::out>;
174using wrapper_int64_out =
175    parameter_wrapper_t<std::int64_t, std::int64_t, parameter_inout_prop::out>;
176using wrapper_float_out =
177    parameter_wrapper_t<float, float, parameter_inout_prop::out>;
178using wrapper_double_out =
179    parameter_wrapper_t<double, double, parameter_inout_prop::out>;
180using wrapper_float2_out =
181    parameter_wrapper_t<sycl::float2, sycl::float2, parameter_inout_prop::out>;
182using wrapper_double2_out = parameter_wrapper_t<sycl::double2, sycl::double2,
183                                                parameter_inout_prop::out>;
184using wrapper_float_inout =
185    parameter_wrapper_t<float, float, parameter_inout_prop::in_out>;
186using wrapper_double_inout =
187    parameter_wrapper_t<double, double, parameter_inout_prop::in_out>;
188using wrapper_float2_inout = parameter_wrapper_t<sycl::float2, sycl::float2,
189                                                 parameter_inout_prop::in_out>;
190using wrapper_double2_inout = parameter_wrapper_t<sycl::double2, sycl::double2,
191                                                  parameter_inout_prop::in_out>;
192using wrapper_float_in =
193    parameter_wrapper_t<float, float, parameter_inout_prop::in>;
194using wrapper_double_in =
195    parameter_wrapper_t<double, double, parameter_inout_prop::in>;
196
197/// Copy matrix data synchronously. The default leading dimension is column.
198/// \param [out] to_ptr A pointer points to the destination location.
199/// \param [in] from_ptr A pointer points to the source location.
200/// \param [in] to_ld The leading dimension the destination matrix.
201/// \param [in] from_ld The leading dimension the source matrix.
202/// \param [in] rows The number of rows of the source matrix.
203/// \param [in] cols The number of columns of the source matrix.
204/// \param [in] elem_size The element size in bytes.
205/// \param [in] direction The direction of the data copy.
206/// \param [in] queue The queue where the routine should be executed.
207/// \param [in] deps A list of events to wait for before starting copy.
208inline void
209matrix_mem_copy_sync(void *to_ptr, const void *from_ptr, std::int64_t to_ld,
210                     std::int64_t from_ld, std::int64_t rows, std::int64_t cols,
211                     std::int64_t elem_size,
212                     ::dpct::cs::memcpy_direction direction =
213                         ::dpct::cs::memcpy_direction::automatic,
214                     sycl::queue &queue = ::dpct::cs::get_default_queue(),
215                     const std::vector<sycl::event> &deps = {}) {
216  if (to_ptr == from_ptr && to_ld == from_ld)
217    return;
218  if (to_ld == from_ld) {
219    size_t copy_size = elem_size * ((cols - 1) * (size_t)to_ld + rows);
220    ::dpct::cs::memcpy(queue, to_ptr, from_ptr, copy_size, direction, deps)
221        .wait();
222  } else {
223    sycl::event::wait(::dpct::cs::memcpy(
224        queue, to_ptr, from_ptr, elem_size * to_ld, elem_size * from_ld,
225        elem_size * rows, cols, direction, deps));
226  }
227}
228/// Copy matrix data asynchronously. The default leading dimension is column.
229/// \return Output event to wait on to ensure copy is complete.
230/// \param [out] to_ptr A pointer points to the destination location.
231/// \param [in] from_ptr A pointer points to the source location.
232/// \param [in] to_ld The leading dimension the destination matrix.
233/// \param [in] from_ld The leading dimension the source matrix.
234/// \param [in] rows The number of rows of the source matrix.
235/// \param [in] cols The number of columns of the source matrix.
236/// \param [in] elem_size The element size in bytes.
237/// \param [in] direction The direction of the data copy.
238/// \param [in] queue The queue where the routine should be executed.
239/// \param [in] deps A list of events to wait for before starting copy.
240inline sycl::event
241matrix_mem_copy_async(void *to_ptr, const void *from_ptr, std::int64_t to_ld,
242                      std::int64_t from_ld, std::int64_t rows,
243                      std::int64_t cols, std::int64_t elem_size,
244                      ::dpct::cs::memcpy_direction direction =
245                          ::dpct::cs::memcpy_direction::automatic,
246                      sycl::queue &queue = ::dpct::cs::get_default_queue(),
247                      const std::vector<sycl::event> &deps = {}) {
248  if (to_ptr == from_ptr && to_ld == from_ld)
249    return sycl::event();
250  if (to_ld == from_ld) {
251    size_t copy_size = elem_size * ((cols - 1) * (size_t)to_ld + rows);
252    return ::dpct::cs::memcpy(queue, to_ptr, from_ptr, copy_size, direction,
253                              deps);
254  }
255  auto events = ::dpct::cs::memcpy(queue, to_ptr, from_ptr, elem_size * to_ld,
256                                   elem_size * from_ld, elem_size * rows, cols,
257                                   direction, deps);
258  return queue.single_task(events, [] {});
259}
260
261/// Copy matrix data. The default leading dimension is column.
262/// \param [out] to_ptr A pointer points to the destination location.
263/// \param [in] from_ptr A pointer points to the source location.
264/// \param [in] to_ld The leading dimension the destination matrix.
265/// \param [in] from_ld The leading dimension the source matrix.
266/// \param [in] rows The number of rows of the source matrix.
267/// \param [in] cols The number of columns of the source matrix.
268/// \param [in] elem_size The element size in bytes.
269/// \param [in] direction The direction of the data copy.
270/// \param [in] queue The queue where the routine should be executed.
271/// \param [in] async If this argument is true, the return of the function
272/// does NOT guarantee the copy is completed.
273inline void
274matrix_mem_copy(void *to_ptr, const void *from_ptr, std::int64_t to_ld,
275                std::int64_t from_ld, std::int64_t rows, std::int64_t cols,
276                std::int64_t elem_size,
277                ::dpct::cs::memcpy_direction direction =
278                    ::dpct::cs::memcpy_direction::automatic,
279                sycl::queue &queue = ::dpct::cs::get_default_queue(),
280                bool async = false) {
281  if (async)
282    matrix_mem_copy_async(to_ptr, from_ptr, to_ld, from_ld, rows, cols,
283                          elem_size, direction, queue);
284  else
285    matrix_mem_copy_sync(to_ptr, from_ptr, to_ld, from_ld, rows, cols,
286                         elem_size, direction, queue);
287}
288
289enum class math_mode : int {
290  mm_default,
291  mm_tf32,
292};
293
294using ::dpct::compute_type;
295
296class descriptor {
297public:
298  void set_queue(::dpct::cs::queue_ptr q_ptr) noexcept { _queue_ptr = q_ptr; }
299  sycl::queue &get_queue() noexcept { return *_queue_ptr; }
300  void set_math_mode(math_mode mm) noexcept { _mm = mm; }
301  math_mode get_math_mode() const noexcept { return _mm; }
302  static inline void set_saved_queue(::dpct::cs::queue_ptr q_ptr) noexcept {
303    _saved_queue_ptr = q_ptr;
304  }
305  static inline sycl::queue &get_saved_queue() noexcept {
306    if (!_saved_queue_ptr) _saved_queue_ptr = &::dpct::cs::get_default_queue();
307    return *_saved_queue_ptr;
308  }
309
310private:
311  ::dpct::cs::queue_ptr _queue_ptr = &::dpct::cs::get_default_queue();
312  math_mode _mm = math_mode::mm_default;
313  static inline ::dpct::cs::queue_ptr _saved_queue_ptr = nullptr;
314};
315
316using descriptor_ptr = descriptor *;
317} // namespace blas
318
319/// Get the value of \p s.
320/// Copy the data to host synchronously, then return the data.
321/// \param [in] p The pointer points the data.
322/// \param [in] q The queue where the memory copy should be executed.
323template <typename T> inline T get_value(const T *s, sycl::queue &q) {
324  return detail::get_value(s, q);
325}
326template <typename T>
327inline std::complex<T> get_value(const sycl::vec<T, 2> *s, sycl::queue &q) {
328  return detail::get_value(s, q);
329}
330
331inline oneapi::mkl::transpose get_transpose(int t) {
332  if (t == 0) {
333    return oneapi::mkl::transpose::nontrans;
334  } else if (t == 1) {
335    return oneapi::mkl::transpose::trans;
336  } else {
337    return oneapi::mkl::transpose::conjtrans;
338  }
339}
340
341/// Computes the LU factorizations of a batch of general matrices.
342/// \param [in] exec_queue The queue where the routine should be executed.
343/// \param [in] n The order of the matrices.
344/// \param [in, out] a Array of pointers to matrices. These matrices will be
345/// overwritten by lower triangulars with unit diagonal elements and upper
346/// triangulars.
347/// \param [in] lda The leading dimension of the matrices.
348/// \param [out] ipiv An array stores the pivot indices. If \p ipiv is nullptr,
349/// non-pivoting LU factorization is computed.
350/// \param [out] info An array stores the error information.
351/// \param [in] batch_size The size of the batch.
352template <typename T>
353inline void getrf_batch_wrapper(sycl::queue &exec_queue, int n, T *a[], int lda,
354                                int *ipiv, int *info, int batch_size) {
355  if (ipiv == nullptr) {
356    detail::getrfnp_batch_wrapper(exec_queue, n, a, lda, info, batch_size);
357    return;
358  }
359  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
360  // Set the info array value to 0
361  ::dpct::cs::fill<unsigned char>(exec_queue, info, 0,
362                                  sizeof(int) * batch_size);
363#ifdef DPCT_USM_LEVEL_NONE
364  std::int64_t stride_a = n * lda;
365  std::int64_t stride_ipiv = n;
366  std::int64_t scratchpad_size =
367      oneapi::mkl::lapack::getrf_batch_scratchpad_size<Ty>(
368          exec_queue, n, n, lda, stride_a, stride_ipiv, batch_size);
369
370  T *a_buffer_ptr;
371  a_buffer_ptr = (T *)::dpct::cs::malloc(stride_a * batch_size * sizeof(T));
372
373  T **host_a = (T **)std::malloc(batch_size * sizeof(T *));
374  dpct_memcpy(host_a, a, batch_size * sizeof(T *));
375  for (std::int64_t i = 0; i < batch_size; ++i)
376    dpct_memcpy(a_buffer_ptr + i * stride_a, host_a[i], n * lda * sizeof(T));
377
378  {
379    sycl::buffer<std::int64_t, 1> ipiv_buf(
380        sycl::range<1>(batch_size * stride_ipiv));
381    sycl::buffer<Ty, 1> scratchpad{sycl::range<1>(scratchpad_size)};
382    auto a_buffer = get_buffer<Ty>(a_buffer_ptr);
383    oneapi::mkl::lapack::getrf_batch(exec_queue, n, n, a_buffer, lda, stride_a,
384                                     ipiv_buf, stride_ipiv, batch_size,
385                                     scratchpad, scratchpad_size);
386
387    auto to_buffer = get_buffer<int>(ipiv);
388    exec_queue.submit([&](sycl::handler &cgh) {
389      auto from_acc = ipiv_buf.get_access<sycl::access_mode::read>(cgh);
390      auto to_acc = to_buffer.get_access<sycl::access_mode::write>(cgh);
391      cgh.parallel_for<
392          ::dpct::cs::kernel_name<class getrf_device_int64_to_int, T>>(
393          sycl::range<2>(batch_size, n), [=](sycl::id<2> id) {
394            to_acc[id.get(0) * n + id.get(1)] =
395                static_cast<int>(from_acc[id.get(0) * stride_ipiv + id.get(1)]);
396          });
397    });
398  }
399
400  // Copy back to the original buffers
401  std::vector<sycl::event> events;
402  for (std::int64_t i = 0; i < batch_size; ++i)
403    events.push_back(::dpct::cs::memcpy(
404        exec_queue, host_a[i], a_buffer_ptr + i * stride_a, n * lda * sizeof(T),
405        ::dpct::cs::memcpy_direction::automatic));
406
407  std::vector<void *> ptrs{host_a};
408  std::thread mem_free_thread(
409      [=](std::vector<void *> pointers_array,
410          std::vector<sycl::event> events_array) {
411        sycl::event::wait(events_array);
412        for (auto p : pointers_array)
413          std::free(p);
414      },
415      ptrs, events);
416  mem_free_thread.detach();
417#else
418  std::int64_t *m_int64 = new std::int64_t;
419  std::int64_t *n_int64 = new std::int64_t;
420  std::int64_t *lda_int64 = new std::int64_t;
421  std::int64_t *group_sizes = new std::int64_t;
422  *m_int64 = n;
423  *n_int64 = n;
424  *lda_int64 = lda;
425  *group_sizes = batch_size;
426  std::int64_t scratchpad_size =
427      oneapi::mkl::lapack::getrf_batch_scratchpad_size<Ty>(
428          exec_queue, m_int64, n_int64, lda_int64, 1, group_sizes);
429
430  Ty *scratchpad = sycl::malloc_device<Ty>(scratchpad_size, exec_queue);
431  std::int64_t *ipiv_int64 =
432      sycl::malloc_device<std::int64_t>(batch_size * n, exec_queue);
433  std::int64_t **ipiv_int64_ptr =
434      sycl::malloc_shared<std::int64_t *>(batch_size, exec_queue);
435  T **a_shared = sycl::malloc_shared<T *>(batch_size, exec_queue);
436  exec_queue.memcpy(a_shared, a, batch_size * sizeof(T *)).wait();
437  for (std::int64_t i = 0; i < batch_size; ++i)
438    ipiv_int64_ptr[i] = ipiv_int64 + n * i;
439
440  oneapi::mkl::lapack::getrf_batch(exec_queue, m_int64, n_int64,
441                                   (Ty **)a_shared, lda_int64, ipiv_int64_ptr,
442                                   1, group_sizes, scratchpad, scratchpad_size);
443
444  sycl::event e = exec_queue.submit([&](sycl::handler &cgh) {
445    cgh.parallel_for<
446        ::dpct::cs::kernel_name<class getrf_device_int64_to_int, T>>(
447        sycl::range<1>(batch_size * n), [=](sycl::id<1> idx) {
448          ipiv[idx] = static_cast<int>(ipiv_int64[idx]);
449        });
450  });
451
452  exec_queue.submit([&](sycl::handler &cgh) {
453    cgh.depends_on(e);
454    cgh.host_task([=] {
455      delete m_int64;
456      delete n_int64;
457      delete lda_int64;
458      delete group_sizes;
459    });
460  });
461  std::vector<void *> ptrs{scratchpad, ipiv_int64, ipiv_int64_ptr, a_shared};
462  ::dpct::cs::enqueue_free(ptrs, {e}, exec_queue);
463#endif
464}
465
466/// Solves a system of linear equations with a batch of LU-factored square
467/// coefficient matrices, with multiple right-hand sides.
468/// \param [in] exec_queue The queue where the routine should be executed.
469/// \param [in] trans Indicates the form of the linear equations.
470/// \param [in] n The order of the matrices.
471/// \param [in] nrhs The number of right hand sides.
472/// \param [in] a Array of pointers to matrices.
473/// \param [in] lda The leading dimension of the matrices in \p a.
474/// \param [in] ipiv An array stores the pivots.
475/// \param [in, out] b Array of pointers to matrices, whose columns are
476/// the right-hand sides for the systems of equations.
477/// \param [in] ldb The leading dimension of the matrices in \p b.
478/// \param [out] info A value stores the error information.
479/// \param [in] batch_size The size of the batch.
480template <typename T>
481inline void getrs_batch_wrapper(sycl::queue &exec_queue,
482                                oneapi::mkl::transpose trans, int n, int nrhs,
483                                const T *a[], int lda, const int *ipiv, T *b[],
484                                int ldb, int *info, int batch_size) {
485  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
486  // Set the info value to 0
487  *info = 0;
488#ifdef DPCT_USM_LEVEL_NONE
489  std::int64_t stride_a = n * lda;
490  std::int64_t stride_b = nrhs * ldb;
491  std::int64_t stride_ipiv = n;
492  std::int64_t scratchpad_size =
493      oneapi::mkl::lapack::getrs_batch_scratchpad_size<Ty>(
494          exec_queue, trans, n, nrhs, lda, stride_a, stride_ipiv, ldb, stride_b,
495          batch_size);
496
497  T *a_buffer_ptr, *b_buffer_ptr;
498  a_buffer_ptr = (T *)::dpct::cs::malloc(stride_a * batch_size * sizeof(T));
499  b_buffer_ptr = (T *)::dpct::cs::malloc(stride_b * batch_size * sizeof(T));
500
501  T **host_a = (T **)std::malloc(batch_size * sizeof(T *));
502  T **host_b = (T **)std::malloc(batch_size * sizeof(T *));
503  dpct_memcpy(host_a, a, batch_size * sizeof(T *));
504  dpct_memcpy(host_b, b, batch_size * sizeof(T *));
505  for (std::int64_t i = 0; i < batch_size; ++i) {
506    dpct_memcpy(a_buffer_ptr + i * stride_a, host_a[i], n * lda * sizeof(T));
507    dpct_memcpy(b_buffer_ptr + i * stride_b, host_b[i], nrhs * ldb * sizeof(T));
508  }
509
510  {
511    auto a_buffer = get_buffer<Ty>(a_buffer_ptr);
512    auto b_buffer = get_buffer<Ty>(b_buffer_ptr);
513    sycl::buffer<Ty, 1> scratchpad{sycl::range<1>(scratchpad_size)};
514    sycl::buffer<std::int64_t, 1> ipiv_buf(
515        sycl::range<1>(batch_size * stride_ipiv));
516    auto from_buf = get_buffer<int>(ipiv);
517    exec_queue.submit([&](sycl::handler &cgh) {
518      auto from_acc = from_buf.get_access<sycl::access_mode::read>(cgh);
519      auto to_acc = ipiv_buf.get_access<sycl::access_mode::write>(cgh);
520      cgh.parallel_for<
521          ::dpct::cs::kernel_name<class getrs_device_int64_to_int, T>>(
522          sycl::range<2>(batch_size, n), [=](sycl::id<2> id) {
523            to_acc[id.get(0) * stride_ipiv + id.get(1)] =
524                static_cast<std::int64_t>(from_acc[id.get(0) * n + id.get(1)]);
525          });
526    });
527
528    oneapi::mkl::lapack::getrs_batch(exec_queue, trans, n, nrhs, a_buffer, lda,
529                                     stride_a, ipiv_buf, stride_ipiv, b_buffer,
530                                     ldb, stride_b, batch_size, scratchpad,
531                                     scratchpad_size);
532  }
533
534  // Copy back to the original buffers
535  std::vector<sycl::event> events;
536  for (std::int64_t i = 0; i < batch_size; ++i)
537    events.push_back(::dpct::cs::memcpy(
538        exec_queue, host_b[i], b_buffer_ptr + i * stride_b,
539        nrhs * ldb * sizeof(T), ::dpct::cs::memcpy_direction::automatic));
540  std::vector<void *> ptrs{host_a, host_b};
541  std::thread mem_free_thread(
542      [=](std::vector<void *> pointers_array,
543          std::vector<sycl::event> events_array) {
544        sycl::event::wait(events_array);
545        for (auto p : pointers_array)
546          std::free(p);
547      },
548      ptrs, events);
549  mem_free_thread.detach();
550#else
551  std::int64_t *n_int64 = new std::int64_t;
552  std::int64_t *nrhs_int64 = new std::int64_t;
553  std::int64_t *lda_int64 = new std::int64_t;
554  std::int64_t *ldb_int64 = new std::int64_t;
555  std::int64_t *group_sizes = new std::int64_t;
556  oneapi::mkl::transpose *trans_array = new oneapi::mkl::transpose;
557  *n_int64 = n;
558  *nrhs_int64 = nrhs;
559  *lda_int64 = lda;
560  *ldb_int64 = ldb;
561  *group_sizes = batch_size;
562  *trans_array = trans;
563  std::int64_t scratchpad_size =
564      oneapi::mkl::lapack::getrs_batch_scratchpad_size<Ty>(
565          exec_queue, trans_array, n_int64, nrhs_int64, lda_int64, ldb_int64, 1,
566          group_sizes);
567
568  Ty *scratchpad = sycl::malloc_device<Ty>(scratchpad_size, exec_queue);
569  std::int64_t *ipiv_int64 =
570      sycl::malloc_device<std::int64_t>(batch_size * n, exec_queue);
571  std::int64_t **ipiv_int64_ptr =
572      sycl::malloc_shared<std::int64_t *>(batch_size, exec_queue);
573  T **a_shared = sycl::malloc_shared<T *>(batch_size, exec_queue);
574  T **b_shared = sycl::malloc_shared<T *>(batch_size, exec_queue);
575  exec_queue.memcpy(a_shared, a, batch_size * sizeof(T *));
576  exec_queue.memcpy(b_shared, b, batch_size * sizeof(T *));
577
578  exec_queue
579      .submit([&](sycl::handler &cgh) {
580        cgh.parallel_for<
581            ::dpct::cs::kernel_name<class getrs_device_int64_to_int, T>>(
582            sycl::range<1>(batch_size * n), [=](sycl::id<1> idx) {
583              ipiv_int64[idx] = static_cast<std::int64_t>(ipiv[idx]);
584            });
585      })
586      .wait();
587
588  for (std::int64_t i = 0; i < batch_size; ++i)
589    ipiv_int64_ptr[i] = ipiv_int64 + n * i;
590
591  sycl::event e = oneapi::mkl::lapack::getrs_batch(
592      exec_queue, trans_array, n_int64, nrhs_int64, (Ty **)a_shared, lda_int64,
593      ipiv_int64_ptr, (Ty **)b_shared, ldb_int64, 1, group_sizes, scratchpad,
594      scratchpad_size);
595
596  exec_queue.submit([&](sycl::handler &cgh) {
597    cgh.depends_on(e);
598    cgh.host_task([=] {
599      delete n_int64;
600      delete nrhs_int64;
601      delete lda_int64;
602      delete ldb_int64;
603      delete group_sizes;
604      delete trans_array;
605    });
606  });
607  std::vector<void *> ptrs{scratchpad, ipiv_int64_ptr, ipiv_int64, a_shared,
608                           b_shared};
609  ::dpct::cs::enqueue_free(ptrs, {e}, exec_queue);
610#endif
611}
612
613/// Computes the inverses of a batch of LU-factored matrices.
614/// \param [in] exec_queue The queue where the routine should be executed.
615/// \param [in] n The order of the matrices.
616/// \param [in] a Array of pointers to matrices.
617/// \param [in] lda The leading dimension of the matrices in \p a.
618/// \param [in] ipiv An array stores the pivots.
619/// \param [out] b Array of pointers to inverse matrices.
620/// \param [in] ldb The leading dimension of the matrices in \p b.
621/// \param [out] info An array stores the error information.
622/// \param [in] batch_size The size of the batch.
623template <typename T>
624inline void getri_batch_wrapper(sycl::queue &exec_queue, int n, const T *a[],
625                                int lda, int *ipiv, T *b[], int ldb, int *info,
626                                int batch_size) {
627  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
628  // Set the info array value to 0
629  ::dpct::cs::fill<unsigned char>(exec_queue, info, 0,
630                                  sizeof(int) * batch_size);
631#ifdef DPCT_USM_LEVEL_NONE
632  std::int64_t stride_b = n * ldb;
633  std::int64_t stride_ipiv = n;
634  std::int64_t scratchpad_size =
635      oneapi::mkl::lapack::getri_batch_scratchpad_size<Ty>(
636          exec_queue, n, ldb, stride_b, stride_ipiv, batch_size);
637
638  T *b_buffer_ptr;
639  b_buffer_ptr = (T *)::dpct::cs::malloc(stride_b * batch_size * sizeof(T));
640
641  T **host_a = (T **)std::malloc(batch_size * sizeof(T *));
642  T **host_b = (T **)std::malloc(batch_size * sizeof(T *));
643  dpct_memcpy(host_a, a, batch_size * sizeof(T *));
644  dpct_memcpy(host_b, b, batch_size * sizeof(T *));
645
646  for (std::int64_t i = 0; i < batch_size; ++i) {
647    // Need to create a copy of input matrices "a" to keep them unchanged.
648    // Matrices "b" (copy of matrices "a") will be used as input and output
649    // parameter in oneapi::mkl::lapack::getri_batch call.
650    blas::matrix_mem_copy(b_buffer_ptr + i * stride_b, host_a[i], ldb, lda, n,
651                          n, sizeof(T), dpct::device_to_device, exec_queue);
652  }
653
654  {
655    auto b_buffer = get_buffer<Ty>(b_buffer_ptr);
656    sycl::buffer<Ty, 1> scratchpad{sycl::range<1>(scratchpad_size)};
657    sycl::buffer<std::int64_t, 1> ipiv_buf(
658        sycl::range<1>(batch_size * stride_ipiv));
659    auto from_buf = get_buffer<int>(ipiv);
660    exec_queue.submit([&](sycl::handler &cgh) {
661      auto from_acc = from_buf.get_access<sycl::access_mode::read>(cgh);
662      auto to_acc = ipiv_buf.get_access<sycl::access_mode::write>(cgh);
663      cgh.parallel_for<
664          ::dpct::cs::kernel_name<class getri_device_int64_to_int, T>>(
665          sycl::range<2>(batch_size, n), [=](sycl::id<2> id) {
666            to_acc[id.get(0) * stride_ipiv + id.get(1)] =
667                static_cast<std::int64_t>(from_acc[id.get(0) * n + id.get(1)]);
668          });
669    });
670
671    oneapi::mkl::lapack::getri_batch(exec_queue, n, b_buffer, ldb, stride_b,
672                                     ipiv_buf, stride_ipiv, batch_size,
673                                     scratchpad, scratchpad_size);
674  }
675
676  // Copy back to the original buffers
677  std::vector<sycl::event> events;
678  for (std::int64_t i = 0; i < batch_size; ++i)
679    events.push_back(::dpct::cs::memcpy(
680        exec_queue, host_b[i], b_buffer_ptr + i * stride_b, n * ldb * sizeof(T),
681        ::dpct::cs::memcpy_direction::automatic));
682  std::vector<void *> ptrs{host_a, host_b};
683  std::thread mem_free_thread(
684      [=](std::vector<void *> pointers_array,
685          std::vector<sycl::event> events_array) {
686        sycl::event::wait(events_array);
687        for (auto p : pointers_array)
688          std::free(p);
689      },
690      ptrs, events);
691  mem_free_thread.detach();
692#else
693  std::int64_t *n_int64 = new std::int64_t;
694  std::int64_t *ldb_int64 = new std::int64_t;
695  std::int64_t *group_sizes = new std::int64_t;
696  *n_int64 = n;
697  *ldb_int64 = ldb;
698  *group_sizes = batch_size;
699  std::int64_t scratchpad_size =
700      oneapi::mkl::lapack::getri_batch_scratchpad_size<Ty>(
701          exec_queue, n_int64, ldb_int64, 1, group_sizes);
702
703  Ty *scratchpad = sycl::malloc_device<Ty>(scratchpad_size, exec_queue);
704  std::int64_t *ipiv_int64 =
705      sycl::malloc_device<std::int64_t>(batch_size * n, exec_queue);
706  std::int64_t **ipiv_int64_ptr =
707      sycl::malloc_shared<std::int64_t *>(batch_size, exec_queue);
708
709  exec_queue.submit([&](sycl::handler &cgh) {
710    cgh.parallel_for<
711        ::dpct::cs::kernel_name<class getri_device_int64_to_int, T>>(
712        sycl::range<1>(batch_size * n), [=](sycl::id<1> idx) {
713          ipiv_int64[idx] = static_cast<std::int64_t>(ipiv[idx]);
714        });
715  });
716
717  T **a_shared = sycl::malloc_shared<T *>(batch_size, exec_queue);
718  T **b_shared = sycl::malloc_shared<T *>(batch_size, exec_queue);
719  exec_queue.memcpy(a_shared, a, batch_size * sizeof(T *));
720  exec_queue.memcpy(b_shared, b, batch_size * sizeof(T *)).wait();
721  for (std::int64_t i = 0; i < batch_size; ++i) {
722    ipiv_int64_ptr[i] = ipiv_int64 + n * i;
723    // Need to create a copy of input matrices "a" to keep them unchanged.
724    // Matrices "b" (copy of matrices "a") will be used as input and output
725    // parameter in oneapi::mkl::lapack::getri_batch call.
726    blas::matrix_mem_copy(b_shared[i], a_shared[i], ldb, lda, n, n, sizeof(T),
727                          ::dpct::cs::memcpy_direction::device_to_device,
728                          exec_queue);
729  }
730
731  sycl::event e = oneapi::mkl::lapack::getri_batch(
732      exec_queue, n_int64, (Ty **)b_shared, ldb_int64, ipiv_int64_ptr, 1,
733      group_sizes, scratchpad, scratchpad_size);
734
735  exec_queue.submit([&](sycl::handler &cgh) {
736    cgh.depends_on(e);
737    cgh.host_task([=] {
738      delete n_int64;
739      delete ldb_int64;
740      delete group_sizes;
741    });
742  });
743  std::vector<void *> ptrs{scratchpad, ipiv_int64_ptr, ipiv_int64, a_shared,
744                           b_shared};
745  ::dpct::cs::enqueue_free(ptrs, {e}, exec_queue);
746#endif
747}
748
749/// Computes the QR factorizations of a batch of general matrices.
750/// \param [in] exec_queue The queue where the routine should be executed.
751/// \param [in] m The number of rows in the matrices.
752/// \param [in] n The number of columns in the matrices.
753/// \param [in, out] a Array of pointers to matrices. These
754/// matrices will be overwritten by the factorization data.
755/// \param [in] lda The leading dimension of the matrices in \p a.
756/// \param [out] tau An array stores the scalars.
757/// \param [out] info A value stores the error information.
758/// \param [in] batch_size The size of the batch.
759template <typename T>
760inline void geqrf_batch_wrapper(sycl::queue exec_queue, int m, int n, T *a[],
761                                int lda, T *tau[], int *info, int batch_size) {
762  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
763  // Set the info value to 0
764  *info = 0;
765#ifdef DPCT_USM_LEVEL_NONE
766  std::int64_t stride_a = n * lda;
767  std::int64_t stride_tau = (std::max)(1, (std::min)(m, n));
768  std::int64_t scratchpad_size =
769      oneapi::mkl::lapack::geqrf_batch_scratchpad_size<Ty>(
770          exec_queue, m, n, lda, stride_a, stride_tau, batch_size);
771
772  T *a_buffer_ptr, *tau_buffer_ptr;
773  a_buffer_ptr = (T *)::dpct::cs::malloc(stride_a * batch_size * sizeof(T));
774  tau_buffer_ptr = (T *)::dpct::cs::malloc(stride_tau * batch_size * sizeof(T));
775
776  T **host_a = (T **)std::malloc(batch_size * sizeof(T *));
777  T **host_tau = (T **)std::malloc(batch_size * sizeof(T *));
778  dpct_memcpy(host_a, a, batch_size * sizeof(T *));
779  dpct_memcpy(host_tau, tau, batch_size * sizeof(T *));
780
781  for (std::int64_t i = 0; i < batch_size; ++i)
782    dpct_memcpy(a_buffer_ptr + i * stride_a, host_a[i], n * lda * sizeof(T));
783  {
784    auto a_buffer = get_buffer<Ty>(a_buffer_ptr);
785    auto tau_buffer = get_buffer<Ty>(tau_buffer_ptr);
786    sycl::buffer<Ty, 1> scratchpad{sycl::range<1>(scratchpad_size)};
787    oneapi::mkl::lapack::geqrf_batch(exec_queue, m, n, a_buffer, lda, stride_a,
788                                     tau_buffer, stride_tau, batch_size,
789                                     scratchpad, scratchpad_size);
790  }
791
792  // Copy back to the original buffers
793  std::vector<sycl::event> events_a;
794  std::vector<sycl::event> events_tau;
795  for (std::int64_t i = 0; i < batch_size; ++i) {
796    events_a.push_back(::dpct::cs::memcpy(
797        exec_queue, host_a[i], a_buffer_ptr + i * stride_a, n * lda * sizeof(T),
798        ::dpct::cs::memcpy_direction::automatic));
799    events_tau.push_back(::dpct::cs::memcpy(
800        exec_queue, host_tau[i], tau_buffer_ptr + i * stride_tau,
801        (std::max)(1, (std::min)(m, n)) * sizeof(T),
802        ::dpct::cs::memcpy_direction::automatic));
803  }
804  std::vector<void *> ptr_a{host_a};
805  std::vector<void *> ptr_tau{host_tau};
806  std::thread mem_free_thread_a(
807      [=](std::vector<void *> pointers_array,
808          std::vector<sycl::event> events_array) {
809        sycl::event::wait(events_array);
810        for (auto p : pointers_array)
811          std::free(p);
812      },
813      ptr_a, events_a);
814  std::thread mem_free_thread_tau(
815      [=](std::vector<void *> pointers_array,
816          std::vector<sycl::event> events_array) {
817        sycl::event::wait(events_array);
818        for (auto p : pointers_array)
819          std::free(p);
820      },
821      ptr_tau, events_tau);
822  mem_free_thread_a.detach();
823  mem_free_thread_tau.detach();
824#else
825  std::int64_t *m_int64 = new std::int64_t;
826  std::int64_t *n_int64 = new std::int64_t;
827  std::int64_t *lda_int64 = new std::int64_t;
828  std::int64_t *group_sizes = new std::int64_t;
829  *m_int64 = n;
830  *n_int64 = n;
831  *lda_int64 = lda;
832  *group_sizes = batch_size;
833  std::int64_t scratchpad_size =
834      oneapi::mkl::lapack::geqrf_batch_scratchpad_size<Ty>(
835          exec_queue, m_int64, n_int64, lda_int64, 1, group_sizes);
836
837  Ty *scratchpad = sycl::malloc_device<Ty>(scratchpad_size, exec_queue);
838  T **a_shared = sycl::malloc_shared<T *>(batch_size, exec_queue);
839  T **tau_shared = sycl::malloc_shared<T *>(batch_size, exec_queue);
840  exec_queue.memcpy(a_shared, a, batch_size * sizeof(T *));
841  exec_queue.memcpy(tau_shared, tau, batch_size * sizeof(T *)).wait();
842
843  sycl::event e = oneapi::mkl::lapack::geqrf_batch(
844      exec_queue, m_int64, n_int64, (Ty **)a_shared, lda_int64,
845      (Ty **)tau_shared, 1, group_sizes, scratchpad, scratchpad_size);
846
847  exec_queue.submit([&](sycl::handler &cgh) {
848    cgh.depends_on(e);
849    cgh.host_task([=] {
850      delete m_int64;
851      delete n_int64;
852      delete lda_int64;
853      delete group_sizes;
854    });
855  });
856  std::vector<void *> ptrs{scratchpad, a_shared, tau_shared};
857  ::dpct::cs::enqueue_free(ptrs, {e}, exec_queue);
858#endif
859}
860
861namespace blas {
862#ifdef __INTEL_MKL__
863template <typename T>
864inline oneapi::mkl::blas::compute_mode
865deduce_compute_mode(std::optional<compute_type> ct, math_mode mm) {
866  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
867  if (ct) {
868    switch (ct.value()) {
869    case compute_type::f16_standard:
870    case compute_type::f32_standard:
871    case compute_type::f64_standard:
872    case compute_type::i32_standard:
873      return oneapi::mkl::blas::compute_mode::standard;
874    case compute_type::f32:
875      if constexpr (std::is_same_v<Ty, std::complex<float>> ||
876                    std::is_same_v<Ty, std::complex<double>>)
877        return oneapi::mkl::blas::compute_mode::complex_3m;
878      break;
879    case compute_type::f32_fast_bf16:
880      return oneapi::mkl::blas::compute_mode::float_to_bf16;
881    case compute_type::f32_fast_tf32:
882      return oneapi::mkl::blas::compute_mode::float_to_tf32;
883    default:
884      [[fallthrough]];
885    }
886  }
887  if (mm == math_mode::mm_tf32)
888    return oneapi::mkl::blas::compute_mode::float_to_tf32;
889  return oneapi::mkl::blas::compute_mode::unset;
890}
891
892inline oneapi::mkl::blas::compute_mode
893deduce_compute_mode(std::optional<compute_type> ct, math_mode mm,
894                    bool is_complex) {
895  if (is_complex)
896    return deduce_compute_mode<std::complex<float>>(ct, mm);
897  return deduce_compute_mode<float>(ct, mm);
898}
899#endif
900
901/// Computes matrix-matrix product with general matrices.
902/// \param [in] desc_ptr Descriptor.
903/// \param [in] a_trans Specifies the operation applied to A.
904/// \param [in] b_trans Specifies the operation applied to B.
905/// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
906/// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
907/// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
908/// \param [in] alpha Scaling factor for the matrix-matrix product.
909/// \param [in] a Input matrix A.
910/// \param [in] a_type Data type of the matrix A.
911/// \param [in] lda Leading dimension of A.
912/// \param [in] b Input matrix B.
913/// \param [in] b_type Data type of the matrix B.
914/// \param [in] ldb Leading dimension of B.
915/// \param [in] beta Scaling factor for matrix C.
916/// \param [in, out] c Input/Output matrix C.
917/// \param [in] c_type Data type of the matrix C.
918/// \param [in] ldc Leading dimension of C.
919/// \param [in] ct Compute type.
920inline void gemm(descriptor_ptr desc_ptr, oneapi::mkl::transpose a_trans,
921                 oneapi::mkl::transpose b_trans, std::int64_t m, std::int64_t n,
922                 std::int64_t k, const void *alpha, const void *a,
923                 library_data_t a_type, std::int64_t lda, const void *b,
924                 library_data_t b_type, std::int64_t ldb, const void *beta,
925                 void *c, library_data_t c_type, std::int64_t ldc,
926                 std::variant<compute_type, library_data_t> ct) {
927#ifndef __INTEL_MKL__
928  throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
929                           "Project does not support this API.");
930#else
931  sycl::queue q = desc_ptr->get_queue();
932  oneapi::mkl::blas::compute_mode cm = oneapi::mkl::blas::compute_mode::unset;
933  library_data_t scaling_type;
934  if (auto ct_p = std::get_if<compute_type>(&ct)) {
935    cm = deduce_compute_mode(*ct_p, desc_ptr->get_math_mode(),
936                             a_type == library_data_t::complex_float ||
937                                 a_type == library_data_t::complex_double);
938    scaling_type = detail::compute_type_to_library_data_t(*ct_p);
939  } else {
940    cm = deduce_compute_mode(std::nullopt, desc_ptr->get_math_mode(),
941                             a_type == library_data_t::complex_float ||
942                                 a_type == library_data_t::complex_double);
943    scaling_type = std::get<library_data_t>(ct);
944  }
945
946  if (scaling_type == library_data_t::real_float &&
947      c_type == library_data_t::complex_float) {
948    scaling_type = library_data_t::complex_float;
949  } else if (scaling_type == library_data_t::real_double &&
950             c_type == library_data_t::complex_double) {
951    scaling_type = library_data_t::complex_double;
952  }
953
954  std::uint64_t key = dpct::detail::get_type_combination_id(
955      a_type, b_type, c_type, scaling_type);
956  switch (key) {
957  case dpct::detail::get_type_combination_id(
958      library_data_t::real_float, library_data_t::real_float,
959      library_data_t::real_float, library_data_t::real_float): {
960    dpct::detail::gemm_impl<float, float, float, float>(
961        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc, cm);
962    break;
963  }
964  case dpct::detail::get_type_combination_id(
965      library_data_t::real_double, library_data_t::real_double,
966      library_data_t::real_double, library_data_t::real_double): {
967    dpct::detail::gemm_impl<double, double, double, double>(
968        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc, cm);
969    break;
970  }
971  case dpct::detail::get_type_combination_id(
972      library_data_t::complex_float, library_data_t::complex_float,
973      library_data_t::complex_float, library_data_t::complex_float): {
974    dpct::detail::gemm_impl<std::complex<float>, std::complex<float>,
975                            std::complex<float>, std::complex<float>>(
976        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc, cm);
977    break;
978  }
979  case dpct::detail::get_type_combination_id(
980      library_data_t::complex_double, library_data_t::complex_double,
981      library_data_t::complex_double, library_data_t::complex_double): {
982    dpct::detail::gemm_impl<std::complex<double>, std::complex<double>,
983                            std::complex<double>, std::complex<double>>(
984        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc, cm);
985    break;
986  }
987  case dpct::detail::get_type_combination_id(
988      library_data_t::real_half, library_data_t::real_half,
989      library_data_t::real_half, library_data_t::real_half): {
990    dpct::detail::gemm_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
991        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc, cm);
992    break;
993  }
994  case dpct::detail::get_type_combination_id(
995      library_data_t::real_bfloat16, library_data_t::real_bfloat16,
996      library_data_t::real_float, library_data_t::real_float): {
997    dpct::detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
998                            float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
999                                   b, ldb, beta, c, ldc, cm);
1000    break;
1001  }
1002  case dpct::detail::get_type_combination_id(
1003      library_data_t::real_half, library_data_t::real_half,
1004      library_data_t::real_float, library_data_t::real_float): {
1005    dpct::detail::gemm_impl<sycl::half, sycl::half, float, float>(
1006        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc, cm);
1007    break;
1008  }
1009  case dpct::detail::get_type_combination_id(
1010      library_data_t::real_half, library_data_t::real_half,
1011      library_data_t::real_half, library_data_t::real_float): {
1012    float alpha_value =
1013        dpct::get_value(reinterpret_cast<const float *>(alpha), q);
1014    float beta_value =
1015        dpct::get_value(reinterpret_cast<const float *>(beta), q);
1016    sycl::half alpha_half(alpha_value);
1017    sycl::half beta_half(beta_value);
1018    dpct::detail::gemm_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
1019        q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, b, ldb, &beta_half,
1020        c, ldc, cm);
1021    break;
1022  }
1023  case dpct::detail::get_type_combination_id(
1024      library_data_t::real_int8, library_data_t::real_int8,
1025      library_data_t::real_float, library_data_t::real_float): {
1026    dpct::detail::gemm_impl<std::int8_t, std::int8_t, float, float>(
1027        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc, cm);
1028    break;
1029  }
1030  case dpct::detail::get_type_combination_id(
1031      library_data_t::real_bfloat16, library_data_t::real_bfloat16,
1032      library_data_t::real_bfloat16, library_data_t::real_float): {
1033    dpct::detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
1034                            oneapi::mkl::bfloat16, float>(
1035        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc, cm);
1036    break;
1037  }
1038  case dpct::detail::get_type_combination_id(
1039      library_data_t::real_int8, library_data_t::real_int8,
1040      library_data_t::real_int32, library_data_t::real_int32): {
1041    float alpha_float =
1042        dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
1043    float beta_float =
1044        dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
1045    dpct::detail::gemm_impl<std::int8_t, std::int8_t, std::int32_t, float>(
1046        q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, b, ldb, &beta_float,
1047        c, ldc, cm);
1048    break;
1049  }
1050  default:
1051    throw std::runtime_error("the combination of data type is unsupported");
1052  }
1053#endif
1054}
1055
1056/// Computes a batch of matrix-matrix product with general matrices.
1057/// \param [in] desc_ptr Descriptor.
1058/// \param [in] a_trans Specifies the operation applied to A.
1059/// \param [in] b_trans Specifies the operation applied to B.
1060/// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
1061/// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
1062/// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
1063/// \param [in] alpha Scaling factor for the matrix-matrix product.
1064/// \param [in] a Input matrix A.
1065/// \param [in] a_type Data type of the matrix A.
1066/// \param [in] lda Leading dimension of A.
1067/// \param [in] b Input matrix B.
1068/// \param [in] b_type Data type of the matrix B.
1069/// \param [in] ldb Leading dimension of B.
1070/// \param [in] beta Scaling factor for matrix C.
1071/// \param [in, out] c Input/Output matrix C.
1072/// \param [in] c_type Data type of the matrix C.
1073/// \param [in] ldc Leading dimension of C.
1074/// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
1075/// \param [in] scaling_type Data type of the scaling factors.
1076/// \param [in] ct Compute type.
1077inline void gemm_batch(descriptor_ptr desc_ptr, oneapi::mkl::transpose a_trans,
1078                       oneapi::mkl::transpose b_trans, std::int64_t m,
1079                       std::int64_t n, std::int64_t k, const void *alpha,
1080                       const void *a[], library_data_t a_type, std::int64_t lda,
1081                       const void *b[], library_data_t b_type, std::int64_t ldb,
1082                       const void *beta, void *c[], library_data_t c_type,
1083                       std::int64_t ldc, std::int64_t batch_size,
1084                       std::variant<compute_type, library_data_t> ct) {
1085#ifdef DPCT_USM_LEVEL_NONE
1086  throw std::runtime_error("this API is unsupported when USM level is none");
1087#else
1088  sycl::queue q = desc_ptr->get_queue();
1089#ifdef __INTEL_MKL__
1090  oneapi::mkl::blas::compute_mode cm = oneapi::mkl::blas::compute_mode::unset;
1091#endif
1092  library_data_t scaling_type;
1093  if (auto ct_p = std::get_if<compute_type>(&ct)) {
1094#ifdef __INTEL_MKL__
1095    cm = deduce_compute_mode(*ct_p, desc_ptr->get_math_mode(),
1096                             a_type == library_data_t::complex_float ||
1097                                 a_type == library_data_t::complex_double);
1098#endif
1099    scaling_type = detail::compute_type_to_library_data_t(*ct_p);
1100  } else {
1101#ifdef __INTEL_MKL__
1102    cm = deduce_compute_mode(std::nullopt, desc_ptr->get_math_mode(),
1103                             a_type == library_data_t::complex_float ||
1104                                 a_type == library_data_t::complex_double);
1105#endif
1106    scaling_type = std::get<library_data_t>(ct);
1107  }
1108
1109  if (scaling_type == library_data_t::real_float &&
1110      c_type == library_data_t::complex_float) {
1111    scaling_type = library_data_t::complex_float;
1112  } else if (scaling_type == library_data_t::real_double &&
1113             c_type == library_data_t::complex_double) {
1114    scaling_type = library_data_t::complex_double;
1115  }
1116
1117  std::uint64_t key = dpct::detail::get_type_combination_id(
1118      a_type, b_type, c_type, scaling_type);
1119  switch (key) {
1120  case dpct::detail::get_type_combination_id(
1121      library_data_t::real_float, library_data_t::real_float,
1122      library_data_t::real_float, library_data_t::real_float): {
1123    dpct::detail::gemm_batch_impl<float, float, float, float>(
1124        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
1125        batch_size DPCT_COMPUTE_MODE_ARG);
1126    break;
1127  }
1128  case dpct::detail::get_type_combination_id(
1129      library_data_t::real_double, library_data_t::real_double,
1130      library_data_t::real_double, library_data_t::real_double): {
1131    dpct::detail::gemm_batch_impl<double, double, double, double>(
1132        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
1133        batch_size DPCT_COMPUTE_MODE_ARG);
1134    break;
1135  }
1136  case dpct::detail::get_type_combination_id(
1137      library_data_t::complex_float, library_data_t::complex_float,
1138      library_data_t::complex_float, library_data_t::complex_float): {
1139    dpct::detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
1140                                  std::complex<float>, std::complex<float>>(
1141        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
1142        batch_size DPCT_COMPUTE_MODE_ARG);
1143    break;
1144  }
1145  case dpct::detail::get_type_combination_id(
1146      library_data_t::complex_double, library_data_t::complex_double,
1147      library_data_t::complex_double, library_data_t::complex_double): {
1148    dpct::detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
1149                                  std::complex<double>, std::complex<double>>(
1150        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
1151        batch_size DPCT_COMPUTE_MODE_ARG);
1152    break;
1153  }
1154  case dpct::detail::get_type_combination_id(
1155      library_data_t::real_half, library_data_t::real_half,
1156      library_data_t::real_half, library_data_t::real_half): {
1157    dpct::detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
1158                                  sycl::half>(q, a_trans, b_trans, m, n, k,
1159                                              alpha, a, lda, b, ldb, beta, c,
1160                                              ldc, batch_size DPCT_COMPUTE_MODE_ARG);
1161    break;
1162  }
1163#ifdef __INTEL_MKL__
1164  case dpct::detail::get_type_combination_id(
1165      library_data_t::real_bfloat16, library_data_t::real_bfloat16,
1166      library_data_t::real_bfloat16, library_data_t::real_float): {
1167    dpct::detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
1168                                  oneapi::mkl::bfloat16, float>(
1169        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
1170        batch_size, cm);
1171    break;
1172  }
1173  case dpct::detail::get_type_combination_id(
1174      library_data_t::real_bfloat16, library_data_t::real_bfloat16,
1175      library_data_t::real_float, library_data_t::real_float): {
1176    dpct::detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
1177                                  float, float>(q, a_trans, b_trans, m, n, k,
1178                                                alpha, a, lda, b, ldb, beta, c,
1179                                                ldc, batch_size, cm);
1180    break;
1181  }
1182#endif
1183  case dpct::detail::get_type_combination_id(
1184      library_data_t::real_int8, library_data_t::real_int8,
1185      library_data_t::real_int32, library_data_t::real_int32): {
1186    float alpha_float =
1187        dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
1188    float beta_float =
1189        dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
1190    dpct::detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
1191                                  float>(
1192        q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, b, ldb, &beta_float,
1193        c, ldc, batch_size DPCT_COMPUTE_MODE_ARG);
1194    break;
1195  }
1196  case dpct::detail::get_type_combination_id(
1197      library_data_t::real_int8, library_data_t::real_int8,
1198      library_data_t::real_float, library_data_t::real_float): {
1199    dpct::detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
1200        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
1201        batch_size DPCT_COMPUTE_MODE_ARG);
1202    break;
1203  }
1204  case dpct::detail::get_type_combination_id(
1205      library_data_t::real_half, library_data_t::real_half,
1206      library_data_t::real_float, library_data_t::real_float): {
1207    dpct::detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
1208        q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
1209        batch_size DPCT_COMPUTE_MODE_ARG);
1210    break;
1211  }
1212  case dpct::detail::get_type_combination_id(
1213      library_data_t::real_half, library_data_t::real_half,
1214      library_data_t::real_half, library_data_t::real_float): {
1215    float alpha_value =
1216        dpct::get_value(reinterpret_cast<const float *>(alpha), q);
1217    float beta_value =
1218        dpct::get_value(reinterpret_cast<const float *>(beta), q);
1219    sycl::half alpha_half(alpha_value);
1220    sycl::half beta_half(beta_value);
1221    dpct::detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
1222                                  sycl::half>(
1223        q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, b, ldb, &beta_half,
1224        c, ldc, batch_size DPCT_COMPUTE_MODE_ARG);
1225    break;
1226  }
1227  default:
1228    throw std::runtime_error("the combination of data type is unsupported");
1229  }
1230#endif
1231}
1232
1233/// Computes a batch of matrix-matrix product with general matrices.
1234/// \param [in] desc_ptr Descriptor.
1235/// \param [in] a_trans Specifies the operation applied to A.
1236/// \param [in] b_trans Specifies the operation applied to B.
1237/// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
1238/// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
1239/// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
1240/// \param [in] alpha Scaling factor for the matrix-matrix product.
1241/// \param [in] a Input matrix A.
1242/// \param [in] a_type Data type of the matrix A.
1243/// \param [in] lda Leading dimension of A.
1244/// \param [in] stride_a Stride between the different A matrices.
1245/// \param [in] b Input matrix B.
1246/// \param [in] b_type Data type of the matrix B.
1247/// \param [in] ldb Leading dimension of B.
1248/// \param [in] stride_b Stride between the different B matrices.
1249/// \param [in] beta Scaling factor for matrix C.
1250/// \param [in, out] c Input/Output matrix C.
1251/// \param [in] c_type Data type of the matrix C.
1252/// \param [in] ldc Leading dimension of C.
1253/// \param [in] stride_c Stride between the different C matrices.
1254/// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
1255/// \param [in] ct Compute type.
1256inline void gemm_batch(descriptor_ptr desc_ptr, oneapi::mkl::transpose a_trans,
1257                       oneapi::mkl::transpose b_trans, std::int64_t m,
1258                       std::int64_t n, std::int64_t k, const void *alpha,
1259                       const void *a, library_data_t a_type, std::int64_t lda,
1260                       long long int stride_a, const void *b,
1261                       library_data_t b_type, std::int64_t ldb,
1262                       long long int stride_b, const void *beta, void *c,
1263                       library_data_t c_type, std::int64_t ldc,
1264                       long long int stride_c, std::int64_t batch_size,
1265                       std::variant<compute_type, library_data_t> ct) {
1266  sycl::queue q = desc_ptr->get_queue();
1267#ifdef __INTEL_MKL__
1268  oneapi::mkl::blas::compute_mode cm = oneapi::mkl::blas::compute_mode::unset;
1269#endif
1270  library_data_t scaling_type;
1271  if (auto ct_p = std::get_if<compute_type>(&ct)) {
1272#ifdef __INTEL_MKL__
1273    cm = deduce_compute_mode(*ct_p, desc_ptr->get_math_mode(),
1274                             a_type == library_data_t::complex_float ||
1275                                 a_type == library_data_t::complex_double);
1276#endif
1277    scaling_type = detail::compute_type_to_library_data_t(*ct_p);
1278  } else {
1279#ifdef __INTEL_MKL__
1280    cm = deduce_compute_mode(std::nullopt, desc_ptr->get_math_mode(),
1281                             a_type == library_data_t::complex_float ||
1282                                 a_type == library_data_t::complex_double);
1283#endif
1284    scaling_type = std::get<library_data_t>(ct);
1285  }
1286
1287  if (scaling_type == library_data_t::real_float &&
1288      c_type == library_data_t::complex_float) {
1289    scaling_type = library_data_t::complex_float;
1290  } else if (scaling_type == library_data_t::real_double &&
1291             c_type == library_data_t::complex_double) {
1292    scaling_type = library_data_t::complex_double;
1293  }
1294
1295  std::uint64_t key = dpct::detail::get_type_combination_id(
1296      a_type, b_type, c_type, scaling_type);
1297  switch (key) {
1298  case dpct::detail::get_type_combination_id(
1299      library_data_t::real_float, library_data_t::real_float,
1300      library_data_t::real_float, library_data_t::real_float): {
1301    dpct::detail::gemm_batch_impl<float, float, float, float>(
1302        q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
1303        beta, c, ldc, stride_c, batch_size DPCT_COMPUTE_MODE_ARG);
1304    break;
1305  }
1306  case dpct::detail::get_type_combination_id(
1307      library_data_t::real_double, library_data_t::real_double,
1308      library_data_t::real_double, library_data_t::real_double): {
1309    dpct::detail::gemm_batch_impl<double, double, double, double>(
1310        q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
1311        beta, c, ldc, stride_c, batch_size DPCT_COMPUTE_MODE_ARG);
1312    break;
1313  }
1314  case dpct::detail::get_type_combination_id(
1315      library_data_t::complex_float, library_data_t::complex_float,
1316      library_data_t::complex_float, library_data_t::complex_float): {
1317    dpct::detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
1318                                  std::complex<float>, std::complex<float>>(
1319        q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
1320        beta, c, ldc, stride_c, batch_size DPCT_COMPUTE_MODE_ARG);
1321    break;
1322  }
1323  case dpct::detail::get_type_combination_id(
1324      library_data_t::complex_double, library_data_t::complex_double,
1325      library_data_t::complex_double, library_data_t::complex_double): {
1326    dpct::detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
1327                                  std::complex<double>, std::complex<double>>(
1328        q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
1329        beta, c, ldc, stride_c, batch_size DPCT_COMPUTE_MODE_ARG);
1330    break;
1331  }
1332  case dpct::detail::get_type_combination_id(
1333      library_data_t::real_half, library_data_t::real_half,
1334      library_data_t::real_half, library_data_t::real_half): {
1335    dpct::detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
1336                                  sycl::half>(
1337        q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
1338        beta, c, ldc, stride_c, batch_size DPCT_COMPUTE_MODE_ARG);
1339    break;
1340  }
1341#ifdef __INTEL_MKL__
1342  case dpct::detail::get_type_combination_id(
1343      library_data_t::real_bfloat16, library_data_t::real_bfloat16,
1344      library_data_t::real_bfloat16, library_data_t::real_float): {
1345    dpct::detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
1346                                  oneapi::mkl::bfloat16, float>(
1347        q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
1348        beta, c, ldc, stride_c, batch_size, cm);
1349    break;
1350  }
1351  case dpct::detail::get_type_combination_id(
1352      library_data_t::real_bfloat16, library_data_t::real_bfloat16,
1353      library_data_t::real_float, library_data_t::real_float): {
1354    dpct::detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
1355                                  float, float>(
1356        q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
1357        beta, c, ldc, stride_c, batch_size, cm);
1358    break;
1359  }
1360#endif
1361  case dpct::detail::get_type_combination_id(
1362      library_data_t::real_int8, library_data_t::real_int8,
1363      library_data_t::real_int32, library_data_t::real_int32): {
1364    float alpha_float =
1365        dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
1366    float beta_float =
1367        dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
1368    dpct::detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
1369                                  float>(
1370        q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, stride_a, b, ldb,
1371        stride_b, &beta_float, c, ldc, stride_c,
1372        batch_size DPCT_COMPUTE_MODE_ARG);
1373    break;
1374  }
1375  case dpct::detail::get_type_combination_id(
1376      library_data_t::real_int8, library_data_t::real_int8,
1377      library_data_t::real_float, library_data_t::real_float): {
1378    dpct::detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
1379        q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
1380        beta, c, ldc, stride_c, batch_size DPCT_COMPUTE_MODE_ARG);
1381    break;
1382  }
1383  case dpct::detail::get_type_combination_id(
1384      library_data_t::real_half, library_data_t::real_half,
1385      library_data_t::real_float, library_data_t::real_float): {
1386    dpct::detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
1387        q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
1388        beta, c, ldc, stride_c, batch_size DPCT_COMPUTE_MODE_ARG);
1389    break;
1390  }
1391  case dpct::detail::get_type_combination_id(
1392      library_data_t::real_half, library_data_t::real_half,
1393      library_data_t::real_half, library_data_t::real_float): {
1394    float alpha_value =
1395        dpct::get_value(reinterpret_cast<const float *>(alpha), q);
1396    float beta_value =
1397        dpct::get_value(reinterpret_cast<const float *>(beta), q);
1398    sycl::half alpha_half(alpha_value);
1399    sycl::half beta_half(beta_value);
1400    dpct::detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
1401                                  sycl::half>(
1402        q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, stride_a, b, ldb,
1403        stride_b, &beta_half, c, ldc, stride_c, batch_size DPCT_COMPUTE_MODE_ARG);
1404    break;
1405  }
1406  default:
1407    throw std::runtime_error("the combination of data type is unsupported");
1408  }
1409}
1410
1411/// Performs a symmetric/hermitian rank-k update.
1412/// \tparam is_hermitian True means current matrix is hermitian.
1413/// \param [in] desc_ptr Descriptor.
1414/// \param [in] uplo Specifies whether matrix c is upper or lower triangular.
1415/// \param [in] trans Specifies op(a), the transposition operation applied to
1416/// matrix a.
1417/// \param [in] n Number of rows and columns of matrix c.
1418/// \param [in] k Number of columns of matrix op(a).
1419/// \param [in] alpha Scaling factor for the rank-k update.
1420/// \param [in] a Input matrix a.
1421/// \param [in] a_type Data type of the matrix a.
1422/// \param [in] lda Leading dimension of the matrix a.
1423/// \param [in] beta Scaling factor for the rank-k update.
1424/// \param [in, out] c Input/Output matrix c.
1425/// \param [in] c_type Data type of the matrix c.
1426/// \param [in] ldc Leading dimension of the matrix c.
1427/// \param [in] ct Compute type.
1428template <bool is_hermitian>
1429inline void syherk(descriptor_ptr desc_ptr, oneapi::mkl::uplo uplo,
1430                   oneapi::mkl::transpose trans, std::int64_t n, std::int64_t k,
1431                   const void *alpha, const void *a, library_data_t a_type,
1432                   std::int64_t lda, const void *beta, void *c,
1433                   library_data_t c_type, std::int64_t ldc,
1434                   std::variant<compute_type, library_data_t> ct) {
1435  sycl::queue q = desc_ptr->get_queue();
1436#ifdef __INTEL_MKL__
1437  oneapi::mkl::blas::compute_mode cm = oneapi::mkl::blas::compute_mode::unset;
1438  if (auto ct_p = std::get_if<compute_type>(&ct)) {
1439    cm = deduce_compute_mode(*ct_p, desc_ptr->get_math_mode(),
1440                             a_type == library_data_t::complex_float ||
1441                                 a_type == library_data_t::complex_double);
1442  } else {
1443    cm = deduce_compute_mode(std::nullopt, desc_ptr->get_math_mode(),
1444                             a_type == library_data_t::complex_float ||
1445                                 a_type == library_data_t::complex_double);
1446  }
1447#endif
1448  std::uint64_t key = dpct::detail::get_type_combination_id(a_type, c_type);
1449  if (!is_hermitian &&
1450      dpct::detail::get_type_combination_id(
1451          library_data_t::real_float, library_data_t::real_float) == key) {
1452    dpct::detail::syherk_impl<false, float>(q, uplo, trans, n, k, alpha, a, lda,
1453                                            beta, c, ldc DPCT_COMPUTE_MODE_ARG);
1454  } else if (!is_hermitian && dpct::detail::get_type_combination_id(
1455                                  library_data_t::real_double,
1456                                  library_data_t::real_double) == key) {
1457    dpct::detail::syherk_impl<false, double>(q, uplo, trans, n, k, alpha, a,
1458                                             lda, beta, c,
1459                                             ldc DPCT_COMPUTE_MODE_ARG);
1460  } else if (dpct::detail::get_type_combination_id(
1461                 library_data_t::complex_float,
1462                 library_data_t::complex_float) == key) {
1463    dpct::detail::syherk_impl<is_hermitian, std::complex<float>>(
1464        q, uplo, trans, n, k, alpha, a, lda, beta, c,
1465        ldc DPCT_COMPUTE_MODE_ARG);
1466  } else if (dpct::detail::get_type_combination_id(
1467                 library_data_t::complex_double,
1468                 library_data_t::complex_double) == key) {
1469    dpct::detail::syherk_impl<is_hermitian, std::complex<double>>(
1470        q, uplo, trans, n, k, alpha, a, lda, beta, c,
1471        ldc DPCT_COMPUTE_MODE_ARG);
1472  } else {
1473    throw std::runtime_error("the combination of data type is unsupported");
1474  }
1475}
1476
1477/// This routines perform a special rank-k update of a symmetric matrix C by
1478/// general matrices A and B.
1479/// \param [in] desc_ptr Descriptor.
1480/// \param [in] uplo Specifies whether C's data is stored in its upper or lower triangle.
1481/// \param [in] trans Specifies the operation to apply.
1482/// \param [in] n The number of rows and columns in C.
1483/// \param [in] k The inner dimension of matrix multiplications.
1484/// \param [in] alpha Scaling factor for the rank-k update.
1485/// \param [in] a Input matrix A.
1486/// \param [in] lda Leading dimension of A.
1487/// \param [in] b Input matrix B.
1488/// \param [in] ldb Leading dimension of B.
1489/// \param [in] beta Scaling factor for the rank-k update.
1490/// \param [in, out] c Input/Output matrix C.
1491/// \param [in] ldc Leading dimension of C.
1492template <class T>
1493inline void syrk(descriptor_ptr desc_ptr, oneapi::mkl::uplo uplo,
1494                 oneapi::mkl::transpose trans, std::int64_t n, std::int64_t k,
1495                 const T *alpha, const T *a, std::int64_t lda, const T *b,
1496                 std::int64_t ldb, const T *beta, T *c, std::int64_t ldc) {
1497  sycl::queue q = desc_ptr->get_queue();
1498#ifdef __INTEL_MKL__
1499  auto cm = deduce_compute_mode<T>(std::nullopt, desc_ptr->get_math_mode());
1500#endif
1501  dpct::detail::rk_impl<false, T, T>(q, uplo, trans, n, k, alpha, a, lda, b,
1502                                     ldb, beta, c, ldc DPCT_COMPUTE_MODE_ARG);
1503}
1504
1505/// This routines perform a special rank-k update of a Hermitian matrix C by
1506/// general matrices A and B.
1507/// \param [in] desc_ptr Descriptor.
1508/// \param [in] uplo Specifies whether C's data is stored in its upper or lower triangle.
1509/// \param [in] trans Specifies the operation to apply.
1510/// \param [in] n The number of rows and columns in C.
1511/// \param [in] k The inner dimension of matrix multiplications.
1512/// \param [in] alpha Scaling factor for the rank-k update.
1513/// \param [in] a Input matrix A.
1514/// \param [in] lda Leading dimension of A.
1515/// \param [in] b Input matrix B.
1516/// \param [in] ldb Leading dimension of B.
1517/// \param [in] beta Scaling factor for the rank-k update.
1518/// \param [in, out] c Input/Output matrix C.
1519/// \param [in] ldc Leading dimension of C.
1520template <class T, class Tbeta>
1521inline void herk(descriptor_ptr desc_ptr, oneapi::mkl::uplo uplo,
1522                 oneapi::mkl::transpose trans, std::int64_t n, std::int64_t k,
1523                 const T *alpha, const T *a, std::int64_t lda, const T *b,
1524                 std::int64_t ldb, const Tbeta *beta, T *c, std::int64_t ldc) {
1525  sycl::queue q = desc_ptr->get_queue();
1526#ifdef __INTEL_MKL__
1527  auto cm = deduce_compute_mode<T>(std::nullopt, desc_ptr->get_math_mode());
1528#endif
1529  dpct::detail::rk_impl<true, T, Tbeta>(q, uplo, trans, n, k, alpha, a, lda, b,
1530                                        ldb, beta, c,
1531                                        ldc DPCT_COMPUTE_MODE_ARG);
1532}
1533
1534/// This routine performs a group of trsm operations. Each trsm solves an
1535/// equation of the form op(A) * X = alpha * B or X * op(A) = alpha * B.
1536/// \param [in] desc_ptr Descriptor.
1537/// \param [in] left_right Specifies A multiplies X on the left or on the right.
1538/// \param [in] upper_lower Specifies A is upper or lower triangular.
1539/// \param [in] trans Specifies the operation applied to A.
1540/// \param [in] unit_diag Specifies whether A is unit triangular.
1541/// \param [in] m Number of rows of the B matrices.
1542/// \param [in] n Number of columns of the B matrices.
1543/// \param [in] alpha Scaling factor for the solutions.
1544/// \param [in] a Input matrices A.
1545/// \param [in] a_type Data type of the matrices A.
1546/// \param [in] lda Leading dimension of the matrices A.
1547/// \param [in, out] b Input and output matrices B.
1548/// \param [in] b_type Data type of the matrices B.
1549/// \param [in] ldb Leading dimension of the matrices B.
1550/// \param [in] batch_size Specifies the number of trsm operations to perform.
1551/// \param [in] ct Compute type.
1552inline void trsm_batch(descriptor_ptr desc_ptr, oneapi::mkl::side left_right,
1553                       oneapi::mkl::uplo upper_lower,
1554                       oneapi::mkl::transpose trans,
1555                       oneapi::mkl::diag unit_diag, std::int64_t m,
1556                       std::int64_t n, const void *alpha, const void **a,
1557                       library_data_t a_type, std::int64_t lda, void **b,
1558                       library_data_t b_type, std::int64_t ldb,
1559                       std::int64_t batch_size,
1560                       std::variant<compute_type, library_data_t> ct) {
1561#ifdef DPCT_USM_LEVEL_NONE
1562  throw std::runtime_error("this API is unsupported when USM level is none");
1563#else
1564  sycl::queue q = desc_ptr->get_queue();
1565#ifdef __INTEL_MKL__
1566  oneapi::mkl::blas::compute_mode cm = oneapi::mkl::blas::compute_mode::unset;
1567  if (auto ct_p = std::get_if<compute_type>(&ct))
1568    cm = deduce_compute_mode(*ct_p, desc_ptr->get_math_mode(),
1569                             a_type == library_data_t::complex_float ||
1570                                 a_type == library_data_t::complex_double);
1571  else
1572    cm = deduce_compute_mode(std::nullopt, desc_ptr->get_math_mode(),
1573                             a_type == library_data_t::complex_float ||
1574                                 a_type == library_data_t::complex_double);
1575#endif
1576  std::uint64_t key = dpct::detail::get_type_combination_id(a_type, b_type);
1577  switch (key) {
1578  case dpct::detail::get_type_combination_id(library_data_t::real_float,
1579                                             library_data_t::real_float): {
1580    dpct::detail::trsm_batch_impl<float, float, float>(
1581        q, left_right, upper_lower, trans, unit_diag, m, n, alpha, a, lda, b,
1582        ldb, batch_size DPCT_COMPUTE_MODE_ARG);
1583    break;
1584  }
1585  case dpct::detail::get_type_combination_id(library_data_t::real_double,
1586                                             library_data_t::real_double): {
1587    dpct::detail::trsm_batch_impl<double, double, double>(
1588        q, left_right, upper_lower, trans, unit_diag, m, n, alpha, a, lda, b,
1589        ldb, batch_size DPCT_COMPUTE_MODE_ARG);
1590    break;
1591  }
1592  case dpct::detail::get_type_combination_id(library_data_t::complex_float,
1593                                             library_data_t::complex_float): {
1594    dpct::detail::trsm_batch_impl<std::complex<float>, std::complex<float>,
1595                                  std::complex<float>>(
1596        q, left_right, upper_lower, trans, unit_diag, m, n, alpha, a, lda, b,
1597        ldb, batch_size DPCT_COMPUTE_MODE_ARG);
1598    break;
1599  }
1600  case dpct::detail::get_type_combination_id(library_data_t::complex_double,
1601                                             library_data_t::complex_double): {
1602    dpct::detail::trsm_batch_impl<std::complex<double>, std::complex<double>,
1603                                  std::complex<double>>(
1604        q, left_right, upper_lower, trans, unit_diag, m, n, alpha, a, lda, b,
1605        ldb, batch_size DPCT_COMPUTE_MODE_ARG);
1606    break;
1607  }
1608  default:
1609    throw std::runtime_error("the combination of data type is unsupported");
1610  }
1611#endif
1612}
1613
1614/// Computes a triangular matrix-general matrix product.
1615/// \param [in] desc_ptr Descriptor.
1616/// \param [in] left_right Specifies A is on the left or right side of the
1617/// multiplication.
1618/// \param [in] upper_lower Specifies A is upper or lower triangular.
1619/// \param [in] trans Specifies the operation applied to A.
1620/// \param [in] unit_diag Specifies whether A is unit triangular.
1621/// \param [in] m Number of rows of B.
1622/// \param [in] n Number of columns of B.
1623/// \param [in] alpha Scaling factor for the matrix-matrix product.
1624/// \param [in] a Input matrices A.
1625/// \param [in] lda Leading dimension of the matrices A.
1626/// \param [in] b Input matrices B.
1627/// \param [in] ldb Leading dimension of the matrices B.
1628/// \param [out] c Output matrices C.
1629/// \param [in] ldc Leading dimension of the matrices C.
1630template <class T>
1631inline void trmm(descriptor_ptr desc_ptr, oneapi::mkl::side left_right,
1632                 oneapi::mkl::uplo upper_lower, oneapi::mkl::transpose trans,
1633                 oneapi::mkl::diag unit_diag, std::int64_t m, std::int64_t n,
1634                 const T *alpha, const T *a, std::int64_t lda, const T *b,
1635                 std::int64_t ldb, T *c, std::int64_t ldc) {
1636  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
1637  sycl::queue q = desc_ptr->get_queue();
1638#ifdef __INTEL_MKL__
1639  auto cm = deduce_compute_mode<Ty>(std::nullopt, desc_ptr->get_math_mode());
1640#endif
1641  auto alpha_val = dpct::get_value(alpha, q);
1642  if (b != c) {
1643    dpct::blas::matrix_mem_copy(c, b, ldc, ldb, m, n, sizeof(Ty),
1644                                ::dpct::cs::memcpy_direction::device_to_device,
1645                                q);
1646  }
1647  auto data_a = dpct::detail::get_memory<const Ty>(a);
1648  auto data_c = dpct::detail::get_memory<Ty>(c);
1649  oneapi::mkl::blas::column_major::trmm(q, left_right, upper_lower, trans,
1650                                        unit_diag, m, n, alpha_val, data_a, lda,
1651                                        data_c, ldc DPCT_COMPUTE_MODE_ARG);
1652}
1653
1654/// Computes the Euclidean norm of a vector.
1655/// \param [in] desc_ptr Descriptor.
1656/// \param [in] n Number of elements in vector x.
1657/// \param [in] x Input vector x.
1658/// \param [in] x_type Data type of the vector x.
1659/// \param [in] incx Stride of vector x.
1660/// \param [out] result The result scalar.
1661/// \param [in] result_type Data type of the result.
1662inline void nrm2(descriptor_ptr desc_ptr, std::int64_t n, const void *x,
1663                 library_data_t x_type, std::int64_t incx, void *result,
1664                 library_data_t result_type) {
1665  sycl::queue q = desc_ptr->get_queue();
1666  std::uint64_t key =
1667      ::dpct::detail::get_type_combination_id(x_type, result_type);
1668  switch (key) {
1669  case ::dpct::detail::get_type_combination_id(library_data_t::real_float,
1670                                               library_data_t::real_float): {
1671    ::dpct::detail::nrm2_impl<float, float>(q, n, x, incx, result);
1672    break;
1673  }
1674  case ::dpct::detail::get_type_combination_id(library_data_t::real_double,
1675                                               library_data_t::real_double): {
1676    ::dpct::detail::nrm2_impl<double, double>(q, n, x, incx, result);
1677    break;
1678  }
1679  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float,
1680                                               library_data_t::real_float): {
1681    ::dpct::detail::nrm2_impl<std::complex<float>, float>(q, n, x, incx,
1682                                                          result);
1683    break;
1684  }
1685  case ::dpct::detail::get_type_combination_id(library_data_t::complex_double,
1686                                               library_data_t::real_double): {
1687    ::dpct::detail::nrm2_impl<std::complex<double>, double>(q, n, x, incx,
1688                                                            result);
1689    break;
1690  }
1691#ifdef __INTEL_MKL__
1692  case ::dpct::detail::get_type_combination_id(library_data_t::real_half,
1693                                               library_data_t::real_half): {
1694    ::dpct::detail::nrm2_impl<sycl::half, sycl::half>(q, n, x, incx, result);
1695    break;
1696  }
1697#endif
1698  default:
1699    throw std::runtime_error("the combination of data type is unsupported");
1700  }
1701}
1702
1703/// Computes the dot product of two vectors.
1704/// \param [in] desc_ptr Descriptor.
1705/// \param [in] n Number of elements in vector x.
1706/// \param [in] x Input vector x.
1707/// \param [in] x_type Data type of the vector x.
1708/// \param [in] incx Stride of vector x.
1709/// \param [in] y Input vector y.
1710/// \param [in] y_type Data type of the vector y.
1711/// \param [in] incy Stride of vector y.
1712/// \param [out] result The result scalar.
1713/// \param [in] result_type Data type of the result.
1714inline void dot(descriptor_ptr desc_ptr, std::int64_t n, const void *x,
1715                library_data_t x_type, std::int64_t incx, const void *y,
1716                library_data_t y_type, std::int64_t incy, void *result,
1717                library_data_t result_type) {
1718  sycl::queue q = desc_ptr->get_queue();
1719  ::dpct::detail::dotuc<false>(q, n, x, x_type, incx, y, y_type, incy, result,
1720                               result_type);
1721}
1722
1723/// Computes the dot product of two vectors, conjugating the first vector.
1724/// \param [in] desc_ptr Descriptor.
1725/// \param [in] n Number of elements in vector x.
1726/// \param [in] x Input vector x.
1727/// \param [in] x_type Data type of the vector x.
1728/// \param [in] incx Stride of vector x.
1729/// \param [in] y Input vector y.
1730/// \param [in] y_type Data type of the vector y.
1731/// \param [in] incy Stride of vector y.
1732/// \param [out] result The result scalar.
1733/// \param [in] result_type Data type of the result.
1734inline void dotc(descriptor_ptr desc_ptr, std::int64_t n, const void *x,
1735                 library_data_t x_type, std::int64_t incx, const void *y,
1736                 library_data_t y_type, std::int64_t incy, void *result,
1737                 library_data_t result_type) {
1738  sycl::queue q = desc_ptr->get_queue();
1739  ::dpct::detail::dotuc<true>(q, n, x, x_type, incx, y, y_type, incy, result,
1740                              result_type);
1741}
1742
1743/// Computes the product of a vector by a scalar.
1744/// \param [in] desc_ptr Descriptor.
1745/// \param [in] n Number of elements in vector x.
1746/// \param [in] alpha The scale factor alpha.
1747/// \param [in] alpha_type The data type of alpha.
1748/// \param [in, out] x Input/Output vector x.
1749/// \param [in] x_type Data type of the vector x.
1750/// \param [in] incx Stride of vector x.
1751inline void scal(descriptor_ptr desc_ptr, std::int64_t n, const void *alpha,
1752                 library_data_t alpha_type, void *x, library_data_t x_type,
1753                 std::int64_t incx) {
1754  sycl::queue q = desc_ptr->get_queue();
1755  std::uint64_t key = ::dpct::detail::get_type_combination_id(x_type);
1756  switch (key) {
1757  case ::dpct::detail::get_type_combination_id(library_data_t::real_float): {
1758    ::dpct::detail::scal_impl<float, float>(q, n, alpha, x, incx);
1759    break;
1760  }
1761  case ::dpct::detail::get_type_combination_id(library_data_t::real_double): {
1762    ::dpct::detail::scal_impl<double, double>(q, n, alpha, x, incx);
1763    break;
1764  }
1765  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float): {
1766    ::dpct::detail::scal_impl<std::complex<float>, std::complex<float>>(
1767        q, n, alpha, x, incx);
1768    break;
1769  }
1770  case ::dpct::detail::get_type_combination_id(
1771      library_data_t::complex_double): {
1772    ::dpct::detail::scal_impl<std::complex<double>, std::complex<double>>(
1773        q, n, alpha, x, incx);
1774    break;
1775  }
1776#ifdef __INTEL_MKL__
1777  case ::dpct::detail::get_type_combination_id(library_data_t::real_half): {
1778    float alpha_value =
1779        dpct::get_value(reinterpret_cast<const float *>(alpha), q);
1780    sycl::half alaph_half(alpha_value);
1781    ::dpct::detail::scal_impl<sycl::half, sycl::half>(q, n, &alaph_half, x,
1782                                                      incx);
1783    break;
1784  }
1785#endif
1786  default:
1787    throw std::runtime_error("the combination of data type is unsupported");
1788  }
1789}
1790
1791/// Computes a vector-scalar product and adds the result to a vector.
1792/// \param [in] desc_ptr Descriptor.
1793/// \param [in] n Number of elements in vector x.
1794/// \param [in] alpha The scale factor alpha.
1795/// \param [in] alpha_type The data type of alpha.
1796/// \param [in] x Input vector x.
1797/// \param [in] x_type Data type of the vector x.
1798/// \param [in] incx Stride of vector x.
1799/// \param [in, out] y Input/Output vector y.
1800/// \param [in] y_type Data type of the vector y.
1801/// \param [in] incy Stride of vector y.
1802inline void axpy(descriptor_ptr desc_ptr, std::int64_t n, const void *alpha,
1803                 library_data_t alpha_type, const void *x,
1804                 library_data_t x_type, std::int64_t incx, void *y,
1805                 library_data_t y_type, std::int64_t incy) {
1806  sycl::queue q = desc_ptr->get_queue();
1807  std::uint64_t key =
1808      ::dpct::detail::get_type_combination_id(x_type, alpha_type);
1809  switch (key) {
1810  case ::dpct::detail::get_type_combination_id(library_data_t::real_float,
1811                                               library_data_t::real_float): {
1812    ::dpct::detail::axpy_impl<float, float>(q, n, alpha, x, incx, y, incy);
1813    break;
1814  }
1815  case ::dpct::detail::get_type_combination_id(library_data_t::real_double,
1816                                               library_data_t::real_double): {
1817    ::dpct::detail::axpy_impl<double, double>(q, n, alpha, x, incx, y, incy);
1818    break;
1819  }
1820  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float,
1821                                               library_data_t::complex_float): {
1822    ::dpct::detail::axpy_impl<std::complex<float>, std::complex<float>>(
1823        q, n, alpha, x, incx, y, incy);
1824    break;
1825  }
1826  case ::dpct::detail::get_type_combination_id(
1827      library_data_t::complex_double, library_data_t::complex_double): {
1828    ::dpct::detail::axpy_impl<std::complex<double>, std::complex<double>>(
1829        q, n, alpha, x, incx, y, incy);
1830    break;
1831  }
1832#ifdef __INTEL_MKL__
1833  case ::dpct::detail::get_type_combination_id(library_data_t::real_half,
1834                                               library_data_t::real_float): {
1835    float alpha_value =
1836        dpct::get_value(reinterpret_cast<const float *>(alpha), q);
1837    sycl::half alaph_half(alpha_value);
1838    ::dpct::detail::axpy_impl<sycl::half, sycl::half>(q, n, &alaph_half, x,
1839                                                      incx, y, incy);
1840    break;
1841  }
1842#endif
1843  default:
1844    throw std::runtime_error("the combination of data type is unsupported");
1845  }
1846}
1847
1848/// Performs rotation of points in the plane.
1849/// \param [in] desc_ptr Descriptor.
1850/// \param [in] n Number of elements in vector x.
1851/// \param [in, out] x Input/Output vector x.
1852/// \param [in] x_type Data type of the vector x.
1853/// \param [in] incx Stride of vector x.
1854/// \param [in, out] y Input/Output vector y.
1855/// \param [in] y_type Data type of the vector y.
1856/// \param [in] incy Stride of vector y.
1857/// \param [in] c Scaling factor.
1858/// \param [in] s Scaling factor.
1859/// \param [in] cs_type Data type of the scaling factors.
1860inline void rot(descriptor_ptr desc_ptr, std::int64_t n, void *x,
1861                library_data_t x_type, std::int64_t incx, void *y,
1862                library_data_t y_type, std::int64_t incy, const void *c,
1863                const void *s, library_data_t cs_type) {
1864  sycl::queue q = desc_ptr->get_queue();
1865  std::uint64_t key = ::dpct::detail::get_type_combination_id(x_type, cs_type);
1866  switch (key) {
1867  case ::dpct::detail::get_type_combination_id(library_data_t::real_float,
1868                                               library_data_t::real_float): {
1869    ::dpct::detail::rot_impl<float, float, float>(q, n, x, incx, y, incy, c, s);
1870    break;
1871  }
1872  case ::dpct::detail::get_type_combination_id(library_data_t::real_double,
1873                                               library_data_t::real_double): {
1874    ::dpct::detail::rot_impl<double, double, double>(q, n, x, incx, y, incy, c,
1875                                                     s);
1876    break;
1877  }
1878  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float,
1879                                               library_data_t::real_float): {
1880    ::dpct::detail::rot_impl<std::complex<float>, float, float>(q, n, x, incx,
1881                                                                y, incy, c, s);
1882    break;
1883  }
1884  case ::dpct::detail::get_type_combination_id(library_data_t::complex_double,
1885                                               library_data_t::real_double): {
1886    ::dpct::detail::rot_impl<std::complex<double>, double, double>(
1887        q, n, x, incx, y, incy, c, s);
1888    break;
1889  }
1890#ifdef __INTEL_MKL__
1891  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float,
1892                                               library_data_t::complex_float): {
1893    ::dpct::detail::rot_impl<std::complex<float>, float, std::complex<float>>(
1894        q, n, x, incx, y, incy, c, s);
1895    break;
1896  }
1897  case ::dpct::detail::get_type_combination_id(
1898      library_data_t::complex_double, library_data_t::complex_double): {
1899    ::dpct::detail::rot_impl<std::complex<double>, double,
1900                             std::complex<double>>(q, n, x, incx, y, incy, c,
1901                                                   s);
1902    break;
1903  }
1904  case ::dpct::detail::get_type_combination_id(library_data_t::real_half,
1905                                               library_data_t::real_half): {
1906    ::dpct::detail::rot_impl<sycl::half, sycl::half, sycl::half>(q, n, x, incx,
1907                                                                 y, incy, c, s);
1908    break;
1909  }
1910  case ::dpct::detail::get_type_combination_id(library_data_t::real_bfloat16,
1911                                               library_data_t::real_bfloat16): {
1912    ::dpct::detail::rot_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
1913                             oneapi::mkl::bfloat16>(q, n, x, incx, y, incy, c,
1914                                                    s);
1915    break;
1916  }
1917#endif
1918  default:
1919    throw std::runtime_error("the combination of data type is unsupported");
1920  }
1921}
1922
1923/// Performs modified Givens rotation of points in the plane.
1924/// \param [in] desc_ptr Descriptor.
1925/// \param [in] n Number of elements in vector x.
1926/// \param [in, out] x Input/Output vector x.
1927/// \param [in] x_type Data type of the vector x.
1928/// \param [in] incx Stride of vector x.
1929/// \param [in, out] y Input/Output vector y.
1930/// \param [in] y_type Data type of the vector y.
1931/// \param [in] incy Stride of vector y.
1932/// \param [in] param Array of 5 parameters.
1933/// \param [in] param_type Data type of \p param.
1934inline void rotm(descriptor_ptr desc_ptr, std::int64_t n, void *x,
1935                 library_data_t x_type, int64_t incx, void *y,
1936                 library_data_t y_type, int64_t incy, const void *param,
1937                 library_data_t param_type) {
1938  sycl::queue q = desc_ptr->get_queue();
1939  std::uint64_t key =
1940      ::dpct::detail::get_type_combination_id(x_type, param_type);
1941  switch (key) {
1942  case ::dpct::detail::get_type_combination_id(library_data_t::real_float,
1943                                               library_data_t::real_float,
1944                                               library_data_t::real_float): {
1945    ::dpct::detail::rotm_impl<float, float, float>(q, n, x, incx, y, incy,
1946                                                   param);
1947    break;
1948  }
1949  case ::dpct::detail::get_type_combination_id(library_data_t::real_double,
1950                                               library_data_t::real_double,
1951                                               library_data_t::real_double): {
1952    ::dpct::detail::rotm_impl<double, double, double>(q, n, x, incx, y, incy,
1953                                                      param);
1954    break;
1955  }
1956  default:
1957    throw std::runtime_error("the combination of data type is unsupported");
1958  }
1959}
1960
1961/// Copies a vector to another vector.
1962/// \param [in] desc_ptr Descriptor.
1963/// \param [in] n Number of elements in vector x.
1964/// \param [in] x Input vector x.
1965/// \param [in] x_type Data type of the vector x.
1966/// \param [in] incx Stride of vector x.
1967/// \param [out] y Output vector y.
1968/// \param [in] y_type Data type of the vector y.
1969/// \param [in] incy Stride of vector y.
1970inline void copy(descriptor_ptr desc_ptr, std::int64_t n, const void *x,
1971                 library_data_t x_type, std::int64_t incx, void *y,
1972                 library_data_t y_type, std::int64_t incy) {
1973  sycl::queue q = desc_ptr->get_queue();
1974  std::uint64_t key = ::dpct::detail::get_type_combination_id(x_type, y_type);
1975  switch (key) {
1976  case ::dpct::detail::get_type_combination_id(library_data_t::real_float,
1977                                               library_data_t::real_float): {
1978    ::dpct::detail::copy_impl<float, float>(q, n, x, incx, y, incy);
1979    break;
1980  }
1981  case ::dpct::detail::get_type_combination_id(library_data_t::real_double,
1982                                               library_data_t::real_double): {
1983    ::dpct::detail::copy_impl<double, double>(q, n, x, incx, y, incy);
1984    break;
1985  }
1986  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float,
1987                                               library_data_t::complex_float): {
1988    ::dpct::detail::copy_impl<std::complex<float>, std::complex<float>>(
1989        q, n, x, incx, y, incy);
1990    break;
1991  }
1992  case ::dpct::detail::get_type_combination_id(
1993      library_data_t::complex_double, library_data_t::complex_double): {
1994    ::dpct::detail::copy_impl<std::complex<double>, std::complex<double>>(
1995        q, n, x, incx, y, incy);
1996    break;
1997  }
1998  default:
1999    throw std::runtime_error("the combination of data type is unsupported");
2000  }
2001}
2002
2003/// Swaps a vector with another vector.
2004/// \param [in] desc_ptr Descriptor.
2005/// \param [in] n Number of elements in vector x.
2006/// \param [in, out] x Input/Output vector x.
2007/// \param [in] x_type Data type of the vector x.
2008/// \param [in] incx Stride of vector x.
2009/// \param [in, out] y Input/Output vector y.
2010/// \param [in] y_type Data type of the vector y.
2011/// \param [in] incy Stride of vector y.
2012inline void swap(descriptor_ptr desc_ptr, std::int64_t n, void *x,
2013                 library_data_t x_type, std::int64_t incx, void *y,
2014                 library_data_t y_type, std::int64_t incy) {
2015  sycl::queue q = desc_ptr->get_queue();
2016  std::uint64_t key = ::dpct::detail::get_type_combination_id(x_type, y_type);
2017  switch (key) {
2018  case ::dpct::detail::get_type_combination_id(library_data_t::real_float,
2019                                               library_data_t::real_float): {
2020    ::dpct::detail::swap_impl<float, float>(q, n, x, incx, y, incy);
2021    break;
2022  }
2023  case ::dpct::detail::get_type_combination_id(library_data_t::real_double,
2024                                               library_data_t::real_double): {
2025    ::dpct::detail::swap_impl<double, double>(q, n, x, incx, y, incy);
2026    break;
2027  }
2028  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float,
2029                                               library_data_t::complex_float): {
2030    ::dpct::detail::swap_impl<std::complex<float>, std::complex<float>>(
2031        q, n, x, incx, y, incy);
2032    break;
2033  }
2034  case ::dpct::detail::get_type_combination_id(
2035      library_data_t::complex_double, library_data_t::complex_double): {
2036    ::dpct::detail::swap_impl<std::complex<double>, std::complex<double>>(
2037        q, n, x, incx, y, incy);
2038    break;
2039  }
2040  default:
2041    throw std::runtime_error("the combination of data type is unsupported");
2042  }
2043}
2044
2045/// Computes the sum of magnitudes of the vector elements.
2046/// \param [in] desc_ptr Descriptor.
2047/// \param [in] n Number of elements in vector x.
2048/// \param [in] x Input vector x.
2049/// \param [in] x_type Data type of the vector x.
2050/// \param [in] incx Stride of vector x.
2051/// \param [out] result The scalar result.
2052/// \param [in] result_type Data type of \p result.
2053inline void asum(descriptor_ptr desc_ptr, std::int64_t n, const void *x,
2054                 library_data_t x_type, std::int64_t incx, void *result,
2055                 library_data_t result_type) {
2056  sycl::queue q = desc_ptr->get_queue();
2057  std::uint64_t key =
2058      ::dpct::detail::get_type_combination_id(x_type, result_type);
2059  switch (key) {
2060  case ::dpct::detail::get_type_combination_id(library_data_t::real_float,
2061                                               library_data_t::real_float): {
2062    ::dpct::detail::asum_impl<float, float>(q, n, x, incx, result);
2063    break;
2064  }
2065  case ::dpct::detail::get_type_combination_id(library_data_t::real_double,
2066                                               library_data_t::real_double): {
2067    ::dpct::detail::asum_impl<double, double>(q, n, x, incx, result);
2068    break;
2069  }
2070  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float,
2071                                               library_data_t::real_float): {
2072    ::dpct::detail::asum_impl<std::complex<float>, float>(q, n, x, incx,
2073                                                          result);
2074    break;
2075  }
2076  case ::dpct::detail::get_type_combination_id(library_data_t::complex_double,
2077                                               library_data_t::real_double): {
2078    ::dpct::detail::asum_impl<std::complex<double>, double>(q, n, x, incx,
2079                                                            result);
2080    break;
2081  }
2082  default:
2083    throw std::runtime_error("the combination of data type is unsupported");
2084  }
2085}
2086
2087/// Finds the index of the element with the largest absolute value in a vector.
2088/// \param [in] desc_ptr Descriptor.
2089/// \param [in] n Number of elements in vector x.
2090/// \param [in] x Input vector x.
2091/// \param [in] x_type Data type of the vector x.
2092/// \param [in] incx Stride of vector x.
2093/// \param [out] result The index of the maximal element.
2094inline void iamax(descriptor_ptr desc_ptr, std::int64_t n, const void *x,
2095                  library_data_t x_type, std::int64_t incx,
2096                  std::int64_t *result) {
2097  sycl::queue q = desc_ptr->get_queue();
2098  std::uint64_t key = ::dpct::detail::get_type_combination_id(x_type);
2099  switch (key) {
2100  case ::dpct::detail::get_type_combination_id(library_data_t::real_float): {
2101    ::dpct::detail::iamax_impl<float>(q, n, x, incx, result);
2102    break;
2103  }
2104  case ::dpct::detail::get_type_combination_id(library_data_t::real_double): {
2105    ::dpct::detail::iamax_impl<double>(q, n, x, incx, result);
2106    break;
2107  }
2108  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float): {
2109    ::dpct::detail::iamax_impl<std::complex<float>>(q, n, x, incx, result);
2110    break;
2111  }
2112  case ::dpct::detail::get_type_combination_id(
2113      library_data_t::complex_double): {
2114    ::dpct::detail::iamax_impl<std::complex<double>>(q, n, x, incx, result);
2115    break;
2116  }
2117  default:
2118    throw std::runtime_error("the combination of data type is unsupported");
2119  }
2120}
2121
2122/// Finds the index of the element with the smallest absolute value in a vector.
2123/// \param [in] desc_ptr Descriptor.
2124/// \param [in] n Number of elements in vector x.
2125/// \param [in] x Input vector x.
2126/// \param [in] x_type Data type of the vector x.
2127/// \param [in] incx Stride of vector x.
2128/// \param [out] result The index of the minimum element.
2129inline void iamin(descriptor_ptr desc_ptr, std::int64_t n, const void *x,
2130                  library_data_t x_type, std::int64_t incx,
2131                  std::int64_t *result) {
2132  sycl::queue q = desc_ptr->get_queue();
2133  std::uint64_t key = ::dpct::detail::get_type_combination_id(x_type);
2134  switch (key) {
2135  case ::dpct::detail::get_type_combination_id(library_data_t::real_float): {
2136    ::dpct::detail::iamin_impl<float>(q, n, x, incx, result);
2137    break;
2138  }
2139  case ::dpct::detail::get_type_combination_id(library_data_t::real_double): {
2140    ::dpct::detail::iamin_impl<double>(q, n, x, incx, result);
2141    break;
2142  }
2143  case ::dpct::detail::get_type_combination_id(library_data_t::complex_float): {
2144    ::dpct::detail::iamin_impl<std::complex<float>>(q, n, x, incx, result);
2145    break;
2146  }
2147  case ::dpct::detail::get_type_combination_id(
2148      library_data_t::complex_double): {
2149    ::dpct::detail::iamin_impl<std::complex<double>>(q, n, x, incx, result);
2150    break;
2151  }
2152  default:
2153    throw std::runtime_error("the combination of data type is unsupported");
2154  }
2155}
2156
2157/// Finds the index of the element with the largest absolute value in a vector.
2158/// \param [in] desc_ptr Descriptor.
2159/// \param [in] n Number of elements in vector x.
2160/// \param [in] x Input vector x.
2161/// \param [in] x_type Data type of the vector x.
2162/// \param [in] incx Stride of vector x.
2163/// \param [out] result The index of the maximal element.
2164inline void iamax(descriptor_ptr desc_ptr, std::int64_t n, const void *x,
2165                  library_data_t x_type, std::int64_t incx, int *result) {
2166  dpct::blas::wrapper_int_to_int64_out wrapper(desc_ptr->get_queue(), result);
2167  iamax(desc_ptr, n, x, x_type, incx, wrapper.get_ptr());
2168}
2169
2170/// Finds the index of the element with the smallest absolute value in a vector.
2171/// \param [in] desc_ptr Descriptor.
2172/// \param [in] n Number of elements in vector x.
2173/// \param [in] x Input vector x.
2174/// \param [in] x_type Data type of the vector x.
2175/// \param [in] incx Stride of vector x.
2176/// \param [out] result The index of the minimum element.
2177inline void iamin(descriptor_ptr desc_ptr, std::int64_t n, const void *x,
2178                  library_data_t x_type, std::int64_t incx, int *result) {
2179  dpct::blas::wrapper_int_to_int64_out wrapper(desc_ptr->get_queue(), result);
2180  iamin(desc_ptr, n, x, x_type, incx, wrapper.get_ptr());
2181}
2182
2183/// Finds the least squares solutions for a batch of overdetermined linear
2184/// systems. Uses the QR factorization to solve a grouped batch of linear
2185/// systems with full rank matrices.
2186/// \param [in] desc_ptr Descriptor.
2187/// \param [in] trans Operation applied to \p a.
2188/// \param [in] m The number of rows of \p a.
2189/// \param [in] n The number of columns of \p a.
2190/// \param [in] nrhs The number of columns of \p b.
2191/// \param [in, out] a Array of pointers to matrices.
2192/// \param [in] lda The leading dimension of \p a.
2193/// \param [in, out] b Array of pointers to matrices.
2194/// \param [in] ldb The leading dimension of \p b.
2195/// \param [out] info Set to 0 if no error.
2196/// \param [out] dev_info Optional. If it is not NULL : dev_info[i]==0 means the
2197/// i-th problem is successful; dev_info[i]!=0 means dev_info[i] is the first
2198/// zero diagonal element of the i-th \p a .
2199/// \param [in] batch_size The size of the batch.
2200template <typename T>
2201inline sycl::event
2202gels_batch_wrapper(descriptor_ptr desc_ptr, oneapi::mkl::transpose trans, int m,
2203                   int n, int nrhs, T *const a[], int lda, T *const b[],
2204                   int ldb, int *info, int *dev_info, int batch_size) {
2205#if defined(DPCT_USM_LEVEL_NONE) || !defined(__INTEL_MKL__)
2206#if defined(DPCT_USM_LEVEL_NONE)
2207  throw std::runtime_error("this API is unsupported when USM level is none");
2208#else
2209  throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
2210                           "Project does not support this API.");
2211#endif
2212#else
2213  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
2214  sycl::queue exec_queue = desc_ptr->get_queue();
2215  struct matrix_info_t {
2216    oneapi::mkl::transpose trans_info;
2217    std::int64_t m_info;
2218    std::int64_t n_info;
2219    std::int64_t nrhs_info;
2220    std::int64_t lda_info;
2221    std::int64_t ldb_info;
2222    std::int64_t group_size_info;
2223  };
2224  matrix_info_t *matrix_info =
2225      (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
2226  matrix_info->trans_info = trans;
2227  matrix_info->m_info = m;
2228  matrix_info->n_info = n;
2229  matrix_info->nrhs_info = nrhs;
2230  matrix_info->lda_info = lda;
2231  matrix_info->ldb_info = ldb;
2232  matrix_info->group_size_info = batch_size;
2233  std::int64_t scratchpad_size =
2234      oneapi::mkl::lapack::gels_batch_scratchpad_size<Ty>(
2235          exec_queue, &(matrix_info->trans_info), &(matrix_info->m_info),
2236          &(matrix_info->n_info), &(matrix_info->nrhs_info),
2237          &(matrix_info->lda_info), &(matrix_info->ldb_info), 1,
2238          &(matrix_info->group_size_info));
2239  Ty *scratchpad = sycl::malloc_device<Ty>(scratchpad_size, exec_queue);
2240
2241  *info = 0;
2242  if (dev_info)
2243    exec_queue.memset(dev_info, 0, batch_size * sizeof(int));
2244  static const std::vector<sycl::event> empty_events{};
2245  static const std::string api_name = "oneapi::mkl::lapack::gels_batch";
2246  sycl::event e = ::dpct::detail::catch_batch_error_f<sycl::event>(
2247      nullptr, api_name, exec_queue, info, dev_info, batch_size,
2248      oneapi::mkl::lapack::gels_batch, exec_queue, &(matrix_info->trans_info),
2249      &(matrix_info->m_info), &(matrix_info->n_info), &(matrix_info->nrhs_info),
2250      (Ty **)a, &(matrix_info->lda_info), (Ty **)b, &(matrix_info->ldb_info),
2251      (std::int64_t)1, &(matrix_info->group_size_info), (Ty *)scratchpad,
2252      (std::int64_t)scratchpad_size, empty_events);
2253
2254  return exec_queue.submit([&](sycl::handler &cgh) {
2255    cgh.host_task([=]() mutable {
2256      ::dpct::detail::catch_batch_error(
2257          nullptr, api_name, exec_queue, info, dev_info,
2258          matrix_info->group_size_info,
2259          [](sycl::event _e) {
2260            _e.wait_and_throw();
2261            return 0;
2262          },
2263          e);
2264      std::free(matrix_info);
2265      sycl::free(scratchpad, exec_queue);
2266    });
2267  });
2268#endif
2269}
2270} // namespace blas
2271
2272/// Computes matrix-matrix product with general matrices.
2273/// \param [in] q The queue where the routine should be executed.
2274/// \param [in] a_trans Specifies the operation applied to A.
2275/// \param [in] b_trans Specifies the operation applied to B.
2276/// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
2277/// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
2278/// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
2279/// \param [in] alpha Scaling factor for the matrix-matrix product.
2280/// \param [in] a Input matrix A.
2281/// \param [in] a_type Data type of the matrix A.
2282/// \param [in] lda Leading dimension of A.
2283/// \param [in] b Input matrix B.
2284/// \param [in] b_type Data type of the matrix B.
2285/// \param [in] ldb Leading dimension of B.
2286/// \param [in] beta Scaling factor for matrix C.
2287/// \param [in, out] c Input/Output matrix C.
2288/// \param [in] c_type Data type of the matrix C.
2289/// \param [in] ldc Leading dimension of C.
2290/// \param [in] scaling_type Data type of the scaling factors.
2291[[deprecated("Please use dpct::blas::gemm(...) instead.")]] inline void
2292gemm(sycl::queue &q, oneapi::mkl::transpose a_trans,
2293     oneapi::mkl::transpose b_trans, int m, int n, int k, const void *alpha,
2294     const void *a, library_data_t a_type, int lda, const void *b,
2295     library_data_t b_type, int ldb, const void *beta, void *c,
2296     library_data_t c_type, int ldc, library_data_t scaling_type) {
2297  blas::descriptor desc;
2298  desc.set_queue(&q);
2299  blas::gemm(&desc, a_trans, b_trans, m, n, k, alpha, a, a_type, lda, b, b_type,
2300             ldb, beta, c, c_type, ldc, scaling_type);
2301}
2302
2303/// Computes a batch of matrix-matrix product with general matrices.
2304/// \param [in] q The queue where the routine should be executed.
2305/// \param [in] a_trans Specifies the operation applied to A.
2306/// \param [in] b_trans Specifies the operation applied to B.
2307/// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
2308/// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
2309/// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
2310/// \param [in] alpha Scaling factor for the matrix-matrix product.
2311/// \param [in] a Input matrix A.
2312/// \param [in] a_type Data type of the matrix A.
2313/// \param [in] lda Leading dimension of A.
2314/// \param [in] b Input matrix B.
2315/// \param [in] b_type Data type of the matrix B.
2316/// \param [in] ldb Leading dimension of B.
2317/// \param [in] beta Scaling factor for matrix C.
2318/// \param [in, out] c Input/Output matrix C.
2319/// \param [in] c_type Data type of the matrix C.
2320/// \param [in] ldc Leading dimension of C.
2321/// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
2322/// \param [in] scaling_type Data type of the scaling factors.
2323[[deprecated("Please use dpct::blas::gemm_batch(...) instead.")]] inline void
2324gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
2325           oneapi::mkl::transpose b_trans, int m, int n, int k,
2326           const void *alpha, const void *a[], library_data_t a_type, int lda,
2327           const void *b[], library_data_t b_type, int ldb, const void *beta,
2328           void *c[], library_data_t c_type, int ldc, int batch_size,
2329           library_data_t scaling_type) {
2330  blas::descriptor desc;
2331  desc.set_queue(&q);
2332  blas::gemm_batch(&desc, a_trans, b_trans, m, n, k, alpha, a, a_type, lda, b,
2333                   b_type, ldb, beta, c, c_type, ldc, batch_size, scaling_type);
2334}
2335
2336/// Computes a batch of matrix-matrix product with general matrices.
2337/// \param [in] q The queue where the routine should be executed.
2338/// \param [in] a_trans Specifies the operation applied to A.
2339/// \param [in] b_trans Specifies the operation applied to B.
2340/// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
2341/// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
2342/// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
2343/// \param [in] alpha Scaling factor for the matrix-matrix product.
2344/// \param [in] a Input matrix A.
2345/// \param [in] a_type Data type of the matrix A.
2346/// \param [in] lda Leading dimension of A.
2347/// \param [in] stride_a Stride between the different A matrices.
2348/// \param [in] b Input matrix B.
2349/// \param [in] b_type Data type of the matrix B.
2350/// \param [in] ldb Leading dimension of B.
2351/// \param [in] stride_b Stride between the different B matrices.
2352/// \param [in] beta Scaling factor for matrix C.
2353/// \param [in, out] c Input/Output matrix C.
2354/// \param [in] c_type Data type of the matrix C.
2355/// \param [in] ldc Leading dimension of C.
2356/// \param [in] stride_c Stride between the different C matrices.
2357/// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
2358/// \param [in] scaling_type Data type of the scaling factors.
2359[[deprecated("Please use dpct::blas::gemm_batch(...) instead.")]] inline void
2360gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
2361           oneapi::mkl::transpose b_trans, int m, int n, int k,
2362           const void *alpha, const void *a, library_data_t a_type, int lda,
2363           long long int stride_a, const void *b, library_data_t b_type,
2364           int ldb, long long int stride_b, const void *beta, void *c,
2365           library_data_t c_type, int ldc, long long int stride_c,
2366           int batch_size, library_data_t scaling_type) {
2367  blas::descriptor desc;
2368  desc.set_queue(&q);
2369  blas::gemm_batch(&desc, a_trans, b_trans, m, n, k, alpha, a, a_type, lda,
2370                   stride_a, b, b_type, ldb, stride_b, beta, c, c_type, ldc,
2371                   stride_c, batch_size, scaling_type);
2372}
2373
2374/// This routines perform a special rank-k update of a symmetric matrix C by
2375/// general matrices A and B.
2376/// \param [in] q The queue where the routine should be executed.
2377/// \param [in] uplo Specifies whether C's data is stored in its upper or lower triangle.
2378/// \param [in] trans Specifies the operation to apply.
2379/// \param [in] n The number of rows and columns in C.
2380/// \param [in] k The inner dimension of matrix multiplications.
2381/// \param [in] alpha Scaling factor for the rank-k update.
2382/// \param [in] a Input matrix A.
2383/// \param [in] lda Leading dimension of A.
2384/// \param [in] b Input matrix B.
2385/// \param [in] ldb Leading dimension of B.
2386/// \param [in] beta Scaling factor for the rank-k update.
2387/// \param [in, out] c Input/Output matrix C.
2388/// \param [in] ldc Leading dimension of C.
2389template <class T>
2390[[deprecated("Please use dpct::blas::syrk(...) instead.")]] inline void
2391syrk(sycl::queue &q, oneapi::mkl::uplo uplo, oneapi::mkl::transpose trans,
2392     int n, int k, const T *alpha, const T *a, int lda, const T *b, int ldb,
2393     const T *beta, T *c, int ldc) {
2394  blas::descriptor desc;
2395  desc.set_queue(&q);
2396  blas::syrk<T>(&desc, uplo, trans, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
2397}
2398
2399/// This routines perform a special rank-k update of a Hermitian matrix C by
2400/// general matrices A and B.
2401/// \param [in] q The queue where the routine should be executed.
2402/// \param [in] uplo Specifies whether C's data is stored in its upper or lower triangle.
2403/// \param [in] trans Specifies the operation to apply.
2404/// \param [in] n The number of rows and columns in C.
2405/// \param [in] k The inner dimension of matrix multiplications.
2406/// \param [in] alpha Scaling factor for the rank-k update.
2407/// \param [in] a Input matrix A.
2408/// \param [in] lda Leading dimension of A.
2409/// \param [in] b Input matrix B.
2410/// \param [in] ldb Leading dimension of B.
2411/// \param [in] beta Scaling factor for the rank-k update.
2412/// \param [in, out] c Input/Output matrix C.
2413/// \param [in] ldc Leading dimension of C.
2414template <class T, class Tbeta>
2415[[deprecated("Please use dpct::blas::herk(...) instead.")]] inline void
2416herk(sycl::queue &q, oneapi::mkl::uplo uplo, oneapi::mkl::transpose trans,
2417     int n, int k, const T *alpha, const T *a, int lda, const T *b, int ldb,
2418     const Tbeta *beta, T *c, int ldc) {
2419  blas::descriptor desc;
2420  desc.set_queue(&q);
2421  blas::herk(&desc, uplo, trans, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
2422}
2423
2424/// This routine performs a group of trsm operations. Each trsm solves an
2425/// equation of the form op(A) * X = alpha * B or X * op(A) = alpha * B.
2426/// \param [in] q The queue where the routine should be executed.
2427/// \param [in] left_right Specifies A multiplies X on the left or on the right.
2428/// \param [in] upper_lower Specifies A is upper or lower triangular.
2429/// \param [in] trans Specifies the operation applied to A.
2430/// \param [in] unit_diag Specifies whether A is unit triangular.
2431/// \param [in] m Number of rows of the B matrices.
2432/// \param [in] n Number of columns of the B matrices.
2433/// \param [in] alpha Scaling factor for the solutions.
2434/// \param [in] a Input matrices A.
2435/// \param [in] a_type Data type of the matrices A.
2436/// \param [in] lda Leading dimension of the matrices A.
2437/// \param [in, out] b Input and output matrices B.
2438/// \param [in] b_type Data type of the matrices B.
2439/// \param [in] ldb Leading dimension of the matrices B.
2440/// \param [in] batch_size Specifies the number of trsm operations to perform.
2441/// \param [in] scaling_type Data type of the scaling factors.
2442[[deprecated("Please use dpct::blas::trsm_batch(...) instead.")]] inline void
2443trsm_batch(sycl::queue &q, oneapi::mkl::side left_right,
2444           oneapi::mkl::uplo upper_lower, oneapi::mkl::transpose trans,
2445           oneapi::mkl::diag unit_diag, int m, int n, const void *alpha,
2446           const void **a, library_data_t a_type, int lda, void **b,
2447           library_data_t b_type, int ldb, int batch_size,
2448           library_data_t scaling_type) {
2449  blas::descriptor desc;
2450  desc.set_queue(&q);
2451  blas::trsm_batch(&desc, left_right, upper_lower, trans, unit_diag, m, n,
2452                   alpha, a, a_type, lda, b, b_type, ldb, batch_size,
2453                   scaling_type);
2454}
2455
2456/// Computes a triangular matrix-general matrix product.
2457/// \param [in] q The queue where the routine should be executed.
2458/// \param [in] left_right Specifies A is on the left or right side of the
2459/// multiplication.
2460/// \param [in] upper_lower Specifies A is upper or lower triangular.
2461/// \param [in] trans Specifies the operation applied to A.
2462/// \param [in] unit_diag Specifies whether A is unit triangular.
2463/// \param [in] m Number of rows of B.
2464/// \param [in] n Number of columns of B.
2465/// \param [in] alpha Scaling factor for the matrix-matrix product.
2466/// \param [in] a Input matrices A.
2467/// \param [in] lda Leading dimension of the matrices A.
2468/// \param [in] b Input matrices B.
2469/// \param [in] ldb Leading dimension of the matrices B.
2470/// \param [out] c Output matrices C.
2471/// \param [in] ldc Leading dimension of the matrices C.
2472template <class T>
2473[[deprecated("Please use dpct::blas::trmm(...) instead.")]] inline void
2474trmm(sycl::queue &q, oneapi::mkl::side left_right,
2475     oneapi::mkl::uplo upper_lower, oneapi::mkl::transpose trans,
2476     oneapi::mkl::diag unit_diag, int m, int n, const T *alpha, const T *a,
2477     int lda, const T *b, int ldb, T *c, int ldc) {
2478  blas::descriptor desc;
2479  desc.set_queue(&q);
2480  blas::trmm<T>(&desc, left_right, upper_lower, trans, unit_diag, m, n, alpha,
2481                a, lda, b, ldb, c, ldc);
2482}
2483
2484/// Computes the Euclidean norm of a vector.
2485/// \param [in] q The queue where the routine should be executed.
2486/// \param [in] n Number of elements in vector x.
2487/// \param [in] x Input vector x.
2488/// \param [in] x_type Data type of the vector x.
2489/// \param [in] incx Stride of vector x.
2490/// \param [out] result The result scalar.
2491/// \param [in] result_type Data type of the result.
2492[[deprecated("Please use dpct::blas::nrm2(...) instead.")]] inline void
2493nrm2(sycl::queue &q, int n, const void *x, library_data_t x_type, int incx,
2494     void *result, library_data_t result_type) {
2495  blas::descriptor desc;
2496  desc.set_queue(&q);
2497  blas::nrm2(&desc, n, x, x_type, incx, result, result_type);
2498}
2499
2500/// Computes the dot product of two vectors.
2501/// \param [in] q The queue where the routine should be executed.
2502/// \param [in] n Number of elements in vector x.
2503/// \param [in] x Input vector x.
2504/// \param [in] x_type Data type of the vector x.
2505/// \param [in] incx Stride of vector x.
2506/// \param [in] y Input vector y.
2507/// \param [in] y_type Data type of the vector y.
2508/// \param [in] incy Stride of vector y.
2509/// \param [out] result The result scalar.
2510/// \param [in] result_type Data type of the result.
2511[[deprecated("Please use dpct::blas::dot(...) instead.")]] inline void
2512dot(sycl::queue &q, int n, const void *x, library_data_t x_type, int incx,
2513    const void *y, library_data_t y_type, int incy, void *result,
2514    library_data_t result_type) {
2515  blas::descriptor desc;
2516  desc.set_queue(&q);
2517  blas::dot(&desc, n, x, x_type, incx, y, y_type, incy, result, result_type);
2518}
2519
2520/// Computes the dot product of two vectors, conjugating the first vector.
2521/// \param [in] q The queue where the routine should be executed.
2522/// \param [in] n Number of elements in vector x.
2523/// \param [in] x Input vector x.
2524/// \param [in] x_type Data type of the vector x.
2525/// \param [in] incx Stride of vector x.
2526/// \param [in] y Input vector y.
2527/// \param [in] y_type Data type of the vector y.
2528/// \param [in] incy Stride of vector y.
2529/// \param [out] result The result scalar.
2530/// \param [in] result_type Data type of the result.
2531[[deprecated("Please use dpct::blas::dotc(...) instead.")]] inline void
2532dotc(sycl::queue &q, int n, const void *x, library_data_t x_type, int incx,
2533     const void *y, library_data_t y_type, int incy, void *result,
2534     library_data_t result_type) {
2535  blas::descriptor desc;
2536  desc.set_queue(&q);
2537  blas::dotc(&desc, n, x, x_type, incx, y, y_type, incy, result, result_type);
2538}
2539
2540/// Computes the product of a vector by a scalar.
2541/// \param [in] q The queue where the routine should be executed.
2542/// \param [in] n Number of elements in vector x.
2543/// \param [in] alpha The scale factor alpha.
2544/// \param [in] alpha_type The data type of alpha.
2545/// \param [in, out] x Input/Output vector x.
2546/// \param [in] x_type Data type of the vector x.
2547/// \param [in] incx Stride of vector x.
2548[[deprecated("Please use dpct::blas::scal(...) instead.")]] inline void
2549scal(sycl::queue &q, int n, const void *alpha, library_data_t alpha_type,
2550     void *x, library_data_t x_type, int incx) {
2551  blas::descriptor desc;
2552  desc.set_queue(&q);
2553  blas::scal(&desc, n, alpha, alpha_type, x, x_type, incx);
2554}
2555
2556/// Computes a vector-scalar product and adds the result to a vector.
2557/// \param [in] q The queue where the routine should be executed.
2558/// \param [in] n Number of elements in vector x.
2559/// \param [in] alpha The scale factor alpha.
2560/// \param [in] alpha_type The data type of alpha.
2561/// \param [in] x Input vector x.
2562/// \param [in] x_type Data type of the vector x.
2563/// \param [in] incx Stride of vector x.
2564/// \param [in, out] y Input/Output vector y.
2565/// \param [in] y_type Data type of the vector y.
2566/// \param [in] incy Stride of vector y.
2567[[deprecated("Please use dpct::blas::axpy(...) instead.")]] inline void
2568axpy(sycl::queue &q, int n, const void *alpha, library_data_t alpha_type,
2569     const void *x, library_data_t x_type, int incx, void *y,
2570     library_data_t y_type, int incy) {
2571  blas::descriptor desc;
2572  desc.set_queue(&q);
2573  blas::axpy(&desc, n, alpha, alpha_type, x, x_type, incx, y, y_type, incy);
2574}
2575
2576/// Performs rotation of points in the plane.
2577/// \param [in] q The queue where the routine should be executed.
2578/// \param [in] n Number of elements in vector x.
2579/// \param [in, out] x Input/Output vector x.
2580/// \param [in] x_type Data type of the vector x.
2581/// \param [in] incx Stride of vector x.
2582/// \param [in, out] y Input/Output vector y.
2583/// \param [in] y_type Data type of the vector y.
2584/// \param [in] incy Stride of vector y.
2585/// \param [in] c Scaling factor.
2586/// \param [in] s Scaling factor.
2587/// \param [in] cs_type Data type of the scaling factors.
2588[[deprecated("Please use dpct::blas::rot(...) instead.")]] inline void
2589rot(sycl::queue &q, int n, void *x, library_data_t x_type, int incx, void *y,
2590    library_data_t y_type, int incy, const void *c, const void *s,
2591    library_data_t cs_type) {
2592  blas::descriptor desc;
2593  desc.set_queue(&q);
2594  blas::rot(&desc, n, x, x_type, incx, y, y_type, incy, c, s, cs_type);
2595}
2596} // namespace dpct
2597#undef DPCT_COMPUTE_MODE_ARG
2598#undef DPCT_COMPUTE_MODE_PARAM
2599#endif // __DPCT_BLAS_UTILS_HPP__