baracuda-kernels 0.0.1-alpha.68

Unified ML op facade for the baracuda CUDA ecosystem. Exposes every primitive an ML framework would expect (union of PyTorch torch.* + nn.functional and JAX lax.* / numpy ops) through a single Plan-based Rust surface, internally dispatching to baracuda-cutlass, the baracuda-* NVIDIA-library wrappers, or bespoke baracuda-kernels-sys kernels.
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
//! Multi-dimensional RFFT (real → complex) and IRFFT (complex → real)
//! — `RfftNdPlan<T>` / `IrfftNdPlan<T>` for `T = f32` / `f64`.
//! Milestone 6.8.
//!
//! Wraps cuFFT's `cufftPlanMany` with `R2C` / `D2Z` (forward) and
//! `C2R` / `Z2D` (inverse) for `rank`-D transforms (trailblazer:
//! `rank` in `1..=3`).
//!
//! Layout contract: the transformed axes are the **trailing** `rank`
//! axes; anything earlier is flattened into the cuFFT `batch`. cuFFT's
//! Hermitian-half convention applies to the *last* transformed axis
//! only — the complex side has extent `dims[rank-1] / 2 + 1` along
//! that axis, with the earlier transformed axes carrying their full
//! length on both sides. So the buffer sizes are:
//!
//! - Real side:    `batch * dims[0] * dims[1] * ... * dims[rank-1]`
//! - Complex side: `batch * dims[0] * dims[1] * ... * (dims[rank-1]/2 + 1)`
//!
//! Normalization: forward unnormalized; inverse divided by
//! `N = product(dims[..rank])` (the *real* element count, matching
//! PyTorch's `norm="backward"`).

use core::cell::Cell;
use core::ffi::c_void;
use core::marker::PhantomData;

use baracuda_cutlass::{Error, Result};
use baracuda_driver::{DeviceSlice, DeviceSliceMut, Stream};
use baracuda_kernels_sys::{
    baracuda_kernels_scale_inplace_real_f32_run, baracuda_kernels_scale_inplace_real_f64_run,
    cufftComplex, cufftDestroy, cufftDoubleComplex, cufftExecC2R, cufftExecD2Z, cufftExecR2C,
    cufftExecZ2D, cufftHandle, cufftPlanMany, cufftSetStream, CUFFT_C2R, CUFFT_D2Z, CUFFT_R2C,
    CUFFT_Z2D,
};
use baracuda_kernels_types::{
    ArchSku, BackendKind, Complex32, Complex64, Element, ElementKind, FftKind, KernelSku,
    MathPrecision, OpCategory, PlanPreference, PrecisionGuarantee, Workspace,
};

use super::fft::{cufft_to_status, map_status};

const HANDLE_UNINIT: cufftHandle = -1;
const MAX_RANK: usize = 4;

// =============================================================================
// RFFT-ND — real → complex
// =============================================================================

/// Descriptor for an ND RFFT.
///
/// `dims[..rank]` are the per-axis transform extents — *as measured on
/// the real side* (the last-axis Hermitian-half halving is implicit on
/// the complex output). `batch` is the cuFFT batch.
#[derive(Copy, Clone, Debug)]
pub struct RfftNdDescriptor {
    /// Real-side per-axis extents for the transformed axes
    /// (only `dims[..rank]` is read).
    pub dims: [i32; MAX_RANK],
    /// Number of transformed axes. `1..=3` supported by the trailblazer.
    pub rank: u8,
    /// Number of independent transforms.
    pub batch: i32,
    /// Real-side element type — `F32` / `F64`.
    pub element: ElementKind,
}

impl RfftNdDescriptor {
    /// Real-side element count per batched transform.
    #[inline]
    pub fn real_numel(&self) -> i64 {
        let mut n: i64 = 1;
        for i in 0..self.rank as usize {
            n = n.saturating_mul(self.dims[i] as i64);
        }
        n
    }

