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