13#include <KokkosFFT.hpp>
14#include <Kokkos_Core.hpp>
19
20
21
22
23template <
typename Dim>
27
28
29
30
37
38
39
40
47
48
49
50
55namespace ddc::detail::fft {
64struct real_type<Kokkos::complex<T>>
70using real_type_t =
typename real_type<T>::type;
74struct is_complex : std::false_type
79struct is_complex<Kokkos::complex<T>> : std::true_type
84constexpr bool is_complex_v = is_complex<T>::value;
87
88
89
90
98template <
typename... DDimX>
99KokkosFFT::axis_type<
sizeof...(DDimX)> axes()
101 return KokkosFFT::axis_type<
sizeof...(DDimX)> {
102 static_cast<
int>(
ddc::type_seq_rank_v<DDimX,
ddc::detail::TypeSeq<DDimX...>>)...};
105inline KokkosFFT::Normalization ddc_fft_normalization_to_kokkos_fft(
110 return KokkosFFT::Normalization::none;
114 return KokkosFFT::Normalization::forward;
118 return KokkosFFT::Normalization::backward;
122 return KokkosFFT::Normalization::ortho;
125 throw std::runtime_error(
"ddc::FFT_Normalization not handled");
130 typename ElementType,
133 typename MemorySpace,
136 ExecSpace
const& exec_space,
137 ddc::
ChunkSpan<ElementType, DDom, Layout, MemorySpace>
const& chunk_span,
140 ddc::parallel_for_each(
141 "ddc_fft_normalization",
144 KOKKOS_LAMBDA(
typename DDom::discrete_element_type
const i) {
145 chunk_span(i) *= value;
150Real forward_full_norm_coef(
DiscreteDomain<DDim>
const& ddom)
noexcept
152 return rlength(ddom) / Kokkos::sqrt(2 * Kokkos::numbers::pi_v<Real>)
153 / (ddom.extents() - 1).value();
157Real backward_full_norm_coef(
DiscreteDomain<DDim>
const& ddom)
noexcept
159 return 1 / (forward_full_norm_coef(ddom) * ddom.extents().value());
167 typename MemorySpace,
173 ExecSpace
const& exec_space,
176 kwArgs_impl
const& kwargs)
179 std::is_same_v<real_type_t<Tin>,
float> || std::is_same_v<real_type_t<Tin>,
double>,
180 "Base type of Tin (and Tout) must be float or double.");
182 std::is_same_v<real_type_t<Tin>, real_type_t<Tout>>,
183 "Types Tin and Tout must be based on same type (float or double)");
185 Kokkos::SpaceAccessibility<ExecSpace, MemorySpace>::accessible,
186 "MemorySpace has to be accessible for ExecutionSpace.");
189 ddc::detail::mdspan_to_kokkos_element_t<Tin,
sizeof...(DDimIn)>,
190 ddc::detail::mdspan_to_kokkos_layout_t<LayoutIn>,
191 MemorySpace>
const in_view
192 = in.allocation_kokkos_view();
194 ddc::detail::mdspan_to_kokkos_element_t<Tout,
sizeof...(DDimIn)>,
195 ddc::detail::mdspan_to_kokkos_layout_t<LayoutOut>,
196 MemorySpace>
const out_view
197 = out.allocation_kokkos_view();
198 KokkosFFT::Normalization
const kokkos_fft_normalization
199 = ddc_fft_normalization_to_kokkos_fft(kwargs.normalization);
202 if constexpr (std::is_same_v<Tin, Tout>) {
209 kokkos_fft_normalization);
216 kokkos_fft_normalization);
220 if constexpr (is_complex_v<Tout>) {
221 assert(kwargs.direction == ddc::FFT_Direction::FORWARD);
227 kokkos_fft_normalization);
229 assert(kwargs.direction == ddc::FFT_Direction::BACKWARD);
235 kokkos_fft_normalization);
244 norm_coef = (forward_full_norm_coef(DiscreteDomain<DDimIn>(ddom_in)) * ...);
247 norm_coef = (backward_full_norm_coef(DiscreteDomain<DDimOut>(ddom_out)) * ...);
250 rescale(exec_space, out,
static_cast<real_type_t<Tout>>(norm_coef));
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277template <
typename DDimFx,
typename DDimX>
282 is_uniform_point_sampling_v<DDimX>,
283 "DDimX dimension must derive from UniformPointSampling");
285 is_periodic_sampling_v<DDimFx>,
286 "DDimFx dimension must derive from PeriodicSampling");
287 using CDimFx =
typename DDimFx::continuous_dimension_type;
288 using CDimX =
typename DDimX::continuous_dimension_type;
290 std::is_same_v<CDimFx,
ddc::Fourier<CDimX>>,
291 "DDimX and DDimFx dimensions must be defined over the same continuous dimension");
293 DiscreteVectorElement
const nx = get<DDimX>(x_mesh.extents());
294 double const lx =
ddc::rlength(x_mesh);
295 auto [impl, ddom] = DDimFx::
template init<DDimFx>(
296 ddc::Coordinate<CDimFx>(0),
297 ddc::Coordinate<CDimFx>(2 * (nx - 1) * (nx - 1) / (nx * lx) * Kokkos::numbers::pi),
300 return std::move(impl);
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318template <
typename... DDimFx,
typename... DDimX>
322 (is_uniform_point_sampling_v<DDimX> && ...),
323 "DDimX dimensions should derive from UniformPointSampling");
325 (is_periodic_sampling_v<DDimFx> && ...),
326 "DDimFx dimensions should derive from PeriodicPointSampling");
329 detail::array(extents).back() = detail::array(extents).back() / 2 + 1;
332 ddc::DiscreteElement<DDimFx>(0),
337
338
339
340
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
374 typename MemorySpace,
378 ExecSpace
const& exec_space,
384 std::is_same_v<LayoutIn, Kokkos::layout_right>
385 && std::is_same_v<LayoutOut, Kokkos::layout_right>,
386 "Layouts must be right-handed");
388 (is_uniform_point_sampling_v<DDimX> && ...),
389 "DDimX dimensions should derive from UniformPointSampling");
391 (is_periodic_sampling_v<DDimFx> && ...),
392 "DDimFx dimensions should derive from PeriodicPointSampling");
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
427 typename MemorySpace,
431 ExecSpace
const& exec_space,
437 std::is_same_v<LayoutIn, Kokkos::layout_right>
438 && std::is_same_v<LayoutOut, Kokkos::layout_right>,
439 "Layouts must be right-handed");
441 (is_uniform_point_sampling_v<DDimX> && ...),
442 "DDimX dimensions should derive from UniformPointSampling");
444 (is_periodic_sampling_v<DDimFx> && ...),
445 "DDimFx dimensions should derive from PeriodicPointSampling");
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
The top-level namespace of DDC.
ddc::FFT_Normalization normalization
Enum member to identify the type of normalization performed.
void ifft(ExecSpace const &exec_space, ddc::ChunkSpan< Tout, ddc::DiscreteDomain< DDimX... >, LayoutOut, MemorySpace > out, ddc::ChunkSpan< Tin, ddc::DiscreteDomain< DDimFx... >, LayoutIn, MemorySpace > in, ddc::kwArgs_fft kwargs={ddc::FFT_Normalization::OFF})
Perform an inverse Fast Fourier Transform.
void fft(ExecSpace const &exec_space, ddc::ChunkSpan< Tout, ddc::DiscreteDomain< DDimFx... >, LayoutOut, MemorySpace > out, ddc::ChunkSpan< Tin, ddc::DiscreteDomain< DDimX... >, LayoutIn, MemorySpace > in, ddc::kwArgs_fft kwargs={ddc::FFT_Normalization::OFF})
Perform a direct Fast Fourier Transform.
FFT_Normalization
A named argument to choose the type of normalization of the FFT.
@ BACKWARD
No normalization for forward FFT, multiply by 1/N for backward FFT.
@ OFF
No normalization. Un-normalized FFT is sum_j f(x_j)*e^-ikx_j.
@ ORTHO
Multiply by 1/sqrt(N)
@ FULL
Multiply by dx/sqrt(2*pi) for forward FFT and dk/sqrt(2*pi) for backward FFT.
@ FORWARD
Multiply by 1/N for forward FFT, no normalization for backward FFT.
ddc::DiscreteDomain< DDimFx... > fourier_mesh(ddc::DiscreteDomain< DDimX... > x_mesh, bool C2C)
Get the Fourier mesh.
DDimFx::template Impl< DDimFx, Kokkos::HostSpace > init_fourier_space(ddc::DiscreteDomain< DDimX > x_mesh)
Initialize a Fourier discrete dimension.
FFT_Direction
A named argument to choose the direction of the FFT.
@ BACKWARD
Backward, corresponds to inverse FFT up to normalization.
@ FORWARD
Forward, corresponds to direct FFT up to normalization.
A structure embedding the configuration of the exposed FFT function with the type of normalization.