use super::CudaRuntime;
use super::allocator::CudaAllocator;
use super::client::CudaClient;
use super::kernels;
use crate::algorithm::fft_bluestein::cached_tables;
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::{AllocGuard, Runtime, RuntimeClient};
pub use kernels::BluesteinInput;
fn upload<'a, T: bytemuck::Pod>(
client: &'a CudaClient,
host: &[T],
) -> Result<AllocGuard<'a, CudaAllocator>> {
let bytes: &[u8] = bytemuck::cast_slice(host);
let guard = AllocGuard::new(client.allocator(), bytes.len())?;
CudaRuntime::copy_to_device(bytes, guard.ptr(), client.device())?;
Ok(guard)
}
fn stockham(
client: &CudaClient,
dtype: DType,
src_ptr: u64,
dst_ptr: u64,
m: usize,
batch_size: usize,
inverse: bool,
scale: f64,
) -> Result<()> {
let device = client.device();
let bytes = batch_size * m * dtype.size_in_bytes();
if m <= kernels::MAX_SHARED_MEM_FFT_SIZE {
unsafe {
kernels::launch_stockham_fft_batched(
client.context(),
client.stream(),
device.index,
dtype,
src_ptr,
dst_ptr,
m,
batch_size,
inverse,
scale,
)?;
}
return Ok(());
}
let temp_guard = AllocGuard::new(client.allocator(), bytes)?;
CudaRuntime::copy_within_device(src_ptr, dst_ptr, bytes, device)?;
let mut cur = dst_ptr;
let mut other = temp_guard.ptr();
let log_m = m.trailing_zeros() as usize;
for stage in 0..log_m {
unsafe {
kernels::launch_stockham_fft_stage(
client.context(),
client.stream(),
device.index,
dtype,
cur,
other,
m,
stage,
batch_size,
inverse,
)?;
}
std::mem::swap(&mut cur, &mut other);
}
if (scale - 1.0).abs() > 1e-10 {
unsafe {
kernels::launch_scale_complex(
client.context(),
client.stream(),
device.index,
dtype,
cur,
scale,
batch_size * m,
)?;
}
}
if cur != dst_ptr {
CudaRuntime::copy_within_device(cur, dst_ptr, bytes, device)?;
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(super) fn bluestein_transform<'a>(
client: &'a CudaClient,
complex_dtype: DType,
kind: BluesteinInput,
input_ptr: u64,
n: usize,
out_n: usize,
batch_size: usize,
inverse: bool,
scale: f64,
) -> Result<AllocGuard<'a, CudaAllocator>> {
if n == 0 {
return Err(Error::InvalidArgument {
arg: "n",
reason: "Bluestein transform requires N >= 1".to_string(),
});
}
let tables = cached_tables(n, inverse)?;
let m = tables.m;
let device = client.device();
let elem = complex_dtype.size_in_bytes();
let (chirp_guard, kernel_guard) = match complex_dtype {
DType::Complex64 => (
upload(client, &tables.chirp_f32())?,
upload(client, &tables.kernel_spectrum_f32())?,
),
DType::Complex128 => (
upload(client, &tables.chirp_f64())?,
upload(client, &tables.kernel_spectrum_f64())?,
),
_ => {
return Err(Error::UnsupportedDType {
dtype: complex_dtype,
op: "bluestein_transform",
});
}
};
let conv_bytes = batch_size * m * elem;
let a_guard = AllocGuard::new(client.allocator(), conv_bytes)?;
let spec_guard = AllocGuard::new(client.allocator(), conv_bytes)?;
unsafe {
kernels::launch_bluestein_premultiply(
client.context(),
client.stream(),
device.index,
complex_dtype,
kind,
input_ptr,
chirp_guard.ptr(),
a_guard.ptr(),
n,
m,
batch_size,
)?;
}
stockham(
client,
complex_dtype,
a_guard.ptr(),
spec_guard.ptr(),
m,
batch_size,
false,
1.0,
)?;
unsafe {
kernels::launch_bluestein_pointwise_mul(
client.context(),
client.stream(),
device.index,
complex_dtype,
spec_guard.ptr(),
kernel_guard.ptr(),
m,
batch_size,
)?;
}
stockham(
client,
complex_dtype,
spec_guard.ptr(),
a_guard.ptr(),
m,
batch_size,
true,
1.0 / m as f64,
)?;
let out_guard = AllocGuard::new(client.allocator(), batch_size * out_n * elem)?;
unsafe {
kernels::launch_bluestein_postmultiply(
client.context(),
client.stream(),
device.index,
complex_dtype,
a_guard.ptr(),
chirp_guard.ptr(),
out_guard.ptr(),
m,
out_n,
batch_size,
scale,
)?;
}
client.synchronize();
Ok(out_guard)
}