DPNP C++ backend kernel library 0.20.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
evd_common.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 <oneapi/mkl.hpp>
32#include <pybind11/pybind11.h>
33
34// dpctl tensor headers
35#include "utils/type_dispatch.hpp"
36
37#include "common_helpers.hpp"
38#include "evd_common_utils.hpp"
39#include "types_matrix.hpp"
40
41namespace dpnp::extensions::lapack::evd
42{
43using dpnp::extensions::lapack::helper::check_zeros_shape;
44
45typedef sycl::event (*evd_impl_fn_ptr_t)(sycl::queue &,
46 const oneapi::mkl::job,
47 const oneapi::mkl::uplo,
48 const std::int64_t,
49 char *,
50 char *,
51 const std::vector<sycl::event> &);
52
53namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
54namespace py = pybind11;
55
56template <typename dispatchT>
57std::pair<sycl::event, sycl::event>
58 evd_func(sycl::queue &exec_q,
59 const std::int8_t jobz,
60 const std::int8_t upper_lower,
61 const dpctl::tensor::usm_ndarray &eig_vecs,
62 const dpctl::tensor::usm_ndarray &eig_vals,
63 const std::vector<sycl::event> &depends,
64 const dispatchT &evd_dispatch_table)
65{
66 const int eig_vecs_nd = eig_vecs.get_ndim();
67
68 const py::ssize_t *eig_vecs_shape = eig_vecs.get_shape_raw();
69 const py::ssize_t *eig_vals_shape = eig_vals.get_shape_raw();
70
71 constexpr int expected_eig_vecs_nd = 2;
72 constexpr int expected_eig_vals_nd = 1;
73
74 common_evd_checks(exec_q, eig_vecs, eig_vals, eig_vecs_shape,
75 expected_eig_vecs_nd, expected_eig_vals_nd);
76
77 if (eig_vecs_shape[0] != eig_vals_shape[0]) {
78 throw py::value_error(
79 "Eigenvectors and eigenvalues have different shapes");
80 }
81
82 if (check_zeros_shape(eig_vecs_nd, eig_vecs_shape)) {
83 // nothing to do
84 return std::make_pair(sycl::event(), sycl::event());
85 }
86
87 auto array_types = dpctl_td_ns::usm_ndarray_types();
88 const int eig_vecs_type_id =
89 array_types.typenum_to_lookup_id(eig_vecs.get_typenum());
90 const int eig_vals_type_id =
91 array_types.typenum_to_lookup_id(eig_vals.get_typenum());
92
93 evd_impl_fn_ptr_t evd_fn =
94 evd_dispatch_table[eig_vecs_type_id][eig_vals_type_id];
95 if (evd_fn == nullptr) {
96 throw py::value_error(
97 "No evd implementation is available for the specified data type "
98 "of the input and output arrays.");
99 }
100
101 char *eig_vecs_data = eig_vecs.get_data();
102 char *eig_vals_data = eig_vals.get_data();
103
104 const std::int64_t n = eig_vecs_shape[0];
105 const oneapi::mkl::job jobz_val = static_cast<oneapi::mkl::job>(jobz);
106 const oneapi::mkl::uplo uplo_val =
107 static_cast<oneapi::mkl::uplo>(upper_lower);
108
109 sycl::event evd_ev = evd_fn(exec_q, jobz_val, uplo_val, n, eig_vecs_data,
110 eig_vals_data, depends);
111
112 sycl::event ht_ev =
113 dpctl::utils::keep_args_alive(exec_q, {eig_vecs, eig_vals}, {evd_ev});
114
115 return std::make_pair(ht_ev, evd_ev);
116}
117} // namespace dpnp::extensions::lapack::evd