-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdit.rs
More file actions
313 lines (283 loc) · 10.2 KB
/
Copy pathdit.rs
File metadata and controls
313 lines (283 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! Decimation-in-Time (DIT) FFT Implementation
//!
//! The DIT algorithm decomposes the DFT from small to large sub-problems. Input is processed in
//! bit-reversed order, and output is produced in natural order.
//!
//! ## Algorithm Overview
//!
//! 1. Apply bit-reversal to input data
//! 2. Start with small butterflies (size 2)
//! 3. Work up to stage `log(N)`, where `N` is the size of the input.
//!
//! ## Memory Access Pattern
//!
//! DIT starts with fine-grained memory access and progressively works with
//! larger contiguous chunks.
//!
use fearless_simd::{dispatch, Simd};
use crate::algorithms::bravo::{bit_rev_bravo_f32, bit_rev_bravo_f64};
use crate::kernels::codelets::{fft_dit_codelet_32_f32, fft_dit_codelet_32_f64};
use crate::kernels::dit::*;
use crate::options::Options;
use crate::parallel::run_maybe_in_parallel;
use crate::planner::{Direction, PlannerDit32, PlannerDit64};
/// L1 cache block size in complex elements (8KB for f32, 16KB for f64)
const L1_BLOCK_SIZE: usize = 1024;
/// Recursive cache-blocked DIT FFT for f64 using post-order traversal.
///
/// Recursively divides by 2 until reaching L1 cache size, processes stages within
/// each block, then processes cross-block stages on return.
fn recursive_dit_fft_f64<S: Simd>(
simd: S,
reals: &mut [f64],
imags: &mut [f64],
size: usize,
planner: &PlannerDit64,
opts: &Options,
) {
let log_size = size.ilog2() as usize;
if size <= L1_BLOCK_SIZE {
// Use FFT-32 codelet to fuse stages 0-4 into a single pass per 32-element chunk
let start_stage = if planner.use_codelet_32 {
fft_dit_codelet_32_f64(simd, &mut reals[..size], &mut imags[..size]);
5
} else {
0
};
for stage in start_stage..log_size {
execute_dit_stage_f64(simd, &mut reals[..size], &mut imags[..size], stage);
}
} else {
let half = size / 2;
let log_half = half.ilog2() as usize;
let (re_first_half, re_second_half) = reals.split_at_mut(half);
let (im_first_half, im_second_half) = imags.split_at_mut(half);
// Recursively process both halves
run_maybe_in_parallel(
size > opts.smallest_parallel_chunk_size,
|| recursive_dit_fft_f64(simd, re_first_half, im_first_half, half, planner, opts),
|| recursive_dit_fft_f64(simd, re_second_half, im_second_half, half, planner, opts),
);
// Process remaining stages that span both halves
for stage in log_half..log_size {
execute_dit_stage_f64(simd, &mut reals[..size], &mut imags[..size], stage);
}
}
}
/// Recursive cache-blocked DIT FFT for f32 using post-order traversal.
fn recursive_dit_fft_f32<S: Simd>(
simd: S,
reals: &mut [f32],
imags: &mut [f32],
size: usize,
planner: &PlannerDit32,
opts: &Options,
) {
let log_size = size.ilog2() as usize;
if size <= L1_BLOCK_SIZE {
// Use FFT-32 codelet to fuse stages 0-4 into a single pass per 32-element chunk
let start_stage = if planner.use_codelet_32 {
fft_dit_codelet_32_f32(simd, &mut reals[..size], &mut imags[..size]);
5
} else {
0
};
for stage in start_stage..log_size {
execute_dit_stage_f32(simd, &mut reals[..size], &mut imags[..size], stage);
}
} else {
let half = size / 2;
let log_half = half.ilog2() as usize;
let (re_first_half, re_second_half) = reals.split_at_mut(half);
let (im_first_half, im_second_half) = imags.split_at_mut(half);
// Recursively process both halves
run_maybe_in_parallel(
size > opts.smallest_parallel_chunk_size,
|| recursive_dit_fft_f32(simd, re_first_half, im_first_half, half, planner, opts),
|| recursive_dit_fft_f32(simd, re_second_half, im_second_half, half, planner, opts),
);
// Process remaining stages that span both halves
for stage in log_half..log_size {
execute_dit_stage_f32(simd, &mut reals[..size], &mut imags[..size], stage);
}
}
}
/// Execute a single DIT stage, dispatching to appropriate kernel based on chunk size.
fn execute_dit_stage_f64<S: Simd>(simd: S, reals: &mut [f64], imags: &mut [f64], stage: usize) {
let dist = 1 << stage; // 2.pow(stage)
let chunk_size = dist * 2;
if chunk_size == 2 {
simd.vectorize(|| fft_dit_chunk_2(simd, reals, imags));
} else if chunk_size == 4 {
fft_dit_chunk_4_f64(simd, reals, imags);
} else if chunk_size == 8 {
fft_dit_chunk_8_f64(simd, reals, imags);
} else if chunk_size == 16 {
fft_dit_chunk_16_f64(simd, reals, imags);
} else if chunk_size == 32 {
fft_dit_chunk_32_f64(simd, reals, imags);
} else if chunk_size == 64 {
fft_dit_chunk_64_f64(simd, reals, imags);
} else {
// For larger chunks, generate twiddles on the fly
fft_dit_chunk_n_f64(simd, reals, imags, chunk_size, dist);
}
}
/// Execute a single DIT stage, dispatching to appropriate kernel based on chunk size.
fn execute_dit_stage_f32<S: Simd>(simd: S, reals: &mut [f32], imags: &mut [f32], stage: usize) {
let dist = 1 << stage; // 2.pow(stage)
let chunk_size = dist * 2;
if chunk_size == 2 {
simd.vectorize(|| fft_dit_chunk_2(simd, reals, imags));
} else if chunk_size == 4 {
fft_dit_chunk_4_f32(simd, reals, imags);
} else if chunk_size == 8 {
fft_dit_chunk_8_f32(simd, reals, imags);
} else if chunk_size == 16 {
fft_dit_chunk_16_f32(simd, reals, imags);
} else if chunk_size == 32 {
fft_dit_chunk_32_f32(simd, reals, imags);
} else if chunk_size == 64 {
fft_dit_chunk_64_f32(simd, reals, imags);
} else {
// For larger chunks, generate twiddles on the fly
fft_dit_chunk_n_f32(simd, reals, imags, chunk_size, dist);
}
}
/// DIT FFT for f64 with pre-computed planner and options
///
/// This implementation uses the Decimation-in-Time algorithm which:
/// - Requires bit-reversed input (performed automatically)
/// - Produces output in natural order
/// - Processes from small butterflies to large
///
/// # Arguments
///
/// * `reals` - Real components of the signal (modified in-place)
/// * `imags` - Imaginary components of the signal (modified in-place)
/// * `planner` - Pre-computed planner with twiddle factors
/// * `opts` - Options controlling optimization strategies
///
/// # Panics
///
/// Panics if input length is not a power of 2 or if real and imaginary arrays have different lengths
///
pub fn fft_64_dit_with_planner_and_opts(
reals: &mut [f64],
imags: &mut [f64],
planner: &PlannerDit64,
opts: &Options,
) {
// Dynamic dispatch overhead becomes really noticeable at small FFT sizes.
// Dispatch only once at the top of the program to
dispatch!(planner.simd_level, simd => fft_64_dit_with_planner_and_opts_impl(simd, reals, imags, planner, opts))
}
#[inline(always)] // required by fearless_simd
fn fft_64_dit_with_planner_and_opts_impl<S: Simd>(
simd: S,
reals: &mut [f64],
imags: &mut [f64],
planner: &PlannerDit64,
opts: &Options,
) {
assert_eq!(reals.len(), imags.len());
assert!(reals.len().is_power_of_two());
let n = reals.len();
let log_n = n.ilog2() as usize;
assert_eq!(log_n, planner.log_n);
// DIT requires bit-reversed input
run_maybe_in_parallel(
opts.multithreaded_bit_reversal,
|| {
simd.vectorize(
#[inline(always)]
|| bit_rev_bravo_f64(simd, reals, log_n),
)
},
|| {
simd.vectorize(
#[inline(always)]
|| bit_rev_bravo_f64(simd, imags, log_n),
)
},
);
// Handle inverse FFT
if let Direction::Reverse = planner.direction {
for z_im in imags.iter_mut() {
*z_im = -*z_im;
}
}
simd.vectorize(
#[inline(always)]
|| recursive_dit_fft_f64(simd, reals, imags, n, planner, opts),
);
// Scaling for inverse transform
if let Direction::Reverse = planner.direction {
let scaling_factor = 1.0 / n as f64;
for (z_re, z_im) in reals.iter_mut().zip(imags.iter_mut()) {
*z_re *= scaling_factor;
*z_im *= -scaling_factor;
}
}
}
/// DIT FFT for f32 with pre-computed planner and options
///
/// Single-precision version of the DIT FFT algorithm.
/// See [`fft_64_dit_with_planner_and_opts`] for `f64` version.
pub fn fft_32_dit_with_planner_and_opts(
reals: &mut [f32],
imags: &mut [f32],
planner: &PlannerDit32,
opts: &Options,
) {
// Dynamic dispatch overhead becomes really noticeable at small FFT sizes.
// Dispatch only once at the top of the program to
dispatch!(planner.simd_level, simd => fft_32_dit_with_planner_and_opts_impl(simd, reals, imags, planner, opts))
}
fn fft_32_dit_with_planner_and_opts_impl<S: Simd>(
simd: S,
reals: &mut [f32],
imags: &mut [f32],
planner: &PlannerDit32,
opts: &Options,
) {
assert_eq!(reals.len(), imags.len());
assert!(reals.len().is_power_of_two());
let n = reals.len();
let log_n = n.ilog2() as usize;
assert_eq!(log_n, planner.log_n);
// DIT requires bit-reversed input
run_maybe_in_parallel(
opts.multithreaded_bit_reversal,
|| {
simd.vectorize(
#[inline(always)]
|| bit_rev_bravo_f32(simd, reals, log_n),
)
},
|| {
simd.vectorize(
#[inline(always)]
|| bit_rev_bravo_f32(simd, imags, log_n),
)
},
);
// Handle inverse FFT
if let Direction::Reverse = planner.direction {
for z_im in imags.iter_mut() {
*z_im = -*z_im;
}
}
simd.vectorize(
#[inline(always)]
|| recursive_dit_fft_f32(simd, reals, imags, n, planner, opts),
);
// Scaling for inverse transform
if let Direction::Reverse = planner.direction {
let scaling_factor = 1.0 / n as f32;
for (z_re, z_im) in reals.iter_mut().zip(imags.iter_mut()) {
*z_re *= scaling_factor;
*z_im *= -scaling_factor;
}
}
}