use core::ffi::c_long;
use core::marker::PhantomData;
use core::ptr;
use num_complex::Complex;
use onemkl_sys::{self as sys, DFTI_DESCRIPTOR_HANDLE};
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FftPrecision {
Single,
Double,
}
impl FftPrecision {
#[inline]
fn as_dfti(self) -> sys::DFTI_CONFIG_VALUE::Type {
match self {
Self::Single => sys::DFTI_CONFIG_VALUE::DFTI_SINGLE,
Self::Double => sys::DFTI_CONFIG_VALUE::DFTI_DOUBLE,
}
}
}
pub trait FftScalar: Copy + 'static {
const PRECISION: FftPrecision;
fn to_f64(self) -> f64;
}
impl FftScalar for f32 {
const PRECISION: FftPrecision = FftPrecision::Single;
#[inline]
fn to_f64(self) -> f64 {
self as f64
}
}
impl FftScalar for f64 {
const PRECISION: FftPrecision = FftPrecision::Double;
#[inline]
fn to_f64(self) -> f64 {
self
}
}
#[inline]
fn check_dfti(status: c_long) -> Result<()> {
if status == 0 {
Ok(())
} else {
Err(Error::DftiStatus(status as i64))
}
}
#[inline]
fn apply_scales<T: FftScalar>(
handle: DFTI_DESCRIPTOR_HANDLE,
scales: Option<(T, T)>,
) -> Result<()> {
if let Some((forward, backward)) = scales {
let status = unsafe {
sys::DftiSetValue(
handle,
sys::DFTI_CONFIG_PARAM::DFTI_FORWARD_SCALE,
forward.to_f64(),
)
};
check_dfti(status)?;
let status = unsafe {
sys::DftiSetValue(
handle,
sys::DFTI_CONFIG_PARAM::DFTI_BACKWARD_SCALE,
backward.to_f64(),
)
};
check_dfti(status)?;
}
Ok(())
}
#[inline]
fn create_complex_descriptor<T: FftScalar>(
dims: &[usize],
placement: sys::DFTI_CONFIG_VALUE::Type,
scales: Option<(T, T)>,
) -> Result<DFTI_DESCRIPTOR_HANDLE> {
if dims.is_empty() {
return Err(Error::InvalidArgument(
"FFT plan needs at least one dimension",
));
}
let dims_long: Vec<c_long> = dims
.iter()
.map(|&d| {
c_long::try_from(d).map_err(|_| Error::DimensionOverflow)
})
.collect::<Result<Vec<_>>>()?;
let mut handle: DFTI_DESCRIPTOR_HANDLE = ptr::null_mut();
let ndim_long: c_long = c_long::try_from(dims.len())
.map_err(|_| Error::DimensionOverflow)?;
let precision = T::PRECISION.as_dfti();
let domain = sys::DFTI_CONFIG_VALUE::DFTI_COMPLEX;
let status = unsafe {
if dims.len() == 1 {
sys::DftiCreateDescriptor(&mut handle, precision, domain, ndim_long, dims_long[0])
} else {
sys::DftiCreateDescriptor(
&mut handle,
precision,
domain,
ndim_long,
dims_long.as_ptr(),
)
}
};
check_dfti(status)?;
let status = unsafe {
sys::DftiSetValue(
handle,
sys::DFTI_CONFIG_PARAM::DFTI_PLACEMENT,
placement,
)
};
if let Err(e) = check_dfti(status) {
unsafe {
let mut h = handle;
let _ = sys::DftiFreeDescriptor(&mut h);
}
return Err(e);
}
if let Err(e) = apply_scales(handle, scales) {
unsafe {
let mut h = handle;
let _ = sys::DftiFreeDescriptor(&mut h);
}
return Err(e);
}
let status = unsafe { sys::DftiCommitDescriptor(handle) };
if let Err(e) = check_dfti(status) {
unsafe {
let mut h = handle;
let _ = sys::DftiFreeDescriptor(&mut h);
}
return Err(e);
}
Ok(handle)
}
#[inline]
fn create_real_descriptor<T: FftScalar>(
dims: &[usize],
placement: sys::DFTI_CONFIG_VALUE::Type,
scales: Option<(T, T)>,
) -> Result<DFTI_DESCRIPTOR_HANDLE> {
if dims.is_empty() {
return Err(Error::InvalidArgument(
"FFT plan needs at least one dimension",
));
}
let dims_long: Vec<c_long> = dims
.iter()
.map(|&d| c_long::try_from(d).map_err(|_| Error::DimensionOverflow))
.collect::<Result<Vec<_>>>()?;
let mut handle: DFTI_DESCRIPTOR_HANDLE = ptr::null_mut();
let ndim_long: c_long = c_long::try_from(dims.len())
.map_err(|_| Error::DimensionOverflow)?;
let precision = T::PRECISION.as_dfti();
let domain = sys::DFTI_CONFIG_VALUE::DFTI_REAL;
let status = unsafe {
if dims.len() == 1 {
sys::DftiCreateDescriptor(&mut handle, precision, domain, ndim_long, dims_long[0])
} else {
sys::DftiCreateDescriptor(
&mut handle,
precision,
domain,
ndim_long,
dims_long.as_ptr(),
)
}
};
check_dfti(status)?;
let status = unsafe {
sys::DftiSetValue(
handle,
sys::DFTI_CONFIG_PARAM::DFTI_CONJUGATE_EVEN_STORAGE,
sys::DFTI_CONFIG_VALUE::DFTI_COMPLEX_COMPLEX,
)
};
if let Err(e) = check_dfti(status) {
unsafe {
let mut h = handle;
let _ = sys::DftiFreeDescriptor(&mut h);
}
return Err(e);
}
if placement == sys::DFTI_CONFIG_VALUE::DFTI_NOT_INPLACE && dims.len() > 1 {
let last = dims.len() - 1;
let mut input_strides: Vec<c_long> = Vec::with_capacity(dims.len() + 1);
input_strides.push(0);
let mut acc: c_long = 1;
for &d in &dims_long {
input_strides.push(acc);
acc = acc.checked_mul(d).ok_or(Error::DimensionOverflow)?;
}
let mut output_strides: Vec<c_long> = Vec::with_capacity(dims.len() + 1);
output_strides.push(0);
let cce_last: c_long = c_long::try_from(cce_complex_len(dims[last]))
.map_err(|_| Error::DimensionOverflow)?;
let mut acc: c_long = 1;
for (i, &d) in dims_long.iter().enumerate() {
output_strides.push(acc);
let step = if i == last { cce_last } else { d };
acc = acc.checked_mul(step).ok_or(Error::DimensionOverflow)?;
}
let status = unsafe {
sys::DftiSetValue(
handle,
sys::DFTI_CONFIG_PARAM::DFTI_INPUT_STRIDES,
input_strides.as_ptr(),
)
};
if let Err(e) = check_dfti(status) {
unsafe {
let mut h = handle;
let _ = sys::DftiFreeDescriptor(&mut h);
}
return Err(e);
}
let status = unsafe {
sys::DftiSetValue(
handle,
sys::DFTI_CONFIG_PARAM::DFTI_OUTPUT_STRIDES,
output_strides.as_ptr(),
)
};
if let Err(e) = check_dfti(status) {
unsafe {
let mut h = handle;
let _ = sys::DftiFreeDescriptor(&mut h);
}
return Err(e);
}
}
let status = unsafe {
sys::DftiSetValue(
handle,
sys::DFTI_CONFIG_PARAM::DFTI_PLACEMENT,
placement,
)
};
if let Err(e) = check_dfti(status) {
unsafe {
let mut h = handle;
let _ = sys::DftiFreeDescriptor(&mut h);
}
return Err(e);
}
if let Err(e) = apply_scales(handle, scales) {
unsafe {
let mut h = handle;
let _ = sys::DftiFreeDescriptor(&mut h);
}
return Err(e);
}
let status = unsafe { sys::DftiCommitDescriptor(handle) };
if let Err(e) = check_dfti(status) {
unsafe {
let mut h = handle;
let _ = sys::DftiFreeDescriptor(&mut h);
}
return Err(e);
}
Ok(handle)
}
#[inline]
#[must_use]
pub fn cce_complex_len(real_n: usize) -> usize {
real_n / 2 + 1
}
pub struct FftPlan<T: FftScalar> {
handle: DFTI_DESCRIPTOR_HANDLE,
total_complex_len: usize,
_marker: PhantomData<T>,
}
unsafe impl<T: FftScalar> Send for FftPlan<T> {}
impl<T: FftScalar> FftPlan<T> {
pub fn complex_1d(n: usize) -> Result<Self> {
Self::complex_nd(&[n])
}
pub fn complex_2d(n0: usize, n1: usize) -> Result<Self> {
Self::complex_nd(&[n0, n1])
}
pub fn complex_3d(n0: usize, n1: usize, n2: usize) -> Result<Self> {
Self::complex_nd(&[n0, n1, n2])
}
pub fn complex_nd(dims: &[usize]) -> Result<Self> {
let handle = create_complex_descriptor::<T>(
dims,
sys::DFTI_CONFIG_VALUE::DFTI_INPLACE,
None,
)?;
let total: usize = dims.iter().product();
Ok(Self {
handle,
total_complex_len: total,
_marker: PhantomData,
})
}
pub fn complex_nd_with_scales(
dims: &[usize],
forward_scale: T,
backward_scale: T,
) -> Result<Self> {
let handle = create_complex_descriptor::<T>(
dims,
sys::DFTI_CONFIG_VALUE::DFTI_INPLACE,
Some((forward_scale, backward_scale)),
)?;
let total: usize = dims.iter().product();
Ok(Self {
handle,
total_complex_len: total,
_marker: PhantomData,
})
}
pub fn forward_in_place(&mut self, x: &mut [Complex<T>]) -> Result<()> {
if x.len() < self.total_complex_len {
return Err(Error::InvalidArgument(
"buffer too small for FFT plan size",
));
}
let status = unsafe {
sys::DftiComputeForward(self.handle, x.as_mut_ptr().cast())
};
check_dfti(status)
}
pub fn backward_in_place(&mut self, x: &mut [Complex<T>]) -> Result<()> {
if x.len() < self.total_complex_len {
return Err(Error::InvalidArgument(
"buffer too small for FFT plan size",
));
}
let status = unsafe {
sys::DftiComputeBackward(self.handle, x.as_mut_ptr().cast())
};
check_dfti(status)
}
}
impl<T: FftScalar> Drop for FftPlan<T> {
fn drop(&mut self) {
if !self.handle.is_null() {
unsafe {
let _ = sys::DftiFreeDescriptor(&mut self.handle);
}
}
}
}
pub struct FftPlanOutOfPlace<T: FftScalar> {
handle: DFTI_DESCRIPTOR_HANDLE,
total_complex_len: usize,
_marker: PhantomData<T>,
}
unsafe impl<T: FftScalar> Send for FftPlanOutOfPlace<T> {}
impl<T: FftScalar> FftPlanOutOfPlace<T> {
pub fn complex_1d(n: usize) -> Result<Self> {
Self::complex_nd(&[n])
}
pub fn complex_2d(n0: usize, n1: usize) -> Result<Self> {
Self::complex_nd(&[n0, n1])
}
pub fn complex_3d(n0: usize, n1: usize, n2: usize) -> Result<Self> {
Self::complex_nd(&[n0, n1, n2])
}
pub fn complex_nd(dims: &[usize]) -> Result<Self> {
let handle = create_complex_descriptor::<T>(
dims,
sys::DFTI_CONFIG_VALUE::DFTI_NOT_INPLACE,
None,
)?;
let total: usize = dims.iter().product();
Ok(Self {
handle,
total_complex_len: total,
_marker: PhantomData,
})
}
pub fn complex_nd_with_scales(
dims: &[usize],
forward_scale: T,
backward_scale: T,
) -> Result<Self> {
let handle = create_complex_descriptor::<T>(
dims,
sys::DFTI_CONFIG_VALUE::DFTI_NOT_INPLACE,
Some((forward_scale, backward_scale)),
)?;
let total: usize = dims.iter().product();
Ok(Self {
handle,
total_complex_len: total,
_marker: PhantomData,
})
}
pub fn forward(&mut self, input: &[Complex<T>], out: &mut [Complex<T>]) -> Result<()> {
if input.len() < self.total_complex_len || out.len() < self.total_complex_len {
return Err(Error::InvalidArgument(
"buffer too small for FFT plan size",
));
}
let status = unsafe {
sys::DftiComputeForward(
self.handle,
input.as_ptr() as *mut core::ffi::c_void,
out.as_mut_ptr().cast::<core::ffi::c_void>(),
)
};
check_dfti(status)
}
pub fn backward(&mut self, input: &[Complex<T>], out: &mut [Complex<T>]) -> Result<()> {
if input.len() < self.total_complex_len || out.len() < self.total_complex_len {
return Err(Error::InvalidArgument(
"buffer too small for FFT plan size",
));
}
let status = unsafe {
sys::DftiComputeBackward(
self.handle,
input.as_ptr() as *mut core::ffi::c_void,
out.as_mut_ptr().cast::<core::ffi::c_void>(),
)
};
check_dfti(status)
}
}
impl<T: FftScalar> Drop for FftPlanOutOfPlace<T> {
fn drop(&mut self) {
if !self.handle.is_null() {
unsafe {
let _ = sys::DftiFreeDescriptor(&mut self.handle);
}
}
}
}
pub struct RealFftPlan<T: FftScalar> {
handle: DFTI_DESCRIPTOR_HANDLE,
real_total_len: usize,
complex_total_len: usize,
_marker: PhantomData<T>,
}
unsafe impl<T: FftScalar> Send for RealFftPlan<T> {}
impl<T: FftScalar> RealFftPlan<T> {
pub fn real_1d(n: usize) -> Result<Self> {
Self::real_nd(&[n])
}
pub fn real_2d(n0: usize, n1: usize) -> Result<Self> {
Self::real_nd(&[n0, n1])
}
pub fn real_3d(n0: usize, n1: usize, n2: usize) -> Result<Self> {
Self::real_nd(&[n0, n1, n2])
}
pub fn real_nd(dims: &[usize]) -> Result<Self> {
Self::real_nd_inner(dims, None)
}
pub fn real_nd_with_scales(
dims: &[usize],
forward_scale: T,
backward_scale: T,
) -> Result<Self> {
Self::real_nd_inner(dims, Some((forward_scale, backward_scale)))
}
fn real_nd_inner(dims: &[usize], scales: Option<(T, T)>) -> Result<Self> {
let handle = create_real_descriptor::<T>(
dims,
sys::DFTI_CONFIG_VALUE::DFTI_NOT_INPLACE,
scales,
)?;
let real_total: usize = dims.iter().product();
let mut complex_dims = dims.to_vec();
let last = complex_dims.len() - 1;
complex_dims[last] = cce_complex_len(complex_dims[last]);
let complex_total: usize = complex_dims.iter().product();
Ok(Self {
handle,
real_total_len: real_total,
complex_total_len: complex_total,
_marker: PhantomData,
})
}
#[inline]
#[must_use]
pub fn real_len(&self) -> usize {
self.real_total_len
}
#[inline]
#[must_use]
pub fn complex_len(&self) -> usize {
self.complex_total_len
}
pub fn forward(&mut self, input: &[T], out: &mut [Complex<T>]) -> Result<()> {
if input.len() < self.real_total_len {
return Err(Error::InvalidArgument(
"real input buffer too small for FFT plan size",
));
}
if out.len() < self.complex_total_len {
return Err(Error::InvalidArgument(
"complex output buffer too small (need n/2 + 1 along first dim)",
));
}
let status = unsafe {
sys::DftiComputeForward(
self.handle,
input.as_ptr() as *mut core::ffi::c_void,
out.as_mut_ptr().cast::<core::ffi::c_void>(),
)
};
check_dfti(status)
}
pub fn backward(&mut self, input: &[Complex<T>], out: &mut [T]) -> Result<()> {
if input.len() < self.complex_total_len {
return Err(Error::InvalidArgument(
"complex input buffer too small (need n/2 + 1 along first dim)",
));
}
if out.len() < self.real_total_len {
return Err(Error::InvalidArgument(
"real output buffer too small for FFT plan size",
));
}
let status = unsafe {
sys::DftiComputeBackward(
self.handle,
input.as_ptr() as *mut core::ffi::c_void,
out.as_mut_ptr().cast::<core::ffi::c_void>(),
)
};
check_dfti(status)
}
}
impl<T: FftScalar> Drop for RealFftPlan<T> {
fn drop(&mut self) {
if !self.handle.is_null() {
unsafe {
let _ = sys::DftiFreeDescriptor(&mut self.handle);
}
}
}
}