32#include <pybind11/numpy.h>
33#include <pybind11/pybind11.h>
34#include <sycl/sycl.hpp>
37#include "utils/math_utils.hpp"
38#include "utils/type_dispatch.hpp"
39#include "utils/type_utils.hpp"
41namespace type_utils = dpctl::tensor::type_utils;
42namespace type_dispatch = dpctl::tensor::type_dispatch;
47template <
typename N,
typename D>
48constexpr auto CeilDiv(N n, D d)
50 return (n + d - 1) / d;
53template <
typename N,
typename D>
54constexpr auto Align(N n, D d)
56 return CeilDiv(n, d) * d;
59template <
typename T, sycl::memory_order Order, sycl::memory_scope Scope>
62 static void add(T &lhs,
const T &value)
64 if constexpr (type_utils::is_complex_v<T>) {
65 using vT =
typename T::value_type;
66 vT *_lhs =
reinterpret_cast<vT(&)[2]
>(lhs);
67 const vT *_val =
reinterpret_cast<const vT(&)[2]
>(value);
73 sycl::atomic_ref<T, Order, Scope> lh(lhs);
82 bool operator()(
const T &lhs,
const T &rhs)
const
84 if constexpr (type_utils::is_complex_v<T>) {
85 return dpctl::tensor::math_utils::less_complex(lhs, rhs);
88 return std::less{}(lhs, rhs);
96 static bool isnan(
const T &v)
98 if constexpr (type_utils::is_complex_v<T>) {
99 using vT =
typename T::value_type;
101 const vT real1 = std::real(v);
102 const vT imag1 = std::imag(v);
106 else if constexpr (std::is_floating_point_v<T> ||
107 std::is_same_v<T, sycl::half>) {
108 return sycl::isnan(v);
115template <
typename T,
bool hasValueType>
127 using type =
typename T::value_type;
136size_t get_max_local_size(
const sycl::device &device);
137size_t get_max_local_size(
const sycl::device &device,
138 int cpu_local_size_limit,
139 int gpu_local_size_limit);
141inline size_t get_max_local_size(
const sycl::queue &queue)
143 return get_max_local_size(queue.get_device());
146inline size_t get_max_local_size(
const sycl::queue &queue,
147 int cpu_local_size_limit,
148 int gpu_local_size_limit)
150 return get_max_local_size(queue.get_device(), cpu_local_size_limit,
151 gpu_local_size_limit);
154size_t get_local_mem_size_in_bytes(
const sycl::device &device);
155size_t get_local_mem_size_in_bytes(
const sycl::device &device,
size_t reserve);
157inline size_t get_local_mem_size_in_bytes(
const sycl::queue &queue)
159 return get_local_mem_size_in_bytes(queue.get_device());
162inline size_t get_local_mem_size_in_bytes(
const sycl::queue &queue,
165 return get_local_mem_size_in_bytes(queue.get_device(), reserve);
169size_t get_local_mem_size_in_items(
const sycl::device &device)
171 return get_local_mem_size_in_bytes(device) /
sizeof(T);
175size_t get_local_mem_size_in_items(
const sycl::device &device,
size_t reserve)
177 return get_local_mem_size_in_bytes(device,
sizeof(T) * reserve) /
sizeof(T);
181inline size_t get_local_mem_size_in_items(
const sycl::queue &queue)
183 return get_local_mem_size_in_items<T>(queue.get_device());
187inline size_t get_local_mem_size_in_items(
const sycl::queue &queue,
190 return get_local_mem_size_in_items<T>(queue.get_device(), reserve);
194sycl::nd_range<Dims> make_ndrange(
const sycl::range<Dims> &global_range,
195 const sycl::range<Dims> &local_range,
196 const sycl::range<Dims> &work_per_item)
198 sycl::range<Dims> aligned_global_range;
200 for (
int i = 0; i < Dims; ++i) {
201 aligned_global_range[i] =
202 Align(CeilDiv(global_range[i], work_per_item[i]), local_range[i]);
205 return sycl::nd_range<Dims>(aligned_global_range, local_range);
209 make_ndrange(
size_t global_size,
size_t local_range,
size_t work_per_item);
213pybind11::dtype dtype_from_typenum(
int dst_typenum);
215template <
typename dispatchT,
216 template <
typename fnT,
typename T>
218 int _num_types = type_dispatch::num_types>
219inline void init_dispatch_vector(dispatchT dispatch_vector[])
221 type_dispatch::DispatchVectorBuilder<dispatchT, factoryT, _num_types> dvb;
222 dvb.populate_dispatch_vector(dispatch_vector);
225template <
typename dispatchT,
226 template <
typename fnT,
typename D,
typename S>
228 int _num_types = type_dispatch::num_types>
229inline void init_dispatch_table(dispatchT dispatch_table[][_num_types])
231 type_dispatch::DispatchTableBuilder<dispatchT, factoryT, _num_types> dtb;
232 dtb.populate_dispatch_table(dispatch_table);
236#include "ext/details/common_internal.hpp"