DPNP C++ backend kernel library 0.19.0dev6
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//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23// THE POSSIBILITY OF SUCH DAMAGE.
24//*****************************************************************************
25
26#pragma once
27
28#include <pybind11/pybind11.h>
29#include <pybind11/stl.h>
30#include <sycl/sycl.hpp>
31
32#include "dpctl4pybind11.hpp"
33
34// dpctl tensor headers
35#include "utils/output_validation.hpp"
36#include "utils/type_dispatch.hpp"
37#include "utils/type_utils.hpp"
38
39namespace dpnp::extensions::window
40{
41
42namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
43
44namespace py = pybind11;
45
46typedef sycl::event (*window_fn_ptr_t)(sycl::queue &,
47 char *,
48 const std::size_t,
49 const std::vector<sycl::event> &);
50
51template <typename T, template <typename> class Functor>
52sycl::event window_impl(sycl::queue &exec_q,
53 char *result,
54 const std::size_t nelems,
55 const std::vector<sycl::event> &depends)
56{
57 dpctl::tensor::type_utils::validate_type_for_device<T>(exec_q);
58
59 T *res = reinterpret_cast<T *>(result);
60
61 sycl::event window_ev = exec_q.submit([&](sycl::handler &cgh) {
62 cgh.depends_on(depends);
63
64 using WindowKernel = Functor<T>;
65 cgh.parallel_for<WindowKernel>(sycl::range<1>(nelems),
66 WindowKernel(res, nelems));
67 });
68
69 return window_ev;
70}
71
72template <typename funcPtrT>
73std::tuple<size_t, char *, funcPtrT>
74 window_fn(sycl::queue &exec_q,
75 const dpctl::tensor::usm_ndarray &result,
76 const funcPtrT *window_dispatch_vector)
77{
78 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(result);
79
80 const int nd = result.get_ndim();
81 if (nd != 1) {
82 throw py::value_error("Array should be 1d");
83 }
84
85 if (!dpctl::utils::queues_are_compatible(exec_q, {result.get_queue()})) {
86 throw py::value_error(
87 "Execution queue is not compatible with allocation queue.");
88 }
89
90 const bool is_result_c_contig = result.is_c_contiguous();
91 if (!is_result_c_contig) {
92 throw py::value_error("The result array is not c-contiguous.");
93 }
94
95 const std::size_t nelems = result.get_size();
96 if (nelems == 0) {
97 return std::make_tuple(nelems, nullptr, nullptr);
98 }
99
100 const int result_typenum = result.get_typenum();
101 auto array_types = dpctl_td_ns::usm_ndarray_types();
102 const int result_type_id = array_types.typenum_to_lookup_id(result_typenum);
103 funcPtrT fn = window_dispatch_vector[result_type_id];
104
105 if (fn == nullptr) {
106 throw std::runtime_error("Type of given array is not supported");
107 }
108
109 char *result_typeless_ptr = result.get_data();
110 return std::make_tuple(nelems, result_typeless_ptr, fn);
111}
112
113inline std::pair<sycl::event, sycl::event>
114 py_window(sycl::queue &exec_q,
115 const dpctl::tensor::usm_ndarray &result,
116 const std::vector<sycl::event> &depends,
117 const window_fn_ptr_t *window_dispatch_vector)
118{
119 auto [nelems, result_typeless_ptr, fn] =
120 window_fn<window_fn_ptr_t>(exec_q, result, window_dispatch_vector);
121
122 if (nelems == 0) {
123 return std::make_pair(sycl::event{}, sycl::event{});
124 }
125
126 sycl::event window_ev = fn(exec_q, result_typeless_ptr, nelems, depends);
127 sycl::event args_ev =
128 dpctl::utils::keep_args_alive(exec_q, {result}, {window_ev});
129
130 return std::make_pair(args_ev, window_ev);
131}
132} // namespace dpnp::extensions::window