|
| 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 | +// |
| 13 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | +//***************************************************************************** |
| 25 | + |
| 26 | +#include <pybind11/pybind11.h> |
| 27 | + |
| 28 | +// dpctl tensor headers |
| 29 | +#include "utils/memory_overlap.hpp" |
| 30 | +#include "utils/type_utils.hpp" |
| 31 | + |
| 32 | +#include "linalg_exceptions.hpp" |
| 33 | +#include "potrf.hpp" |
| 34 | +#include "types_matrix.hpp" |
| 35 | + |
| 36 | +#include "dpnp_utils.hpp" |
| 37 | + |
| 38 | +namespace dpnp |
| 39 | +{ |
| 40 | +namespace backend |
| 41 | +{ |
| 42 | +namespace ext |
| 43 | +{ |
| 44 | +namespace lapack |
| 45 | +{ |
| 46 | +namespace mkl_lapack = oneapi::mkl::lapack; |
| 47 | +namespace py = pybind11; |
| 48 | +namespace type_utils = dpctl::tensor::type_utils; |
| 49 | + |
| 50 | +typedef sycl::event (*potrf_impl_fn_ptr_t)(sycl::queue, |
| 51 | + const oneapi::mkl::uplo, |
| 52 | + const std::int64_t, |
| 53 | + char *, |
| 54 | + std::int64_t, |
| 55 | + std::vector<sycl::event> &, |
| 56 | + const std::vector<sycl::event> &); |
| 57 | + |
| 58 | +static potrf_impl_fn_ptr_t potrf_dispatch_vector[dpctl_td_ns::num_types]; |
| 59 | + |
| 60 | +template <typename T> |
| 61 | +static sycl::event potrf_impl(sycl::queue exec_q, |
| 62 | + const oneapi::mkl::uplo upper_lower, |
| 63 | + const std::int64_t n, |
| 64 | + char *in_a, |
| 65 | + std::int64_t lda, |
| 66 | + std::vector<sycl::event> &host_task_events, |
| 67 | + const std::vector<sycl::event> &depends) |
| 68 | +{ |
| 69 | + type_utils::validate_type_for_device<T>(exec_q); |
| 70 | + |
| 71 | + T *a = reinterpret_cast<T *>(in_a); |
| 72 | + |
| 73 | + const std::int64_t scratchpad_size = |
| 74 | + mkl_lapack::potrf_scratchpad_size<T>(exec_q, upper_lower, n, lda); |
| 75 | + T *scratchpad = nullptr; |
| 76 | + |
| 77 | + std::stringstream error_msg; |
| 78 | + std::int64_t info = 0; |
| 79 | + bool is_exception_caught = false; |
| 80 | + |
| 81 | + sycl::event potrf_event; |
| 82 | + try { |
| 83 | + scratchpad = sycl::malloc_device<T>(scratchpad_size, exec_q); |
| 84 | + |
| 85 | + potrf_event = mkl_lapack::potrf( |
| 86 | + exec_q, |
| 87 | + upper_lower, // An enumeration value of type oneapi::mkl::uplo: |
| 88 | + // oneapi::mkl::uplo::upper for the upper triangular |
| 89 | + // part; oneapi::mkl::uplo::lower for the lower |
| 90 | + // triangular part. |
| 91 | + n, // Order of the square matrix; (0 ≤ n). |
| 92 | + a, // Pointer to the n-by-n matrix. |
| 93 | + lda, // The leading dimension of `a`. |
| 94 | + scratchpad, // Pointer to scratchpad memory to be used by MKL |
| 95 | + // routine for storing intermediate results. |
| 96 | + scratchpad_size, depends); |
| 97 | + } catch (mkl_lapack::exception const &e) { |
| 98 | + is_exception_caught = true; |
| 99 | + info = e.info(); |
| 100 | + if (info < 0) { |
| 101 | + error_msg << "Parameter number " << -info |
| 102 | + << " had an illegal value."; |
| 103 | + } |
| 104 | + else if (info == scratchpad_size && e.detail() != 0) { |
| 105 | + error_msg |
| 106 | + << "Insufficient scratchpad size. Required size is at least " |
| 107 | + << e.detail(); |
| 108 | + } |
| 109 | + else if (info > 0 && e.detail() == 0) { |
| 110 | + sycl::free(scratchpad, exec_q); |
| 111 | + throw LinAlgError("Matrix is not positive definite."); |
| 112 | + } |
| 113 | + else { |
| 114 | + error_msg << "Unexpected MKL exception caught during getrf() " |
| 115 | + "call:\nreason: " |
| 116 | + << e.what() << "\ninfo: " << e.info(); |
| 117 | + } |
| 118 | + } catch (sycl::exception const &e) { |
| 119 | + is_exception_caught = true; |
| 120 | + error_msg << "Unexpected SYCL exception caught during potrf() call:\n" |
| 121 | + << e.what(); |
| 122 | + } |
| 123 | + |
| 124 | + if (is_exception_caught) // an unexpected error occurs |
| 125 | + { |
| 126 | + if (scratchpad != nullptr) { |
| 127 | + sycl::free(scratchpad, exec_q); |
| 128 | + } |
| 129 | + throw std::runtime_error(error_msg.str()); |
| 130 | + } |
| 131 | + |
| 132 | + sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { |
| 133 | + cgh.depends_on(potrf_event); |
| 134 | + auto ctx = exec_q.get_context(); |
| 135 | + cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); }); |
| 136 | + }); |
| 137 | + host_task_events.push_back(clean_up_event); |
| 138 | + return potrf_event; |
| 139 | +} |
| 140 | + |
| 141 | +std::pair<sycl::event, sycl::event> |
| 142 | + potrf(sycl::queue q, |
| 143 | + dpctl::tensor::usm_ndarray a_array, |
| 144 | + const std::int8_t upper_lower, |
| 145 | + const std::vector<sycl::event> &depends) |
| 146 | +{ |
| 147 | + const int a_array_nd = a_array.get_ndim(); |
| 148 | + |
| 149 | + if (a_array_nd != 2) { |
| 150 | + throw py::value_error( |
| 151 | + "The input array has ndim=" + std::to_string(a_array_nd) + |
| 152 | + ", but a 2-dimensional array is expected."); |
| 153 | + } |
| 154 | + |
| 155 | + const py::ssize_t *a_array_shape = a_array.get_shape_raw(); |
| 156 | + |
| 157 | + if (a_array_shape[0] != a_array_shape[1]) { |
| 158 | + throw py::value_error("The input array must be square," |
| 159 | + " but got a shape of (" + |
| 160 | + std::to_string(a_array_shape[0]) + ", " + |
| 161 | + std::to_string(a_array_shape[1]) + ")."); |
| 162 | + } |
| 163 | + |
| 164 | + bool is_a_array_c_contig = a_array.is_c_contiguous(); |
| 165 | + if (!is_a_array_c_contig) { |
| 166 | + throw py::value_error("The input array " |
| 167 | + "must be C-contiguous"); |
| 168 | + } |
| 169 | + |
| 170 | + auto array_types = dpctl_td_ns::usm_ndarray_types(); |
| 171 | + int a_array_type_id = |
| 172 | + array_types.typenum_to_lookup_id(a_array.get_typenum()); |
| 173 | + |
| 174 | + potrf_impl_fn_ptr_t potrf_fn = potrf_dispatch_vector[a_array_type_id]; |
| 175 | + if (potrf_fn == nullptr) { |
| 176 | + throw py::value_error( |
| 177 | + "No potrf implementation defined for the provided type " |
| 178 | + "of the input matrix."); |
| 179 | + } |
| 180 | + |
| 181 | + char *a_array_data = a_array.get_data(); |
| 182 | + const std::int64_t n = a_array_shape[0]; |
| 183 | + const std::int64_t lda = std::max<size_t>(1UL, n); |
| 184 | + const oneapi::mkl::uplo uplo_val = |
| 185 | + static_cast<oneapi::mkl::uplo>(upper_lower); |
| 186 | + |
| 187 | + std::vector<sycl::event> host_task_events; |
| 188 | + sycl::event potrf_ev = |
| 189 | + potrf_fn(q, uplo_val, n, a_array_data, lda, host_task_events, depends); |
| 190 | + |
| 191 | + sycl::event args_ev = |
| 192 | + dpctl::utils::keep_args_alive(q, {a_array}, host_task_events); |
| 193 | + |
| 194 | + return std::make_pair(args_ev, potrf_ev); |
| 195 | +} |
| 196 | + |
| 197 | +template <typename fnT, typename T> |
| 198 | +struct PotrfContigFactory |
| 199 | +{ |
| 200 | + fnT get() |
| 201 | + { |
| 202 | + if constexpr (types::PotrfTypePairSupportFactory<T>::is_defined) { |
| 203 | + return potrf_impl<T>; |
| 204 | + } |
| 205 | + else { |
| 206 | + return nullptr; |
| 207 | + } |
| 208 | + } |
| 209 | +}; |
| 210 | + |
| 211 | +void init_potrf_dispatch_vector(void) |
| 212 | +{ |
| 213 | + dpctl_td_ns::DispatchVectorBuilder<potrf_impl_fn_ptr_t, PotrfContigFactory, |
| 214 | + dpctl_td_ns::num_types> |
| 215 | + contig; |
| 216 | + contig.populate_dispatch_vector(potrf_dispatch_vector); |
| 217 | +} |
| 218 | +} // namespace lapack |
| 219 | +} // namespace ext |
| 220 | +} // namespace backend |
| 221 | +} // namespace dpnp |
0 commit comments