    /// Complex-side element count per batched transform — same as
    /// `real_numel` except the last transformed axis is replaced by
    /// `dims[rank-1] / 2 + 1`.
    #[inline]
    pub fn complex_numel(&self) -> i64 {
        let rank = self.rank as usize;
        if rank == 0 {
            return 1;
        }
        let mut n: i64 = 1;
        for i in 0..rank - 1 {
            n = n.saturating_mul(self.dims[i] as i64);
        }
        n = n.saturating_mul((self.dims[rank - 1] / 2 + 1) as i64);
        n
    }
}

/// Args for an ND RFFT. `T` is the real type, `C` the matching complex.
pub struct RfftNdArgs<'a, T: Element, C: Element> {
    /// Real input — `batch * product(dims[..rank])` cells.
    pub x: DeviceSlice<'a, T>,
    /// Complex output — `batch * complex_numel()` cells.
    pub y: DeviceSliceMut<'a, C>,
}

/// Multi-dimensional RFFT plan — real → Hermitian-half complex.
///
/// Wraps cuFFT's `cufftPlanMany` with `R2C` / `D2Z`. The transformed
/// axes are the trailing `rank` axes of the operand; the Hermitian-half
/// halving is applied to the **last** transformed axis only.
///
/// **When to use**: ND forward FFT of real-valued data. Permute to
/// move transform axes into the trailing suffix if needed.
///
/// **Dtypes**: `f32` → `Complex32`; `f64` → `Complex64`.
///
/// **Shape**: real-side `batch * product(dims[..rank])`; complex-side
/// `batch * product(dims[..rank-1]) * (dims[rank-1]/2 + 1)`. Rank in
/// `1..=3` is wired.
///
/// **Normalization**: unnormalized (`norm="backward"`).
///
/// **Workspace**: zero — cuFFT manages internal workspace.
///
/// **Precision guarantee**: deterministic; not bit-stable across
/// cuFFT versions.
///
/// Owns a lazy cuFFT handle (`!Sync` / `!Send`); destroyed on `Drop`.
pub struct RfftNdPlan<T: Element> {
    desc: RfftNdDescriptor,
    sku: KernelSku,
    handle: Cell<cufftHandle>,
    _marker: PhantomData<T>,
}

