DPNP C++ backend kernel library 0.19.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
dpnp_utils.hpp
1//*****************************************************************************
2// Copyright (c) 2016-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#ifndef BACKEND_UTILS_H // Cython compatibility
28#define BACKEND_UTILS_H
29
30#include <algorithm>
31#include <cassert>
32#include <complex>
33#include <iostream>
34#include <iterator>
35#include <stdexcept>
36
37#include <sycl/sycl.hpp>
38
39#include <dpnp_iface_fptr.hpp>
40
45#ifndef __SYCL_COMPILER_VECTOR_ABS_CHANGED
46#define __SYCL_COMPILER_VECTOR_ABS_CHANGED 20230503L
47#endif
48
52#ifndef __INTEL_MKL_2023_0_0_VERSION_REQUIRED
53#define __INTEL_MKL_2023_0_0_VERSION_REQUIRED 20230000
54#endif
55
62#ifndef __INTEL_MKL_2023_2_0_VERSION_REQUIRED
63#define __INTEL_MKL_2023_2_0_VERSION_REQUIRED 20230002L
64#endif
65
86template <typename _DataType>
87void get_shape_offsets_inkernel(const _DataType *shape,
88 size_t shape_size,
89 _DataType *offsets)
90{
91 size_t dim_prod_input = 1;
92 for (size_t i = 0; i < shape_size; ++i) {
93 long i_reverse = shape_size - 1 - i;
94 offsets[i_reverse] = dim_prod_input;
95 dim_prod_input *= shape[i_reverse];
96 }
97
98 return;
99}
100
117template <typename _DataType>
118_DataType get_xyz_id_by_id_inkernel(size_t global_id,
119 const _DataType *offsets,
120 size_t offsets_size,
121 size_t axis)
122{
123 /* avoid warning unused variable*/
124 (void)offsets_size;
125
126 assert(axis < offsets_size);
127
128 _DataType xyz_id = 0;
129 long reminder = global_id;
130 for (size_t i = 0; i < axis + 1; ++i) {
131 const _DataType axis_val = offsets[i];
132 xyz_id = reminder / axis_val;
133 reminder = reminder % axis_val;
134 }
135
136 return xyz_id;
137}
138
150template <typename _DataType>
151static inline bool array_equal(const _DataType *input1,
152 const size_t input1_size,
153 const _DataType *input2,
154 const size_t input2_size)
155{
156 if (input1_size != input2_size)
157 return false;
158
159 const std::vector<_DataType> input1_vec(input1, input1 + input1_size);
160 const std::vector<_DataType> input2_vec(input2, input2 + input2_size);
161
162 return std::equal(std::begin(input1_vec), std::end(input1_vec),
163 std::begin(input2_vec));
164}
165
176template <typename T>
177static inline void validate_type_for_device(const sycl::device &d)
178{
179 if constexpr (std::is_same_v<T, double>) {
180 if (!d.has(sycl::aspect::fp64)) {
181 throw std::runtime_error("Device " +
182 d.get_info<sycl::info::device::name>() +
183 " does not support type 'double'");
184 }
185 }
186 else if constexpr (std::is_same_v<T, std::complex<double>>) {
187 if (!d.has(sycl::aspect::fp64)) {
188 throw std::runtime_error(
189 "Device " + d.get_info<sycl::info::device::name>() +
190 " does not support type 'complex<double>'");
191 }
192 }
193 else if constexpr (std::is_same_v<T, sycl::half>) {
194 if (!d.has(sycl::aspect::fp16)) {
195 throw std::runtime_error("Device " +
196 d.get_info<sycl::info::device::name>() +
197 " does not support type 'half'");
198 }
199 }
200}
201
212template <typename T>
213static inline void validate_type_for_device(const sycl::queue &q)
214{
215 validate_type_for_device<T>(q.get_device());
216}
217
226template <typename T>
227std::ostream &operator<<(std::ostream &out, const std::vector<T> &vec)
228{
229 std::string delimiter;
230 out << "{";
231 // std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(out, ", "));
232 // out << "\b\b}"; // last two 'backspaces' needs to eliminate last
233 // delimiter. ex: {2, 3, 4, }
234 for (auto &elem : vec) {
235 out << delimiter << elem;
236 if (delimiter.empty()) {
237 delimiter.assign(", ");
238 }
239 }
240 out << "}";
241
242 return out;
243}
244
254template <typename T>
255std::ostream &operator<<(std::ostream &out, DPNPFuncType elem)
256{
257 out << static_cast<size_t>(elem);
258
259 return out;
260}
261
262#endif // BACKEND_UTILS_H
DPNPFuncType
Template types which are used in this interface.
std::ostream & operator<<(std::ostream &out, const std::vector< T > &vec)
print std::vector to std::ostream.
void get_shape_offsets_inkernel(const _DataType *shape, size_t shape_size, _DataType *offsets)
Shape offset calculation used in kernels.
_DataType get_xyz_id_by_id_inkernel(size_t global_id, const _DataType *offsets, size_t offsets_size, size_t axis)
Calculate xyz id for given axis from linear index.