Skip to content

Commit

Permalink
Merge pull request #54 from CExA-project/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
yasahi-hpc authored Feb 17, 2024
2 parents 66c3000 + 62abc35 commit 1020adc
Show file tree
Hide file tree
Showing 17 changed files with 518 additions and 510 deletions.
24 changes: 24 additions & 0 deletions common/src/KokkosFFT_common_types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef KOKKOSFFT_COMMON_TYPES_HPP
#define KOKKOSFFT_COMMON_TYPES_HPP

namespace KokkosFFT {
// Define type to specify transform axis
template <std::size_t DIM>
using axis_type = std::array<int, DIM>;

// Define type to specify new shape
template <std::size_t DIM>
using shape_type = std::array<std::size_t, DIM>;

// Tag to specify when and how to normalize
enum class Normalization { forward, backward, ortho, none };

// Tag to specify FFT direction
enum class Direction {
forward,
backward,
};

} // namespace KokkosFFT

#endif
2 changes: 1 addition & 1 deletion common/src/KokkosFFT_layouts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <tuple>
#include <iostream>
#include <numeric>
#include "KokkosFFT_default_types.hpp"
#include "KokkosFFT_common_types.hpp"
#include "KokkosFFT_utils.hpp"
#include "KokkosFFT_transpose.hpp"

Expand Down
23 changes: 13 additions & 10 deletions common/src/KokkosFFT_normalization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define KOKKOSFFT_NORMALIZATION_HPP

#include <tuple>
#include "KokkosFFT_default_types.hpp"
#include "KokkosFFT_common_types.hpp"
#include "KokkosFFT_utils.hpp"