impl<T: Element> RfftNdPlan<T> {
    /// Pick a kernel + validate the descriptor.
    pub fn select(
        _stream: &Stream,
        desc: &RfftNdDescriptor,
        _pref: PlanPreference,
    ) -> Result<Self> {
        if desc.element != T::KIND {
            return Err(Error::Unsupported(
                "baracuda-kernels::RfftNdPlan: descriptor.element != T::KIND",
            ));
        }
        if !matches!(T::KIND, ElementKind::F32 | ElementKind::F64) {
            return Err(Error::Unsupported(
                "baracuda-kernels::RfftNdPlan: R2C ND FFT supports f32 + f64 only",
            ));
        }
        if !(1..=3).contains(&desc.rank) {
            return Err(Error::Unsupported(
                "baracuda-kernels::RfftNdPlan: rank must be in 1..=3 (trailblazer)",
            ));
        }
        if desc.batch <= 0 {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::RfftNdPlan: batch must be > 0",
            ));
        }
        for i in 0..desc.rank as usize {
            if desc.dims[i] <= 0 {
                return Err(Error::InvalidProblem(
                    "baracuda-kernels::RfftNdPlan: every transformed-axis dim must be > 0",
                ));
            }
        }
        let math_precision = match T::KIND {
            ElementKind::F64 => MathPrecision::F64,
            _ => MathPrecision::F32,
        };
        let aux = match T::KIND {
            ElementKind::F32 => Some(ElementKind::Complex32),
            ElementKind::F64 => Some(ElementKind::Complex64),
            _ => None,
        };
        let precision_guarantee = PrecisionGuarantee {
            math_precision,
            accumulator: T::KIND,
            bit_stable_on_same_hardware: false,
            deterministic: true,
        };
        let sku = KernelSku {
            category: OpCategory::Fft,
            op: FftKind::Rfft as u16,
            element: T::KIND,
            aux_element: aux,
            layout: None,
            epilogue: None,
            arch: ArchSku::Sm80,
            backend: BackendKind::Cufft,
            precision_guarantee,
        };
        Ok(Self {
            desc: *desc,
            sku,
            handle: Cell::new(HANDLE_UNINIT),
            _marker: PhantomData,
        })
    }

    /// Kernel SKU identity.
    #[inline]
    pub fn sku(&self) -> KernelSku {
        self.sku
    }

    /// Numerical guarantees.
    #[inline]
    pub fn precision_guarantee(&self) -> PrecisionGuarantee {
        self.sku.precision_guarantee
    }

    /// Workspace size in bytes.
    #[inline]
    pub fn workspace_size(&self) -> usize {
        0
    }

    fn ensure_handle(&self) -> Result<cufftHandle> {
        let h = self.handle.get();
        if h != HANDLE_UNINIT {
            return Ok(h);
        }
        let fft_type = match T::KIND {
            ElementKind::F32 => CUFFT_R2C,
            ElementKind::F64 => CUFFT_D2Z,
            _ => unreachable!("select() gates on F32 / F64"),
        };
        let rank = self.desc.rank as i32;
        let mut n: [i32; MAX_RANK] = self.desc.dims;
        let real_dist = self.desc.real_numel() as i32;
        let complex_dist = self.desc.complex_numel() as i32;
        let mut handle: cufftHandle = HANDLE_UNINIT;
        // Default-layout R2C: input distance is the real numel,
        // output distance is the complex (Hermitian-half) numel.
        let status = unsafe {
            cufftPlanMany(
                &mut handle as *mut _,
                rank,
                n.as_mut_ptr(),
                core::ptr::null_mut(),
                1,
                real_dist,
                core::ptr::null_mut(),
                1,
                complex_dist,
                fft_type,
                self.desc.batch,
            )
        };
        if status != 0 {
            return Err(Error::CutlassInternal(cufft_to_status(status)));
        }
        self.handle.set(handle);
        Ok(handle)
    }

    fn bind_stream(&self, handle: cufftHandle, stream: &Stream) -> Result<()> {
        let stream_ptr = stream.as_raw() as *mut c_void;
        let status = unsafe { cufftSetStream(handle, stream_ptr) };
        if status != 0 {
            return Err(Error::CutlassInternal(cufft_to_status(status)));
        }
        Ok(())
    }
}

impl RfftNdPlan<f32> {
    /// Run the ND R2C FFT (single precision).
    pub fn run(
        &self,
        stream: &Stream,
        _workspace: Workspace<'_>,
        args: RfftNdArgs<'_, f32, Complex32>,
    ) -> Result<()> {
        let real_total = self.desc.real_numel().saturating_mul(self.desc.batch as i64);
        let complex_total = self
            .desc
            .complex_numel()
            .saturating_mul(self.desc.batch as i64);
        if (args.x.len() as i64) < real_total {
            return Err(Error::BufferTooSmall {
                needed: real_total as usize,
                got: args.x.len(),
            });
        }
        if (args.y.len() as i64) < complex_total {
            return Err(Error::BufferTooSmall {
                needed: complex_total as usize,
                got: args.y.len(),
            });
        }
        if real_total == 0 {
            return Ok(());
        }

        let handle = self.ensure_handle()?;
        self.bind_stream(handle, stream)?;

        let idata = args.x.as_raw().0 as *mut f32;
        let odata = args.y.as_raw().0 as *mut cufftComplex;
        let status = unsafe { cufftExecR2C(handle, idata, odata) };
        if status != 0 {
            return Err(Error::CutlassInternal(cufft_to_status(status)));
        }
        Ok(())
    }
}

