DPNP C++ backend kernel library 0.20.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
gesv_common_utils.hpp
1//*****************************************************************************
2// Copyright (c) 2024, Intel Corporation
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7// - Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// - Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12// - Neither the name of the copyright holder nor the names of its contributors
13// may be used to endorse or promote products derived from this software
14// without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26// THE POSSIBILITY OF SUCH DAMAGE.
27//*****************************************************************************
28
29#pragma once
30
31#include <pybind11/pybind11.h>
32
33// dpctl tensor headers
34#include "utils/memory_overlap.hpp"
35#include "utils/output_validation.hpp"
36#include "utils/sycl_alloc_utils.hpp"
37#include "utils/type_dispatch.hpp"
38
39#include "common_helpers.hpp"
40#include "linalg_exceptions.hpp"
41
42namespace dpnp::extensions::lapack::gesv_utils
43{
44namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
45namespace py = pybind11;
46
47inline void common_gesv_checks(sycl::queue &exec_q,
48 const dpctl::tensor::usm_ndarray &coeff_matrix,
49 const dpctl::tensor::usm_ndarray &dependent_vals,
50 const py::ssize_t *coeff_matrix_shape,
51 const py::ssize_t *dependent_vals_shape,
52 const int expected_coeff_matrix_ndim,
53 const int min_dependent_vals_ndim,
54 const int max_dependent_vals_ndim)
55{
56 const int coeff_matrix_nd = coeff_matrix.get_ndim();
57 const int dependent_vals_nd = dependent_vals.get_ndim();
58
59 if (coeff_matrix_nd != expected_coeff_matrix_ndim) {
60 throw py::value_error("The coefficient matrix has ndim=" +
61 std::to_string(coeff_matrix_nd) + ", but a " +
62 std::to_string(expected_coeff_matrix_ndim) +
63 "-dimensional array is expected.");
64 }
65
66 if (dependent_vals_nd < min_dependent_vals_ndim ||
67 dependent_vals_nd > max_dependent_vals_ndim)
68 {
69 throw py::value_error("The dependent values array has ndim=" +
70 std::to_string(dependent_vals_nd) + ", but a " +
71 std::to_string(min_dependent_vals_ndim) +
72 "-dimensional or a " +
73 std::to_string(max_dependent_vals_ndim) +
74 "-dimensional array is expected.");
75 }
76
77 // The coeff_matrix and dependent_vals arrays must be F-contiguous arrays
78 // for gesv
79 // with the shapes (n, n) and (n, nrhs) or (n, ) respectively;
80 // for gesv_batch
81 // with the shapes (n, n, batch_size) and (n, nrhs, batch_size) or
82 // (n, batch_size) respectively
83 if (coeff_matrix_shape[0] != coeff_matrix_shape[1]) {
84 throw py::value_error("The coefficient matrix must be square,"
85 " but got a shape of (" +
86 std::to_string(coeff_matrix_shape[0]) + ", " +
87 std::to_string(coeff_matrix_shape[1]) + ").");
88 }
89 if (coeff_matrix_shape[0] != dependent_vals_shape[0]) {
90 throw py::value_error("The first dimension (n) of coeff_matrix and"
91 " dependent_vals must be the same, but got " +
92 std::to_string(coeff_matrix_shape[0]) + " and " +
93 std::to_string(dependent_vals_shape[0]) + ".");
94 }
95
96 // check compatibility of execution queue and allocation queue
97 if (!dpctl::utils::queues_are_compatible(exec_q,
98 {coeff_matrix, dependent_vals}))
99 {
100 throw py::value_error(
101 "Execution queue is not compatible with allocation queues.");
102 }
103
104 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
105 if (overlap(coeff_matrix, dependent_vals)) {
106 throw py::value_error(
107 "The arrays of coefficients and dependent variables "
108 "are overlapping segments of memory.");
109 }
110
111 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(
112 dependent_vals);
113
114 const bool is_coeff_matrix_f_contig = coeff_matrix.is_f_contiguous();
115 if (!is_coeff_matrix_f_contig) {
116 throw py::value_error("The coefficient matrix "
117 "must be F-contiguous.");
118 }
119
120 const bool is_dependent_vals_f_contig = dependent_vals.is_f_contiguous();
121 if (!is_dependent_vals_f_contig) {
122 throw py::value_error("The array of dependent variables "
123 "must be F-contiguous.");
124 }
125
126 auto array_types = dpctl_td_ns::usm_ndarray_types();
127 const int coeff_matrix_type_id =
128 array_types.typenum_to_lookup_id(coeff_matrix.get_typenum());
129 const int dependent_vals_type_id =
130 array_types.typenum_to_lookup_id(dependent_vals.get_typenum());
131
132 if (coeff_matrix_type_id != dependent_vals_type_id) {
133 throw py::value_error("The types of the coefficient matrix and "
134 "dependent variables are mismatched.");
135 }
136}
137
138template <typename T>
139inline void handle_lapack_exc(sycl::queue &exec_q,
140 const std::int64_t lda,
141 T *a,
142 std::int64_t scratchpad_size,
143 T *scratchpad,
144 std::int64_t *ipiv,
145 const oneapi::mkl::lapack::exception &e,
146 std::stringstream &error_msg)
147{
148 std::int64_t info = e.info();
149 if (info < 0) {
150 error_msg << "Parameter number " << -info << " had an illegal value.";
151 }
152 else if (info == scratchpad_size && e.detail() != 0) {
153 error_msg << "Insufficient scratchpad size. Required size is at least "
154 << e.detail();
155 }
156 else if (info > 0) {
157 T host_U;
158 exec_q.memcpy(&host_U, &a[(info - 1) * lda + info - 1], sizeof(T))
159 .wait();
160
161 using ThresholdType = typename helper::value_type_of<T>::type;
162
163 const auto threshold =
164 std::numeric_limits<ThresholdType>::epsilon() * 100;
165 if (std::abs(host_U) < threshold) {
166 using dpctl::tensor::alloc_utils::sycl_free_noexcept;
167
168 if (scratchpad != nullptr)
169 sycl_free_noexcept(scratchpad, exec_q);
170 if (ipiv != nullptr)
171 sycl_free_noexcept(ipiv, exec_q);
172 throw LinAlgError("The input coefficient matrix is singular.");
173 }
174 else {
175 error_msg << "Unexpected MKL exception caught during gesv() "
176 "call:\nreason: "
177 << e.what() << "\ninfo: " << e.info();
178 }
179 }
180 else {
181 error_msg
182 << "Unexpected MKL exception caught during gesv() call:\nreason: "
183 << e.what() << "\ninfo: " << e.info();
184 }
185}
186} // namespace dpnp::extensions::lapack::gesv_utils