Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Generic Optimizations #79

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/common/subgroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define PORTFFT_COMMON_SUBGROUP_HPP

#include <common/helpers.hpp>
#include <common/transfers.hpp>
#include <common/twiddle.hpp>
#include <common/twiddle_calc.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are twiddle.hpp and twiddle_calc.hpp needed?

#include <common/workitem.hpp>
Expand Down Expand Up @@ -100,12 +101,15 @@ __attribute__((always_inline)) inline void cross_sg_naive_dft(T& real, T& imag,
T res_imag = 0;

unrolled_loop<0, N, 1>([&](int idx_in) __attribute__((always_inline)) {
const T multi_re = twiddle<T>::Re[N][idx_in * idx_out % N];
const T multi_re = static_cast<T>(
sycl::cos(static_cast<float>(-2 * M_PI) * static_cast<float>(idx_in * idx_out % N) / static_cast<float>(N)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put static_cast<float>(-2 * M_PI) * static_cast<float>(idx_in * idx_out % N) / static_cast<float>(N) in a variable, it's a lot to read.

const T multi_im = [&]() __attribute__((always_inline)) {
if constexpr (Dir == direction::FORWARD) {
return twiddle<T>::Im[N][idx_in * idx_out % N];
return static_cast<T>(sycl::sin(static_cast<float>(-2 * M_PI) * static_cast<float>(idx_in * idx_out % N) /
static_cast<float>(N)));
}
return -twiddle<T>::Im[N][idx_in * idx_out % N];
return static_cast<T>(sycl::sin(static_cast<float>(-2 * M_PI) * static_cast<float>(idx_in * idx_out % N) /
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

negation missing

static_cast<float>(N)));
}
();
std::size_t source_wi_id = static_cast<std::size_t>(fft_start + idx_in * Stride);
Expand Down Expand Up @@ -177,12 +181,15 @@ __attribute__((always_inline)) inline void cross_sg_cooley_tukey_dft(T& real, T&
// transpose
cross_sg_transpose<N, M, Stride>(real, imag, sg);
// twiddle
const T multi_re = twiddle<T>::Re[N * M][k * n];
const T multi_re =
static_cast<T>(sycl::cos(static_cast<float>(-2 * M_PI) * static_cast<float>(k * n % N) / static_cast<float>(N)));
const T multi_im = [&]() __attribute__((always_inline)) {
if constexpr (Dir == direction::FORWARD) {
return twiddle<T>::Im[N * M][k * n];
return static_cast<T>(
sycl::sin(static_cast<float>(-2 * M_PI) * static_cast<float>(k * n % N) / static_cast<float>(N)));
}
return -twiddle<T>::Im[N * M][k * n];
return static_cast<T>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

negation missing

sycl::sin(static_cast<float>(-2 * M_PI) * static_cast<float>(k * n % N) / static_cast<float>(N)));
}
();
T tmp_real = real * multi_re - imag * multi_im;
Expand Down Expand Up @@ -269,7 +276,8 @@ constexpr bool fits_in_sg(TIndex N, int sg_size) {
* commit
*/
template <direction Dir, int M, int N, typename T>
__attribute__((always_inline)) inline void sg_dft(T* inout, sycl::sub_group& sg, const T* sg_twiddles) {
__attribute__((always_inline)) inline void sg_dft(T* inout, sycl::sub_group& sg, const T* sg_twiddles_real,
const T* sg_twiddles_imag) {
int idx_of_wi_in_fft = static_cast<int>(sg.get_local_linear_id()) % N;

detail::unrolled_loop<0, M, 1>([&](int idx_of_element_in_wi) __attribute__((always_inline)) {
Expand All @@ -279,8 +287,8 @@ __attribute__((always_inline)) inline void sg_dft(T* inout, sycl::sub_group& sg,
if constexpr (N > 1) {
detail::cross_sg_dft<Dir, N, 1>(real, imag, sg);
if (idx_of_element_in_wi > 0) {
T twiddle_real = sg_twiddles[idx_of_element_in_wi * N + idx_of_wi_in_fft];
T twiddle_imag = sg_twiddles[(idx_of_element_in_wi + M) * N + idx_of_wi_in_fft];
T twiddle_real = sg_twiddles_real[detail::pad_local(idx_of_element_in_wi * N + idx_of_wi_in_fft, 1)];
T twiddle_imag = sg_twiddles_imag[detail::pad_local(idx_of_element_in_wi * N + idx_of_wi_in_fft, 1)];
if constexpr (Dir == direction::BACKWARD) {
twiddle_imag = -twiddle_imag;
}
Expand Down
15 changes: 7 additions & 8 deletions src/common/workgroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace detail {
*/
template <direction Dir, detail::transpose TransposeIn, int DFTSize, int StrideWithinDFT, int NDFTsInOuterDimension,
int SubgroupSize, std::size_t BankLinesPerPad, typename T>
__attribute__((always_inline)) inline void dimension_dft(T* loc, T* loc_twiddles, const T* wg_twiddles,
__attribute__((always_inline)) inline void dimension_dft(T* loc, T* loc_real, T* loc_complex, const T* wg_twiddles,
T scaling_factor, std::size_t max_num_batches_in_local_mem,
std::size_t sub_batch_num, sycl::nd_item<1> it) {
constexpr int OuterStride = DFTSize * StrideWithinDFT;
Expand Down Expand Up @@ -155,7 +155,7 @@ __attribute__((always_inline)) inline void dimension_dft(T* loc, T* loc_twiddles
});
}
}
sg_dft<Dir, FactWi, FactSg>(priv, sg, loc_twiddles);
sg_dft<Dir, FactWi, FactSg>(priv, sg, loc_real, loc_complex);
if (working) {
if constexpr (TransposeIn == detail::transpose::TRANSPOSED) {
transfer_strided<detail::transfer_direction::PRIVATE_TO_LOCAL, detail::pad::DO_PAD, FactWi>(
Expand All @@ -170,7 +170,6 @@ __attribute__((always_inline)) inline void dimension_dft(T* loc, T* loc_twiddles
}
}
}

};

/**
Expand All @@ -195,16 +194,16 @@ __attribute__((always_inline)) inline void dimension_dft(T* loc, T* loc_twiddles
*/
template <direction Dir, detail::transpose TransposeIn, int FFTSize, int N, int M, int SubgroupSize,
std::size_t BankLinesPerPad, typename T>
__attribute__((always_inline)) inline void wg_dft(T* loc, T* loc_twiddles, const T* wg_twiddles, sycl::nd_item<1> it,
T scaling_factor, std::size_t max_num_batches_in_local_mem,
std::size_t sub_batch_num) {
__attribute__((always_inline)) inline void wg_dft(T* loc, T* loc_real, T* loc_complex, const T* wg_twiddles,
sycl::nd_item<1> it, T scaling_factor,
std::size_t max_num_batches_in_local_mem, std::size_t sub_batch_num) {
// column-wise DFTs
detail::dimension_dft<Dir, TransposeIn, N, M, 1, SubgroupSize, BankLinesPerPad, T>(
loc, loc_twiddles + (2 * M), nullptr, 1, max_num_batches_in_local_mem, sub_batch_num, it);
loc, loc_real + 2 * M, loc_complex + (2 * M), nullptr, 1, max_num_batches_in_local_mem, sub_batch_num, it);
sycl::group_barrier(it.get_group());
// row-wise DFTs, including twiddle multiplications and scaling
detail::dimension_dft<Dir, TransposeIn, M, 1, N, SubgroupSize, BankLinesPerPad, T>(
loc, loc_twiddles, wg_twiddles, scaling_factor, max_num_batches_in_local_mem, sub_batch_num, it);
loc, loc_real, loc_complex, wg_twiddles, scaling_factor, max_num_batches_in_local_mem, sub_batch_num, it);
}

} // namespace portfft
Expand Down
Loading