DPNP C++ backend kernel library 0.18.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
evd_common_utils.hpp
1//*****************************************************************************
2// Copyright (c) 2024-2025, 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#pragma once
27
28#include <pybind11/pybind11.h>
29
30// dpctl tensor headers
31#include "utils/memory_overlap.hpp"
32#include "utils/output_validation.hpp"
33#include "utils/type_dispatch.hpp"
34
35namespace dpnp::extensions::lapack::evd
36{
37namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
38namespace py = pybind11;
39
40template <typename dispatchT,
41 template <typename fnT, typename T, typename RealT>
42 typename factoryT>
43void init_evd_dispatch_table(
44 dispatchT evd_dispatch_table[][dpctl_td_ns::num_types])
45{
46 dpctl_td_ns::DispatchTableBuilder<dispatchT, factoryT,
47 dpctl_td_ns::num_types>
48 contig;
49 contig.populate_dispatch_table(evd_dispatch_table);
50}
51
52inline void common_evd_checks(sycl::queue &exec_q,
53 const dpctl::tensor::usm_ndarray &eig_vecs,
54 const dpctl::tensor::usm_ndarray &eig_vals,
55 const py::ssize_t *eig_vecs_shape,
56 const int expected_eig_vecs_nd,
57 const int expected_eig_vals_nd)
58{
59 const int eig_vecs_nd = eig_vecs.get_ndim();
60 const int eig_vals_nd = eig_vals.get_ndim();
61
62 if (eig_vecs_nd != expected_eig_vecs_nd) {
63 throw py::value_error("The output eigenvectors array has ndim=" +
64 std::to_string(eig_vecs_nd) + ", but a " +
65 std::to_string(expected_eig_vecs_nd) +
66 "-dimensional array is expected.");
67 }
68 else if (eig_vals_nd != expected_eig_vals_nd) {
69 throw py::value_error("The output eigenvalues array has ndim=" +
70 std::to_string(eig_vals_nd) + ", but a " +
71 std::to_string(expected_eig_vals_nd) +
72 "-dimensional array is expected.");
73 }
74
75 if (eig_vecs_shape[0] != eig_vecs_shape[1]) {
76 throw py::value_error("Output array with eigenvectors must be square");
77 }
78
79 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vecs);
80 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(eig_vals);
81
82 // check compatibility of execution queue and allocation queue
83 if (!dpctl::utils::queues_are_compatible(exec_q, {eig_vecs, eig_vals})) {
84 throw py::value_error(
85 "Execution queue is not compatible with allocation queues");
86 }
87
88 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
89 if (overlap(eig_vecs, eig_vals)) {
90 throw py::value_error("Arrays with eigenvectors and eigenvalues are "
91 "overlapping segments of memory");
92 }
93
94 const bool is_eig_vecs_f_contig = eig_vecs.is_f_contiguous();
95 const bool is_eig_vals_c_contig = eig_vals.is_c_contiguous();
96 if (!is_eig_vecs_f_contig) {
97 throw py::value_error(
98 "An array with input matrix / output eigenvectors "
99 "must be F-contiguous");
100 }
101 else if (!is_eig_vals_c_contig) {
102 throw py::value_error(
103 "An array with output eigenvalues must be C-contiguous");
104 }
105}
106} // namespace dpnp::extensions::lapack::evd