namespace KokkosFFT {
Expand All @@ -28,25 +28,27 @@ auto _coefficients(const ViewType& inout, Direction direction,
[[maybe_unused]] bool to_normalize = false;

switch (normalization) {
case Normalization::FORWARD:
if (direction == Direction::Forward) {
case Normalization::forward:
if (direction == Direction::forward) {
coef = static_cast<value_type>(1) / static_cast<value_type>(fft_size);
to_normalize = true;
}

break;
case Normalization::BACKWARD:
if (direction == Direction::Backward) {
case Normalization::backward:
if (direction == Direction::backward) {
coef = static_cast<value_type>(1) / static_cast<value_type>(fft_size);
to_normalize = true;
}

break;
case Normalization::ORTHO:
case Normalization::ortho:
coef = static_cast<value_type>(1) /
Kokkos::sqrt(static_cast<value_type>(fft_size));
to_normalize = true;

break;
default: // No normalization
break;
};
return std::tuple<value_type, bool>({coef, to_normalize});
Expand All @@ -62,11 +64,12 @@ void normalize(const ExecutionSpace& exec_space, ViewType& inout,
}

inline auto swap_direction(Normalization normalization) {
Normalization new_direction = Normalization::FORWARD;
Normalization new_direction = Normalization::none;
switch (normalization) {
case Normalization::FORWARD: new_direction = Normalization::BACKWARD; break;
case Normalization::BACKWARD: new_direction = Normalization::FORWARD; break;
case Normalization::ORTHO: new_direction = Normalization::ORTHO; break;
case Normalization::forward: new_direction = Normalization::backward; break;
case Normalization::backward: new_direction = Normalization::forward; break;
case Normalization::ortho: new_direction = Normalization::ortho; break;
default: break;
};
return new_direction;
}
Expand Down
2 changes: 1 addition & 1 deletion common/src/KokkosFFT_padding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define KOKKOSFFT_PADDING_HPP

#include <tuple>
#include "KokkosFFT_default_types.hpp"
#include "KokkosFFT_common_types.hpp"
#include "KokkosFFT_utils.hpp"

namespace KokkosFFT {
Expand Down
2 changes: 1 addition & 1 deletion common/src/KokkosFFT_transpose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <numeric>
#include <tuple>
#include "KokkosFFT_default_types.hpp"
#include "KokkosFFT_common_types.hpp"
#include "KokkosFFT_utils.hpp"

namespace KokkosFFT {
Expand Down
63 changes: 45 additions & 18 deletions common/unit_test/Test_Normalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ TEST(Normalization, Forward) {

Kokkos::fence();

// Backward FFT with Forward Normalization -> Do nothing
// Backward FFT with forward Normalization -> Do nothing
KokkosFFT::Impl::normalize(execution_space(), x,
KokkosFFT::Impl::Direction::Backward,
KokkosFFT::Normalization::FORWARD, len);
KokkosFFT::Direction::backward,
KokkosFFT::Normalization::forward, len);
EXPECT_TRUE(allclose(x, ref_b, 1.e-5, 1.e-12));

// Forward FFT with Forward Normalization -> 1/N normalization
// Forward FFT with forward Normalization -> 1/N normalization
KokkosFFT::Impl::normalize(execution_space(), x,
KokkosFFT::Impl::Direction::Forward,
KokkosFFT::Normalization::FORWARD, len);
KokkosFFT::Direction::forward,
KokkosFFT::Normalization::forward, len);
EXPECT_TRUE(allclose(x, ref_f, 1.e-5, 1.e-12));
}

Expand All @@ -47,16 +47,16 @@ TEST(Normalization, Backward) {

Kokkos::fence();

// Forward FFT with Backward Normalization -> Do nothing
// Forward FFT with backward Normalization -> Do nothing
KokkosFFT::Impl::normalize(execution_space(), x,
KokkosFFT::Impl::Direction::Forward,
KokkosFFT::Normalization::BACKWARD, len);
KokkosFFT::Direction::forward,
KokkosFFT::Normalization::backward, len);
EXPECT_TRUE(allclose(x, ref_f, 1.e-5, 1.e-12));

// Backward FFT with Backward Normalization -> 1/N normalization
// Backward FFT with backward Normalization -> 1/N normalization
KokkosFFT::Impl::normalize(execution_space(), x,
KokkosFFT::Impl::Direction::Backward,
KokkosFFT::Normalization::BACKWARD, len);
KokkosFFT::Direction::backward,
KokkosFFT::Normalization::backward, len);
EXPECT_TRUE(allclose(x, ref_b, 1.e-5, 1.e-12));
}

Expand All @@ -78,15 +78,42 @@ TEST(Normalization, Ortho) {

Kokkos::fence();

// Forward FFT with Ortho Normalization -> 1 / sqrt(N) normalization
// Forward FFT with ortho Normalization -> 1 / sqrt(N) normalization
KokkosFFT::Impl::normalize(execution_space(), x_f,
KokkosFFT::Impl::Direction::Forward,
KokkosFFT::Normalization::ORTHO, len);
KokkosFFT::Direction::forward,
KokkosFFT::Normalization::ortho, len);
EXPECT_TRUE(allclose(x_f, ref_f, 1.e-5, 1.e-12));

// Backward FFT with Ortho Normalization -> 1 / sqrt(N) normalization
// Backward FFT with ortho Normalization -> 1 / sqrt(N) normalization
KokkosFFT::Impl::normalize(execution_space(), x_b,
KokkosFFT::Impl::Direction::Backward,
KokkosFFT::Normalization::ORTHO, len);
KokkosFFT::Direction::backward,
KokkosFFT::Normalization::ortho, len);
EXPECT_TRUE(allclose(x_b, ref_b, 1.e-5, 1.e-12));
}