impl RfftNdPlan<f64> {
    /// Run the ND R2C FFT (double precision).
    pub fn run(
        &self,
        stream: &Stream,
        _workspace: Workspace<'_>,
        args: RfftNdArgs<'_, f64, Complex64>,
    ) -> Result<()> {
        let real_total = self.desc.real_numel().saturating_mul(self.desc.batch as i64);
        let complex_total = self
            .desc
            .complex_numel()
            .saturating_mul(self.desc.batch as i64);
        if (args.x.len() as i64) < real_total {
            return Err(Error::BufferTooSmall {
                needed: real_total as usize,
                got: args.x.len(),
            });
        }
        if (args.y.len() as i64) < complex_total {
            return Err(Error::BufferTooSmall {
                needed: complex_total as usize,
                got: args.y.len(),
            });
        }
        if real_total == 0 {
            return Ok(());
        }

        let handle = self.ensure_handle()?;
        self.bind_stream(handle, stream)?;

        let idata = args.x.as_raw().0 as *mut f64;
        let odata = args.y.as_raw().0 as *mut cufftDoubleComplex;
        let status = unsafe { cufftExecD2Z(handle, idata, odata) };
        if status != 0 {
            return Err(Error::CutlassInternal(cufft_to_status(status)));
        }
        Ok(())
    }
}

impl<T: Element> Drop for RfftNdPlan<T> {
    fn drop(&mut self) {
        let h = self.handle.get();
        if h != HANDLE_UNINIT {
            unsafe {
                let _ = cufftDestroy(h);
            }
            self.handle.set(HANDLE_UNINIT);
        }
    }
}

// =============================================================================
// IRFFT-ND — complex → real
// =============================================================================

/// Descriptor for an ND IRFFT.
///
/// `dims[..rank]` are the *real-side* per-axis extents — the complex
/// input has its last transformed axis halved (`dims[rank-1]/2 + 1`).
/// cuFFT cannot infer the real-side last-axis length from the complex
/// shape alone, so `dims[rank-1]` is required.
#[derive(Copy, Clone, Debug)]
pub struct IrfftNdDescriptor {
    /// Real-side per-axis extents.
    pub dims: [i32; MAX_RANK],
    /// Number of transformed axes. `1..=3`.
    pub rank: u8,
    /// Number of independent transforms.
    pub batch: i32,
    /// Real-side element type (output dtype).
    pub element: ElementKind,
}

impl IrfftNdDescriptor {
    /// Real-side element count per batched transform.
    #[inline]
    pub fn real_numel(&self) -> i64 {
        let mut n: i64 = 1;
        for i in 0..self.rank as usize {
            n = n.saturating_mul(self.dims[i] as i64);
        }
        n
    }

    /// Complex-side element count per batched transform.
    #[inline]
    pub fn complex_numel(&self) -> i64 {
        let rank = self.rank as usize;
        if rank == 0 {
            return 1;
        }
        let mut n: i64 = 1;
        for i in 0..rank - 1 {
            n = n.saturating_mul(self.dims[i] as i64);
        }
        n = n.saturating_mul((self.dims[rank - 1] / 2 + 1) as i64);
        n
    }
}

/// Args for an ND IRFFT.
pub struct IrfftNdArgs<'a, T: Element, C: Element> {
    /// Complex input — `batch * complex_numel()` cells.
    pub x: DeviceSlice<'a, C>,
    /// Real output — `batch * real_numel()` cells.
    pub y: DeviceSliceMut<'a, T>,
}

/// Multi-dimensional IRFFT plan — Hermitian-half complex → real.
///
/// Wraps cuFFT's `cufftPlanMany` with `C2R` / `Z2D`, followed by a
/// `scale_inplace_real_*` launch for the `1/N` normalization. The
/// real-side extent of the last transformed axis is **explicit** in
/// the descriptor (cannot be inferred from the Hermitian half).
///
/// **When to use**: ND inverse FFT producing real-valued data.
///
/// **Dtypes**: `Complex32` → `f32`; `Complex64` → `f64`.
///
/// **Shape**: complex-side `batch * product(dims[..rank-1]) *
/// (dims[rank-1]/2 + 1)`; real-side `batch * product(dims[..rank])`.
///
/// **Normalization**: divided by `N = product(dims[..rank])` (real
/// element count; PyTorch `norm="backward"`).
///
/// **Workspace**: zero — cuFFT manages internal workspace.
///
/// **Precision guarantee**: deterministic; not bit-stable across
/// cuFFT versions.
///
/// Owns a lazy cuFFT handle (`!Sync` / `!Send`); destroyed on `Drop`.
pub struct IrfftNdPlan<T: Element> {
    desc: IrfftNdDescriptor,
    sku: KernelSku,
    handle: Cell<cufftHandle>,
    _marker: PhantomData<T>,
}

