28#include <pybind11/pybind11.h>
29#include <pybind11/stl.h>
30#include <sycl/sycl.hpp>
32#include "dpctl4pybind11.hpp"
33#include "utils/output_validation.hpp"
34#include "utils/type_dispatch.hpp"
35#include "utils/type_utils.hpp"
37namespace dpnp::extensions::window
40namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
42namespace py = pybind11;
44typedef sycl::event (*window_fn_ptr_t)(sycl::queue &,
47 const std::vector<sycl::event> &);
49template <
typename T,
template <
typename>
class Functor>
50sycl::event window_impl(sycl::queue &q,
52 const std::size_t nelems,
53 const std::vector<sycl::event> &depends)
55 dpctl::tensor::type_utils::validate_type_for_device<T>(q);
57 T *res =
reinterpret_cast<T *
>(result);
59 sycl::event window_ev = q.submit([&](sycl::handler &cgh) {
60 cgh.depends_on(depends);
62 using WindowKernel = Functor<T>;
63 cgh.parallel_for<WindowKernel>(sycl::range<1>(nelems),
64 WindowKernel(res, nelems));
70std::pair<sycl::event, sycl::event>
71 py_window(sycl::queue &exec_q,
72 const dpctl::tensor::usm_ndarray &result,
73 const std::vector<sycl::event> &depends,
74 const window_fn_ptr_t *window_dispatch_vector)
76 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(result);
78 int nd = result.get_ndim();
80 throw py::value_error(
"Array should be 1d");
83 if (!dpctl::utils::queues_are_compatible(exec_q, {result.get_queue()})) {
84 throw py::value_error(
85 "Execution queue is not compatible with allocation queue.");
88 const bool is_result_c_contig = result.is_c_contiguous();
89 if (!is_result_c_contig) {
90 throw py::value_error(
"The result input array is not c-contiguous.");
93 size_t nelems = result.get_size();
95 return std::make_pair(sycl::event{}, sycl::event{});
98 int result_typenum = result.get_typenum();
99 auto array_types = dpctl_td_ns::usm_ndarray_types();
100 int result_type_id = array_types.typenum_to_lookup_id(result_typenum);
101 auto fn = window_dispatch_vector[result_type_id];
104 throw std::runtime_error(
"Type of given array is not supported");
107 char *result_typeless_ptr = result.get_data();
108 sycl::event window_ev = fn(exec_q, result_typeless_ptr, nelems, depends);
109 sycl::event args_ev =
110 dpctl::utils::keep_args_alive(exec_q, {result}, {window_ev});
112 return std::make_pair(args_ev, window_ev);
115template <
template <
typename fnT,
typename T>
typename factoryT>
116void init_window_dispatch_vectors(window_fn_ptr_t window_dispatch_vector[])
118 dpctl_td_ns::DispatchVectorBuilder<window_fn_ptr_t, factoryT,
119 dpctl_td_ns::num_types>
121 contig.populate_dispatch_vector(window_dispatch_vector);