DPNP C++ backend kernel library 0.20.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
common.hpp
1//*****************************************************************************
2// Copyright (c) 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// - 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 <pybind11/pybind11.h>
32#include <pybind11/stl.h>
33#include <sycl/sycl.hpp>
34
35#include "dpctl4pybind11.hpp"
36
37// dpctl tensor headers
38#include "utils/output_validation.hpp"
39#include "utils/type_dispatch.hpp"
40#include "utils/type_utils.hpp"
41
42namespace dpnp::extensions::window
43{
44
45namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
46
47namespace py = pybind11;
48
49typedef sycl::event (*window_fn_ptr_t)(sycl::queue &,
50 char *,
51 const std::size_t,
52 const std::vector<sycl::event> &);
53
54template <typename T, template <typename> class Functor>
55sycl::event window_impl(sycl::queue &exec_q,
56 char *result,
57 const std::size_t nelems,
58 const std::vector<sycl::event> &depends)
59{
60 dpctl::tensor::type_utils::validate_type_for_device<T>(exec_q);
61
62 T *res = reinterpret_cast<T *>(result);
63
64 sycl::event window_ev = exec_q.submit([&](sycl::handler &cgh) {
65 cgh.depends_on(depends);
66
67 using WindowKernel = Functor<T>;
68 cgh.parallel_for<WindowKernel>(sycl::range<1>(nelems),
69 WindowKernel(res, nelems));
70 });
71
72 return window_ev;
73}
74
75template <typename funcPtrT>
76std::tuple<size_t, char *, funcPtrT>
77 window_fn(sycl::queue &exec_q,
78 const dpctl::tensor::usm_ndarray &result,
79 const funcPtrT *window_dispatch_vector)
80{
81 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(result);
82
83 const int nd = result.get_ndim();
84 if (nd != 1) {
85 throw py::value_error("Array should be 1d");
86 }
87
88 if (!dpctl::utils::queues_are_compatible(exec_q, {result.get_queue()})) {
89 throw py::value_error(
90 "Execution queue is not compatible with allocation queue.");
91 }
92
93 const bool is_result_c_contig = result.is_c_contiguous();
94 if (!is_result_c_contig) {
95 throw py::value_error("The result array is not c-contiguous.");
96 }
97
98 const std::size_t nelems = result.get_size();
99 if (nelems == 0) {
100 return std::make_tuple(nelems, nullptr, nullptr);
101 }
102
103 const int result_typenum = result.get_typenum();
104 auto array_types = dpctl_td_ns::usm_ndarray_types();
105 const int result_type_id = array_types.typenum_to_lookup_id(result_typenum);
106 funcPtrT fn = window_dispatch_vector[result_type_id];
107
108 if (fn == nullptr) {
109 throw std::runtime_error("Type of given array is not supported");
110 }
111
112 char *result_typeless_ptr = result.get_data();
113 return std::make_tuple(nelems, result_typeless_ptr, fn);
114}
115
116inline std::pair<sycl::event, sycl::event>
117 py_window(sycl::queue &exec_q,
118 const dpctl::tensor::usm_ndarray &result,
119 const std::vector<sycl::event> &depends,
120 const window_fn_ptr_t *window_dispatch_vector)
121{
122 auto [nelems, result_typeless_ptr, fn] =
123 window_fn<window_fn_ptr_t>(exec_q, result, window_dispatch_vector);
124
125 if (nelems == 0) {
126 return std::make_pair(sycl::event{}, sycl::event{});
127 }
128
129 sycl::event window_ev = fn(exec_q, result_typeless_ptr, nelems, depends);
130 sycl::event args_ev =
131 dpctl::utils::keep_args_alive(exec_q, {result}, {window_ev});
132
133 return std::make_pair(args_ev, window_ev);
134}
135} // namespace dpnp::extensions::window