+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
28#include <pybind11/pybind11.h>
+
+
+
31#include "utils/memory_overlap.hpp"
+
32#include "utils/output_validation.hpp"
+
33#include "utils/type_dispatch.hpp"
+
+
35#include "common_helpers.hpp"
+
36#include "linalg_exceptions.hpp"
+
+
38namespace dpnp::extensions::lapack::gesv_utils
+
+
40namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
+
41namespace py = pybind11;
+
+
43inline void common_gesv_checks(sycl::queue &exec_q,
+
44 dpctl::tensor::usm_ndarray coeff_matrix,
+
45 dpctl::tensor::usm_ndarray dependent_vals,
+
46 const py::ssize_t *coeff_matrix_shape,
+
47 const py::ssize_t *dependent_vals_shape,
+
48 const int expected_coeff_matrix_ndim,
+
49 const int min_dependent_vals_ndim,
+
50 const int max_dependent_vals_ndim)
+
+
52 const int coeff_matrix_nd = coeff_matrix.get_ndim();
+
53 const int dependent_vals_nd = dependent_vals.get_ndim();
+
+
55 if (coeff_matrix_nd != expected_coeff_matrix_ndim) {
+
56 throw py::value_error(
"The coefficient matrix has ndim=" +
+
57 std::to_string(coeff_matrix_nd) +
", but a " +
+
58 std::to_string(expected_coeff_matrix_ndim) +
+
59 "-dimensional array is expected.");
+
+
+
62 if (dependent_vals_nd < min_dependent_vals_ndim ||
+
63 dependent_vals_nd > max_dependent_vals_ndim)
+
+
65 throw py::value_error(
"The dependent values array has ndim=" +
+
66 std::to_string(dependent_vals_nd) +
", but a " +
+
67 std::to_string(min_dependent_vals_ndim) +
+
68 "-dimensional or a " +
+
69 std::to_string(max_dependent_vals_ndim) +
+
70 "-dimensional array is expected.");
+
+
+
+
+
+
+
+
+
79 if (coeff_matrix_shape[0] != coeff_matrix_shape[1]) {
+
80 throw py::value_error(
"The coefficient matrix must be square,"
+
81 " but got a shape of (" +
+
82 std::to_string(coeff_matrix_shape[0]) +
", " +
+
83 std::to_string(coeff_matrix_shape[1]) +
").");
+
+
85 if (coeff_matrix_shape[0] != dependent_vals_shape[0]) {
+
86 throw py::value_error(
"The first dimension (n) of coeff_matrix and"
+
87 " dependent_vals must be the same, but got " +
+
88 std::to_string(coeff_matrix_shape[0]) +
" and " +
+
89 std::to_string(dependent_vals_shape[0]) +
".");
+
+
+
+
93 if (!dpctl::utils::queues_are_compatible(exec_q,
+
94 {coeff_matrix, dependent_vals}))
+
+
96 throw py::value_error(
+
97 "Execution queue is not compatible with allocation queues.");
+
+
+
100 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
+
101 if (overlap(coeff_matrix, dependent_vals)) {
+
102 throw py::value_error(
+
103 "The arrays of coefficients and dependent variables "
+
104 "are overlapping segments of memory.");
+
+
+
107 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(
+
+
+
110 const bool is_coeff_matrix_f_contig = coeff_matrix.is_f_contiguous();
+
111 if (!is_coeff_matrix_f_contig) {
+
112 throw py::value_error(
"The coefficient matrix "
+
113 "must be F-contiguous.");
+
+
+
116 const bool is_dependent_vals_f_contig = dependent_vals.is_f_contiguous();
+
117 if (!is_dependent_vals_f_contig) {
+
118 throw py::value_error(
"The array of dependent variables "
+
119 "must be F-contiguous.");
+
+
+
122 auto array_types = dpctl_td_ns::usm_ndarray_types();
+
123 const int coeff_matrix_type_id =
+
124 array_types.typenum_to_lookup_id(coeff_matrix.get_typenum());
+
125 const int dependent_vals_type_id =
+
126 array_types.typenum_to_lookup_id(dependent_vals.get_typenum());
+
+
128 if (coeff_matrix_type_id != dependent_vals_type_id) {
+
129 throw py::value_error(
"The types of the coefficient matrix and "
+
130 "dependent variables are mismatched.");
+
+
+
+
+
135inline void handle_lapack_exc(sycl::queue &exec_q,
+
136 const std::int64_t lda,
+
+
138 std::int64_t scratchpad_size,
+
+
+
141 const oneapi::mkl::lapack::exception &e,
+
142 std::stringstream &error_msg)
+
+
144 std::int64_t info = e.info();
+
+
146 error_msg <<
"Parameter number " << -info <<
" had an illegal value.";
+
+
148 else if (info == scratchpad_size && e.detail() != 0) {
+
149 error_msg <<
"Insufficient scratchpad size. Required size is at least "
+
+
+
+
+
154 exec_q.memcpy(&host_U, &a[(info - 1) * lda + info - 1],
sizeof(T))
+
+
+
157 using ThresholdType =
typename helper::value_type_of<T>::type;
+
+
159 const auto threshold =
+
160 std::numeric_limits<ThresholdType>::epsilon() * 100;
+
161 if (std::abs(host_U) < threshold) {
+
162 if (scratchpad !=
nullptr)
+
163 sycl::free(scratchpad, exec_q);
+
+
165 sycl::free(ipiv, exec_q);
+
166 throw LinAlgError(
"The input coefficient matrix is singular.");
+
+
+
169 error_msg <<
"Unexpected MKL exception caught during gesv() "
+
+
171 << e.what() <<
"\ninfo: " << e.info();
+
+
+
+
+
176 <<
"Unexpected MKL exception caught during gesv() call:\nreason: "
+
177 << e.what() <<
"\ninfo: " << e.info();
+
+
+
+