okami.mom/strata

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

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

main
63.9 KiB1286 linesraw
1//==---- lapack_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_LAPACK_UTILS_HPP__
10#define __DPCT_LAPACK_UTILS_HPP__
11
12#include "compat_service.hpp"
13#include "lib_common_utils.hpp"
14
15#include "detail/lapack_utils_detail.hpp"
16
17namespace dpct {
18namespace lapack {
19/// Computes all eigenvalues and, optionally, eigenvectors of a real generalized
20/// symmetric definite eigenproblem using a divide and conquer method.
21/// \return Returns 0 if no synchronous exception, otherwise returns 1.
22/// \param [in] queue Device queue where calculations will be performed. It must
23/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
24/// not defined).
25/// \param [in] itype Must be 1 or 2 or 3. Specifies the problem type to be solved.
26/// \param [in] jobz Must be job::novec or job::vec.
27/// \param [in] uplo Must be uplo::upper or uplo::lower.
28/// \param [in] n The order of the matrices A and B.
29/// \param [in,out] a The symmetric matrix A.
30/// \param [in] lda The leading dimension of matrix A.
31/// \param [in,out] b The symmetric matrix B.
32/// \param [in] ldb The leading dimension of matrix B.
33/// \param [out] w Eigenvalues.
34/// \param [in] scratchpad Scratchpad memory to be used by the routine
35/// for storing intermediate results.
36/// \param [in] scratchpad_size Size of scratchpad memory as a number of
37/// floating point elements of type T.
38/// \param [out] info If lapack synchronous exception is caught, the value
39/// returned from info() method of the exception is set to \p info.
40template <typename T>
41inline int sygvd(sycl::queue &queue, std::int64_t itype, oneapi::mkl::job jobz,
42                 oneapi::mkl::uplo uplo, int n, T *a, int lda, T *b, int ldb,
43                 T *w, T *scratchpad, int scratchpad_size, int *info) {
44#ifdef DPCT_USM_LEVEL_NONE
45  auto info_buf = get_buffer<int>(info);
46  auto a_buffer = get_buffer<T>(a);
47  auto b_buffer = get_buffer<T>(b);
48  auto w_buffer = get_buffer<T>(w);
49  auto scratchpad_buffer = get_buffer<T>(scratchpad);
50  int info_val = 0;
51  int ret_val = 0;
52  try {
53    oneapi::mkl::lapack::sygvd(queue, itype, jobz, uplo, n, a_buffer, lda,
54                               b_buffer, ldb, w_buffer, scratchpad_buffer,
55                               scratchpad_size);
56  } catch (oneapi::mkl::lapack::exception const& e) {
57    std::cerr << "Unexpected exception caught during call to LAPACK API: sygvd"
58              << std::endl
59              << "reason: " << e.what() << std::endl
60              << "info: " << e.info() << std::endl;
61    info_val = static_cast<int>(e.info());
62    ret_val = 1;
63  } catch (sycl::exception const& e) {
64    std::cerr << "Caught synchronous SYCL exception:" << std::endl
65              << "reason: " << e.what() << std::endl;
66    ret_val = 1;
67  }
68  queue.submit([&, info_val](sycl::handler &cgh) {
69    auto info_acc = info_buf.get_access<sycl::access_mode::write>(cgh);
70    cgh.single_task<::dpct::cs::kernel_name<class sygvd_set_info, T>>(
71        [=]() { info_acc[0] = info_val; });
72  });
73  return ret_val;
74#else
75  try {
76    oneapi::mkl::lapack::sygvd(queue, itype, jobz, uplo, n, a, lda, b, ldb, w,
77                               scratchpad, scratchpad_size);
78  } catch (oneapi::mkl::lapack::exception const& e) {
79    std::cerr << "Unexpected exception caught during call to LAPACK API: sygvd"
80              << std::endl
81              << "reason: " << e.what() << std::endl
82              << "info: " << e.info() << std::endl;
83    int info_val = static_cast<int>(e.info());
84    queue.memcpy(info, &info_val, sizeof(int)).wait();
85    return 1;
86  } catch (sycl::exception const& e) {
87    std::cerr << "Caught synchronous SYCL exception:" << std::endl
88              << "reason: " << e.what() << std::endl;
89    queue.memset(info, 0, sizeof(int)).wait();
90    return 1;
91  }
92  queue.memset(info, 0, sizeof(int));
93  return 0;
94#endif
95}
96/// Computes all the eigenvalues, and optionally, the eigenvectors of a complex
97/// generalized Hermitian positive-definite eigenproblem using a divide and
98/// conquer method.
99/// \return Returns 0 if no synchronous exception, otherwise returns 1.
100/// \param [in] queue Device queue where calculations will be performed. It must
101/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
102/// not defined).
103/// \param [in] itype Must be 1 or 2 or 3. Specifies the problem type to be solved.
104/// \param [in] jobz Must be job::novec or job::vec.
105/// \param [in] uplo Must be uplo::upper or uplo::lower.
106/// \param [in] n The order of the matrices A and B.
107/// \param [in,out] a The Hermitian matrix A.
108/// \param [in] lda The leading dimension of matrix A.
109/// \param [in,out] b The Hermitian matrix B.
110/// \param [in] ldb The leading dimension of matrix B.
111/// \param [in] w Eigenvalues.
112/// \param [in] scratchpad Scratchpad memory to be used by the routine
113/// for storing intermediate results.
114/// \param [in] scratchpad_size Size of scratchpad memory as a number of
115/// floating point elements of type T.
116/// \param [out] info If lapack synchronous exception is caught, the value
117/// returned from info() method of the exception is set to \p info.
118template <typename T, typename Tw>
119inline int hegvd(sycl::queue &queue, std::int64_t itype, oneapi::mkl::job jobz,
120                 oneapi::mkl::uplo uplo, int n, T *a, int lda, T *b, int ldb,
121                 Tw *w, T *scratchpad, int scratchpad_size, int *info) {
122  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
123#ifdef DPCT_USM_LEVEL_NONE
124  auto info_buf = get_buffer<int>(info);
125  auto a_buffer = get_buffer<Ty>(a);
126  auto b_buffer = get_buffer<Ty>(b);
127  auto w_buffer = get_buffer<Tw>(w);
128  auto scratchpad_buffer = get_buffer<Ty>(scratchpad);
129  int info_val = 0;
130  int ret_val = 0;
131  try {
132    oneapi::mkl::lapack::hegvd(queue, itype, jobz, uplo, n, a_buffer, lda,
133                               b_buffer, ldb, w_buffer, scratchpad_buffer,
134                               scratchpad_size);
135  } catch (oneapi::mkl::lapack::exception const& e) {
136    std::cerr << "Unexpected exception caught during call to LAPACK API: hegvd"
137              << std::endl
138              << "reason: " << e.what() << std::endl
139              << "info: " << e.info() << std::endl;
140    info_val = static_cast<int>(e.info());
141    ret_val = 1;
142  } catch (sycl::exception const& e) {
143    std::cerr << "Caught synchronous SYCL exception:" << std::endl
144              << "reason: " << e.what() << std::endl;
145    ret_val = 1;
146  }
147  queue.submit([&, info_val](sycl::handler &cgh) {
148    auto info_acc = info_buf.get_access<sycl::access_mode::write>(cgh);
149    cgh.single_task<::dpct::cs::kernel_name<class hegvd_set_info, T>>(
150        [=]() { info_acc[0] = info_val; });
151  });
152  return ret_val;
153#else
154  try {
155    oneapi::mkl::lapack::hegvd(queue, itype, jobz, uplo, n, (Ty *)a, lda, (Ty *)b,
156                               ldb, w, (Ty *)scratchpad, scratchpad_size);
157  } catch (oneapi::mkl::lapack::exception const& e) {
158    std::cerr << "Unexpected exception caught during call to LAPACK API: hegvd"
159              << std::endl
160              << "reason: " << e.what() << std::endl
161              << "info: " << e.info() << std::endl;
162    int info_val = static_cast<int>(e.info());
163    queue.memcpy(info, &info_val, sizeof(int)).wait();
164    return 1;
165  } catch (sycl::exception const& e) {
166    std::cerr << "Caught synchronous SYCL exception:" << std::endl
167              << "reason: " << e.what() << std::endl;
168    queue.memset(info, 0, sizeof(int)).wait();
169    return 1;
170  }
171  queue.memset(info, 0, sizeof(int));
172  return 0;
173#endif
174}
175/// Computes the Cholesky factorizations of a batch of symmetric (or Hermitian,
176/// for complex data) positive-definite matrices.
177/// \return Returns 0 if no synchronous exception, otherwise returns 1.
178/// \param [in] queue Device queue where calculations will be performed. It must
179/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
180/// not defined).
181/// \param [in] uplo Must be uplo::upper or uplo::lower.
182/// \param [in] n The order of the matrix A.
183/// \param [in,out] a Array of pointers to matrix A.
184/// \param [in] lda The leading dimension of matrix A.
185/// \param [out] info If lapack synchronous exception is caught, the value
186/// returned from info() method of the exception is set to \p info.
187/// \param [in] group_size The batch size.
188template <typename T>
189inline int potrf_batch(sycl::queue &queue, oneapi::mkl::uplo uplo, int n,
190                       T *a[], int lda, int *info, int group_size) {
191#ifdef DPCT_USM_LEVEL_NONE
192  throw std::runtime_error("this API is unsupported when USM level is none");
193#else
194  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
195  struct matrix_info_t {
196    oneapi::mkl::uplo uplo_info;
197    std::int64_t n_info;
198    std::int64_t lda_info;
199    std::int64_t group_size_info;
200  };
201  matrix_info_t *matrix_info =
202      (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
203  matrix_info->uplo_info = uplo;
204  matrix_info->n_info = n;
205  matrix_info->lda_info = lda;
206  matrix_info->group_size_info = group_size;
207  std::int64_t scratchpad_size =
208      oneapi::mkl::lapack::potrf_batch_scratchpad_size<Ty>(
209          queue, &(matrix_info->uplo_info), &(matrix_info->n_info),
210          &(matrix_info->lda_info), 1, &(matrix_info->group_size_info));
211  Ty *scratchpad = sycl::malloc_device<Ty>(scratchpad_size, queue);
212  int has_execption = 0;
213
214  static const std::vector<sycl::event> empty_events{};
215  static const std::string api_name = "oneapi::mkl::lapack::potrf_batch";
216  if (info)
217    queue.memset(info, 0, group_size * sizeof(int));
218  sycl::event e = ::dpct::detail::catch_batch_error_f<sycl::event>(
219      &has_execption, api_name, queue, nullptr, info,
220      matrix_info->group_size_info, oneapi::mkl::lapack::potrf_batch, queue,
221      &(matrix_info->uplo_info), &(matrix_info->n_info), (Ty **)a,
222      &(matrix_info->lda_info), (std::int64_t)1,
223      &(matrix_info->group_size_info), (Ty *)scratchpad,
224      (std::int64_t)scratchpad_size, empty_events);
225
226  queue.submit([&](sycl::handler &cgh) {
227    cgh.host_task([=]() mutable {
228      ::dpct::detail::catch_batch_error(
229          nullptr, api_name, queue, nullptr, info, matrix_info->group_size_info,
230          [](sycl::event _e) {
231            _e.wait_and_throw();
232            return 0;
233          },
234          e);
235      std::free(matrix_info);
236      sycl::free(scratchpad, queue);
237    });
238  });
239  return has_execption;
240#endif
241}
242/// Solves a batch of systems of linear equations with a Cholesky-factored
243/// symmetric (Hermitian) positive-definite coefficient matrices.
244/// \return Returns 0 if no synchronous exception, otherwise returns 1.
245/// \param [in] queue Device queue where calculations will be performed. It must
246/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
247/// not defined).
248/// \param [in] uplo Must be uplo::upper or uplo::lower.
249/// \param [in] n The order of the matrix A.
250/// \param [in] nrhs The number of right-hand sides.
251/// \param [in,out] a Array of pointers to matrix A.
252/// \param [in] lda The leading dimension of matrix A.
253/// \param [in,out] b Array of pointers to matrix B.
254/// \param [in] ldb The leading dimension of matrix B.
255/// \param [out] info If lapack synchronous exception is caught, the value
256/// returned from info() method of the exception is set to \p info.
257/// \param [in] group_size The batch size.
258template <typename T>
259inline int potrs_batch(sycl::queue &queue, oneapi::mkl::uplo uplo, int n,
260                       int nrhs, T *a[], int lda, T *b[], int ldb, int *info,
261                       int group_size) {
262#ifdef DPCT_USM_LEVEL_NONE
263  throw std::runtime_error("this API is unsupported when USM level is none");
264#else
265  using Ty = typename ::dpct::detail::lib_data_traits_t<T>;
266  struct matrix_info_t {
267    oneapi::mkl::uplo uplo_info;
268    std::int64_t n_info;
269    std::int64_t nrhs_info;
270    std::int64_t lda_info;
271    std::int64_t ldb_info;
272    std::int64_t group_size_info;
273  };
274  matrix_info_t *matrix_info =
275      (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
276  matrix_info->uplo_info = uplo;
277  matrix_info->n_info = n;
278  matrix_info->nrhs_info = nrhs;
279  matrix_info->lda_info = lda;
280  matrix_info->ldb_info = ldb;
281  matrix_info->group_size_info = group_size;
282  std::int64_t scratchpad_size =
283      oneapi::mkl::lapack::potrs_batch_scratchpad_size<Ty>(
284          queue, &(matrix_info->uplo_info), &(matrix_info->n_info),
285          &(matrix_info->nrhs_info), &(matrix_info->lda_info),
286          &(matrix_info->ldb_info), 1, &(matrix_info->group_size_info));
287  Ty *scratchpad = sycl::malloc_device<Ty>(scratchpad_size, queue);
288  int has_execption = 0;
289
290  if (info)
291    queue.memset(info, 0, group_size * sizeof(int));
292
293  static const std::vector<sycl::event> empty_events{};
294  static const std::string api_name = "oneapi::mkl::lapack::potrs_batch";
295  sycl::event e = ::dpct::detail::catch_batch_error_f<sycl::event>(
296      &has_execption, api_name, queue, nullptr, info,
297      matrix_info->group_size_info, oneapi::mkl::lapack::potrs_batch, queue,
298      &(matrix_info->uplo_info), &(matrix_info->n_info),
299      &(matrix_info->nrhs_info), (const Ty *const *)a, &(matrix_info->lda_info),
300      (Ty **)b, &(matrix_info->ldb_info), (std::int64_t)1,
301      &(matrix_info->group_size_info), (Ty *)scratchpad,
302      (std::int64_t)scratchpad_size, empty_events);
303
304  queue.submit([&](sycl::handler &cgh) {
305    cgh.host_task([=]() mutable {
306      ::dpct::detail::catch_batch_error(
307          nullptr, api_name, queue, nullptr, info, matrix_info->group_size_info,
308          [](sycl::event _e) {
309            _e.wait_and_throw();
310            return 0;
311          },
312          e);
313      std::free(matrix_info);
314      sycl::free(scratchpad, queue);
315    });
316  });
317  return has_execption;
318#endif
319}
320
321/// Computes the size of workspace memory of getrf function.
322/// \return Returns 0 if no synchronous exception, otherwise returns 1.
323/// \param [in] q Device queue where computation will be performed. It must
324/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
325/// not defined).
326/// \param [in] m The number of rows in the matrix A.
327/// \param [in] n The number of columns in the matrix A.
328/// \param [in] a_type The data type of the matrix A.
329/// \param [in] lda The leading dimension of the matrix A.
330/// \param [out] device_ws_size The workspace size in bytes.
331/// \param [out] host_ws_size The host workspace size in bytes. Currently the
332/// value is always zero.
333inline int getrf_scratchpad_size(sycl::queue &q, std::int64_t m, std::int64_t n,
334                                 library_data_t a_type, std::int64_t lda,
335                                 std::size_t *device_ws_size,
336                                 std::size_t *host_ws_size = nullptr) {
337  if (host_ws_size)
338    *host_ws_size = 0;
339  std::size_t device_ws_size_tmp;
340  int ret = detail::lapack_shim<detail::getrf_scratchpad_size_impl>(
341      q, a_type, nullptr, "getrf_scratchpad_size", q, m, n, a_type, lda,
342      device_ws_size_tmp);
343  *device_ws_size = detail::element_number_to_byte(device_ws_size_tmp, a_type);
344  return ret;
345}
346
347/// Computes the LU factorization of a general m-by-n matrix.
348/// \return Returns 0 if no synchronous exception, otherwise returns 1.
349/// \param [in] q The queue where the routine should be executed. It must
350/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
351/// not defined).
352/// \param [in] m The number of rows in the matrix A.
353/// \param [in] n The number of columns in the matrix A.
354/// \param [in] a_type The data type of the matrix A.
355/// \param [in, out] a The input matrix A. Overwritten by L and U. The unit
356/// diagonal elements of L are not stored.
357/// \param [in] lda The leading dimension of the matrix A.
358/// \param [out] ipiv The pivot indices. If \p ipiv is nullptr, non-pivoting
359/// LU factorization is computed.
360/// \param [in] device_ws The workspace.
361/// \param [in] device_ws_size The workspace size in bytes.
362/// \param [out] info If lapack synchronous exception is caught, the value
363/// returned from info() method of the exception is set to \p info.
364inline int getrf(sycl::queue &q, std::int64_t m, std::int64_t n,
365                 library_data_t a_type, void *a, std::int64_t lda,
366                 std::int64_t *ipiv, void *device_ws,
367                 std::size_t device_ws_size, int *info) {
368#ifndef __INTEL_MKL__
369  throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
370                           "Project does not support this API.");
371#else
372  std::size_t device_ws_size_in_element_number =
373      detail::byte_to_element_number(device_ws_size, a_type);
374  if (ipiv == nullptr) {
375    return detail::lapack_shim<detail::getrfnp_impl>(
376        q, a_type, info, "getrfnp_batch", q, m, n, a_type, a, lda, ipiv,
377        device_ws, device_ws_size_in_element_number, info);
378  }
379  return detail::lapack_shim<detail::getrf_impl>(
380      q, a_type, info, "getrf", q, m, n, a_type, a, lda, ipiv, device_ws,
381      device_ws_size_in_element_number, info);
382#endif
383}
384
385/// Solves a system of linear equations with a LU-factored square coefficient
386/// matrix, with multiple right-hand sides.
387/// \return Returns 0 if no synchronous exception, otherwise returns 1.
388/// \param [in] q The queue where the routine should be executed. It must
389/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
390/// not defined).
391/// \param [in] trans Indicates the form of the linear equation.
392/// \param [in] n The order of the matrix A and the number of rows in matrix B.
393/// \param [in] nrhs The number of right hand sides.
394/// \param [in] a_type The data type of the matrix A.
395/// \param [in] a The input matrix A.
396/// \param [in] lda The leading dimension of the matrix A.
397/// \param [in] ipiv The pivot indices.
398/// \param [in] b_type The data type of the matrix B.
399/// \param [in, out] b The matrix B, whose columns are the right-hand sides
400/// for the systems of equations.
401/// \param [in] ldb The leading dimension of the matrix B.
402/// \param [out] info If lapack synchronous exception is caught, the value
403/// returned from info() method of the exception is set to \p info.
404inline int getrs(sycl::queue &q, oneapi::mkl::transpose trans, std::int64_t n,
405                 std::int64_t nrhs, library_data_t a_type, void *a,
406                 std::int64_t lda, std::int64_t *ipiv, library_data_t b_type,
407                 void *b, std::int64_t ldb, int *info) {
408  return detail::lapack_shim<detail::getrs_impl>(
409      q, a_type, info, "getrs_scratchpad_size/getrs", q, trans, n, nrhs, a_type,
410      a, lda, ipiv, b_type, b, ldb, info);
411}
412
413/// Computes the size of workspace memory of geqrf function.
414/// \return Returns 0 if no synchronous exception, otherwise returns 1.
415/// \param [in] q Device queue where computation will be performed. It must
416/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
417/// not defined).
418/// \param [in] m The number of rows in the matrix A.
419/// \param [in] n The number of columns in the matrix A.
420/// \param [in] a_type The data type of the matrix A.
421/// \param [in] lda The leading dimension of the matrix A.
422/// \param [out] device_ws_size The device workspace size in bytes.
423/// \param [out] host_ws_size The host workspace size in bytes. Currently the
424/// value is always zero.
425inline int geqrf_scratchpad_size(sycl::queue &q, std::int64_t m, std::int64_t n,
426                                 library_data_t a_type, std::int64_t lda,
427                                 std::size_t *device_ws_size,
428                                 std::size_t *host_ws_size = nullptr) {
429  if (host_ws_size)
430    *host_ws_size = 0;
431  std::size_t device_ws_size_tmp;
432  int ret = detail::lapack_shim<detail::geqrf_scratchpad_size_impl>(
433      q, a_type, nullptr, "geqrf_scratchpad_size", q, m, n, a_type, lda,
434      device_ws_size_tmp);
435  *device_ws_size = detail::element_number_to_byte(device_ws_size_tmp, a_type);
436  return ret;
437}
438
439/// Computes the QR factorization of a general m-by-n matrix.
440/// \return Returns 0 if no synchronous exception, otherwise returns 1.
441/// \param [in] q The queue where the routine should be executed. It must
442/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
443/// not defined).
444/// \param [in] m The number of rows in the matrix A.
445/// \param [in] n The number of columns in the matrix A.
446/// \param [in] a_type The data type of the matrix A.
447/// \param [in, out] a The input matrix A. Overwritten by the factorization data.
448/// \param [in] lda The leading dimension of the matrix A.
449/// \param [in] tau_type The data type of the array tau.
450/// \param [in] tau The array contains scalars that define elementary reflectors
451/// for the matrix Q in its decomposition in a product of elementary reflectors.
452/// \param [in] device_ws The workspace.
453/// \param [in] device_ws_size The workspace size in bytes.
454/// \param [out] info If lapack synchronous exception is caught, the value
455/// returned from info() method of the exception is set to \p info.
456inline int geqrf(sycl::queue &q, std::int64_t m, std::int64_t n,
457                 library_data_t a_type, void *a, std::int64_t lda,
458                 library_data_t tau_type, void *tau, void *device_ws,
459                 std::size_t device_ws_size, int *info) {
460  std::size_t device_ws_size_in_element_number =
461      detail::byte_to_element_number(device_ws_size, a_type);
462  return detail::lapack_shim<detail::geqrf_impl>(
463      q, a_type, info, "geqrf", q, m, n, a_type, a, lda, tau_type, tau,
464      device_ws, device_ws_size_in_element_number, info);
465}
466
467/// Computes the size of workspace memory of gesvd function.
468/// \return Returns 0 if no synchronous exception, otherwise returns 1.
469/// \param [in] q Device queue where computation will be performed. It must
470/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
471/// not defined).
472/// \param [in] jobu Must be 'A' (representing jobsvd::vectors), 'S'
473/// (representing jobsvd::somevec), 'O' (representing jobsvd::vectorsina) or 'N'
474/// (representing jobsvd::novec).
475/// \param [in] jobvt Must be 'A' (representing jobsvd::vectors), 'S'
476/// (representing jobsvd::somevec), 'O' (representing jobsvd::vectorsina) or 'N'
477/// (representing jobsvd::novec).
478/// \param [in] m The number of rows in the matrix A.
479/// \param [in] n The number of columns in the matrix A.
480/// \param [in] a_type The data type of the matrix A.
481/// \param [in] lda The leading dimension of the matrix A.
482/// \param [in] u_type The data type of the matrix U.
483/// \param [in] ldu The leading dimension of the matrix U.
484/// \param [in] vt_type The data type of the matrix VT.
485/// \param [in] ldvt The leading dimension of the matrix VT.
486/// \param [out] device_ws_size The device workspace size in bytes.
487/// \param [out] host_ws_size The host workspace size in bytes. Currently the
488/// value is always zero.
489inline int gesvd_scratchpad_size(sycl::queue &q, signed char jobu,
490                                 signed char jobvt, std::int64_t m,
491                                 std::int64_t n, library_data_t a_type,
492                                 std::int64_t lda, library_data_t u_type,
493                                 std::int64_t ldu, library_data_t vt_type,
494                                 std::int64_t ldvt, std::size_t *device_ws_size,
495                                 std::size_t *host_ws_size = nullptr) {
496  oneapi::mkl::jobsvd jobu_enum = detail::char2jobsvd(jobu);
497  oneapi::mkl::jobsvd jobvt_enum = detail::char2jobsvd(jobvt);
498  if (host_ws_size)
499    *host_ws_size = 0;
500  std::size_t device_ws_size_tmp;
501  int ret = detail::lapack_shim<detail::gesvd_scratchpad_size_impl>(
502      q, a_type, nullptr, "gesvd_scratchpad_size", q, jobu_enum, jobvt_enum, m,
503      n, a_type, lda, u_type, ldu, vt_type, ldvt, device_ws_size_tmp);
504  *device_ws_size = detail::element_number_to_byte(device_ws_size_tmp, a_type);
505  return ret;
506}
507
508/// Computes the size of workspace memory of gesvd function.
509/// \return Returns 0 if no synchronous exception, otherwise returns 1.
510/// \param [in] q Device queue where computation will be performed. It must
511/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
512/// not defined).
513/// \param [in] jobz Must be job::vec or job::novec
514/// \param [in] all_vec Only have effects when \param jobz is job::vec.If the
515/// value is zero, all m columns of U are returned in the matrix U, otherwise
516/// the first min( \param m, \param n ) columns of U (the left singular vectors)
517/// are returned in the matrix U.
518/// \param [in] m The number of rows in the matrix A.
519/// \param [in] n The number of columns in the matrix A.
520/// \param [in] a_type The data type of the matrix A.
521/// \param [in] lda The leading dimension of the matrix A.
522/// \param [in] u_type The data type of the matrix U.
523/// \param [in] ldu The leading dimension of the matrix U.
524/// \param [in] vt_type The data type of the matrix VT.
525/// \param [in] ldvt The leading dimension of the matrix VT.
526/// \param [out] device_ws_size The device workspace size as a number of
527/// elements of type \param a_type.
528/// \param [out] host_ws_size The host workspace size as a number of elements
529/// of type \param a_type. Currently the value is always zero.
530inline int gesvd_scratchpad_size(sycl::queue &q, oneapi::mkl::job jobz,
531                                 std::int64_t all_vec, std::int64_t m,
532                                 std::int64_t n, library_data_t a_type,
533                                 std::int64_t lda, library_data_t u_type,
534                                 std::int64_t ldu, library_data_t vt_type,
535                                 std::int64_t ldvt, int *device_ws_size,
536                                 std::size_t *host_ws_size = nullptr) {
537  if (host_ws_size)
538    *host_ws_size = 0;
539  oneapi::mkl::jobsvd jobu;
540  oneapi::mkl::jobsvd jobvt;
541  if (jobz == oneapi::mkl::job::vec) {
542    if (all_vec) {
543      jobu = jobvt = oneapi::mkl::jobsvd::somevec;
544    } else {
545      jobu = jobvt = oneapi::mkl::jobsvd::vectors;
546    }
547  } else if (jobz == oneapi::mkl::job::novec) {
548    jobu = jobvt = oneapi::mkl::jobsvd::novec;
549  } else {
550    throw std::runtime_error("the job type is unsupported");
551  }
552  std::size_t device_ws_size_64;
553  int ret = detail::lapack_shim<detail::gesvd_scratchpad_size_impl>(
554      q, a_type, nullptr, "gesvd_scratchpad_size", q, jobu, jobvt, m, n, a_type,
555      lda, u_type, ldu, vt_type, ldvt, device_ws_size_64);
556  *device_ws_size = device_ws_size_64;
557  return ret;
558}
559
560/// Computes the size of workspace memory of gesvd function.
561/// \return Returns 0 if no synchronous exception, otherwise returns 1.
562/// \param [in] q Device queue where computation will be performed. It must
563/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
564/// not defined).
565/// \param [in] jobu Must be 'A' (representing jobsvd::vectors), 'S'
566/// (representing jobsvd::somevec), 'O' (representing jobsvd::vectorsina) or 'N'
567/// (representing jobsvd::novec).
568/// \param [in] jobvt Must be 'A' (representing jobsvd::vectors), 'S'
569/// (representing jobsvd::somevec), 'O' (representing jobsvd::vectorsina) or 'N'
570/// (representing jobsvd::novec).
571/// \param [in] m The number of rows in the matrix A.
572/// \param [in] n The number of columns in the matrix A.
573/// \param [in] a_type The data type of the matrix A.
574/// \param [in, out] a The input matrix A and it will be overwritten according
575/// to \p jobu and \p jobvt.
576/// \param [in] lda The leading dimension of the matrix A.
577/// \param [in] s_type The data type of the matrix S.
578/// \param [out] s The output matrix S.
579/// \param [in] u_type The data type of the matrix U.
580/// \param [out] u The output matrix U.
581/// \param [in] ldu The leading dimension of the matrix U.
582/// \param [in] vt_type The data type of the matrix VT.
583/// \param [out] vt The output matrix VT.
584/// \param [in] ldvt The leading dimension of the matrix VT.
585/// \param [in] device_ws The workspace.
586/// \param [in] device_ws_size The workspace size in bytes.
587/// \param [out] info If lapack synchronous exception is caught, the value
588/// returned from info() method of the exception is set to \p info.
589inline int gesvd(sycl::queue &q, signed char jobu, signed char jobvt,
590                 std::int64_t m, std::int64_t n, library_data_t a_type, void *a,
591                 std::int64_t lda, library_data_t s_type, void *s,
592                 library_data_t u_type, void *u, std::int64_t ldu,
593                 library_data_t vt_type, void *vt, std::int64_t ldvt,
594                 void *device_ws, std::size_t device_ws_size, int *info) {
595  oneapi::mkl::jobsvd jobu_enum = detail::char2jobsvd(jobu);
596  oneapi::mkl::jobsvd jobvt_enum = detail::char2jobsvd(jobvt);
597  std::size_t device_ws_size_in_element_number =
598      detail::byte_to_element_number(device_ws_size, a_type);
599  return detail::lapack_shim<detail::gesvd_impl>(
600      q, a_type, info, "gesvd", q, jobu_enum, jobvt_enum, m, n, a_type, a, lda,
601      s_type, s, u_type, u, ldu, vt_type, vt, ldvt, device_ws,
602      device_ws_size_in_element_number, info);
603}
604
605/// Computes the size of workspace memory of gesvd function.
606/// \return Returns 0 if no synchronous exception, otherwise returns 1.
607/// \param [in] q Device queue where computation will be performed. It must
608/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
609/// not defined).
610/// \param [in] jobz Must be job::vec or job::novec.
611/// \param [in] all_vec Only have effects when \param jobz is job::vec.If the
612/// value is zero, all m columns of U are returned in the matrix U, otherwise
613/// the first min( \param m, \param n ) columns of U (the left singular vectors)
614/// are returned in the matrix U.
615/// \param [in] m The number of rows in the matrix A.
616/// \param [in] n The number of columns in the matrix A.
617/// \param [in] a_type The data type of the matrix A.
618/// \param [in, out] a The input matrix A and it will be overwritten according
619/// to \p jobu and \p jobvt.
620/// \param [in] lda The leading dimension of the matrix A.
621/// \param [in] s_type The data type of the matrix S.
622/// \param [out] s The output matrix S.
623/// \param [in] u_type The data type of the matrix U.
624/// \param [out] u The output matrix U.
625/// \param [in] ldu The leading dimension of the matrix U.
626/// \param [in] vt_type The data type of the matrix VT.
627/// \param [out] vt The output matrix VT.
628/// \param [in] ldvt The leading dimension of the matrix VT.
629/// \param [in] device_ws The workspace.
630/// \param [in] device_ws_size The device workspace size as a number of
631/// elements of type \param a_type.
632/// \param [out] info If lapack synchronous exception is caught, the value
633/// returned from info() method of the exception is set to \p info.
634inline int gesvd(sycl::queue &q, oneapi::mkl::job jobz, std::int64_t all_vec,
635                 std::int64_t m, std::int64_t n, library_data_t a_type, void *a,
636                 std::int64_t lda, library_data_t s_type, void *s,
637                 library_data_t u_type, void *u, std::int64_t ldu,
638                 library_data_t vt_type, void *vt, std::int64_t ldvt,
639                 void *device_ws, std::size_t device_ws_size, int *info) {
640  oneapi::mkl::jobsvd jobu;
641  oneapi::mkl::jobsvd jobvt;
642  if (jobz == oneapi::mkl::job::vec) {
643    if (all_vec) {
644      jobu = jobvt = oneapi::mkl::jobsvd::somevec;
645    } else {
646      jobu = jobvt = oneapi::mkl::jobsvd::vectors;
647    }
648  } else if (jobz == oneapi::mkl::job::novec) {
649    jobu = jobvt = oneapi::mkl::jobsvd::novec;
650  } else {
651    throw std::runtime_error("the job type is unsupported");
652  }
653
654  detail::lapack_shim<detail::gesvd_conj_impl>(
655      q, a_type, info, "gesvd", q, jobu, jobvt, m, n, a_type, a, lda, s_type, s,
656      u_type, u, ldu, vt_type, vt, ldvt, device_ws, device_ws_size, info);
657  return 0;
658}
659
660/// Computes the size of workspace memory of potrf function.
661/// \return Returns 0 if no synchronous exception, otherwise returns 1.
662/// \param [in] q Device queue where computation will be performed. It must
663/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
664/// not defined).
665/// \param [in] uplo Must be uplo::upper or uplo::lower.
666/// \param [in] n The number of columns in the matrix A.
667/// \param [in] a_type The data type of the matrix A.
668/// \param [in] lda The leading dimension of the matrix A.
669/// \param [out] device_ws_size The device workspace size in bytes.
670/// \param [out] host_ws_size The host workspace size in bytes. Currently the
671/// value is always zero.
672inline int potrf_scratchpad_size(sycl::queue &q, oneapi::mkl::uplo uplo,
673                                 std::int64_t n, library_data_t a_type,
674                                 std::int64_t lda, std::size_t *device_ws_size,
675                                 std::size_t *host_ws_size = nullptr) {
676  if (host_ws_size)
677    *host_ws_size = 0;
678  std::size_t device_ws_size_tmp;
679  int ret = detail::lapack_shim<detail::potrf_scratchpad_size_impl>(
680      q, a_type, nullptr, "potrf_scratchpad_size", q, uplo, n, a_type, lda,
681      device_ws_size_tmp);
682  *device_ws_size = detail::element_number_to_byte(device_ws_size_tmp, a_type);
683  return ret;
684}
685
686/// Computes the Cholesky factorization of a symmetric (Hermitian)
687/// positive-definite matrix.
688/// \return Returns 0 if no synchronous exception, otherwise returns 1.
689/// \param [in] q The queue where the routine should be executed. It must
690/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
691/// not defined).
692/// \param [in] uplo Must be uplo::upper or uplo::lower.
693/// \param [in] n The number of columns in the matrix A.
694/// \param [in] a_type The data type of the matrix A.
695/// \param [in, out] a The input matrix A. Overwritten by the Cholesky factor U
696/// or L, as specified by \p uplo.
697/// \param [in] lda The leading dimension of the matrix A.
698/// \param [in] device_ws The workspace.
699/// \param [in] device_ws_size The workspace size in bytes.
700/// \param [out] info If lapack synchronous exception is caught, the value
701/// returned from info() method of the exception is set to \p info.
702inline int potrf(sycl::queue &q, oneapi::mkl::uplo uplo, std::int64_t n,
703                 library_data_t a_type, void *a, std::int64_t lda,
704                 void *device_ws, std::size_t device_ws_size, int *info) {
705  std::size_t device_ws_size_in_element_number =
706      detail::byte_to_element_number(device_ws_size, a_type);
707  return detail::lapack_shim<detail::potrf_impl>(
708      q, a_type, info, "potrf", q, uplo, n, a_type, a, lda, device_ws,
709      device_ws_size_in_element_number, info);
710}
711
712/// Solves a system of linear equations with a Cholesky-factored symmetric
713/// (Hermitian) positive-definite coefficient matrix.
714/// \return Returns 0 if no synchronous exception, otherwise returns 1.
715/// \param [in] q The queue where the routine should be executed. It must
716/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
717/// not defined).
718/// \param [in] uplo Must be uplo::upper or uplo::lower.
719/// \param [in] n The order of the matrix A and the number of rows in matrix B.
720/// \param [in] nrhs The number of right hand sides.
721/// \param [in] a_type The data type of the matrix A.
722/// \param [in, out] a The input matrix A. Overwritten by the Cholesky factor U
723/// or L, as specified by \p uplo.
724/// \param [in] lda The leading dimension of the matrix A.
725/// \param [in] b_type The data type of the matrix B.
726/// \param [in, out] b The matrix B, whose columns are the right-hand sides
727/// for the systems of equations.
728/// \param [in] ldb The leading dimension of the matrix B.
729/// \param [out] info If lapack synchronous exception is caught, the value
730/// returned from info() method of the exception is set to \p info.
731inline int potrs(sycl::queue &q, oneapi::mkl::uplo uplo, std::int64_t n,
732                 std::int64_t nrhs, library_data_t a_type, void *a,
733                 std::int64_t lda, library_data_t b_type, void *b,
734                 std::int64_t ldb, int *info) {
735  return detail::lapack_shim<detail::potrs_impl>(
736      q, a_type, info, "potrs_scratchpad_size/potrs", q, uplo, n, nrhs, a_type,
737      a, lda, b_type, b, ldb, info);
738}
739
740/// Computes the size of workspace memory of syevx/heevx function.
741/// \return Returns 0 if no synchronous exception, otherwise returns 1.
742/// \param [in] q Device queue where computation will be performed. It must
743/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
744/// not defined).
745/// \param [in] jobz Must be job::novec or job::vec.
746/// \param [in] range Must be rangev::all, rangev::values or uplo::indices.
747/// \param [in] uplo Must be uplo::upper or uplo::lower.
748/// \param [in] n The order of the matrix A.
749/// \param [in] a_type The data type of the matrix A.
750/// \param [in] lda The leading dimension of the matrix A.
751/// \param [in] vl If range == rangev::values, the lower bound of the interval
752/// to be searched for eigenvalues
753/// \param [in] vu If range == rangev::values, the upper bound of the interval
754/// to be searched for eigenvalues
755/// \param [in] il If range == rangev::indices, the indices of the smallest
756/// eigenvalue to be returned.
757/// \param [in] iu If range == rangev::indices, the indices of the largest
758/// eigenvalue to be returned.
759/// \param [in] w_type The data type of the eigenvalues.
760/// \param [out] device_ws_size The device workspace size in bytes.
761/// \param [out] host_ws_size The host workspace size in bytes. Currently the
762/// value is always zero.
763inline int syheevx_scratchpad_size(sycl::queue &q, oneapi::mkl::job jobz,
764                                   oneapi::mkl::rangev range,
765                                   oneapi::mkl::uplo uplo, std::int64_t n,
766                                   library_data_t a_type, std::int64_t lda,
767                                   void *vl, void *vu, std::int64_t il,
768                                   std::int64_t iu, library_data_t w_type,
769                                   std::size_t *device_ws_size,
770                                   std::size_t *host_ws_size = nullptr) {
771  if (host_ws_size)
772    *host_ws_size = 0;
773  oneapi::mkl::compz compz_jobz = detail::job2compz(jobz);
774  std::size_t device_ws_size_tmp;
775  int ret = detail::lapack_shim<detail::syheevx_scratchpad_size_impl>(
776      q, a_type, nullptr, "syevx_scratchpad_size/heevx_scratchpad_size", q,
777      compz_jobz, range, uplo, n, lda, vl, vu, il, iu,
778      device_ws_size_tmp);
779  *device_ws_size = detail::element_number_to_byte(device_ws_size_tmp, a_type);
780  return ret;
781}
782
783/// Computes selected eigenvalues and, optionally, eigenvectors of a
784/// symmetric/Hermitian matrix.
785/// \return Returns 0 if no synchronous exception, otherwise returns 1.
786/// \param [in] q Device queue where computation will be performed. It must
787/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
788/// not defined).
789/// \param [in] jobz Must be job::novec or job::vec.
790/// \param [in] range Must be rangev::all, rangev::values or uplo::indices.
791/// \param [in] uplo Must be uplo::upper or uplo::lower.
792/// \param [in] n The order of the matrix A.
793/// \param [in] a_type The data type of the matrix A.
794/// \param [in, out] a The input matrix A. On exit, the lower or upper triangle is
795/// overwritten.
796/// \param [in] lda The leading dimension of the matrix A.
797/// \param [in] vl If range == rangev::values, the lower bound of the interval
798/// to be searched for eigenvalues
799/// \param [in] vu If range == rangev::values, the upper bound of the interval
800/// to be searched for eigenvalues
801/// \param [in] il If range == rangev::indices, the indices of the smallest
802/// eigenvalue to be returned.
803/// \param [in] iu If range == rangev::indices, the indices of the largest
804/// eigenvalue to be returned.
805/// \param [out] m The total number of eigenvalues found.
806/// \param [in] w_type The data type of the eigenvalues.
807/// \param [out] w The eigenvalues of the matrix A in ascending order.
808/// \param [in] device_ws The workspace.
809/// \param [in] device_ws_size The workspace size in bytes.
810/// \param [out] info If lapack synchronous exception is caught, the value
811/// returned from info() method of the exception is set to \p info.
812inline int syheevx(sycl::queue &q, oneapi::mkl::job jobz,
813                   oneapi::mkl::rangev range, oneapi::mkl::uplo uplo,
814                   std::int64_t n, library_data_t a_type, void *a,
815                   std::int64_t lda, void *vl, void *vu, std::int64_t il,
816                   std::int64_t iu, std::int64_t *m, library_data_t w_type,
817                   void *w, void *device_ws, std::size_t device_ws_size,
818                   int *info) {
819  std::size_t device_ws_size_in_element_number =
820      detail::byte_to_element_number(device_ws_size, a_type);
821  oneapi::mkl::compz compz_jobz = detail::job2compz(jobz);
822  int ret = detail::lapack_shim<detail::syheevx_impl>(
823      q, a_type, info, "syevx/heevx", q, compz_jobz, range, uplo, n, a_type, a,
824      lda, vl, vu, il, iu, m, w_type, w, device_ws,
825      device_ws_size_in_element_number, info);
826  q.wait();
827  return ret;
828}
829
830/// Computes the size of workspace memory of syevx/heevx function.
831/// \return Returns 0 if no synchronous exception, otherwise returns 1.
832/// \param [in] q Device queue where computation will be performed. It must
833/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
834/// not defined).
835/// \param [in] jobz Must be job::novec or job::vec.
836/// \param [in] range Must be rangev::all, rangev::values or uplo::indices.
837/// \param [in] uplo Must be uplo::upper or uplo::lower.
838/// \param [in] n The order of the matrix A.
839/// \param [in] lda The leading dimension of the matrix A.
840/// \param [in] vl If range == rangev::values, the lower bound of the interval
841/// to be searched for eigenvalues
842/// \param [in] vu If range == rangev::values, the upper bound of the interval
843/// to be searched for eigenvalues
844/// \param [in] il If range == rangev::indices, the indices of the smallest
845/// eigenvalue to be returned.
846/// \param [in] iu If range == rangev::indices, the indices of the largest
847/// eigenvalue to be returned.
848/// \param [out] device_ws_size The device workspace size as a number of
849/// elements of type \tparam T.
850template <typename T, typename ValueT>
851inline int syheevx_scratchpad_size(sycl::queue &q, oneapi::mkl::job jobz,
852                                   oneapi::mkl::rangev range,
853                                   oneapi::mkl::uplo uplo, int n, int lda,
854                                   ValueT vl, ValueT vu, int il, int iu,
855                                   int *device_ws_size) {
856  oneapi::mkl::compz compz_jobz = detail::job2compz(jobz);
857  std::size_t device_ws_size_tmp;
858  int ret = detail::lapack_shim<detail::syheevx_scratchpad_size_impl>(
859      q, detail::get_library_data_t_from_type<T>(), nullptr,
860      "syevx_scratchpad_size/heevx_scratchpad_size", q, compz_jobz, range, uplo,
861      n, lda, &vl, &vu, il, iu, device_ws_size_tmp);
862  *device_ws_size = (int)device_ws_size_tmp;
863  return ret;
864}
865
866/// Computes selected eigenvalues and, optionally, eigenvectors of a
867/// symmetric/Hermitian matrix.
868/// \return Returns 0 if no synchronous exception, otherwise returns 1.
869/// \param [in] q Device queue where computation will be performed. It must
870/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
871/// not defined).
872/// \param [in] jobz Must be job::novec or job::vec.
873/// \param [in] range Must be rangev::all, rangev::values or uplo::indices.
874/// \param [in] uplo Must be uplo::upper or uplo::lower.
875/// \param [in] n The order of the matrix A.
876/// \param [in, out] a The input matrix A. On exit, the lower or upper triangle is
877/// overwritten.
878/// \param [in] lda The leading dimension of the matrix A.
879/// \param [in] vl If range == rangev::values, the lower bound of the interval
880/// to be searched for eigenvalues
881/// \param [in] vu If range == rangev::values, the upper bound of the interval
882/// to be searched for eigenvalues
883/// \param [in] il If range == rangev::indices, the indices of the smallest
884/// eigenvalue to be returned.
885/// \param [in] iu If range == rangev::indices, the indices of the largest
886/// eigenvalue to be returned.
887/// \param [out] m The total number of eigenvalues found.
888/// \param [out] w The eigenvalues of the matrix A in ascending order.
889/// \param [in] device_ws The workspace.
890/// \param [in] device_ws_size The device workspace size as a number of
891/// elements of type \tparam T.
892/// \param [out] info If lapack synchronous exception is caught, the value
893/// returned from info() method of the exception is set to \p info.
894template <typename T, typename ValueT>
895inline int syheevx(sycl::queue &q, oneapi::mkl::job jobz,
896                   oneapi::mkl::rangev range, oneapi::mkl::uplo uplo, int n,
897                   T *a, int lda, ValueT vl, ValueT vu, int il, int iu, int *m,
898                   ValueT *w, T *device_ws, int device_ws_size, int *info) {
899  oneapi::mkl::compz compz_jobz = detail::job2compz(jobz);
900  std::int64_t m64;
901  int ret = detail::lapack_shim<detail::syheevx_impl>(
902      q, detail::get_library_data_t_from_type<T>(), info, "syevx/heevx", q,
903      compz_jobz, range, uplo, n, detail::get_library_data_t_from_type<T>(), a,
904      lda, &vl, &vu, il, iu, &m64,
905      detail::get_library_data_t_from_type<ValueT>(), w, device_ws,
906      device_ws_size, info);
907  q.wait();
908  *m = (int)m64;
909  return ret;
910}
911
912/// Computes the size of workspace memory of sygvx/hegvx function.
913/// \return Returns 0 if no synchronous exception, otherwise returns 1.
914/// \param [in] q Device queue where computation will be performed. It must
915/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
916/// not defined).
917/// \param [in] itype Must be 1, 2 or 3.
918/// \param [in] jobz Must be job::novec or job::vec.
919/// \param [in] range Must be rangev::all, rangev::values or uplo::indices.
920/// \param [in] uplo Must be uplo::upper or uplo::lower.
921/// \param [in] n The order of the matrix A.
922/// \param [in] lda The leading dimension of the matrix A.
923/// \param [in] ldb The leading dimension of the matrix B.
924/// \param [in] vl If range == rangev::values, the lower bound of the interval
925/// to be searched for eigenvalues
926/// \param [in] vu If range == rangev::values, the upper bound of the interval
927/// to be searched for eigenvalues
928/// \param [in] il If range == rangev::indices, the indices of the smallest
929/// eigenvalue to be returned.
930/// \param [in] iu If range == rangev::indices, the indices of the largest
931/// eigenvalue to be returned.
932/// \param [in] device_ws_size The device workspace size as a number of
933/// elements of type \tparam T.
934template <typename T, typename ValueT>
935inline int
936syhegvx_scratchpad_size(sycl::queue &q, int itype, oneapi::mkl::job jobz,
937                        oneapi::mkl::rangev range, oneapi::mkl::uplo uplo,
938                        int n, int lda, int ldb, ValueT vl, ValueT vu, int il,
939                        int iu, int *device_ws_size) {
940  oneapi::mkl::compz compz_jobz = detail::job2compz(jobz);
941  std::size_t device_ws_size_tmp;
942  int ret = detail::lapack_shim<detail::syhegvx_scratchpad_size_impl>(
943      q, detail::get_library_data_t_from_type<T>(), nullptr,
944      "sygvx_scratchpad_size/hegvx_scratchpad_size", q, itype, compz_jobz,
945      range, uplo, n, lda, ldb, &vl, &vu, il, iu, device_ws_size_tmp);
946  *device_ws_size = (int)device_ws_size_tmp;
947  return ret;
948}
949
950/// Computes selected eigenvalues and, optionally, eigenvectors of a real
951/// generalized symmetric/Hermitian definite eigenproblem.
952/// \return Returns 0 if no synchronous exception, otherwise returns 1.
953/// \param [in] q Device queue where computation will be performed. It must
954/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
955/// not defined).
956/// \param [in] itype Must be 1, 2 or 3.
957/// \param [in] jobz Must be job::novec or job::vec.
958/// \param [in] range Must be rangev::all, rangev::values or uplo::indices.
959/// \param [in] uplo Must be uplo::upper or uplo::lower.
960/// \param [in] n The order of the matrix A.
961/// \param [in, out] a The input matrix A. On exit, the lower or upper triangle is
962/// overwritten.
963/// \param [in] lda The leading dimension of the matrix A.
964/// \param [in, out] b The input matrix B.
965/// \param [in] ldb The leading dimension of the matrix B.
966/// \param [in] vl If range == rangev::values, the lower bound of the interval
967/// to be searched for eigenvalues
968/// \param [in] vu If range == rangev::values, the upper bound of the interval
969/// to be searched for eigenvalues
970/// \param [in] il If range == rangev::indices, the indices of the smallest
971/// eigenvalue to be returned.
972/// \param [in] iu If range == rangev::indices, the indices of the largest
973/// eigenvalue to be returned.
974/// \param [out] m The total number of eigenvalues found.
975/// \param [out] w The eigenvalues of the matrix A in ascending order.
976/// \param [in] device_ws The workspace.
977/// \param [in] device_ws_size The device workspace size as a number of
978/// elements of type \tparam T.
979/// \param [out] info If lapack synchronous exception is caught, the value
980/// returned from info() method of the exception is set to \p info.
981template <typename T, typename ValueT>
982inline int syhegvx(sycl::queue &q, int itype, oneapi::mkl::job jobz,
983                   oneapi::mkl::rangev range, oneapi::mkl::uplo uplo, int n,
984                   T *a, int lda, T *b, int ldb, ValueT vl, ValueT vu, int il,
985                   int iu, int *m, ValueT *w, T *device_ws, int device_ws_size,
986                   int *info) {
987  oneapi::mkl::compz compz_jobz = detail::job2compz(jobz);
988  std::int64_t m64;
989  int ret = detail::lapack_shim<detail::syhegvx_impl>(
990      q, detail::get_library_data_t_from_type<T>(), info, "sygvx/hegvx", q,
991      itype, compz_jobz, range, uplo, n, a, lda, b, ldb, &vl, &vu, il, iu, &m64,
992      w, device_ws, device_ws_size, info);
993  q.wait();
994  *m = (int)m64;
995  return ret;
996}
997
998/// Computes the size of workspace memory of sygvd/hegvd function.
999/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1000/// \param [in] q Device queue where computation will be performed. It must
1001/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1002/// not defined).
1003/// \param [in] itype Must be 1, 2 or 3.
1004/// \param [in] jobz Must be job::novec or job::vec.
1005/// \param [in] uplo Must be uplo::upper or uplo::lower.
1006/// \param [in] n The order of the matrix A.
1007/// \param [in] lda The leading dimension of the matrix A.
1008/// \param [in] ldb The leading dimension of the matrix B.
1009/// \param [in] device_ws_size The device workspace size as a number of
1010/// elements of type \tparam T.
1011template <typename T>
1012inline int syhegvd_scratchpad_size(sycl::queue &q, int itype,
1013                                   oneapi::mkl::job jobz,
1014                                   oneapi::mkl::uplo uplo, int n, int lda,
1015                                   int ldb, int *device_ws_size) {
1016  std::size_t device_ws_size_tmp;
1017  int ret = detail::lapack_shim<detail::syhegvd_scratchpad_size_impl>(
1018      q, detail::get_library_data_t_from_type<T>(), nullptr,
1019      "sygvd_scratchpad_size/hegvd_scratchpad_size", q, itype, jobz, uplo, n,
1020      lda, ldb, device_ws_size_tmp);
1021  *device_ws_size = (int)device_ws_size_tmp;
1022  return ret;
1023}
1024
1025/// Computes all eigenvalues and, optionally, eigenvectors of a real generalized
1026/// symmetric/Hermitian definite eigenproblem using a divide and conquer method.
1027/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1028/// \param [in] q Device queue where computation will be performed. It must
1029/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1030/// not defined).
1031/// \param [in] itype Must be 1, 2 or 3.
1032/// \param [in] jobz Must be job::novec or job::vec.
1033/// \param [in] uplo Must be uplo::upper or uplo::lower.
1034/// \param [in] n The order of the matrix A.
1035/// \param [in, out] a The input matrix A. On exit, it is overwritten by eigenvectors.
1036/// \param [in] lda The leading dimension of the matrix A.
1037/// \param [in, out] b The input matrix B.
1038/// \param [in] ldb The leading dimension of the matrix B.
1039/// \param [out] w The eigenvalues of the matrix A in ascending order.
1040/// \param [in] device_ws The workspace.
1041/// \param [in] device_ws_size The device workspace size as a number of
1042/// elements of type \tparam T.
1043/// \param [out] info If lapack synchronous exception is caught, the value
1044/// returned from info() method of the exception is set to \p info.
1045template <typename T, typename ValueT>
1046inline int syhegvd(sycl::queue &q, int itype, oneapi::mkl::job jobz,
1047                   oneapi::mkl::uplo uplo, int n, T *a, int lda, T *b, int ldb,
1048                   ValueT *w, T *device_ws, int device_ws_size, int *info) {
1049  return detail::lapack_shim<detail::syhegvd_impl>(
1050      q, detail::get_library_data_t_from_type<T>(), info, "sygvd/hegvd", q,
1051      itype, jobz, uplo, n, a, lda, b, ldb, w, device_ws, device_ws_size, info);
1052}
1053
1054/// Computes the size of workspace memory of syev/heev function.
1055/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1056/// \param [in] q Device queue where computation will be performed. It must
1057/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1058/// not defined).
1059/// \param [in] jobz Must be job::novec or job::vec.
1060/// \param [in] uplo Must be uplo::upper or uplo::lower.
1061/// \param [in] n The order of the matrix A.
1062/// \param [in] lda The leading dimension of the matrix A.
1063/// \param [out] device_ws_size The device workspace size as a number of
1064/// elements of type \tparam T.
1065template <typename T>
1066inline int syheev_scratchpad_size(sycl::queue &q, oneapi::mkl::job jobz,
1067                                  oneapi::mkl::uplo uplo, int n, int lda,
1068                                  int *device_ws_size) {
1069  std::size_t device_ws_size_tmp;
1070  oneapi::mkl::compz compz_jobz = detail::job2compz(jobz);
1071  int ret = detail::lapack_shim<detail::syheev_scratchpad_size_impl>(
1072      q, detail::get_library_data_t_from_type<T>(), nullptr,
1073      "syev_scratchpad_size/heev_scratchpad_size", q, compz_jobz, uplo, n, lda,
1074      device_ws_size_tmp);
1075  *device_ws_size = (int)device_ws_size_tmp;
1076  return ret;
1077}
1078
1079/// Computes all eigenvalues and, optionally, eigenvectors of a real symmetric
1080/// or Hermitian matrix.
1081/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1082/// \param [in] q Device queue where computation will be performed. It must
1083/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1084/// not defined).
1085/// \param [in] jobz Must be job::novec or job::vec.
1086/// \param [in] uplo Must be uplo::upper or uplo::lower.
1087/// \param [in] n The order of the matrix A.
1088/// \param [in, out] a The input matrix A. On exit, it is overwritten by
1089/// eigenvectors.
1090/// \param [in] lda The leading dimension of the matrix A.
1091/// \param [out] w The eigenvalues of the matrix A in ascending order.
1092/// \param [in] device_ws The workspace.
1093/// \param [in] device_ws_size The device workspace size as a number of
1094/// elements of type \tparam T.
1095/// \param [out] info If lapack synchronous exception is caught, the value
1096/// returned from info() method of the exception is set to \p info.
1097template <typename T, typename ValueT>
1098inline int syheev(sycl::queue &q, oneapi::mkl::job jobz, oneapi::mkl::uplo uplo,
1099                  int n, T *a, int lda, ValueT *w, T *device_ws,
1100                  int device_ws_size, int *info) {
1101  oneapi::mkl::compz compz_jobz = detail::job2compz(jobz);
1102  return detail::lapack_shim<detail::syheev_impl>(
1103      q, detail::get_library_data_t_from_type<T>(), info, "syev/heev", q,
1104      compz_jobz, uplo, n, a, lda, w, device_ws, device_ws_size, info);
1105}
1106
1107/// Computes the size of workspace memory of syevd/heevd function.
1108/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1109/// \param [in] q Device queue where computation will be performed. It must
1110/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1111/// not defined).
1112/// \param [in] jobz Must be job::novec or job::vec.
1113/// \param [in] uplo Must be uplo::upper or uplo::lower.
1114/// \param [in] n The order of the matrix A.
1115/// \param [in] a_type The data type of the matrix A.
1116/// \param [in] lda The leading dimension of the matrix A.
1117/// \param [in] w_type The data type of the eigenvalues.
1118/// \param [out] device_ws_size The device workspace in bytes.
1119/// \param [out] host_ws_size The host workspace size in bytes. Currently the
1120/// value is always zero.
1121inline int syheevd_scratchpad_size(sycl::queue &q, oneapi::mkl::job jobz,
1122                                   oneapi::mkl::uplo uplo, std::int64_t n,
1123                                   library_data_t a_type, std::int64_t lda,
1124                                   library_data_t w_type,
1125                                   std::size_t *device_ws_size,
1126                                   std::size_t *host_ws_size = nullptr) {
1127  if (host_ws_size)
1128    *host_ws_size = 0;
1129  std::size_t device_ws_size_tmp;
1130  int ret = detail::lapack_shim<detail::syheevd_scratchpad_size_impl>(
1131      q, a_type, nullptr, "syevd_scratchpad_size/heevd_scratchpad_size", q,
1132      jobz, uplo, n, a_type, lda, device_ws_size_tmp);
1133  *device_ws_size = detail::element_number_to_byte(device_ws_size_tmp, a_type);
1134  return ret;
1135}
1136
1137/// Computes all eigenvalues and, optionally, all eigenvectors of a real
1138/// symmetric or Hermitian matrix using divide and conquer algorithm.
1139/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1140/// \param [in] q Device queue where computation will be performed. It must
1141/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1142/// not defined).
1143/// \param [in] jobz Must be job::novec or job::vec.
1144/// \param [in] uplo Must be uplo::upper or uplo::lower.
1145/// \param [in] n The order of the matrix A.
1146/// \param [in] a_type The data type of the matrix A.
1147/// \param [in, out] a The input matrix A. On exit, it is overwritten by
1148/// eigenvectors.
1149/// \param [in] lda The leading dimension of the matrix A.
1150/// \param [in] w_type The data type of the eigenvalues.
1151/// \param [out] w The eigenvalues of the matrix A in ascending order.
1152/// \param [in] device_ws The workspace.
1153/// \param [in] device_ws_size The workspace size in bytes.
1154/// \param [out] info If lapack synchronous exception is caught, the value
1155/// returned from info() method of the exception is set to \p info.
1156inline int syheevd(sycl::queue &q, oneapi::mkl::job jobz,
1157                   oneapi::mkl::uplo uplo, std::int64_t n,
1158                   library_data_t a_type, void *a, std::int64_t lda,
1159                   library_data_t w_type, void *w, void *device_ws,
1160                   std::size_t device_ws_size, int *info) {
1161  std::size_t device_ws_size_in_element_number =
1162      detail::byte_to_element_number(device_ws_size, a_type);
1163  return detail::lapack_shim<detail::syheevd_impl>(
1164      q, a_type, info, "syevd/heevd", q, jobz, uplo, n, a_type, a, lda, w,
1165      device_ws, device_ws_size_in_element_number, info);
1166}
1167
1168/// Computes the size of workspace memory of syevd/heevd function.
1169/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1170/// \param [in] q Device queue where computation will be performed. It must
1171/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1172/// not defined).
1173/// \param [in] jobz Must be job::novec or job::vec.
1174/// \param [in] uplo Must be uplo::upper or uplo::lower.
1175/// \param [in] n The order of the matrix A.
1176/// \param [in] a_type The data type of the matrix A.
1177/// \param [in] lda The leading dimension of the matrix A.
1178/// \param [in] w_type The data type of the eigenvalues.
1179/// \param [out] device_ws_size The device workspace size as a number of
1180/// elements of type \tparam T.
1181template <typename T>
1182inline int syheevd_scratchpad_size(sycl::queue &q, oneapi::mkl::job jobz,
1183                                   oneapi::mkl::uplo uplo, std::int64_t n,
1184                                   std::int64_t lda, int *device_ws_size) {
1185  std::size_t device_ws_size_tmp;
1186  int ret = detail::lapack_shim<detail::syheevd_scratchpad_size_impl>(
1187      q, detail::get_library_data_t_from_type<T>(), nullptr,
1188      "syevd_scratchpad_size/heevd_scratchpad_size", q, jobz, uplo, n,
1189      detail::get_library_data_t_from_type<T>(), lda, device_ws_size_tmp);
1190  *device_ws_size = (int)device_ws_size_tmp;
1191  return ret;
1192}
1193
1194/// Computes all eigenvalues and, optionally, all eigenvectors of a real
1195/// symmetric or Hermitian matrix using divide and conquer algorithm.
1196/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1197/// \param [in] q Device queue where computation will be performed. It must
1198/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1199/// not defined).
1200/// \param [in] jobz Must be job::novec or job::vec.
1201/// \param [in] uplo Must be uplo::upper or uplo::lower.
1202/// \param [in] n The order of the matrix A.
1203/// \param [in] a_type The data type of the matrix A.
1204/// \param [in, out] a The input matrix A. On exit, it is overwritten by
1205/// eigenvectors.
1206/// \param [in] lda The leading dimension of the matrix A.
1207/// \param [in] w_type The data type of the eigenvalues.
1208/// \param [out] w The eigenvalues of the matrix A in ascending order.
1209/// \param [in] device_ws The workspace.
1210/// \param [in] device_ws_size The workspace size as a number of
1211/// elements of type \tparam T.
1212/// \param [out] info If lapack synchronous exception is caught, the value
1213/// returned from info() method of the exception is set to \p info.
1214template <typename T, typename ValueT>
1215inline int syheevd(sycl::queue &q, oneapi::mkl::job jobz,
1216                   oneapi::mkl::uplo uplo, std::int64_t n, T *a,
1217                   std::int64_t lda, ValueT *w, T *device_ws,
1218                   int device_ws_size, int *info) {
1219  return detail::lapack_shim<detail::syheevd_impl>(
1220      q, detail::get_library_data_t_from_type<T>(), info, "syevd/heevd", q,
1221      jobz, uplo, n, detail::get_library_data_t_from_type<T>(), a, lda, w,
1222      device_ws, device_ws_size, info);
1223}
1224
1225/// Computes the size of workspace memory of trtri function.
1226/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1227/// \param [in] q Device queue where computation will be performed. It must
1228/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1229/// not defined).
1230/// \param [in] uplo Must be uplo::upper or uplo::lower.
1231/// \param [in] diag Must be diag::nonunit or diag::unit.
1232/// \param [in] n The order of the matrix A.
1233/// \param [in] a_type The data type of the matrix A.
1234/// \param [in] lda The leading dimension of the matrix A.
1235/// \param [out] device_ws_size The device workspace in bytes.
1236/// \param [out] host_ws_size The host workspace size in bytes. Currently the
1237/// value is always zero.
1238inline int trtri_scratchpad_size(sycl::queue &q, oneapi::mkl::uplo uplo,
1239                                 oneapi::mkl::diag diag, std::int64_t n,
1240                                 library_data_t a_type, std::int64_t lda,
1241                                 std::size_t *device_ws_size,
1242                                 std::size_t *host_ws_size = nullptr) {
1243  if (host_ws_size)
1244    *host_ws_size = 0;
1245  std::size_t device_ws_size_tmp;
1246  int ret = detail::lapack_shim<detail::trtri_scratchpad_size_impl>(
1247      q, a_type, nullptr, "trtri_scratchpad_size", q, uplo, diag, n, a_type,
1248      lda, device_ws_size_tmp);
1249  *device_ws_size = detail::element_number_to_byte(device_ws_size_tmp, a_type);
1250  return ret;
1251}
1252
1253/// Computes the inverse of a triangular matrix.
1254/// \return Returns 0 if no synchronous exception, otherwise returns 1.
1255/// \param [in] q Device queue where computation will be performed. It must
1256/// have the in_order property when using the USM mode (DPCT_USM_LEVEL_NONE is
1257/// not defined).
1258/// \param [in] uplo Must be uplo::upper or uplo::lower.
1259/// \param [in] diag Must be diag::nonunit or diag::unit.
1260/// \param [in] n The order of the matrix A.
1261/// \param [in] a_type The data type of the matrix A.
1262/// \param [in, out] a The input matrix A. On exit, it is overwritten by
1263/// the inverse matrix of A.
1264/// \param [in] lda The leading dimension of the matrix A.
1265/// \param [in] device_ws The workspace.
1266/// \param [in] device_ws_size The workspace size in bytes.
1267/// \param [out] info If lapack synchronous exception is caught, the value
1268/// returned from info() method of the exception is set to \p info.
1269inline int trtri(sycl::queue &q, oneapi::mkl::uplo uplo, oneapi::mkl::diag diag,
1270                 std::int64_t n, library_data_t a_type, void *a,
1271                 std::int64_t lda, void *device_ws, std::size_t device_ws_size,
1272                 int *info) {
1273#ifdef DPCT_USM_LEVEL_NONE
1274  throw std::runtime_error("this API is unsupported when USM level is none");
1275#else
1276  std::size_t device_ws_size_in_element_number =
1277      detail::byte_to_element_number(device_ws_size, a_type);
1278  return detail::lapack_shim<detail::trtri_impl>(
1279      q, a_type, info, "trtri", q, uplo, diag, n, a_type, a, lda, device_ws,
1280      device_ws_size_in_element_number, info);
1281#endif
1282}
1283} // namespace lapack
1284} // namespace dpct
1285
1286#endif // __DPCT_LAPACK_UTILS_HPP__