TEST(Normalization, None) {
const int len = 30;
View1D<double> x_f("x_f", len), x_b("x_b", len);
View1D<double> ref_f("ref_f", len), ref_b("ref_b", len);

Kokkos::Random_XorShift64_Pool<> random_pool(/*seed=*/12345);
Kokkos::fill_random(x_f, random_pool, 1.0);

Kokkos::deep_copy(x_b, x_f);
Kokkos::deep_copy(ref_f, x_f);
Kokkos::deep_copy(ref_b, x_f);

Kokkos::fence();

// Forward FFT with none Normalization -> Do nothing
KokkosFFT::Impl::normalize(execution_space(), x_f,
KokkosFFT::Direction::forward,
KokkosFFT::Normalization::none, len);
EXPECT_TRUE(allclose(x_f, ref_f, 1.e-5, 1.e-12));

// Backward FFT with none Normalization -> Do nothing
KokkosFFT::Impl::normalize(execution_space(), x_b,
KokkosFFT::Direction::backward,
KokkosFFT::Normalization::none, len);
EXPECT_TRUE(allclose(x_b, ref_b, 1.e-5, 1.e-12));
}
8 changes: 4 additions & 4 deletions examples/04_batchedFFT/04_batchedFFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ int main(int argc, char* argv[]) {
Kokkos::fill_random(xc2c, random_pool, I);

KokkosFFT::fft(execution_space(), xc2c, xc2c_hat,
KokkosFFT::Normalization::BACKWARD, /*axis=*/-1);
KokkosFFT::Normalization::backward, /*axis=*/-1);
KokkosFFT::ifft(execution_space(), xc2c_hat, xc2c_inv,
KokkosFFT::Normalization::BACKWARD, /*axis=*/-1);
KokkosFFT::Normalization::backward, /*axis=*/-1);

// 1D batched R2C FFT
View3D<double> xr2c("xr2c", n0, n1, n2);
View3D<Kokkos::complex<double> > xr2c_hat("xr2c_hat", n0, n1, n2 / 2 + 1);
Kokkos::fill_random(xr2c, random_pool, 1);

KokkosFFT::rfft(execution_space(), xr2c, xr2c_hat,
KokkosFFT::Normalization::BACKWARD, /*axis=*/-1);
KokkosFFT::Normalization::backward, /*axis=*/-1);

// 1D batched C2R FFT
View3D<Kokkos::complex<double> > xc2r("xr2c_hat", n0, n1, n2 / 2 + 1);
View3D<double> xc2r_hat("xc2r", n0, n1, n2);
Kokkos::fill_random(xc2r, random_pool, I);

KokkosFFT::irfft(execution_space(), xc2r, xc2r_hat,
KokkosFFT::Normalization::BACKWARD, /*axis=*/-1);
KokkosFFT::Normalization::backward, /*axis=*/-1);
}
Kokkos::finalize();

Expand Down
9 changes: 4 additions & 5 deletions examples/06_1DFFT_reuse_plans/06_1DFFT_reuse_plans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ int main(int argc, char* argv[]) {

int axis = -1;
KokkosFFT::Impl::Plan fft_plan(execution_space(), xc2c, xc2c_hat,
KokkosFFT::Impl::Direction::Forward, axis);
KokkosFFT::Direction::forward, axis);
KokkosFFT::fft(execution_space(), xc2c, xc2c_hat, fft_plan);

KokkosFFT::Impl::Plan ifft_plan(execution_space(), xc2c_hat, xc2c_inv,
KokkosFFT::Impl::Direction::Backward, axis);
KokkosFFT::Direction::backward, axis);
KokkosFFT::ifft(execution_space(), xc2c_hat, xc2c_inv, ifft_plan);

// 1D R2C FFT
Expand All @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) {
Kokkos::fill_random(xr2c, random_pool, 1);

KokkosFFT::Impl::Plan rfft_plan(execution_space(), xr2c, xr2c_hat,
KokkosFFT::Impl::Direction::Forward, axis);
KokkosFFT::Direction::forward, axis);
KokkosFFT::rfft(execution_space(), xr2c, xr2c_hat, rfft_plan);

// 1D C2R FFT
Expand All @@ -45,8 +45,7 @@ int main(int argc, char* argv[]) {
Kokkos::fill_random(xc2r, random_pool, I);

KokkosFFT::Impl::Plan irfft_plan(execution_space(), xc2r, xc2r_hat,
KokkosFFT::Impl::Direction::Backward,
axis);
KokkosFFT::Direction::backward, axis);
KokkosFFT::irfft(execution_space(), xc2r, xc2r_hat, irfft_plan);
}
Kokkos::finalize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define KOKKOSFFT_CUDA_TYPES_HPP

#include <cufft.h>
#include "KokkosFFT_common_types.hpp"

