DPNP C++ backend kernel library 0.18.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
common.hpp
1//*****************************************************************************
2// Copyright (c) 2024-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 <complex>
29#include <pybind11/numpy.h>
30#include <pybind11/pybind11.h>
31#include <sycl/sycl.hpp>
32
33#include "utils/math_utils.hpp"
34#include "utils/type_utils.hpp"
35
36namespace type_utils = dpctl::tensor::type_utils;
37
38namespace statistics::common
39{
40
41template <typename N, typename D>
42constexpr auto CeilDiv(N n, D d)
43{
44 return (n + d - 1) / d;
45}
46
47template <typename N, typename D>
48constexpr auto Align(N n, D d)
49{
50 return CeilDiv(n, d) * d;
51}
52
53template <typename T, sycl::memory_order Order, sycl::memory_scope Scope>
55{
56 static void add(T &lhs, const T &value)
57 {
58 if constexpr (type_utils::is_complex_v<T>) {
59 using vT = typename T::value_type;
60 vT *_lhs = reinterpret_cast<vT(&)[2]>(lhs);
61 const vT *_val = reinterpret_cast<const vT(&)[2]>(value);
62
63 AtomicOp<vT, Order, Scope>::add(_lhs[0], _val[0]);
64 AtomicOp<vT, Order, Scope>::add(_lhs[1], _val[1]);
65 }
66 else {
67 sycl::atomic_ref<T, Order, Scope> lh(lhs);
68 lh += value;
69 }
70 }
71};
72
73template <typename T>
74struct Less
75{
76 bool operator()(const T &lhs, const T &rhs) const
77 {
78 if constexpr (type_utils::is_complex_v<T>) {
79 return dpctl::tensor::math_utils::less_complex(lhs, rhs);
80 }
81 else {
82 return std::less{}(lhs, rhs);
83 }
84 }
85};
86
87template <typename T>
88struct IsNan
89{
90 static bool isnan(const T &v)
91 {
92 if constexpr (type_utils::is_complex_v<T>) {
93 using vT = typename T::value_type;
94
95 const vT real1 = std::real(v);
96 const vT imag1 = std::imag(v);
97
98 return IsNan<vT>::isnan(real1) || IsNan<vT>::isnan(imag1);
99 }
100 else if constexpr (std::is_floating_point_v<T> ||
101 std::is_same_v<T, sycl::half>) {
102 return sycl::isnan(v);
103 }
104
105 return false;
106 }
107};
108
109size_t get_max_local_size(const sycl::device &device);
110size_t get_max_local_size(const sycl::device &device,
111 int cpu_local_size_limit,
112 int gpu_local_size_limit);
113
114inline size_t get_max_local_size(const sycl::queue &queue)
115{
116 return get_max_local_size(queue.get_device());
117}
118
119inline size_t get_max_local_size(const sycl::queue &queue,
120 int cpu_local_size_limit,
121 int gpu_local_size_limit)
122{
123 return get_max_local_size(queue.get_device(), cpu_local_size_limit,
124 gpu_local_size_limit);
125}
126
127size_t get_local_mem_size_in_bytes(const sycl::device &device);
128size_t get_local_mem_size_in_bytes(const sycl::device &device, size_t reserve);
129
130inline size_t get_local_mem_size_in_bytes(const sycl::queue &queue)
131{
132 return get_local_mem_size_in_bytes(queue.get_device());
133}
134
135inline size_t get_local_mem_size_in_bytes(const sycl::queue &queue,
136 size_t reserve)
137{
138 return get_local_mem_size_in_bytes(queue.get_device(), reserve);
139}
140
141template <typename T>
142size_t get_local_mem_size_in_items(const sycl::device &device)
143{
144 return get_local_mem_size_in_bytes(device) / sizeof(T);
145}
146
147template <typename T>
148size_t get_local_mem_size_in_items(const sycl::device &device, size_t reserve)
149{
150 return get_local_mem_size_in_bytes(device, sizeof(T) * reserve) / sizeof(T);
151}
152
153template <typename T>
154inline size_t get_local_mem_size_in_items(const sycl::queue &queue)
155{
156 return get_local_mem_size_in_items<T>(queue.get_device());
157}
158
159template <typename T>
160inline size_t get_local_mem_size_in_items(const sycl::queue &queue,
161 size_t reserve)
162{
163 return get_local_mem_size_in_items<T>(queue.get_device(), reserve);
164}
165
166template <int Dims>
167sycl::nd_range<Dims> make_ndrange(const sycl::range<Dims> &global_range,
168 const sycl::range<Dims> &local_range,
169 const sycl::range<Dims> &work_per_item)
170{
171 sycl::range<Dims> aligned_global_range;
172
173 for (int i = 0; i < Dims; ++i) {
174 aligned_global_range[i] =
175 Align(CeilDiv(global_range[i], work_per_item[i]), local_range[i]);
176 }
177
178 return sycl::nd_range<Dims>(aligned_global_range, local_range);
179}
180
181sycl::nd_range<1>
182 make_ndrange(size_t global_size, size_t local_range, size_t work_per_item);
183
184// This function is a copy from dpctl because it is not available in the public
185// headers of dpctl.
186pybind11::dtype dtype_from_typenum(int dst_typenum);
187
188} // namespace statistics::common