DPNP C++ backend kernel library 0.20.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
common_internal.hpp
1//*****************************************************************************
2// Copyright (c) 2024, 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// - Neither the name of the copyright holder nor the names of its contributors
13// may be used to endorse or promote products derived from this software
14// without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26// THE POSSIBILITY OF SUCH DAMAGE.
27//*****************************************************************************
28
29#include "ext/common.hpp"
30#include "utils/type_dispatch.hpp"
31#include <pybind11/pybind11.h>
32
33namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
34
35namespace ext::common
36{
37inline size_t get_max_local_size(const sycl::device &device)
38{
39 constexpr const int default_max_cpu_local_size = 256;
40 constexpr const int default_max_gpu_local_size = 0;
41
42 return get_max_local_size(device, default_max_cpu_local_size,
43 default_max_gpu_local_size);
44}
45
46inline size_t get_max_local_size(const sycl::device &device,
47 int cpu_local_size_limit,
48 int gpu_local_size_limit)
49{
50 int max_work_group_size =
51 device.get_info<sycl::info::device::max_work_group_size>();
52 if (device.is_cpu() && cpu_local_size_limit > 0) {
53 return std::min(cpu_local_size_limit, max_work_group_size);
54 }
55 else if (device.is_gpu() && gpu_local_size_limit > 0) {
56 return std::min(gpu_local_size_limit, max_work_group_size);
57 }
58
59 return max_work_group_size;
60}
61
62inline sycl::nd_range<1>
63 make_ndrange(size_t global_size, size_t local_range, size_t work_per_item)
64{
65 return make_ndrange(sycl::range<1>(global_size),
66 sycl::range<1>(local_range),
67 sycl::range<1>(work_per_item));
68}
69
70inline size_t get_local_mem_size_in_bytes(const sycl::device &device)
71{
72 // Reserving 1kb for runtime needs
73 constexpr const size_t reserve = 1024;
74
75 return get_local_mem_size_in_bytes(device, reserve);
76}
77
78inline size_t get_local_mem_size_in_bytes(const sycl::device &device,
79 size_t reserve)
80{
81 size_t local_mem_size =
82 device.get_info<sycl::info::device::local_mem_size>();
83 return local_mem_size - reserve;
84}
85
86inline pybind11::dtype dtype_from_typenum(int dst_typenum)
87{
88 dpctl_td_ns::typenum_t dst_typenum_t =
89 static_cast<dpctl_td_ns::typenum_t>(dst_typenum);
90 switch (dst_typenum_t) {
91 case dpctl_td_ns::typenum_t::BOOL:
92 return py::dtype("?");
93 case dpctl_td_ns::typenum_t::INT8:
94 return py::dtype("i1");
95 case dpctl_td_ns::typenum_t::UINT8:
96 return py::dtype("u1");
97 case dpctl_td_ns::typenum_t::INT16:
98 return py::dtype("i2");
99 case dpctl_td_ns::typenum_t::UINT16:
100 return py::dtype("u2");
101 case dpctl_td_ns::typenum_t::INT32:
102 return py::dtype("i4");
103 case dpctl_td_ns::typenum_t::UINT32:
104 return py::dtype("u4");
105 case dpctl_td_ns::typenum_t::INT64:
106 return py::dtype("i8");
107 case dpctl_td_ns::typenum_t::UINT64:
108 return py::dtype("u8");
109 case dpctl_td_ns::typenum_t::HALF:
110 return py::dtype("f2");
111 case dpctl_td_ns::typenum_t::FLOAT:
112 return py::dtype("f4");
113 case dpctl_td_ns::typenum_t::DOUBLE:
114 return py::dtype("f8");
115 case dpctl_td_ns::typenum_t::CFLOAT:
116 return py::dtype("c8");
117 case dpctl_td_ns::typenum_t::CDOUBLE:
118 return py::dtype("c16");
119 default:
120 throw py::value_error("Unrecognized dst_typeid");
121 }
122}
123
124} // namespace ext::common