DPNP C++ backend kernel library 0.20.0dev4
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 throw py::value_error("The dependent values array has ndim=" +
69 std::to_string(dependent_vals_nd) + ", but a " +
70 std::to_string(min_dependent_vals_ndim) +
71 "-dimensional or a " +
72 std::to_string(max_dependent_vals_ndim) +
73 "-dimensional array is expected.");
74 }
75
76 // The coeff_matrix and dependent_vals arrays must be F-contiguous arrays
77 // for gesv
78 // with the shapes (n, n) and (n, nrhs) or (n, ) respectively;
79 // for gesv_batch
80 // with the shapes (n, n, batch_size) and (n, nrhs, batch_size) or
81 // (n, batch_size) respectively
82 if (coeff_matrix_shape[0] != coeff_matrix_shape[1]) {
83 throw py::value_error("The coefficient matrix must be square,"
84 " but got a shape of (" +
85 std::to_string(coeff_matrix_shape[0]) + ", " +
86 std::to_string(coeff_matrix_shape[1]) + ").");
87 }
88 if (coeff_matrix_shape[0] != dependent_vals_shape[0]) {
89 throw py::value_error("The first dimension (n) of coeff_matrix and"
90 " dependent_vals must be the same, but got " +
91 std::to_string(coeff_matrix_shape[0]) + " and " +
92 std::to_string(dependent_vals_shape[0]) + ".");
93 }
94
95 // check compatibility of execution queue and allocation queue
96 if (!dpctl::utils::queues_are_compatible(exec_q,
97 {coeff_matrix, dependent_vals})) {
98 throw py::value_error(
99 "Execution queue is not compatible with allocation queues.");
100 }
101
102 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
103 if (overlap(coeff_matrix, dependent_vals)) {
104 throw py::value_error(
105 "The arrays of coefficients and dependent variables "
106 "are overlapping segments of memory.");
107 }
108
109 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(
110 dependent_vals);
111
112 const bool is_coeff_matrix_f_contig = coeff_matrix.is_f_contiguous();
113 if (!is_coeff_matrix_f_contig) {
114 throw py::value_error("The coefficient matrix "
115 "must be F-contiguous.");
116 }
117
118 const bool is_dependent_vals_f_contig = dependent_vals.is_f_contiguous();
119 if (!is_dependent_vals_f_contig) {
120 throw py::value_error("The array of dependent variables "
121 "must be F-contiguous.");
122 }
123
124 auto array_types = dpctl_td_ns::usm_ndarray_types();
125 const int coeff_matrix_type_id =
126 array_types.typenum_to_lookup_id(coeff_matrix.get_typenum());
127 const int dependent_vals_type_id =
128 array_types.typenum_to_lookup_id(dependent_vals.get_typenum());
129
130 if (coeff_matrix_type_id != dependent_vals_type_id) {
131 throw py::value_error("The types of the coefficient matrix and "
132 "dependent variables are mismatched.");
133 }
134}
135
136template <typename T>
137inline void handle_lapack_exc(sycl::queue &exec_q,
138 const std::int64_t lda,
139 T *a,
140 std::int64_t scratchpad_size,
141 T *scratchpad,
142 std::int64_t *ipiv,
143 const oneapi::mkl::lapack::exception &e,
144 std::stringstream &error_msg)
145{
146 std::int64_t info = e.info();
147 if (info < 0) {
148 error_msg << "Parameter number " << -info << " had an illegal value.";
149 }
150 else if (info == scratchpad_size && e.detail() != 0) {
151 error_msg << "Insufficient scratchpad size. Required size is at least "
152 << e.detail();
153 }
154 else if (info > 0) {
155 T host_U;
156 exec_q.memcpy(&host_U, &a[(info - 1) * lda + info - 1], sizeof(T))
157 .wait();
158
159 using ThresholdType = typename helper::value_type_of<T>::type;
160
161 const auto threshold =
162 std::numeric_limits<ThresholdType>::epsilon() * 100;
163 if (std::abs(host_U) < threshold) {
164 using dpctl::tensor::alloc_utils::sycl_free_noexcept;
165
166 if (scratchpad != nullptr)
167 sycl_free_noexcept(scratchpad, exec_q);
168 if (ipiv != nullptr)
169 sycl_free_noexcept(ipiv, exec_q);
170 throw LinAlgError("The input coefficient matrix is singular.");
171 }
172 else {
173 error_msg << "Unexpected MKL exception caught during gesv() "
174 "call:\nreason: "
175 << e.what() << "\ninfo: " << e.info();
176 }
177 }
178 else {
179 error_msg
180 << "Unexpected MKL exception caught during gesv() call:\nreason: "
181 << e.what() << "\ninfo: " << e.info();
182 }
183}
184} // namespace dpnp::extensions::lapack::gesv_utils