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