// Check the size of complex type
static_assert(sizeof(cufftComplex) == sizeof(Kokkos::complex<float>));
Expand All @@ -22,11 +23,6 @@ static_assert(alignof(fftw_complex) <= alignof(Kokkos::complex<double>));

namespace KokkosFFT {
namespace Impl {
enum class Direction {
Forward,
Backward,
};

using FFTDirectionType = int;

#ifdef ENABLE_HOST_AND_DEVICE
Expand Down Expand Up @@ -140,7 +136,7 @@ auto direction_type(Direction direction) {
static constexpr FFTDirectionType _BACKWARD =
std::is_same_v<ExecutionSpace, Kokkos::Cuda> ? CUFFT_INVERSE
: FFTW_BACKWARD;
return direction == Direction::Forward ? _FORWARD : _BACKWARD;
return direction == Direction::forward ? _FORWARD : _BACKWARD;
}
#else
template <typename ExecutionSpace>
Expand Down Expand Up @@ -198,7 +194,7 @@ struct transform_type<ExecutionSpace, Kokkos::complex<T1>,

template <typename ExecutionSpace>
auto direction_type(Direction direction) {
return direction == Direction::Forward ? CUFFT_FORWARD : CUFFT_INVERSE;
return direction == Direction::forward ? CUFFT_FORWARD : CUFFT_INVERSE;
}
#endif
} // namespace Impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define KOKKOSFFT_HIP_TYPES_HPP

#include <hipfft/hipfft.h>
#include "KokkosFFT_common_types.hpp"

// Check the size of complex type
static_assert(sizeof(hipfftComplex) == sizeof(Kokkos::complex<float>));
Expand All @@ -22,11 +23,6 @@ static_assert(alignof(fftw_complex) <= alignof(Kokkos::complex<double>));

namespace KokkosFFT {
namespace Impl {
enum class Direction {
Forward,
Backward,
};

using FFTDirectionType = int;

#ifdef ENABLE_HOST_AND_DEVICE
Expand Down Expand Up @@ -140,7 +136,7 @@ auto direction_type(Direction direction) {
static constexpr FFTDirectionType _BACKWARD =
std::is_same_v<ExecutionSpace, Kokkos::HIP> ? HIPFFT_BACKWARD
: FFTW_BACKWARD;
return direction == Direction::Forward ? _FORWARD : _BACKWARD;
return direction == Direction::forward ? _FORWARD : _BACKWARD;
}
#else
template <typename ExecutionSpace>
Expand Down Expand Up @@ -198,7 +194,7 @@ struct transform_type<ExecutionSpace, Kokkos::complex<T1>,

template <typename ExecutionSpace>
auto direction_type(Direction direction) {
return direction == Direction::Forward ? HIPFFT_FORWARD : HIPFFT_BACKWARD;
return direction == Direction::forward ? HIPFFT_FORWARD : HIPFFT_BACKWARD;
}
#endif
} // namespace Impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define KOKKOSFFT_OPENMP_TYPES_HPP

#include <fftw3.h>
#include "KokkosFFT_common_types.hpp"
#include "KokkosFFT_utils.hpp"

// Check the size of complex type
Expand All @@ -13,11 +14,6 @@ static_assert(alignof(fftw_complex) <= alignof(Kokkos::complex<double>));

namespace KokkosFFT {
namespace Impl {
enum class Direction {
Forward,
Backward,
};

enum class FFTWTransformType { R2C, D2Z, C2R, Z2D, C2C, Z2Z };

template <typename ExecutionSpace>
Expand Down Expand Up @@ -86,7 +82,7 @@ struct transform_type<ExecutionSpace, Kokkos::complex<T1>,

template <typename ExecutionSpace>
auto direction_type(Direction direction) {
return direction == Direction::Forward ? FFTW_FORWARD : FFTW_BACKWARD;
return direction == Direction::forward ? FFTW_FORWARD : FFTW_BACKWARD;
}
} // namespace Impl
} // namespace KokkosFFT
Expand Down
Loading

0 comments on commit 1020adc

Please sign in to comment.