okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
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 { 24real_float_to_complex_float = 0 , 25complex_float_to_real_float , 26real_double_to_complex_double , 27complex_double_to_real_double , 28complex_float_to_complex_float , 29complex_double_to_complex_double , 30}; 31 32/// A class to perform FFT calculation. 33class fft_engine { 34public : 35/// Default constructor. 36fft_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. 60void commit (sycl::queue * exec_queue ,int dim ,long long * n , 61long long * inembed ,long long istride ,long long idist , 62library_data_t input_type ,long long * onembed ,long long ostride , 63long long odist ,library_data_t output_type ,long long batch , 64size_t * scratchpad_size , 65 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 66direction_and_placement = std::nullopt ) { 67_q = exec_queue ; 68init < long long > (dim ,n ,inembed ,istride ,idist ,input_type ,onembed , 69ostride ,odist ,output_type ,batch , 70direction_and_placement ); 71if (scratchpad_size ) { 72if (_is_estimate_call ) 73* scratchpad_size = _workspace_estimate_bytes ; 74else 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. 101void commit (sycl::queue * exec_queue ,int dim ,int * n ,int * inembed , 102int istride ,int idist ,library_data_t input_type ,int * onembed , 103int ostride ,int odist ,library_data_t output_type ,int batch , 104size_t * scratchpad_size , 105 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 106direction_and_placement = std::nullopt ) { 107_q = exec_queue ; 108init < int > (dim ,n ,inembed ,istride ,idist ,input_type ,onembed ,ostride , 109odist ,output_type ,batch ,direction_and_placement ); 110if (scratchpad_size ) { 111if (_is_estimate_call ) 112* scratchpad_size = _workspace_estimate_bytes ; 113else 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. 139void commit (sycl::queue * exec_queue ,int dim ,long long * n , 140long long * inembed ,long long istride ,long long idist , 141long long * onembed ,long long ostride ,long long odist , 142fft_type type ,long long batch ,size_t * scratchpad_size , 143 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 144direction_and_placement = std::nullopt ) { 145commit (exec_queue ,dim ,n ,inembed ,istride ,idist , 146fft_type_to_data_type (type ).first ,onembed ,ostride ,odist , 147fft_type_to_data_type (type ).second ,batch ,scratchpad_size , 148direction_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. 172void commit (sycl::queue * exec_queue ,int dim ,int * n ,int * inembed , 173int istride ,int idist ,int * onembed ,int ostride ,int odist , 174fft_type type ,int batch ,size_t * scratchpad_size , 175 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 176direction_and_placement = std::nullopt ) { 177commit (exec_queue ,dim ,n ,inembed ,istride ,idist , 178fft_type_to_data_type (type ).first ,onembed ,ostride ,odist , 179fft_type_to_data_type (type ).second ,batch ,scratchpad_size , 180direction_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. 195void commit (sycl::queue * exec_queue ,int n1 ,fft_type type ,int batch , 196size_t * scratchpad_size , 197 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 198direction_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; 206if (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 } 211config_and_commit_basic (); 212if (scratchpad_size ) { 213if (_is_estimate_call ) 214* scratchpad_size = _workspace_estimate_bytes ; 215else 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. 232void commit (sycl::queue * exec_queue ,int n2 ,int n1 ,fft_type type , 233size_t * scratchpad_size , 234 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 235direction_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; 243if (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 } 248config_and_commit_basic (); 249if (scratchpad_size ) { 250if (_is_estimate_call ) 251* scratchpad_size = _workspace_estimate_bytes ; 252else 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. 270void commit (sycl::queue * exec_queue ,int n3 ,int n2 ,int n1 ,fft_type type , 271size_t * scratchpad_size , 272 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 273direction_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; 282if (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 } 287config_and_commit_basic (); 288if (scratchpad_size ) { 289if (_is_estimate_call ) 290* scratchpad_size = _workspace_estimate_bytes ; 291else 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. 306static fft_engine * 307create (sycl::queue * exec_queue ,int n1 ,fft_type type ,int batch , 308 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 309direction_and_placement = std::nullopt ) { 310fft_engine * engine = new fft_engine (); 311engine -> commit (exec_queue ,n1 ,type ,batch ,nullptr , 312direction_and_placement ); 313return 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. 325static fft_engine * 326create (sycl::queue * exec_queue ,int n2 ,int n1 ,fft_type type , 327 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 328direction_and_placement = std::nullopt ) { 329fft_engine * engine = new fft_engine (); 330engine -> commit (exec_queue ,n2 ,n1 ,type ,nullptr ,direction_and_placement ); 331return 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. 344static fft_engine * 345create (sycl::queue * exec_queue ,int n3 ,int n2 ,int n1 ,fft_type type , 346 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 347direction_and_placement = std::nullopt ) { 348fft_engine * engine = new fft_engine (); 349engine -> commit (exec_queue ,n3 ,n2 ,n1 ,type ,nullptr , 350direction_and_placement ); 351return 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. 372static fft_engine * 373create (sycl::queue * exec_queue ,int dim ,int * n ,int * inembed ,int istride , 374int idist ,int * onembed ,int ostride ,int odist ,fft_type type , 375int batch , 376 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 377direction_and_placement = std::nullopt ) { 378fft_engine * engine = new fft_engine (); 379engine -> commit (exec_queue ,dim ,n ,inembed ,istride ,idist ,onembed , 380ostride ,odist ,type ,batch ,nullptr , 381direction_and_placement ); 382return engine ; 383 } 384/// Create the class for calculate FFT without commit any config. 385static fft_engine * create () { 386fft_engine * engine = new fft_engine (); 387return engine ; 388 } 389/// Destroy the class for calculate FFT. 390/// \param [in] engine Pointer returned from fft_engine::craete. 391static 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. 414static void 415estimate_size (int dim ,long long * n ,long long * inembed ,long long istride , 416long long idist ,long long * onembed ,long long ostride , 417long long odist ,fft_type type ,long long batch , 418size_t * estimated_scratchpad_size , 419 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 420direction_and_placement = std::nullopt ) { 421fft_engine * engine = fft_engine::create (); 422engine -> _is_estimate_call = true; 423engine -> commit (& ::dpct::cs::get_default_queue (),dim ,n ,inembed ,istride , 424idist ,fft_type_to_data_type (type ).first ,onembed ,ostride , 425odist ,fft_type_to_data_type (type ).second ,batch , 426estimated_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. 449static void 450estimate_size (int dim ,int * n ,int * inembed ,int istride ,int idist , 451int * onembed ,int ostride ,int odist ,fft_type type ,int batch , 452size_t * estimated_scratchpad_size , 453 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 454direction_and_placement = std::nullopt ) { 455fft_engine * engine = fft_engine::create (); 456engine -> _is_estimate_call = true; 457engine -> commit (& ::dpct::cs::get_default_queue (),dim ,n ,inembed ,istride , 458idist ,fft_type_to_data_type (type ).first ,onembed ,ostride , 459odist ,fft_type_to_data_type (type ).second ,batch , 460estimated_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. 474static void 475estimate_size (int n1 ,fft_type type ,int batch , 476size_t * estimated_scratchpad_size , 477 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 478direction_and_placement = std::nullopt ) { 479fft_engine * engine = fft_engine::create (); 480engine -> _is_estimate_call = true; 481engine -> commit (& ::dpct::cs::get_default_queue (),n1 ,type ,batch , 482estimated_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. 496static void 497estimate_size (int n2 ,int n1 ,fft_type type , 498size_t * estimated_scratchpad_size , 499 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 500direction_and_placement = std::nullopt ) { 501fft_engine * engine = fft_engine::create (); 502engine -> _is_estimate_call = true; 503engine -> commit (& ::dpct::cs::get_default_queue (),n2 ,n1 ,type , 504estimated_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. 519static void 520estimate_size (int n3 ,int n2 ,int n1 ,fft_type type , 521size_t * estimated_scratchpad_size , 522 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 523direction_and_placement = std::nullopt ) { 524fft_engine * engine = fft_engine::create (); 525engine -> _is_estimate_call = true; 526engine -> commit (& ::dpct::cs::get_default_queue (),n3 ,n2 ,n1 ,type , 527estimated_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. 536template < typename input_t ,typename output_t > 537void compute (input_t * input ,output_t * output ,fft_direction direction ) { 538if (_input_type == library_data_t::complex_float && 539_output_type == library_data_t::complex_float ) { 540compute_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 ) { 544compute_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 ; 549compute_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 ; 554compute_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 ; 559compute_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 ; 564compute_real < double , oneapi::mkl::dft::precision::DOUBLE > ( 565 (double * )input , (double * )output ); 566 } 567 } 568template <> 569void compute (float * input , sycl::float2 * output ,fft_direction direction ) { 570_direction = direction ; 571compute_real < float , oneapi::mkl::dft::precision::SINGLE > ((float * )input , 572 (float * )output ); 573 } 574template <> 575void compute (sycl::float2 * input ,float * output ,fft_direction direction ) { 576_direction = direction ; 577compute_real < float , oneapi::mkl::dft::precision::SINGLE > ((float * )input , 578 (float * )output ); 579 } 580template <> 581void compute (double * input , sycl::double2 * output ,fft_direction direction ) { 582_direction = direction ; 583compute_real < double , oneapi::mkl::dft::precision::DOUBLE > ((double * )input , 584 (double * )output ); 585 } 586template <> 587void compute (sycl::double2 * input ,double * output ,fft_direction direction ) { 588_direction = direction ; 589compute_real < double , oneapi::mkl::dft::precision::DOUBLE > ((double * )input , 590 (double * )output ); 591 } 592template <> 593void compute (sycl::float2 * input , sycl::float2 * output , 594fft_direction direction ) { 595compute_complex < float , oneapi::mkl::dft::precision::SINGLE > ( 596 (float * )input , (float * )output ,direction ); 597 } 598template <> 599void compute (sycl::double2 * input , sycl::double2 * output , 600fft_direction direction ) { 601compute_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. 606void 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. 611void 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. 616void set_workspace (void * ptr ) { 617if (!_use_external_workspace ) { 618return ; 619 } 620if (_input_type == library_data_t::complex_float && 621_output_type == library_data_t::complex_float ) { 622if (_q -> get_device ().is_gpu ()) { 623auto 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 ) { 628if (_q -> get_device ().is_gpu ()) { 629auto 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 )) { 636if (_q -> get_device ().is_gpu ()) { 637auto 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 )) { 644if (_q -> get_device ().is_gpu ()) { 645auto data = dpct::detail::get_memory < double > (ptr ); 646_desc_dr -> set_workspace (data ); 647 } 648 }else { 649throw 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. 656void get_workspace_size (size_t * scratchpad_size ) { 657if (scratchpad_size ) { 658* scratchpad_size = _workspace_bytes ; 659 } 660 } 661 662private : 663static std::pair < library_data_t ,library_data_t > 664fft_type_to_data_type (fft_type type ) { 665switch (type ) { 666case fft_type::real_float_to_complex_float : { 667return std::make_pair (library_data_t::real_float , 668 library_data_t::complex_float ); 669 } 670case fft_type::complex_float_to_real_float : { 671return std::make_pair (library_data_t::complex_float , 672 library_data_t::real_float ); 673 } 674case fft_type::real_double_to_complex_double : { 675return std::make_pair (library_data_t::real_double , 676 library_data_t::complex_double ); 677 } 678case fft_type::complex_double_to_real_double : { 679return std::make_pair (library_data_t::complex_double , 680 library_data_t::real_double ); 681 } 682case fft_type::complex_float_to_complex_float : { 683return std::make_pair (library_data_t::complex_float , 684 library_data_t::complex_float ); 685 } 686case fft_type::complex_double_to_complex_double : { 687return std::make_pair (library_data_t::complex_double , 688 library_data_t::complex_double ); 689 } 690 } 691 } 692 693void config_and_commit_basic () { 694if (_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 ; 700for (auto i :_n ) 701distance = distance * i ; 702_fwd_dist = distance ; 703_bwd_dist = distance ; 704_desc_sc -> set_value (oneapi::mkl::dft::config_param::FWD_DISTANCE , 705distance ); 706_desc_sc -> set_value (oneapi::mkl::dft::config_param::BWD_DISTANCE , 707distance ); 708_desc_sc -> set_value (oneapi::mkl::dft::config_param::NUMBER_OF_TRANSFORMS , 709_batch ); 710if (_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 ); 713else 714_desc_sc -> set_value (oneapi::mkl::dft::config_param::PLACEMENT , 715 oneapi::mkl::dft::config_value::NOT_INPLACE ); 716#ifdef __INTEL_MKL__ 717if (_use_external_workspace ) { 718if (_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 } 724if (_is_estimate_call ) { 725if (_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 ); 732if (_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 ; 746for (auto i :_n ) 747distance = distance * i ; 748_fwd_dist = distance ; 749_bwd_dist = distance ; 750_desc_dc -> set_value (oneapi::mkl::dft::config_param::FWD_DISTANCE , 751distance ); 752_desc_dc -> set_value (oneapi::mkl::dft::config_param::BWD_DISTANCE , 753distance ); 754_desc_dc -> set_value (oneapi::mkl::dft::config_param::NUMBER_OF_TRANSFORMS , 755_batch ); 756if (_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 ); 759else 760_desc_dc -> set_value (oneapi::mkl::dft::config_param::PLACEMENT , 761 oneapi::mkl::dft::config_value::NOT_INPLACE ); 762#ifdef __INTEL_MKL__ 763if (_use_external_workspace ) { 764if (_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 } 770if (_is_estimate_call ) { 771if (_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 ); 778if (_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 ); 793if (_input_type == library_data_t::real_float && 794_output_type == library_data_t::complex_float ) 795_direction = fft_direction::forward ; 796else 797_direction = fft_direction::backward ; 798_desc_sr -> set_value (oneapi::mkl::dft::config_param::NUMBER_OF_TRANSFORMS , 799_batch ); 800if (_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 ); 803set_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 ); 807set_stride_and_distance_basic < false> (_desc_sr ); 808 } 809#ifdef __INTEL_MKL__ 810if (_use_external_workspace ) { 811if (_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 } 817if (_is_estimate_call ) { 818if (_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 ); 825if (_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 ); 840if (_input_type == library_data_t::real_double && 841_output_type == library_data_t::complex_double ) 842_direction = fft_direction::forward ; 843else 844_direction = fft_direction::backward ; 845_desc_dr -> set_value (oneapi::mkl::dft::config_param::NUMBER_OF_TRANSFORMS , 846_batch ); 847if (_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 ); 850set_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 ); 854set_stride_and_distance_basic < false> (_desc_dr ); 855 } 856#ifdef __INTEL_MKL__ 857if (_use_external_workspace ) { 858if (_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 } 864if (_is_estimate_call ) { 865if (_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 ); 872if (_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 { 881throw sycl::exception (sycl::make_error_code (sycl::errc::invalid ), 882"invalid fft type" ); 883 } 884 } 885 886void 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 928if (_input_type == library_data_t::complex_float && 929_output_type == library_data_t::complex_float ) { 930CONFIG_AND_COMMIT (_desc_sc ,SINGLE ,COMPLEX ); 931 }else if (_input_type == library_data_t::complex_double && 932_output_type == library_data_t::complex_double ) { 933CONFIG_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 )) { 938CONFIG_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 )) { 943CONFIG_AND_COMMIT (_desc_dr ,DOUBLE ,REAL ); 944 }else { 945throw 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 952template < typename T > 953void init (int dim ,T * n ,T * inembed ,T istride ,T idist , 954library_data_t input_type ,T * onembed ,T ostride ,T odist , 955library_data_t output_type ,T batch , 956 std::optional < std::pair < fft_direction ,bool /*is_inplace*/ >> 957direction_and_placement ) { 958if (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 ; 968for (int i = 0 ;i < dim ;i ++ ) { 969_n [i ]= n [i ]; 970 } 971if (inembed && onembed ) { 972for (int i = 0 ;i < dim ;i ++ ) { 973_inembed [i ]= inembed [i ]; 974_onembed [i ]= onembed [i ]; 975 } 976_istride = istride ; 977_ostride = ostride ; 978 979if ((_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 { 994if (_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 1009if (_is_basic ) 1010config_and_commit_basic (); 1011else 1012config_and_commit_advanced (); 1013 } 1014template < class Desc_t > 1015void set_stride_advanced (std::shared_ptr < Desc_t > desc ) { 1016if (_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 } 1028if (_direction == fft_direction::backward ) { 1029 std::swap_ranges (_fwd_strides .begin (),_fwd_strides .end (), 1030_bwd_strides .begin ()); 1031 } 1032#ifdef __INTEL_MKL__ 1033desc -> set_value (oneapi::mkl::dft::config_param::FWD_STRIDES ,_fwd_strides ); 1034desc -> set_value (oneapi::mkl::dft::config_param::BWD_STRIDES ,_bwd_strides ); 1035#else 1036desc -> set_value (oneapi::mkl::dft::config_param::FWD_STRIDES , 1037_fwd_strides .data ()); 1038desc -> set_value (oneapi::mkl::dft::config_param::BWD_STRIDES , 1039_bwd_strides .data ()); 1040#endif 1041 } 1042 1043template < class Desc_t > void swap_distance (std::shared_ptr < Desc_t > desc ) { 1044 std::swap (_fwd_dist ,_bwd_dist ); 1045desc -> set_value (oneapi::mkl::dft::config_param::FWD_DISTANCE ,_fwd_dist ); 1046desc -> set_value (oneapi::mkl::dft::config_param::BWD_DISTANCE ,_bwd_dist ); 1047 } 1048 1049template < 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__ 1053desc -> set_value (oneapi::mkl::dft::config_param::FWD_STRIDES ,_fwd_strides ); 1054desc -> set_value (oneapi::mkl::dft::config_param::BWD_STRIDES ,_bwd_strides ); 1055#else 1056desc -> set_value (oneapi::mkl::dft::config_param::FWD_STRIDES , 1057_fwd_strides .data ()); 1058desc -> set_value (oneapi::mkl::dft::config_param::BWD_STRIDES , 1059_bwd_strides .data ()); 1060#endif 1061 } 1062 1063template < bool Is_inplace ,class Desc_t > 1064void 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 ; 1067if (_dim == 1 ) { 1068if constexpr (Is_inplace ) { 1069_fwd_strides = {0 ,1 }; 1070_bwd_strides = {0 ,1 }; 1071forward_distance = 2 * (_n [0 ] /2 + 1 ); 1072backward_distance = _n [0 ] /2 + 1 ; 1073 }else { 1074_fwd_strides = {0 ,1 }; 1075_bwd_strides = {0 ,1 }; 1076forward_distance = _n [0 ]; 1077backward_distance = _n [0 ] /2 + 1 ; 1078 } 1079 }else if (_dim == 2 ) { 1080if constexpr (Is_inplace ) { 1081_bwd_strides = {0 ,_n [1 ] /2 + 1 ,1 }; 1082_fwd_strides = {0 ,2 * (_n [1 ] /2 + 1 ),1 }; 1083forward_distance = _n [0 ]* 2 * (_n [1 ] /2 + 1 ); 1084backward_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 }; 1088forward_distance = _n [0 ]* _n [1 ]; 1089backward_distance = _n [0 ]* (_n [1 ] /2 + 1 ); 1090 } 1091 }else if (_dim == 3 ) { 1092if 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 }; 1095forward_distance = _n [0 ]* _n [1 ]* 2 * (_n [2 ] /2 + 1 ); 1096backward_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 }; 1100forward_distance = _n [0 ]* _n [1 ]* _n [2 ]; 1101backward_distance = _n [0 ]* _n [1 ]* (_n [2 ] /2 + 1 ); 1102 } 1103 } 1104#ifdef __INTEL_MKL__ 1105desc -> set_value (oneapi::mkl::dft::config_param::FWD_STRIDES ,_fwd_strides ); 1106desc -> set_value (oneapi::mkl::dft::config_param::BWD_STRIDES ,_bwd_strides ); 1107#else 1108desc -> set_value (oneapi::mkl::dft::config_param::FWD_STRIDES , 1109_fwd_strides .data ()); 1110desc -> set_value (oneapi::mkl::dft::config_param::BWD_STRIDES , 1111_bwd_strides .data ()); 1112#endif 1113desc -> set_value (oneapi::mkl::dft::config_param::FWD_DISTANCE , 1114forward_distance ); 1115desc -> set_value (oneapi::mkl::dft::config_param::BWD_DISTANCE , 1116backward_distance ); 1117 } 1118 1119template < class Dest_t ,class T > 1120void compute_impl (Dest_t desc ,T * input ,T * output , 1121 std::optional < fft_direction > direction ) { 1122bool is_this_compute_inplace = input == output ; 1123constexpr 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 1131if (!_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. 1136bool need_commit = false; 1137if constexpr (is_complex ) { 1138if (direction .value ()!= _direction ) { 1139need_commit = true; 1140swap_distance (desc ); 1141if (!_is_basic ) 1142swap_strides (desc ); 1143_direction = direction .value (); 1144 } 1145 } 1146if (is_this_compute_inplace != _is_inplace ) { 1147need_commit = true; 1148_is_inplace = is_this_compute_inplace ; 1149if (_is_inplace ) { 1150desc -> set_value (oneapi::mkl::dft::config_param::PLACEMENT , 1151 oneapi::mkl::dft::config_value::INPLACE ); 1152if constexpr (!is_complex ) 1153if (_is_basic ) 1154set_stride_and_distance_basic < true> (desc ); 1155 }else { 1156desc -> set_value (oneapi::mkl::dft::config_param::PLACEMENT , 1157 oneapi::mkl::dft::config_value::NOT_INPLACE ); 1158if constexpr (!is_complex ) 1159if (_is_basic ) 1160set_stride_and_distance_basic < false> (desc ); 1161 } 1162 } 1163if (need_commit ) 1164desc -> commit (* _q ); 1165 } 1166 1167if (_is_inplace ) { 1168auto data_input = dpct::detail::get_memory < T > (input ); 1169if (_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 { 1177auto data_input = dpct::detail::get_memory < T > (input ); 1178auto data_output = dpct::detail::get_memory < T > (output ); 1179if (_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 1189template < class T , oneapi::mkl::dft::precision Precision > 1190void compute_complex (T * input ,T * output ,fft_direction direction ) { 1191if constexpr (Precision == oneapi::mkl::dft::precision::SINGLE ) { 1192compute_impl (_desc_sc ,input ,output ,direction ); 1193 }else { 1194compute_impl (_desc_dc ,input ,output ,direction ); 1195 } 1196 } 1197 1198template < class T , oneapi::mkl::dft::precision Precision > 1199void compute_real (T * input ,T * output ) { 1200if constexpr (Precision == oneapi::mkl::dft::precision::SINGLE ) { 1201compute_impl (_desc_sr ,input ,output , std::nullopt ); 1202 }else { 1203compute_impl (_desc_dr ,input ,output , std::nullopt ); 1204 } 1205 } 1206 1207private : 1208 sycl::queue * _q = nullptr ; 1209int _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 ; 1214library_data_t _input_type ; 1215 std::vector < std::int64_t > _onembed ; 1216 std::int64_t _ostride ; 1217 std::int64_t _bwd_dist ; 1218library_data_t _output_type ; 1219 std::int64_t _batch = 1 ; 1220bool _is_basic = false; 1221bool _is_inplace = false; 1222fft_direction _direction = fft_direction::forward ; 1223bool _is_user_specified_dir_and_placement = false; 1224bool _use_external_workspace = false; 1225void * _external_workspace_ptr = nullptr ; 1226 std::int64_t _workspace_bytes = 0 ; 1227bool _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__