impl<T: Element> IrfftNdPlan<T> {
    /// Pick a kernel + validate the descriptor.
    pub fn select(
        _stream: &Stream,
        desc: &IrfftNdDescriptor,
        _pref: PlanPreference,
    ) -> Result<Self> {
        if desc.element != T::KIND {
            return Err(Error::Unsupported(
                "baracuda-kernels::IrfftNdPlan: descriptor.element != T::KIND",
            ));
        }
        if !matches!(T::KIND, ElementKind::F32 | ElementKind::F64) {
            return Err(Error::Unsupported(
                "baracuda-kernels::IrfftNdPlan: C2R ND FFT supports f32 + f64 only",
            ));
        }
        if !(1..=3).contains(&desc.rank) {
            return Err(Error::Unsupported(
                "baracuda-kernels::IrfftNdPlan: rank must be in 1..=3 (trailblazer)",
            ));
        }
        if desc.batch <= 0 {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::IrfftNdPlan: batch must be > 0",
            ));
        }
        for i in 0..desc.rank as usize {
            if desc.dims[i] <= 0 {
                return Err(Error::InvalidProblem(
                    "baracuda-kernels::IrfftNdPlan: every transformed-axis dim must be > 0",
                ));
            }
        }
        let math_precision = match T::KIND {
            ElementKind::F64 => MathPrecision::F64,
            _ => MathPrecision::F32,
        };
        let aux = match T::KIND {
            ElementKind::F32 => Some(ElementKind::Complex32),
            ElementKind::F64 => Some(ElementKind::Complex64),
            _ => None,
        };
        let precision_guarantee = PrecisionGuarantee {
            math_precision,
            accumulator: T::KIND,
            bit_stable_on_same_hardware: false,
            deterministic: true,
        };
        let sku = KernelSku {
            category: OpCategory::Fft,
            op: FftKind::Irfft as u16,
            element: T::KIND,
            aux_element: aux,
            layout: None,
            epilogue: None,
            arch: ArchSku::Sm80,
            backend: BackendKind::Cufft,
            precision_guarantee,
        };
        Ok(Self {
            desc: *desc,
            sku,
            handle: Cell::new(HANDLE_UNINIT),
            _marker: PhantomData,
        })
    }

    /// Kernel SKU identity.
    #[inline]
    pub fn sku(&self) -> KernelSku {
        self.sku
    }

    /// Numerical guarantees.
    #[inline]
    pub fn precision_guarantee(&self) -> PrecisionGuarantee {
        self.sku.precision_guarantee
    }

    /// Workspace size in bytes.
    #[inline]
    pub fn workspace_size(&self) -> usize {
        0
    }

    fn ensure_handle(&self) -> Result<cufftHandle> {
        let h = self.handle.get();
        if h != HANDLE_UNINIT {
            return Ok(h);
        }
        let fft_type = match T::KIND {
            ElementKind::F32 => CUFFT_C2R,
            ElementKind::F64 => CUFFT_Z2D,
            _ => unreachable!("select() gates on F32 / F64"),
        };
        let rank = self.desc.rank as i32;
        let mut n: [i32; MAX_RANK] = self.desc.dims;
        let real_dist = self.desc.real_numel() as i32;
        let complex_dist = self.desc.complex_numel() as i32;
        let mut handle: cufftHandle = HANDLE_UNINIT;
        // Default-layout C2R: input distance is the complex
        // (Hermitian-half) numel, output distance is the real numel.
        let status = unsafe {
            cufftPlanMany(
                &mut handle as *mut _,
                rank,
                n.as_mut_ptr(),
                core::ptr::null_mut(),
                1,
                complex_dist,
                core::ptr::null_mut(),
                1,
                real_dist,
                fft_type,
                self.desc.batch,
            )
        };
        if status != 0 {
            return Err(Error::CutlassInternal(cufft_to_status(status)));
        }
        self.handle.set(handle);
        Ok(handle)
    }

    fn bind_stream(&self, handle: cufftHandle, stream: &Stream) -> Result<()> {
        let stream_ptr = stream.as_raw() as *mut c_void;
        let status = unsafe { cufftSetStream(handle, stream_ptr) };
        if status != 0 {
            return Err(Error::CutlassInternal(cufft_to_status(status)));
        }
        Ok(())
    }
}

