DPNP C++ backend kernel library 0.21.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
dot_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
33#include <pybind11/pybind11.h>
34
35#include "dpnp4pybind11.hpp"
36
37// dpnp tensor headers
38#include "utils/memory_overlap.hpp"
39#include "utils/output_validation.hpp"
40#include "utils/type_dispatch.hpp"
41#include "utils/type_utils.hpp"
42
43#include "types_matrix.hpp"
44
45namespace dpnp::extensions::blas::dot
46{
47typedef sycl::event (*dot_impl_fn_ptr_t)(sycl::queue &,
48 const std::int64_t,
49 const char *,
50 const std::int64_t,
51 const char *,
52 const std::int64_t,
53 char *,
54 const std::vector<sycl::event> &);
55
56namespace dpnp_td_ns = dpnp::tensor::type_dispatch;
57namespace py = pybind11;
58
59std::pair<sycl::event, sycl::event>
60 dot_func(sycl::queue &exec_q,
61 const dpnp::tensor::usm_ndarray &vectorX,
62 const dpnp::tensor::usm_ndarray &vectorY,
63 const dpnp::tensor::usm_ndarray &result,
64 const std::vector<sycl::event> &depends,
65 const dot_impl_fn_ptr_t *dot_dispatch_vector)
66{
67 const int vectorX_nd = vectorX.get_ndim();
68 const int vectorY_nd = vectorY.get_ndim();
69 const int result_nd = result.get_ndim();
70
71 if ((vectorX_nd != 1)) {
72 throw py::value_error(
73 "The first input array has ndim=" + std::to_string(vectorX_nd) +
74 ", but a 1-dimensional array is expected.");
75 }
76
77 if ((vectorY_nd != 1)) {
78 throw py::value_error(
79 "The second input array has ndim=" + std::to_string(vectorY_nd) +
80 ", but a 1-dimensional array is expected.");
81 }
82
83 if ((result_nd != 0)) {
84 throw py::value_error(
85 "The output array has ndim=" + std::to_string(result_nd) +
86 ", but a 0-dimensional array is expected.");
87 }
88
89 auto const &overlap = dpnp::tensor::overlap::MemoryOverlap();
90 if (overlap(vectorX, result)) {
91 throw py::value_error(
92 "The first input array and output array are overlapping "
93 "segments of memory");
94 }
95 if (overlap(vectorY, result)) {
96 throw py::value_error(
97 "The second input array and output array are overlapping "
98 "segments of memory");
99 }
100
101 if (!dpnp::utils::queues_are_compatible(
102 exec_q,
103 {vectorX.get_queue(), vectorY.get_queue(), result.get_queue()})) {
104 throw py::value_error(
105 "USM allocations are not compatible with the execution queue.");
106 }
107
108 const int src_nelems = 1;
109 dpnp::tensor::validation::CheckWritable::throw_if_not_writable(result);
110 dpnp::tensor::validation::AmpleMemory::throw_if_not_ample(result,
111 src_nelems);
112
113 const py::ssize_t x_size = vectorX.get_size();
114 const py::ssize_t y_size = vectorY.get_size();
115 const std::int64_t n = x_size;
116 if (x_size != y_size) {
117 throw py::value_error("The size of the first input array must be "
118 "equal to the size of the second input array.");
119 }
120
121 const int vectorX_typenum = vectorX.get_typenum();
122 const int vectorY_typenum = vectorY.get_typenum();
123 const int result_typenum = result.get_typenum();
124
125 if (result_typenum != vectorX_typenum ||
126 result_typenum != vectorY_typenum) {
127 throw py::value_error("Given arrays must be of the same type.");
128 }
129
130 auto array_types = dpnp_td_ns::usm_ndarray_types();
131 const int type_id = array_types.typenum_to_lookup_id(vectorX_typenum);
132
133 dot_impl_fn_ptr_t dot_fn = dot_dispatch_vector[type_id];
134 if (dot_fn == nullptr) {
135 throw py::value_error(
136 "No dot implementation is available for the specified data type "
137 "of the input and output arrays.");
138 }
139
140 char *x_typeless_ptr = vectorX.get_data();
141 char *y_typeless_ptr = vectorY.get_data();
142 char *r_typeless_ptr = result.get_data();
143
144 const std::vector<py::ssize_t> x_stride = vectorX.get_strides_vector();
145 const std::vector<py::ssize_t> y_stride = vectorY.get_strides_vector();
146 const int x_elemsize = vectorX.get_elemsize();
147 const int y_elemsize = vectorY.get_elemsize();
148
149 const std::int64_t incx = x_stride[0];
150 const std::int64_t incy = y_stride[0];
151 // In OneMKL, the pointer should always point out to the first element of
152 // the array and OneMKL handle the rest depending on the sign of stride.
153 // In OneMKL, when the stride is positive, the data is read in order and
154 // when it is negative, the data is read in reverse order while pointer
155 // always point to the first element
156 // When the stride is negative, the pointer of the array coming from dpnp
157 // points to the last element. So, we need to adjust the pointer
158 if (incx < 0) {
159 x_typeless_ptr -= (n - 1) * std::abs(incx) * x_elemsize;
160 }
161 if (incy < 0) {
162 y_typeless_ptr -= (n - 1) * std::abs(incy) * y_elemsize;
163 }
164
165 sycl::event dot_ev = dot_fn(exec_q, n, x_typeless_ptr, incx, y_typeless_ptr,
166 incy, r_typeless_ptr, depends);
167
168 sycl::event args_ev = dpnp::utils::keep_args_alive(
169 exec_q, {vectorX, vectorY, result}, {dot_ev});
170
171 return std::make_pair(args_ev, dot_ev);
172}
173} // namespace dpnp::extensions::blas::dot