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