impl IrfftNdPlan<f32> {
    /// Run the ND C2R FFT (single precision). Normalizes by
    /// `1/product(dims[..rank])` to match PyTorch's `norm="backward"`.
    pub fn run(
        &self,
        stream: &Stream,
        _workspace: Workspace<'_>,
        args: IrfftNdArgs<'_, f32, Complex32>,
    ) -> Result<()> {
        let real_total = self.desc.real_numel().saturating_mul(self.desc.batch as i64);
        let complex_total = self
            .desc
            .complex_numel()
            .saturating_mul(self.desc.batch as i64);
        if (args.x.len() as i64) < complex_total {
            return Err(Error::BufferTooSmall {
                needed: complex_total as usize,
                got: args.x.len(),
            });
        }
        if (args.y.len() as i64) < real_total {
            return Err(Error::BufferTooSmall {
                needed: real_total as usize,
                got: args.y.len(),
            });
        }
        if real_total == 0 {
            return Ok(());
        }

        let handle = self.ensure_handle()?;
        self.bind_stream(handle, stream)?;

        let idata = args.x.as_raw().0 as *mut cufftComplex;
        let odata = args.y.as_raw().0 as *mut f32;
        let status = unsafe { cufftExecC2R(handle, idata, odata) };
        if status != 0 {
            return Err(Error::CutlassInternal(cufft_to_status(status)));
        }

        let n = self.desc.real_numel() as f32;
        let scale = 1.0_f32 / n;
        let stream_ptr = stream.as_raw() as *mut c_void;
        let s = unsafe {
            baracuda_kernels_scale_inplace_real_f32_run(
                real_total,
                scale,
                odata as *mut c_void,
                core::ptr::null_mut(),
                0,
                stream_ptr,
            )
        };
        map_status(s)
    }
}

impl IrfftNdPlan<f64> {
    /// Run the ND C2R FFT (double precision).
    pub fn run(
        &self,
        stream: &Stream,
        _workspace: Workspace<'_>,
        args: IrfftNdArgs<'_, f64, Complex64>,
    ) -> Result<()> {
        let real_total = self.desc.real_numel().saturating_mul(self.desc.batch as i64);
        let complex_total = self
            .desc
            .complex_numel()
            .saturating_mul(self.desc.batch as i64);
        if (args.x.len() as i64) < complex_total {
            return Err(Error::BufferTooSmall {
                needed: complex_total as usize,
                got: args.x.len(),
            });
        }
        if (args.y.len() as i64) < real_total {
            return Err(Error::BufferTooSmall {
                needed: real_total as usize,
                got: args.y.len(),
            });
        }
        if real_total == 0 {
            return Ok(());
        }

        let handle = self.ensure_handle()?;
        self.bind_stream(handle, stream)?;

        let idata = args.x.as_raw().0 as *mut cufftDoubleComplex;
        let odata = args.y.as_raw().0 as *mut f64;
        let status = unsafe { cufftExecZ2D(handle, idata, odata) };
        if status != 0 {
            return Err(Error::CutlassInternal(cufft_to_status(status)));
        }

        let n = self.desc.real_numel() as f64;
        let scale = 1.0_f64 / n;
        let stream_ptr = stream.as_raw() as *mut c_void;
        let s = unsafe {
            baracuda_kernels_scale_inplace_real_f64_run(
                real_total,
                scale,
                odata as *mut c_void,
                core::ptr::null_mut(),
                0,
                stream_ptr,
            )
        };
        map_status(s)
    }
}

impl<T: Element> Drop for IrfftNdPlan<T> {
    fn drop(&mut self) {
        let h = self.handle.get();
        if h != HANDLE_UNINIT {
            unsafe {
                let _ = cufftDestroy(h);
            }
            self.handle.set(HANDLE_UNINIT);
        }
    }
}