okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
57.1 KiB1249 linesraw
1//==---- fft_utils.hpp ----------------------------*- C++ -*----------------==//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __DPCT_FFT_UTILS_HPP__
10#define __DPCT_FFT_UTILS_HPP__
11
12#include "compat_service.hpp"
13#include "lib_common_utils.hpp"
14
15#include <optional>
16#include <utility>
17
18namespace dpct {
19namespace fft {
20/// An enumeration type to describe the FFT direction is forward or backward.
21enum fft_direction : int { forward = 0, backward };
22/// An enumeration type to describe the types of FFT input and output data.
23enum fft_type : int {
24  real_float_to_complex_float = 0,
25  complex_float_to_real_float,
26  real_double_to_complex_double,
27  complex_double_to_real_double,
28  complex_float_to_complex_float,
29  complex_double_to_complex_double,
30};
31
32/// A class to perform FFT calculation.
33class fft_engine {
34public:
35  /// Default constructor.
36  fft_engine() {}
37  /// Commit the configuration to calculate n-D FFT.
38  /// \param [in] exec_queue The queue where the calculation should be executed.
39  /// \param [in] dim Dimension number of the data.
40  /// \param [in] n Pointer to an array containing each dimension's size.
41  /// \param [in] inembed Pointer to an array containing each dimension's size
42  /// of the embedded input data.
43  /// \param [in] istride Stride size of the input data.
44  /// \param [in] idist Distance between the two batches of the input data.
45  /// \param [in] input_type Input data type.
46  /// \param [in] onembed Pointer to an array containing each dimension's size
47  /// of the embedded output data.
48  /// \param [in] ostride Stride size of the output data.
49  /// \param [in] odist Distance between the two batches of the output data.
50  /// \param [in] output_type Output data type.
51  /// \param [in] batch The number of FFT operations to perform.
52  /// \param [out] scratchpad_size The workspace size required for this FFT.
53  /// If this value is used to allocate memory, \p direction_and_placement need
54  /// to be specified explicitly to get correct result.
55  /// \param [in] direction_and_placement Explicitly specify the FFT direction
56  /// and placement info. If this value is specified, the direction parameter
57  /// will be ignored in the fft_engine::compute function. If it is not set,
58  /// forward direction(if current FFT is complex-to-complex) and out-of-place
59  /// (false) are set by default.
60  void commit(sycl::queue *exec_queue, int dim, long long *n,
61              long long *inembed, long long istride, long long idist,
62              library_data_t input_type, long long *onembed, long long ostride,
63              long long odist, library_data_t output_type, long long batch,
64              size_t *scratchpad_size,
65              std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
66                  direction_and_placement = std::nullopt) {
67    _q = exec_queue;
68    init<long long>(dim, n, inembed, istride, idist, input_type, onembed,
69                    ostride, odist, output_type, batch,
70                    direction_and_placement);
71    if (scratchpad_size) {
72      if (_is_estimate_call)
73        *scratchpad_size = _workspace_estimate_bytes;
74      else
75        *scratchpad_size = _workspace_bytes;
76    }
77  }
78  /// Commit the configuration to calculate n-D FFT.
79  /// \param [in] exec_queue The queue where the calculation should be executed.
80  /// \param [in] dim Dimension number of the data.
81  /// \param [in] n Pointer to an array containing each dimension's size.
82  /// \param [in] inembed Pointer to an array containing each dimension's size
83  /// of the embedded input data.
84  /// \param [in] istride Stride size of the input data.
85  /// \param [in] idist Distance between the two batches of the input data.
86  /// \param [in] input_type Input data type.
87  /// \param [in] onembed Pointer to an array containing each dimension's size
88  /// of the embedded output data.
89  /// \param [in] ostride Stride size of the output data.
90  /// \param [in] odist Distance between the two batches of the output data.
91  /// \param [in] output_type Output data type.
92  /// \param [in] batch The number of FFT operations to perform.
93  /// \param [out] scratchpad_size The workspace size required for this FFT.
94  /// If this value is used to allocate memory, \p direction_and_placement need
95  /// to be specified explicitly to get correct result.
96  /// \param [in] direction_and_placement Explicitly specify the FFT direction
97  /// and placement info. If this value is specified, the direction parameter
98  /// will be ignored in the fft_engine::compute function. If it is not set,
99  /// forward direction(if current FFT is complex-to-complex) and out-of-place
100  /// (false) are set by default.
101  void commit(sycl::queue *exec_queue, int dim, int *n, int *inembed,
102              int istride, int idist, library_data_t input_type, int *onembed,
103              int ostride, int odist, library_data_t output_type, int batch,
104              size_t *scratchpad_size,
105              std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
106                  direction_and_placement = std::nullopt) {
107    _q = exec_queue;
108    init<int>(dim, n, inembed, istride, idist, input_type, onembed, ostride,
109              odist, output_type, batch, direction_and_placement);
110    if (scratchpad_size) {
111      if (_is_estimate_call)
112        *scratchpad_size = _workspace_estimate_bytes;
113      else
114        *scratchpad_size = _workspace_bytes;
115    }
116  }
117  /// Commit the configuration to calculate n-D FFT.
118  /// \param [in] exec_queue The queue where the calculation should be executed.
119  /// \param [in] dim Dimension number of the data.
120  /// \param [in] n Pointer to an array containing each dimension's size.
121  /// \param [in] inembed Pointer to an array containing each dimension's size
122  /// of the embedded input data.
123  /// \param [in] istride Stride size of the input data.
124  /// \param [in] idist Distance between the two batches of the input data.
125  /// \param [in] onembed Pointer to an array containing each dimension's size
126  /// of the embedded output data.
127  /// \param [in] ostride Stride size of the output data.
128  /// \param [in] odist Distance between the two batches of the output data.
129  /// \param [in] type The FFT type.
130  /// \param [in] batch The number of FFT operations to perform.
131  /// \param [out] scratchpad_size The workspace size required for this FFT.
132  /// If this value is used to allocate memory, \p direction_and_placement need
133  /// to be specified explicitly to get correct result.
134  /// \param [in] direction_and_placement Explicitly specify the FFT direction
135  /// and placement info. If this value is specified, the direction parameter
136  /// will be ignored in the fft_engine::compute function. If it is not set,
137  /// forward direction(if current FFT is complex-to-complex) and out-of-place
138  /// (false) are set by default.
139  void commit(sycl::queue *exec_queue, int dim, long long *n,
140              long long *inembed, long long istride, long long idist,
141              long long *onembed, long long ostride, long long odist,
142              fft_type type, long long batch, size_t *scratchpad_size,
143              std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
144                  direction_and_placement = std::nullopt) {
145    commit(exec_queue, dim, n, inembed, istride, idist,
146           fft_type_to_data_type(type).first, onembed, ostride, odist,
147           fft_type_to_data_type(type).second, batch, scratchpad_size,
148           direction_and_placement);
149  }
150  /// Commit the configuration to calculate n-D FFT.
151  /// \param [in] exec_queue The queue where the calculation should be executed.
152  /// \param [in] dim Dimension number of the data.
153  /// \param [in] n Pointer to an array containing each dimension's size.
154  /// \param [in] inembed Pointer to an array containing each dimension's size
155  /// of the embedded input data.
156  /// \param [in] istride Stride size of the input data.
157  /// \param [in] idist Distance between the two batches of the input data.
158  /// \param [in] onembed Pointer to an array containing each dimension's size
159  /// of the embedded output data.
160  /// \param [in] ostride Stride size of the output data.
161  /// \param [in] odist Distance between the two batches of the output data.
162  /// \param [in] type The FFT type.
163  /// \param [in] batch The number of FFT operations to perform.
164  /// \param [out] scratchpad_size The workspace size required for this FFT.
165  /// If this value is used to allocate memory, \p direction_and_placement need
166  /// to be specified explicitly to get correct result.
167  /// \param [in] direction_and_placement Explicitly specify the FFT direction
168  /// and placement info. If this value is specified, the direction parameter
169  /// will be ignored in the fft_engine::compute function. If it is not set,
170  /// forward direction(if current FFT is complex-to-complex) and out-of-place
171  /// (false) are set by default.
172  void commit(sycl::queue *exec_queue, int dim, int *n, int *inembed,
173              int istride, int idist, int *onembed, int ostride, int odist,
174              fft_type type, int batch, size_t *scratchpad_size,
175              std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
176                  direction_and_placement = std::nullopt) {
177    commit(exec_queue, dim, n, inembed, istride, idist,
178           fft_type_to_data_type(type).first, onembed, ostride, odist,
179           fft_type_to_data_type(type).second, batch, scratchpad_size,
180           direction_and_placement);
181  }
182  /// Commit the configuration to calculate 1-D FFT.
183  /// \param [in] exec_queue The queue where the calculation should be executed.
184  /// \param [in] n1 The size of the dimension of the data.
185  /// \param [in] type The FFT type.
186  /// \param [in] batch The number of FFT operations to perform.
187  /// \param [out] scratchpad_size The workspace size required for this FFT.
188  /// If this value is used to allocate memory, \p direction_and_placement need
189  /// to be specified explicitly to get correct result.
190  /// \param [in] direction_and_placement Explicitly specify the FFT direction
191  /// and placement info. If this value is specified, the direction parameter
192  /// will be ignored in the fft_engine::compute function. If it is not set,
193  /// forward direction(if current FFT is complex-to-complex) and out-of-place
194  /// (false) are set by default.
195  void commit(sycl::queue *exec_queue, int n1, fft_type type, int batch,
196              size_t *scratchpad_size,
197              std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
198                  direction_and_placement = std::nullopt) {
199    _q = exec_queue;
200    _n.resize(1);
201    _n[0] = n1;
202    std::tie(_input_type, _output_type) = fft_type_to_data_type(type);
203    _dim = 1;
204    _batch = batch;
205    _is_basic = true;
206    if (direction_and_placement.has_value()) {
207      _is_user_specified_dir_and_placement = true;
208      _direction = direction_and_placement->first;
209      _is_inplace = direction_and_placement->second;
210    }
211    config_and_commit_basic();
212    if (scratchpad_size) {
213      if (_is_estimate_call)
214        *scratchpad_size = _workspace_estimate_bytes;
215      else
216        *scratchpad_size = _workspace_bytes;
217    }
218  }
219  /// Commit the configuration to calculate 2-D FFT.
220  /// \param [in] exec_queue The queue where the calculation should be executed.
221  /// \param [in] n2 The size of the 2nd dimension (outermost) of the data.
222  /// \param [in] n1 The size of the 1st dimension (innermost) of the data.
223  /// \param [in] type The FFT type.
224  /// \param [out] scratchpad_size The workspace size required for this FFT.
225  /// If this value is used to allocate memory, \p direction_and_placement need
226  /// to be specified explicitly to get correct result.
227  /// \param [in] direction_and_placement Explicitly specify the FFT direction
228  /// and placement info. If this value is specified, the direction parameter
229  /// will be ignored in the fft_engine::compute function. If it is not set,
230  /// forward direction(if current FFT is complex-to-complex) and out-of-place
231  /// (false) are set by default.
232  void commit(sycl::queue *exec_queue, int n2, int n1, fft_type type,
233              size_t *scratchpad_size,
234              std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
235                  direction_and_placement = std::nullopt) {
236    _q = exec_queue;
237    _n.resize(2);
238    _n[0] = n2;
239    _n[1] = n1;
240    std::tie(_input_type, _output_type) = fft_type_to_data_type(type);
241    _dim = 2;
242    _is_basic = true;
243    if (direction_and_placement.has_value()) {
244      _is_user_specified_dir_and_placement = true;
245      _direction = direction_and_placement->first;
246      _is_inplace = direction_and_placement->second;
247    }
248    config_and_commit_basic();
249    if (scratchpad_size) {
250      if (_is_estimate_call)
251        *scratchpad_size = _workspace_estimate_bytes;
252      else
253        *scratchpad_size = _workspace_bytes;
254    }
255  }
256  /// Commit the configuration to calculate 3-D FFT.
257  /// \param [in] exec_queue The queue where the calculation should be executed.
258  /// \param [in] n3 The size of the 3rd dimension (outermost) of the data.
259  /// \param [in] n2 The size of the 2nd dimension of the data.
260  /// \param [in] n1 The size of the 1st dimension (innermost) of the data.
261  /// \param [in] type The FFT type.
262  /// \param [out] scratchpad_size The workspace size required for this FFT.
263  /// If this value is used to allocate memory, \p direction_and_placement need
264  /// to be specified explicitly to get correct result.
265  /// \param [in] direction_and_placement Explicitly specify the FFT direction
266  /// and placement info. If this value is specified, the direction parameter
267  /// will be ignored in the fft_engine::compute function. If it is not set,
268  /// forward direction(if current FFT is complex-to-complex) and out-of-place
269  /// (false) are set by default.
270  void commit(sycl::queue *exec_queue, int n3, int n2, int n1, fft_type type,
271              size_t *scratchpad_size,
272              std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
273                  direction_and_placement = std::nullopt) {
274    _q = exec_queue;
275    _n.resize(3);
276    _n[0] = n3;
277    _n[1] = n2;
278    _n[2] = n1;
279    std::tie(_input_type, _output_type) = fft_type_to_data_type(type);
280    _dim = 3;
281    _is_basic = true;
282    if (direction_and_placement.has_value()) {
283      _is_user_specified_dir_and_placement = true;
284      _direction = direction_and_placement->first;
285      _is_inplace = direction_and_placement->second;
286    }
287    config_and_commit_basic();
288    if (scratchpad_size) {
289      if (_is_estimate_call)
290        *scratchpad_size = _workspace_estimate_bytes;
291      else
292        *scratchpad_size = _workspace_bytes;
293    }
294  }
295
296  /// Create the class for calculate 1-D FFT.
297  /// \param [in] exec_queue The queue where the calculation should be executed.
298  /// \param [in] n1 The size of the dimension of the data.
299  /// \param [in] type The FFT type.
300  /// \param [in] batch The number of FFT operations to perform.
301  /// \param [in] direction_and_placement Explicitly specify the FFT direction
302  /// and placement info. If this value is specified, the direction parameter
303  /// will be ignored in the fft_engine::compute function. If it is not set,
304  /// forward direction(if current FFT is complex-to-complex) and out-of-place
305  /// (false) are set by default.
306  static fft_engine *
307  create(sycl::queue *exec_queue, int n1, fft_type type, int batch,
308         std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
309             direction_and_placement = std::nullopt) {
310    fft_engine *engine = new fft_engine();
311    engine->commit(exec_queue, n1, type, batch, nullptr,
312                   direction_and_placement);
313    return engine;
314  }
315  /// Create the class for calculate 2-D FFT.
316  /// \param [in] exec_queue The queue where the calculation should be executed.
317  /// \param [in] n2 The size of the 2nd dimension (outermost) of the data.
318  /// \param [in] n1 The size of the 1st dimension (innermost) of the data.
319  /// \param [in] type The FFT type.
320  /// \param [in] direction_and_placement Explicitly specify the FFT direction
321  /// and placement info. If this value is specified, the direction parameter
322  /// will be ignored in the fft_engine::compute function. If it is not set,
323  /// forward direction(if current FFT is complex-to-complex) and out-of-place
324  /// (false) are set by default.
325  static fft_engine *
326  create(sycl::queue *exec_queue, int n2, int n1, fft_type type,
327         std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
328             direction_and_placement = std::nullopt) {
329    fft_engine *engine = new fft_engine();
330    engine->commit(exec_queue, n2, n1, type, nullptr, direction_and_placement);
331    return engine;
332  }
333  /// Create the class for calculate 3-D FFT.
334  /// \param [in] exec_queue The queue where the calculation should be executed.
335  /// \param [in] n3 The size of the 3rd dimension (outermost) of the data.
336  /// \param [in] n2 The size of the 2nd dimension of the data.
337  /// \param [in] n1 The size of the 1st dimension (innermost) of the data.
338  /// \param [in] type The FFT type.
339  /// \param [in] direction_and_placement Explicitly specify the FFT direction
340  /// and placement info. If this value is specified, the direction parameter
341  /// will be ignored in the fft_engine::compute function. If it is not set,
342  /// forward direction(if current FFT is complex-to-complex) and out-of-place
343  /// (false) are set by default.
344  static fft_engine *
345  create(sycl::queue *exec_queue, int n3, int n2, int n1, fft_type type,
346         std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
347             direction_and_placement = std::nullopt) {
348    fft_engine *engine = new fft_engine();
349    engine->commit(exec_queue, n3, n2, n1, type, nullptr,
350                   direction_and_placement);
351    return engine;
352  }
353  /// Create the class for calculate n-D FFT.
354  /// \param [in] exec_queue The queue where the calculation should be executed.
355  /// \param [in] dim Dimension number of the data.
356  /// \param [in] n Pointer to an array containing each dimension's size.
357  /// \param [in] inembed Pointer to an array containing each dimension's size
358  /// of the embedded input data.
359  /// \param [in] istride Stride size of the input data.
360  /// \param [in] idist Distance between the two batches of the input data.
361  /// \param [in] onembed Pointer to an array containing each dimension's size
362  /// of the embedded output data.
363  /// \param [in] ostride Stride size of the output data.
364  /// \param [in] odist Distance between the two batches of the output data.
365  /// \param [in] type The FFT type.
366  /// \param [in] batch The number of FFT operations to perform.
367  /// \param [in] direction_and_placement Explicitly specify the FFT direction
368  /// and placement info. If this value is specified, the direction parameter
369  /// will be ignored in the fft_engine::compute function. If it is not set,
370  /// forward direction(if current FFT is complex-to-complex) and out-of-place
371  /// (false) are set by default.
372  static fft_engine *
373  create(sycl::queue *exec_queue, int dim, int *n, int *inembed, int istride,
374         int idist, int *onembed, int ostride, int odist, fft_type type,
375         int batch,
376         std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
377             direction_and_placement = std::nullopt) {
378    fft_engine *engine = new fft_engine();
379    engine->commit(exec_queue, dim, n, inembed, istride, idist, onembed,
380                   ostride, odist, type, batch, nullptr,
381                   direction_and_placement);
382    return engine;
383  }
384  /// Create the class for calculate FFT without commit any config.
385  static fft_engine *create() {
386    fft_engine *engine = new fft_engine();
387    return engine;
388  }
389  /// Destroy the class for calculate FFT.
390  /// \param [in] engine Pointer returned from fft_engine::craete.
391  static void destroy(fft_engine *engine) { delete engine; }
392
393#ifdef __INTEL_MKL__
394  /// Estimates the workspace size for calculating n-D FFT.
395  /// \param [in] dim Dimension number of the data.
396  /// \param [in] n Pointer to an array containing each dimension's size.
397  /// \param [in] inembed Pointer to an array containing each dimension's size
398  /// of the embedded input data.
399  /// \param [in] istride Stride size of the input data.
400  /// \param [in] idist Distance between the two batches of the input data.
401  /// \param [in] onembed Pointer to an array containing each dimension's size
402  /// of the embedded output data.
403  /// \param [in] ostride Stride size of the output data.
404  /// \param [in] odist Distance between the two batches of the output data.
405  /// \param [in] type The FFT type.
406  /// \param [in] batch The number of FFT operations to perform.
407  /// \param [out] estimated_scratchpad_size The estimated workspace size
408  /// required for this FFT. If this value is used to allocate memory,
409  /// \p direction_and_placement need to be specified explicitly to get correct
410  /// result.
411  /// \param [in] direction_and_placement Explicitly specify the FFT
412  /// direction and placement info. If it is not set, forward direction(if
413  /// current FFT is complex-to-complex) and out-of-place (false) are set by default.
414  static void
415  estimate_size(int dim, long long *n, long long *inembed, long long istride,
416                long long idist, long long *onembed, long long ostride,
417                long long odist, fft_type type, long long batch,
418                size_t *estimated_scratchpad_size,
419                std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
420                    direction_and_placement = std::nullopt) {
421    fft_engine *engine = fft_engine::create();
422    engine->_is_estimate_call = true;
423    engine->commit(&::dpct::cs::get_default_queue(), dim, n, inembed, istride,
424                   idist, fft_type_to_data_type(type).first, onembed, ostride,
425                   odist, fft_type_to_data_type(type).second, batch,
426                   estimated_scratchpad_size, direction_and_placement);
427    fft_engine::destroy(engine);
428  }
429  /// Estimates the workspace size for calculating n-D FFT.
430  /// \param [in] dim Dimension number of the data.
431  /// \param [in] n Pointer to an array containing each dimension's size.
432  /// \param [in] inembed Pointer to an array containing each dimension's size
433  /// of the embedded input data.
434  /// \param [in] istride Stride size of the input data.
435  /// \param [in] idist Distance between the two batches of the input data.
436  /// \param [in] onembed Pointer to an array containing each dimension's size
437  /// of the embedded output data.
438  /// \param [in] ostride Stride size of the output data.
439  /// \param [in] odist Distance between the two batches of the output data.
440  /// \param [in] type The FFT type.
441  /// \param [in] batch The number of FFT operations to perform.
442  /// \param [out] estimated_scratchpad_size The estimated workspace size
443  /// required for this FFT. If this value is used to allocate memory,
444  /// \p direction_and_placement need to be specified explicitly to get correct
445  /// result.
446  /// \param [in] direction_and_placement Explicitly specify the FFT
447  /// direction and placement info. If it is not set, forward direction(if
448  /// current FFT is complex-to-complex) and out-of-place (false) are set by default.
449  static void
450  estimate_size(int dim, int *n, int *inembed, int istride, int idist,
451                int *onembed, int ostride, int odist, fft_type type, int batch,
452                size_t *estimated_scratchpad_size,
453                std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
454                    direction_and_placement = std::nullopt) {
455    fft_engine *engine = fft_engine::create();
456    engine->_is_estimate_call = true;
457    engine->commit(&::dpct::cs::get_default_queue(), dim, n, inembed, istride,
458                   idist, fft_type_to_data_type(type).first, onembed, ostride,
459                   odist, fft_type_to_data_type(type).second, batch,
460                   estimated_scratchpad_size, direction_and_placement);
461    fft_engine::destroy(engine);
462  }
463  /// Estimates the workspace size for calculating 1-D FFT.
464  /// \param [in] n1 The size of the dimension of the data.
465  /// \param [in] type The FFT type.
466  /// \param [in] batch The number of FFT operations to perform.
467  /// \param [out] estimated_scratchpad_size The estimated workspace size
468  /// required for this FFT. If this value is used to allocate memory,
469  /// \p direction_and_placement need to be specified explicitly to get correct
470  /// result.
471  /// \param [in] direction_and_placement Explicitly specify the FFT direction
472  /// and placement info. If it is not set, forward direction(if current FFT is
473  /// complex-to-complex) and out-of-place (false) are set by default.
474  static void
475  estimate_size(int n1, fft_type type, int batch,
476                size_t *estimated_scratchpad_size,
477                std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
478                    direction_and_placement = std::nullopt) {
479    fft_engine *engine = fft_engine::create();
480    engine->_is_estimate_call = true;
481    engine->commit(&::dpct::cs::get_default_queue(), n1, type, batch,
482                   estimated_scratchpad_size, direction_and_placement);
483    fft_engine::destroy(engine);
484  }
485  /// Estimates the workspace size for calculating 2-D FFT.
486  /// \param [in] n2 The size of the 2nd dimension (outermost) of the data.
487  /// \param [in] n1 The size of the 1st dimension (innermost) of the data.
488  /// \param [in] type The FFT type.
489  /// \param [out] estimated_scratchpad_size The estimated workspace size
490  /// required for this FFT. If this value is used to allocate memory,
491  /// \p direction_and_placement need to be specified explicitly to get correct
492  /// result.
493  /// \param [in] direction_and_placement Explicitly specify the FFT
494  /// direction and placement info. If it is not set, forward direction(if
495  /// current FFT is complex-to-complex) and out-of-place (false) are set by default.
496  static void
497  estimate_size(int n2, int n1, fft_type type,
498                size_t *estimated_scratchpad_size,
499                std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
500                    direction_and_placement = std::nullopt) {
501    fft_engine *engine = fft_engine::create();
502    engine->_is_estimate_call = true;
503    engine->commit(&::dpct::cs::get_default_queue(), n2, n1, type,
504                   estimated_scratchpad_size, direction_and_placement);
505    fft_engine::destroy(engine);
506  }
507  /// Estimates the workspace size for calculating 3-D FFT.
508  /// \param [in] n3 The size of the 3rd dimension (outermost) of the data.
509  /// \param [in] n2 The size of the 2nd dimension of the data.
510  /// \param [in] n1 The size of the 1st dimension (innermost) of the data.
511  /// \param [in] type The FFT type.
512  /// \param [out] estimated_scratchpad_size The estimated workspace size
513  /// required for this FFT. If this value is used to allocate memory,
514  /// \p direction_and_placement need to be specified explicitly to get correct
515  /// result.
516  /// \param [in] direction_and_placement Explicitly specify the FFT
517  /// direction and placement info. If it is not set, forward direction(if
518  /// current FFT is complex-to-complex) and out-of-place (false) are set by default.
519  static void
520  estimate_size(int n3, int n2, int n1, fft_type type,
521                size_t *estimated_scratchpad_size,
522                std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
523                    direction_and_placement = std::nullopt) {
524    fft_engine *engine = fft_engine::create();
525    engine->_is_estimate_call = true;
526    engine->commit(&::dpct::cs::get_default_queue(), n3, n2, n1, type,
527                   estimated_scratchpad_size, direction_and_placement);
528    fft_engine::destroy(engine);
529  }
530#endif
531
532  /// Execute the FFT calculation.
533  /// \param [in] input Pointer to the input data.
534  /// \param [out] output Pointer to the output data.
535  /// \param [in] direction The FFT direction.
536  template <typename input_t, typename output_t>
537  void compute(input_t *input, output_t *output, fft_direction direction) {
538    if (_input_type == library_data_t::complex_float &&
539        _output_type == library_data_t::complex_float) {
540      compute_complex<float, oneapi::mkl::dft::precision::SINGLE>(
541          (float *)input, (float *)output, direction);
542    } else if (_input_type == library_data_t::complex_double &&
543               _output_type == library_data_t::complex_double) {
544      compute_complex<double, oneapi::mkl::dft::precision::DOUBLE>(
545          (double *)input, (double *)output, direction);
546    } else if (_input_type == library_data_t::real_float &&
547               _output_type == library_data_t::complex_float) {
548      _direction = direction;
549      compute_real<float, oneapi::mkl::dft::precision::SINGLE>((float *)input,
550                                                               (float *)output);
551    } else if (_input_type == library_data_t::complex_float &&
552               _output_type == library_data_t::real_float) {
553      _direction = direction;
554      compute_real<float, oneapi::mkl::dft::precision::SINGLE>((float *)input,
555                                                               (float *)output);
556    } else if (_input_type == library_data_t::real_double &&
557               _output_type == library_data_t::complex_double) {
558      _direction = direction;
559      compute_real<double, oneapi::mkl::dft::precision::DOUBLE>(
560          (double *)input, (double *)output);
561    } else if (_input_type == library_data_t::complex_double &&
562               _output_type == library_data_t::real_double) {
563      _direction = direction;
564      compute_real<double, oneapi::mkl::dft::precision::DOUBLE>(
565          (double *)input, (double *)output);
566    }
567  }
568  template <>
569  void compute(float *input, sycl::float2 *output, fft_direction direction) {
570    _direction = direction;
571    compute_real<float, oneapi::mkl::dft::precision::SINGLE>((float *)input,
572                                                             (float *)output);
573  }
574  template <>
575  void compute(sycl::float2 *input, float *output, fft_direction direction) {
576    _direction = direction;
577    compute_real<float, oneapi::mkl::dft::precision::SINGLE>((float *)input,
578                                                             (float *)output);
579  }
580  template <>
581  void compute(double *input, sycl::double2 *output, fft_direction direction) {
582    _direction = direction;
583    compute_real<double, oneapi::mkl::dft::precision::DOUBLE>((double *)input,
584                                                              (double *)output);
585  }
586  template <>
587  void compute(sycl::double2 *input, double *output, fft_direction direction) {
588    _direction = direction;
589    compute_real<double, oneapi::mkl::dft::precision::DOUBLE>((double *)input,
590                                                              (double *)output);
591  }
592  template <>
593  void compute(sycl::float2 *input, sycl::float2 *output,
594               fft_direction direction) {
595    compute_complex<float, oneapi::mkl::dft::precision::SINGLE>(
596        (float *)input, (float *)output, direction);
597  }
598  template <>
599  void compute(sycl::double2 *input, sycl::double2 *output,
600               fft_direction direction) {
601    compute_complex<double, oneapi::mkl::dft::precision::DOUBLE>(
602        (double *)input, (double *)output, direction);
603  }
604  /// Setting the user's SYCL queue for calculation.
605  /// \param [in] q Pointer to the SYCL queue.
606  void set_queue(sycl::queue *q) { _q = q; }
607#ifdef __INTEL_MKL__
608  /// Setting whether to use external or internal workspace.
609  /// \param [in] flag True means using internal workspace. False means using
610  /// external workspace.
611  void use_internal_workspace(bool flag = true) {
612    _use_external_workspace = !flag;
613  }
614  /// Specify the external workspace.
615  /// \param [in] ptr Pointer to the workspace.
616  void set_workspace(void *ptr) {
617    if (!_use_external_workspace) {
618      return;
619    }
620    if (_input_type == library_data_t::complex_float &&
621        _output_type == library_data_t::complex_float) {
622      if (_q->get_device().is_gpu()) {
623        auto data = dpct::detail::get_memory<float>(ptr);
624        _desc_sc->set_workspace(data);
625      }
626    } else if (_input_type == library_data_t::complex_double &&
627               _output_type == library_data_t::complex_double) {
628      if (_q->get_device().is_gpu()) {
629        auto data = dpct::detail::get_memory<double>(ptr);
630        _desc_dc->set_workspace(data);
631      }
632    } else if ((_input_type == library_data_t::real_float &&
633                _output_type == library_data_t::complex_float) ||
634               (_input_type == library_data_t::complex_float &&
635                _output_type == library_data_t::real_float)) {
636      if (_q->get_device().is_gpu()) {
637        auto data = dpct::detail::get_memory<float>(ptr);
638        _desc_sr->set_workspace(data);
639      }
640    } else if ((_input_type == library_data_t::real_double &&
641                _output_type == library_data_t::complex_double) ||
642               (_input_type == library_data_t::complex_double &&
643                _output_type == library_data_t::real_double)) {
644      if (_q->get_device().is_gpu()) {
645        auto data = dpct::detail::get_memory<double>(ptr);
646        _desc_dr->set_workspace(data);
647      }
648    } else {
649      throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
650                            "invalid fft type");
651    }
652  }
653#endif
654  /// Get the workspace size.
655  /// \param [out] scratchpad_size Workspace size in bytes.
656  void get_workspace_size(size_t *scratchpad_size) {
657    if (scratchpad_size) {
658      *scratchpad_size = _workspace_bytes;
659    }
660  }
661
662private:
663  static std::pair<library_data_t, library_data_t>
664  fft_type_to_data_type(fft_type type) {
665    switch (type) {
666    case fft_type::real_float_to_complex_float: {
667      return std::make_pair(library_data_t::real_float,
668                            library_data_t::complex_float);
669    }
670    case fft_type::complex_float_to_real_float: {
671      return std::make_pair(library_data_t::complex_float,
672                            library_data_t::real_float);
673    }
674    case fft_type::real_double_to_complex_double: {
675      return std::make_pair(library_data_t::real_double,
676                            library_data_t::complex_double);
677    }
678    case fft_type::complex_double_to_real_double: {
679      return std::make_pair(library_data_t::complex_double,
680                            library_data_t::real_double);
681    }
682    case fft_type::complex_float_to_complex_float: {
683      return std::make_pair(library_data_t::complex_float,
684                            library_data_t::complex_float);
685    }
686    case fft_type::complex_double_to_complex_double: {
687      return std::make_pair(library_data_t::complex_double,
688                            library_data_t::complex_double);
689    }
690    }
691  }
692
693  void config_and_commit_basic() {
694    if (_input_type == library_data_t::complex_float &&
695        _output_type == library_data_t::complex_float) {
696      _desc_sc = std::make_shared<
697          oneapi::mkl::dft::descriptor<oneapi::mkl::dft::precision::SINGLE,
698                                       oneapi::mkl::dft::domain::COMPLEX>>(_n);
699      std::int64_t distance = 1;
700      for (auto i : _n)
701        distance = distance * i;
702      _fwd_dist = distance;
703      _bwd_dist = distance;
704      _desc_sc->set_value(oneapi::mkl::dft::config_param::FWD_DISTANCE,
705                          distance);
706      _desc_sc->set_value(oneapi::mkl::dft::config_param::BWD_DISTANCE,
707                          distance);
708      _desc_sc->set_value(oneapi::mkl::dft::config_param::NUMBER_OF_TRANSFORMS,
709                          _batch);
710      if (_is_user_specified_dir_and_placement && _is_inplace)
711        _desc_sc->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
712                            oneapi::mkl::dft::config_value::INPLACE);
713      else
714        _desc_sc->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
715                            oneapi::mkl::dft::config_value::NOT_INPLACE);
716#ifdef __INTEL_MKL__
717      if (_use_external_workspace) {
718        if (_q->get_device().is_gpu()) {
719          _desc_sc->set_value(
720              oneapi::mkl::dft::config_param::WORKSPACE,
721              oneapi::mkl::dft::config_value::WORKSPACE_EXTERNAL);
722        }
723      }
724      if (_is_estimate_call) {
725        if (_q->get_device().is_gpu()) {
726          _desc_sc->get_value(
727              oneapi::mkl::dft::config_param::WORKSPACE_ESTIMATE_BYTES,
728              &_workspace_estimate_bytes);
729        }
730      } else {
731        _desc_sc->commit(*_q);
732        if (_q->get_device().is_gpu()) {
733          _desc_sc->get_value(oneapi::mkl::dft::config_param::WORKSPACE_BYTES,
734                              &_workspace_bytes);
735        }
736      }
737#else
738      _desc_sc->commit(*_q);
739#endif
740    } else if (_input_type == library_data_t::complex_double &&
741               _output_type == library_data_t::complex_double) {
742      _desc_dc = std::make_shared<
743          oneapi::mkl::dft::descriptor<oneapi::mkl::dft::precision::DOUBLE,
744                                       oneapi::mkl::dft::domain::COMPLEX>>(_n);
745      std::int64_t distance = 1;
746      for (auto i : _n)
747        distance = distance * i;
748      _fwd_dist = distance;
749      _bwd_dist = distance;
750      _desc_dc->set_value(oneapi::mkl::dft::config_param::FWD_DISTANCE,
751                          distance);
752      _desc_dc->set_value(oneapi::mkl::dft::config_param::BWD_DISTANCE,
753                          distance);
754      _desc_dc->set_value(oneapi::mkl::dft::config_param::NUMBER_OF_TRANSFORMS,
755                          _batch);
756      if (_is_user_specified_dir_and_placement && _is_inplace)
757        _desc_dc->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
758                            oneapi::mkl::dft::config_value::INPLACE);
759      else
760        _desc_dc->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
761                            oneapi::mkl::dft::config_value::NOT_INPLACE);
762#ifdef __INTEL_MKL__
763      if (_use_external_workspace) {
764        if (_q->get_device().is_gpu()) {
765          _desc_dc->set_value(
766              oneapi::mkl::dft::config_param::WORKSPACE,
767              oneapi::mkl::dft::config_value::WORKSPACE_EXTERNAL);
768        }
769      }
770      if (_is_estimate_call) {
771        if (_q->get_device().is_gpu()) {
772          _desc_dc->get_value(
773              oneapi::mkl::dft::config_param::WORKSPACE_ESTIMATE_BYTES,
774              &_workspace_estimate_bytes);
775        }
776      } else {
777        _desc_dc->commit(*_q);
778        if (_q->get_device().is_gpu()) {
779          _desc_dc->get_value(oneapi::mkl::dft::config_param::WORKSPACE_BYTES,
780                              &_workspace_bytes);
781        }
782      }
783#else
784      _desc_dc->commit(*_q);
785#endif
786    } else if ((_input_type == library_data_t::real_float &&
787                _output_type == library_data_t::complex_float) ||
788               (_input_type == library_data_t::complex_float &&
789                _output_type == library_data_t::real_float)) {
790      _desc_sr = std::make_shared<oneapi::mkl::dft::descriptor<
791          oneapi::mkl::dft::precision::SINGLE, oneapi::mkl::dft::domain::REAL>>(
792          _n);
793      if (_input_type == library_data_t::real_float &&
794          _output_type == library_data_t::complex_float)
795        _direction = fft_direction::forward;
796      else
797        _direction = fft_direction::backward;
798      _desc_sr->set_value(oneapi::mkl::dft::config_param::NUMBER_OF_TRANSFORMS,
799                          _batch);
800      if (_is_user_specified_dir_and_placement && _is_inplace) {
801        _desc_sr->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
802                            oneapi::mkl::dft::config_value::INPLACE);
803        set_stride_and_distance_basic<true>(_desc_sr);
804      } else {
805        _desc_sr->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
806                            oneapi::mkl::dft::config_value::NOT_INPLACE);
807        set_stride_and_distance_basic<false>(_desc_sr);
808      }
809#ifdef __INTEL_MKL__
810      if (_use_external_workspace) {
811        if (_q->get_device().is_gpu()) {
812          _desc_sr->set_value(
813              oneapi::mkl::dft::config_param::WORKSPACE,
814              oneapi::mkl::dft::config_value::WORKSPACE_EXTERNAL);
815        }
816      }
817      if (_is_estimate_call) {
818        if (_q->get_device().is_gpu()) {
819          _desc_sr->get_value(
820              oneapi::mkl::dft::config_param::WORKSPACE_ESTIMATE_BYTES,
821              &_workspace_estimate_bytes);
822        }
823      } else {
824        _desc_sr->commit(*_q);
825        if (_q->get_device().is_gpu()) {
826          _desc_sr->get_value(oneapi::mkl::dft::config_param::WORKSPACE_BYTES,
827                              &_workspace_bytes);
828        }
829      }
830#else
831      _desc_sr->commit(*_q);
832#endif
833    } else if ((_input_type == library_data_t::real_double &&
834                _output_type == library_data_t::complex_double) ||
835               (_input_type == library_data_t::complex_double &&
836                _output_type == library_data_t::real_double)) {
837      _desc_dr = std::make_shared<oneapi::mkl::dft::descriptor<
838          oneapi::mkl::dft::precision::DOUBLE, oneapi::mkl::dft::domain::REAL>>(
839          _n);
840      if (_input_type == library_data_t::real_double &&
841          _output_type == library_data_t::complex_double)
842        _direction = fft_direction::forward;
843      else
844        _direction = fft_direction::backward;
845      _desc_dr->set_value(oneapi::mkl::dft::config_param::NUMBER_OF_TRANSFORMS,
846                          _batch);
847      if (_is_user_specified_dir_and_placement && _is_inplace) {
848        _desc_dr->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
849                            oneapi::mkl::dft::config_value::INPLACE);
850        set_stride_and_distance_basic<true>(_desc_dr);
851      } else {
852        _desc_dr->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
853                            oneapi::mkl::dft::config_value::NOT_INPLACE);
854        set_stride_and_distance_basic<false>(_desc_dr);
855      }
856#ifdef __INTEL_MKL__
857      if (_use_external_workspace) {
858        if (_q->get_device().is_gpu()) {
859          _desc_dr->set_value(
860              oneapi::mkl::dft::config_param::WORKSPACE,
861              oneapi::mkl::dft::config_value::WORKSPACE_EXTERNAL);
862        }
863      }
864      if (_is_estimate_call) {
865        if (_q->get_device().is_gpu()) {
866          _desc_dr->get_value(
867              oneapi::mkl::dft::config_param::WORKSPACE_ESTIMATE_BYTES,
868              &_workspace_estimate_bytes);
869        }
870      } else {
871        _desc_dr->commit(*_q);
872        if (_q->get_device().is_gpu()) {
873          _desc_dr->get_value(oneapi::mkl::dft::config_param::WORKSPACE_BYTES,
874                              &_workspace_bytes);
875        }
876      }
877#else
878      _desc_dr->commit(*_q);
879#endif
880    } else {
881      throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
882                            "invalid fft type");
883    }
884  }
885
886  void config_and_commit_advanced() {
887#define CONFIG_LAYOUT_AND_PLACEMENT(DESC, PREC, DOM)                           \
888  DESC = std::make_shared<oneapi::mkl::dft::descriptor<                        \
889      oneapi::mkl::dft::precision::PREC, oneapi::mkl::dft::domain::DOM>>(_n);  \
890  set_stride_advanced(DESC);                                                   \
891  DESC->set_value(oneapi::mkl::dft::config_param::FWD_DISTANCE, _fwd_dist);    \
892  DESC->set_value(oneapi::mkl::dft::config_param::BWD_DISTANCE, _bwd_dist);    \
893  DESC->set_value(oneapi::mkl::dft::config_param::NUMBER_OF_TRANSFORMS,        \
894                  _batch);                                                     \
895  if (_is_user_specified_dir_and_placement && _is_inplace)                     \
896    DESC->set_value(oneapi::mkl::dft::config_param::PLACEMENT,                 \
897                    oneapi::mkl::dft::config_value::INPLACE);                  \
898  else                                                                         \
899    DESC->set_value(oneapi::mkl::dft::config_param::PLACEMENT,                 \
900                    oneapi::mkl::dft::config_value::NOT_INPLACE);
901
902#ifdef __INTEL_MKL__
903#define CONFIG_AND_COMMIT(DESC, PREC, DOM)                                     \
904  {                                                                            \
905    CONFIG_LAYOUT_AND_PLACEMENT(DESC, PREC, DOM)                               \
906    if (_is_estimate_call) {                                                   \
907      if (_q->get_device().is_gpu()) {                                         \
908        DESC->get_value(                                                       \
909            oneapi::mkl::dft::config_param::WORKSPACE_ESTIMATE_BYTES,          \
910            &_workspace_estimate_bytes);                                       \
911      }                                                                        \
912    } else {                                                                   \
913      DESC->commit(*_q);                                                       \
914      if (_is_estimate_call) {                                                 \
915        DESC->get_value(oneapi::mkl::dft::config_param::WORKSPACE_BYTES,       \
916                        &_workspace_bytes);                                    \
917      }                                                                        \
918    }                                                                          \
919  }
920#else
921#define CONFIG_AND_COMMIT(DESC, PREC, DOM)                                     \
922  {                                                                            \
923    CONFIG_LAYOUT_AND_PLACEMENT(DESC, PREC, DOM)                               \
924    DESC->commit(*_q);                                                         \
925  }
926#endif
927
928    if (_input_type == library_data_t::complex_float &&
929        _output_type == library_data_t::complex_float) {
930      CONFIG_AND_COMMIT(_desc_sc, SINGLE, COMPLEX);
931    } else if (_input_type == library_data_t::complex_double &&
932               _output_type == library_data_t::complex_double) {
933      CONFIG_AND_COMMIT(_desc_dc, DOUBLE, COMPLEX);
934    } else if ((_input_type == library_data_t::real_float &&
935                _output_type == library_data_t::complex_float) ||
936               (_input_type == library_data_t::complex_float &&
937                _output_type == library_data_t::real_float)) {
938      CONFIG_AND_COMMIT(_desc_sr, SINGLE, REAL);
939    } else if ((_input_type == library_data_t::real_double &&
940                _output_type == library_data_t::complex_double) ||
941               (_input_type == library_data_t::complex_double &&
942                _output_type == library_data_t::real_double)) {
943      CONFIG_AND_COMMIT(_desc_dr, DOUBLE, REAL);
944    } else {
945      throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
946                            "invalid fft type");
947    }
948#undef CONFIG_LAYOUT_AND_PLACEMENT
949#undef CONFIG_AND_COMMIT
950  }
951
952  template <typename T>
953  void init(int dim, T *n, T *inembed, T istride, T idist,
954            library_data_t input_type, T *onembed, T ostride, T odist,
955            library_data_t output_type, T batch,
956            std::optional<std::pair<fft_direction, bool /*is_inplace*/>>
957                direction_and_placement) {
958    if (direction_and_placement.has_value()) {
959      _is_user_specified_dir_and_placement = true;
960      _direction = direction_and_placement->first;
961      _is_inplace = direction_and_placement->second;
962    }
963    _n.resize(dim);
964    _inembed.resize(dim);
965    _onembed.resize(dim);
966    _input_type = input_type;
967    _output_type = output_type;
968    for (int i = 0; i < dim; i++) {
969      _n[i] = n[i];
970    }
971    if (inembed && onembed) {
972      for (int i = 0; i < dim; i++) {
973        _inembed[i] = inembed[i];
974        _onembed[i] = onembed[i];
975      }
976      _istride = istride;
977      _ostride = ostride;
978
979      if ((_input_type == library_data_t::real_float &&
980           _output_type == library_data_t::complex_float) ||
981          (_input_type == library_data_t::real_double &&
982           _output_type == library_data_t::complex_double)) {
983        _fwd_dist = idist;
984        _bwd_dist = odist;
985        _direction = fft_direction::forward;
986      } else if ((_output_type == library_data_t::real_float &&
987                  _input_type == library_data_t::complex_float) ||
988                 (_output_type == library_data_t::real_double &&
989                  _input_type == library_data_t::complex_double)) {
990        _fwd_dist = odist;
991        _bwd_dist = idist;
992        _direction = fft_direction::backward;
993      } else {
994        if (_is_user_specified_dir_and_placement &&
995            (_direction == fft_direction::backward)) {
996          _fwd_dist = odist;
997          _bwd_dist = idist;
998        } else {
999          _fwd_dist = idist;
1000          _bwd_dist = odist;
1001        }
1002      }
1003    } else {
1004      _is_basic = true;
1005    }
1006    _batch = batch;
1007    _dim = dim;
1008
1009    if (_is_basic)
1010      config_and_commit_basic();
1011    else
1012      config_and_commit_advanced();
1013  }
1014  template <class Desc_t>
1015  void set_stride_advanced(std::shared_ptr<Desc_t> desc) {
1016    if (_dim == 1) {
1017      _fwd_strides = {0, _istride};
1018      _bwd_strides = {0, _ostride};
1019    } else if (_dim == 2) {
1020      _fwd_strides = {0, _inembed[1] * _istride, _istride};
1021      _bwd_strides = {0, _onembed[1] * _ostride, _ostride};
1022    } else if (_dim == 3) {
1023      _fwd_strides = {0, _inembed[2] * _inembed[1] * _istride,
1024                      _inembed[2] * _istride, _istride};
1025      _bwd_strides = {0, _onembed[2] * _onembed[1] * _ostride,
1026                      _onembed[2] * _ostride, _ostride};
1027    }
1028    if (_direction == fft_direction::backward) {
1029      std::swap_ranges(_fwd_strides.begin(), _fwd_strides.end(),
1030                       _bwd_strides.begin());
1031    }
1032#ifdef __INTEL_MKL__
1033    desc->set_value(oneapi::mkl::dft::config_param::FWD_STRIDES, _fwd_strides);
1034    desc->set_value(oneapi::mkl::dft::config_param::BWD_STRIDES, _bwd_strides);
1035#else
1036    desc->set_value(oneapi::mkl::dft::config_param::FWD_STRIDES,
1037                    _fwd_strides.data());
1038    desc->set_value(oneapi::mkl::dft::config_param::BWD_STRIDES,
1039                    _bwd_strides.data());
1040#endif
1041  }
1042
1043  template <class Desc_t> void swap_distance(std::shared_ptr<Desc_t> desc) {
1044    std::swap(_fwd_dist, _bwd_dist);
1045    desc->set_value(oneapi::mkl::dft::config_param::FWD_DISTANCE, _fwd_dist);
1046    desc->set_value(oneapi::mkl::dft::config_param::BWD_DISTANCE, _bwd_dist);
1047  }
1048
1049  template <class Desc_t> void swap_strides(std::shared_ptr<Desc_t> desc) {
1050    std::swap_ranges(_fwd_strides.begin(), _fwd_strides.end(),
1051                     _bwd_strides.begin());
1052#ifdef __INTEL_MKL__
1053    desc->set_value(oneapi::mkl::dft::config_param::FWD_STRIDES, _fwd_strides);
1054    desc->set_value(oneapi::mkl::dft::config_param::BWD_STRIDES, _bwd_strides);
1055#else
1056    desc->set_value(oneapi::mkl::dft::config_param::FWD_STRIDES,
1057                    _fwd_strides.data());
1058    desc->set_value(oneapi::mkl::dft::config_param::BWD_STRIDES,
1059                    _bwd_strides.data());
1060#endif
1061  }
1062
1063  template <bool Is_inplace, class Desc_t>
1064  void set_stride_and_distance_basic(std::shared_ptr<Desc_t> desc) {
1065    std::int64_t forward_distance = 0;
1066    std::int64_t backward_distance = 0;
1067    if (_dim == 1) {
1068      if constexpr (Is_inplace) {
1069        _fwd_strides = {0, 1};
1070        _bwd_strides = {0, 1};
1071        forward_distance = 2 * (_n[0] / 2 + 1);
1072        backward_distance = _n[0] / 2 + 1;
1073      } else {
1074        _fwd_strides = {0, 1};
1075        _bwd_strides = {0, 1};
1076        forward_distance = _n[0];
1077        backward_distance = _n[0] / 2 + 1;
1078      }
1079    } else if (_dim == 2) {
1080      if constexpr (Is_inplace) {
1081        _bwd_strides = {0, _n[1] / 2 + 1, 1};
1082        _fwd_strides = {0, 2 * (_n[1] / 2 + 1), 1};
1083        forward_distance = _n[0] * 2 * (_n[1] / 2 + 1);
1084        backward_distance = _n[0] * (_n[1] / 2 + 1);
1085      } else {
1086        _bwd_strides = {0, _n[1] / 2 + 1, 1};
1087        _fwd_strides = {0, _n[1], 1};
1088        forward_distance = _n[0] * _n[1];
1089        backward_distance = _n[0] * (_n[1] / 2 + 1);
1090      }
1091    } else if (_dim == 3) {
1092      if constexpr (Is_inplace) {
1093        _bwd_strides = {0, _n[1] * (_n[2] / 2 + 1), _n[2] / 2 + 1, 1};
1094        _fwd_strides = {0, _n[1] * 2 * (_n[2] / 2 + 1), 2 * (_n[2] / 2 + 1), 1};
1095        forward_distance = _n[0] * _n[1] * 2 * (_n[2] / 2 + 1);
1096        backward_distance = _n[0] * _n[1] * (_n[2] / 2 + 1);
1097      } else {
1098        _bwd_strides = {0, _n[1] * (_n[2] / 2 + 1), _n[2] / 2 + 1, 1};
1099        _fwd_strides = {0, _n[1] * _n[2], _n[2], 1};
1100        forward_distance = _n[0] * _n[1] * _n[2];
1101        backward_distance = _n[0] * _n[1] * (_n[2] / 2 + 1);
1102      }
1103    }
1104#ifdef __INTEL_MKL__
1105    desc->set_value(oneapi::mkl::dft::config_param::FWD_STRIDES, _fwd_strides);
1106    desc->set_value(oneapi::mkl::dft::config_param::BWD_STRIDES, _bwd_strides);
1107#else
1108    desc->set_value(oneapi::mkl::dft::config_param::FWD_STRIDES,
1109                    _fwd_strides.data());
1110    desc->set_value(oneapi::mkl::dft::config_param::BWD_STRIDES,
1111                    _bwd_strides.data());
1112#endif
1113    desc->set_value(oneapi::mkl::dft::config_param::FWD_DISTANCE,
1114                    forward_distance);
1115    desc->set_value(oneapi::mkl::dft::config_param::BWD_DISTANCE,
1116                    backward_distance);
1117  }
1118
1119  template <class Dest_t, class T>
1120  void compute_impl(Dest_t desc, T *input, T *output,
1121                    std::optional<fft_direction> direction) {
1122    bool is_this_compute_inplace = input == output;
1123    constexpr bool is_complex =
1124        std::is_same_v<Dest_t, std::shared_ptr<oneapi::mkl::dft::descriptor<
1125                                   oneapi::mkl::dft::precision::SINGLE,
1126                                   oneapi::mkl::dft::domain::COMPLEX>>> ||
1127        std::is_same_v<Dest_t, std::shared_ptr<oneapi::mkl::dft::descriptor<
1128                                   oneapi::mkl::dft::precision::DOUBLE,
1129                                   oneapi::mkl::dft::domain::COMPLEX>>>;
1130
1131    if (!_is_user_specified_dir_and_placement) {
1132      // The descriptor need different config values if the FFT direction
1133      // or placement is different.
1134      // Here we check the conditions, and new config values are set and
1135      // re-committed if needed.
1136      bool need_commit = false;
1137      if constexpr (is_complex) {
1138        if (direction.value() != _direction) {
1139          need_commit = true;
1140          swap_distance(desc);
1141          if (!_is_basic)
1142            swap_strides(desc);
1143          _direction = direction.value();
1144        }
1145      }
1146      if (is_this_compute_inplace != _is_inplace) {
1147        need_commit = true;
1148        _is_inplace = is_this_compute_inplace;
1149        if (_is_inplace) {
1150          desc->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
1151                          oneapi::mkl::dft::config_value::INPLACE);
1152          if constexpr (!is_complex)
1153            if (_is_basic)
1154              set_stride_and_distance_basic<true>(desc);
1155        } else {
1156          desc->set_value(oneapi::mkl::dft::config_param::PLACEMENT,
1157                          oneapi::mkl::dft::config_value::NOT_INPLACE);
1158          if constexpr (!is_complex)
1159            if (_is_basic)
1160              set_stride_and_distance_basic<false>(desc);
1161        }
1162      }
1163      if (need_commit)
1164        desc->commit(*_q);
1165    }
1166
1167    if (_is_inplace) {
1168      auto data_input = dpct::detail::get_memory<T>(input);
1169      if (_direction == fft_direction::forward) {
1170        oneapi::mkl::dft::compute_forward<typename Dest_t::element_type, T>(
1171            *desc, data_input);
1172      } else {
1173        oneapi::mkl::dft::compute_backward<typename Dest_t::element_type, T>(
1174            *desc, data_input);
1175      }
1176    } else {
1177      auto data_input = dpct::detail::get_memory<T>(input);
1178      auto data_output = dpct::detail::get_memory<T>(output);
1179      if (_direction == fft_direction::forward) {
1180        oneapi::mkl::dft::compute_forward<typename Dest_t::element_type, T, T>(
1181            *desc, data_input, data_output);
1182      } else {
1183        oneapi::mkl::dft::compute_backward<typename Dest_t::element_type, T, T>(
1184            *desc, data_input, data_output);
1185      }
1186    }
1187  }
1188
1189  template <class T, oneapi::mkl::dft::precision Precision>
1190  void compute_complex(T *input, T *output, fft_direction direction) {
1191    if constexpr (Precision == oneapi::mkl::dft::precision::SINGLE) {
1192      compute_impl(_desc_sc, input, output, direction);
1193    } else {
1194      compute_impl(_desc_dc, input, output, direction);
1195    }
1196  }
1197
1198  template <class T, oneapi::mkl::dft::precision Precision>
1199  void compute_real(T *input, T *output) {
1200    if constexpr (Precision == oneapi::mkl::dft::precision::SINGLE) {
1201      compute_impl(_desc_sr, input, output, std::nullopt);
1202    } else {
1203      compute_impl(_desc_dr, input, output, std::nullopt);
1204    }
1205  }
1206
1207private:
1208  sycl::queue *_q = nullptr;
1209  int _dim;
1210  std::vector<std::int64_t> _n;
1211  std::vector<std::int64_t> _inembed;
1212  std::int64_t _istride;
1213  std::int64_t _fwd_dist;
1214  library_data_t _input_type;
1215  std::vector<std::int64_t> _onembed;
1216  std::int64_t _ostride;
1217  std::int64_t _bwd_dist;
1218  library_data_t _output_type;
1219  std::int64_t _batch = 1;
1220  bool _is_basic = false;
1221  bool _is_inplace = false;
1222  fft_direction _direction = fft_direction::forward;
1223  bool _is_user_specified_dir_and_placement = false;
1224  bool _use_external_workspace = false;
1225  void *_external_workspace_ptr = nullptr;
1226  std::int64_t _workspace_bytes = 0;
1227  bool _is_estimate_call = false;
1228  std::int64_t _workspace_estimate_bytes = 0;
1229  std::shared_ptr<oneapi::mkl::dft::descriptor<
1230      oneapi::mkl::dft::precision::SINGLE, oneapi::mkl::dft::domain::REAL>>
1231      _desc_sr;
1232  std::shared_ptr<oneapi::mkl::dft::descriptor<
1233      oneapi::mkl::dft::precision::DOUBLE, oneapi::mkl::dft::domain::REAL>>
1234      _desc_dr;
1235  std::shared_ptr<oneapi::mkl::dft::descriptor<
1236      oneapi::mkl::dft::precision::SINGLE, oneapi::mkl::dft::domain::COMPLEX>>
1237      _desc_sc;
1238  std::shared_ptr<oneapi::mkl::dft::descriptor<
1239      oneapi::mkl::dft::precision::DOUBLE, oneapi::mkl::dft::domain::COMPLEX>>
1240      _desc_dc;
1241  std::vector<std::int64_t> _fwd_strides;
1242  std::vector<std::int64_t> _bwd_strides;
1243};
1244
1245using fft_engine_ptr = fft_engine *;
1246} // namespace fft
1247} // namespace dpct
1248
1249#endif // __DPCT_FFT_UTILS_HPP__