|
| 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 <oneapi/mkl.hpp> |
| 29 | +#include <sycl/sycl.hpp> |
| 30 | + |
| 31 | +#include <dpctl4pybind11.hpp> |
| 32 | + |
| 33 | +namespace dpnp::extensions::fft |
| 34 | +{ |
| 35 | +namespace mkl_dft = oneapi::mkl::dft; |
| 36 | +namespace py = pybind11; |
| 37 | + |
| 38 | +template <mkl_dft::precision prec> |
| 39 | +class ComplexDescriptorWrapper |
| 40 | +{ |
| 41 | +public: |
| 42 | + using descr_type = mkl_dft::descriptor<prec, mkl_dft::domain::COMPLEX>; |
| 43 | + |
| 44 | + ComplexDescriptorWrapper(std::int64_t n) : descr_(n), queue_ptr_{} {} |
| 45 | + ComplexDescriptorWrapper(std::vector<std::int64_t> dimensions) |
| 46 | + : descr_(dimensions), queue_ptr_{} |
| 47 | + { |
| 48 | + } |
| 49 | + ~ComplexDescriptorWrapper() {} |
| 50 | + |
| 51 | + void commit(sycl::queue &q) |
| 52 | + { |
| 53 | + mkl_dft::precision fft_prec = get_precision(); |
| 54 | + if (fft_prec == mkl_dft::precision::DOUBLE && |
| 55 | + !q.get_device().has(sycl::aspect::fp64)) |
| 56 | + { |
| 57 | + throw py::value_error("Descriptor is double precision but the " |
| 58 | + "device does not support double precision."); |
| 59 | + } |
| 60 | + |
| 61 | + descr_.commit(q); |
| 62 | + queue_ptr_ = std::make_unique<sycl::queue>(q); |
| 63 | + } |
| 64 | + |
| 65 | + descr_type &get_descriptor() { return descr_; } |
| 66 | + |
| 67 | + const sycl::queue &get_queue() const |
| 68 | + { |
| 69 | + if (queue_ptr_) { |
| 70 | + return *queue_ptr_; |
| 71 | + } |
| 72 | + else { |
| 73 | + throw std::runtime_error( |
| 74 | + "Attempt to get queue when it is not yet set"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // config_param::DIMENSION |
| 79 | + template <typename valT = std::int64_t> |
| 80 | + valT get_dim() |
| 81 | + { |
| 82 | + valT dim = -1; |
| 83 | + descr_.get_value(mkl_dft::config_param::DIMENSION, &dim); |
| 84 | + |
| 85 | + return dim; |
| 86 | + } |
| 87 | + |
| 88 | + // config_param::NUMBER_OF_TRANSFORMS |
| 89 | + template <typename valT = std::int64_t> |
| 90 | + valT get_number_of_transforms() |
| 91 | + { |
| 92 | + valT transforms_count{}; |
| 93 | + |
| 94 | + descr_.get_value(mkl_dft::config_param::NUMBER_OF_TRANSFORMS, |
| 95 | + &transforms_count); |
| 96 | + return transforms_count; |
| 97 | + } |
| 98 | + |
| 99 | + template <typename valT = std::int64_t> |
| 100 | + void set_number_of_transforms(const valT num) |
| 101 | + { |
| 102 | + descr_.set_value(mkl_dft::config_param::NUMBER_OF_TRANSFORMS, num); |
| 103 | + } |
| 104 | + |
| 105 | + // config_param::FWD_STRIDES |
| 106 | + template <typename valT = std::vector<std::int64_t>> |
| 107 | + valT get_fwd_strides() |
| 108 | + { |
| 109 | + const typename valT::value_type dim = get_dim(); |
| 110 | + |
| 111 | + valT fwd_strides(dim + 1); |
| 112 | + descr_.get_value(mkl_dft::config_param::FWD_STRIDES, |
| 113 | + fwd_strides.data()); |
| 114 | + return fwd_strides; |
| 115 | + } |
| 116 | + |
| 117 | + template <typename valT = std::vector<std::int64_t>> |
| 118 | + void set_fwd_strides(const valT &strides) |
| 119 | + { |
| 120 | + const typename valT::value_type dim = get_dim(); |
| 121 | + |
| 122 | + if (static_cast<size_t>(dim + 1) != strides.size()) { |
| 123 | + throw py::value_error( |
| 124 | + "Strides length does not match descriptor's dimension"); |
| 125 | + } |
| 126 | + descr_.set_value(mkl_dft::config_param::FWD_STRIDES, strides.data()); |
| 127 | + } |
| 128 | + |
| 129 | + // config_param::BWD_STRIDES |
| 130 | + template <typename valT = std::vector<std::int64_t>> |
| 131 | + valT get_bwd_strides() |
| 132 | + { |
| 133 | + const typename valT::value_type dim = get_dim(); |
| 134 | + |
| 135 | + valT bwd_strides(dim + 1); |
| 136 | + descr_.get_value(mkl_dft::config_param::BWD_STRIDES, |
| 137 | + bwd_strides.data()); |
| 138 | + return bwd_strides; |
| 139 | + } |
| 140 | + |
| 141 | + template <typename valT = std::vector<std::int64_t>> |
| 142 | + void set_bwd_strides(const valT &strides) |
| 143 | + { |
| 144 | + const typename valT::value_type dim = get_dim(); |
| 145 | + |
| 146 | + if (static_cast<size_t>(dim + 1) != strides.size()) { |
| 147 | + throw py::value_error( |
| 148 | + "Strides length does not match descriptor's dimension"); |
| 149 | + } |
| 150 | + descr_.set_value(mkl_dft::config_param::BWD_STRIDES, strides.data()); |
| 151 | + } |
| 152 | + |
| 153 | + // config_param::FWD_DISTANCE |
| 154 | + template <typename valT = std::int64_t> |
| 155 | + valT get_fwd_distance() |
| 156 | + { |
| 157 | + valT dist = 0; |
| 158 | + |
| 159 | + descr_.get_value(mkl_dft::config_param::FWD_DISTANCE, &dist); |
| 160 | + return dist; |
| 161 | + } |
| 162 | + |
| 163 | + template <typename valT = std::int64_t> |
| 164 | + void set_fwd_distance(const valT dist) |
| 165 | + { |
| 166 | + descr_.set_value(mkl_dft::config_param::FWD_DISTANCE, dist); |
| 167 | + } |
| 168 | + |
| 169 | + // config_param::BWD_DISTANCE |
| 170 | + template <typename valT = std::int64_t> |
| 171 | + valT get_bwd_distance() |
| 172 | + { |
| 173 | + valT dist = 0; |
| 174 | + |
| 175 | + descr_.get_value(mkl_dft::config_param::BWD_DISTANCE, &dist); |
| 176 | + return dist; |
| 177 | + } |
| 178 | + |
| 179 | + template <typename valT = std::int64_t> |
| 180 | + void set_bwd_distance(const valT dist) |
| 181 | + { |
| 182 | + descr_.set_value(mkl_dft::config_param::BWD_DISTANCE, dist); |
| 183 | + } |
| 184 | + |
| 185 | + // config_param::PLACEMENT |
| 186 | + bool get_in_place() |
| 187 | + { |
| 188 | + DFTI_CONFIG_VALUE placement; |
| 189 | + |
| 190 | + descr_.get_value(mkl_dft::config_param::PLACEMENT, &placement); |
| 191 | + return (placement == DFTI_CONFIG_VALUE::DFTI_INPLACE); |
| 192 | + } |
| 193 | + |
| 194 | + void set_in_place(const bool in_place_request) |
| 195 | + { |
| 196 | + descr_.set_value(mkl_dft::config_param::PLACEMENT, |
| 197 | + (in_place_request) |
| 198 | + ? DFTI_CONFIG_VALUE::DFTI_INPLACE |
| 199 | + : DFTI_CONFIG_VALUE::DFTI_NOT_INPLACE); |
| 200 | + } |
| 201 | + |
| 202 | + // config_param::PRECISION |
| 203 | + mkl_dft::precision get_precision() |
| 204 | + { |
| 205 | + mkl_dft::precision fft_prec; |
| 206 | + |
| 207 | + descr_.get_value(mkl_dft::config_param::PRECISION, &fft_prec); |
| 208 | + return fft_prec; |
| 209 | + } |
| 210 | + |
| 211 | + // config_param::COMMIT_STATUS |
| 212 | + bool is_committed() |
| 213 | + { |
| 214 | + DFTI_CONFIG_VALUE committed; |
| 215 | + |
| 216 | + descr_.get_value(mkl_dft::config_param::COMMIT_STATUS, &committed); |
| 217 | + return (committed == DFTI_CONFIG_VALUE::DFTI_COMMITTED); |
| 218 | + } |
| 219 | + |
| 220 | +private: |
| 221 | + mkl_dft::descriptor<prec, mkl_dft::domain::COMPLEX> descr_; |
| 222 | + std::unique_ptr<sycl::queue> queue_ptr_; |
| 223 | +}; |
| 224 | + |
| 225 | +// forward declaration |
| 226 | +template <mkl_dft::precision prec> |
| 227 | +std::pair<sycl::event, sycl::event> |
| 228 | + compute_fft_out_of_place(ComplexDescriptorWrapper<prec> &descr, |
| 229 | + const dpctl::tensor::usm_ndarray &in, |
| 230 | + const dpctl::tensor::usm_ndarray &out, |
| 231 | + const bool is_forward, |
| 232 | + const std::vector<sycl::event> &depends); |
| 233 | + |
| 234 | +template <mkl_dft::precision prec> |
| 235 | +std::pair<sycl::event, sycl::event> |
| 236 | + compute_fft_in_place(ComplexDescriptorWrapper<prec> &descr, |
| 237 | + const dpctl::tensor::usm_ndarray &in_out, |
| 238 | + const bool is_forward, |
| 239 | + const std::vector<sycl::event> &depends); |
| 240 | + |
| 241 | +} // namespace dpnp::extensions::fft |
0 commit comments