baracuda-cufft 0.0.1-alpha.68

Safe Rust wrappers for NVIDIA cuFFT.
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
//! Safe Rust wrappers for NVIDIA cuFFT.
//!
//! v0.1 covers `cufftPlan1d`/`cufftPlan2d`/`cufftPlan3d` and the R2C/C2R/C2C
//! single-precision transforms. Multi-GPU (`cufftXt`) and batched
//! descriptor-style plans land in a follow-up.
//!
//! ```no_run
//! use baracuda_driver::{Context, Device, DeviceBuffer};
//! use baracuda_cufft::{Plan1d, Transform};
//!
//! # fn demo() -> Result<(), Box<dyn std::error::Error>> {
//! let device = Device::get(0)?;
//! let ctx = Context::new(&device)?;
//! let host: Vec<f32> = (0..1024).map(|i| (i as f32 * 0.05).sin()).collect();
//! let mut d_in = DeviceBuffer::from_slice(&ctx, &host)?;
//! let mut d_out: DeviceBuffer<baracuda_types::Complex32> =
//!     DeviceBuffer::new(&ctx, host.len() / 2 + 1)?;
//!
//! let plan = Plan1d::new(host.len() as i32, Transform::R2C, 1)?;
//! plan.exec_r2c(&mut d_in, &mut d_out)?;
//! # Ok(()) }
//! ```

#![warn(missing_debug_implementations)]

use baracuda_cufft_sys::{
    cufft, cufftComplex, cufftDoubleComplex, cufftHandle, cufftResult, cufftType,
};
use baracuda_driver::{DeviceBuffer, Stream};
use baracuda_types::{Complex32, Complex64};

/// Error type for cuFFT operations.
pub type Error = baracuda_core::Error<cufftResult>;
/// Result alias.
pub type Result<T, E = Error> = core::result::Result<T, E>;

#[inline]
fn check(status: cufftResult) -> Result<()> {
    Error::check(status)
}

/// Transform kind.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Transform {
    /// Real → Complex (forward), f32.
    R2C,
    /// Complex → Real (inverse), f32.
    C2R,
    /// Complex → Complex (f32, direction passed at exec time).
    C2C,
    /// Double Real → Complex (forward), f64.
    D2Z,
    /// Complex → Double Real (inverse), f64.
    Z2D,
    /// Complex → Complex (f64, direction passed at exec time).
    Z2Z,
}

impl Transform {
    fn raw(self) -> cufftType {
        match self {
            Transform::R2C => cufftType::R2C,
            Transform::C2R => cufftType::C2R,
            Transform::C2C => cufftType::C2C,
            Transform::D2Z => cufftType::D2Z,
            Transform::Z2D => cufftType::Z2D,
            Transform::Z2Z => cufftType::Z2Z,
        }
    }
}

/// Direction for `C2C` transforms.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub enum Direction {
    /// Forward transform (`exp(-2πi…)`).
    #[default]
    Forward,
    /// Inverse / unnormalized backward transform (`exp(+2πi…)`).
    Inverse,
}

impl Direction {
    fn raw(self) -> core::ffi::c_int {
        match self {
            Direction::Forward => baracuda_cufft_sys::CUFFT_FORWARD,
            Direction::Inverse => baracuda_cufft_sys::CUFFT_INVERSE,
        }
    }
}

/// A 1-D cuFFT plan.
pub struct Plan1d {
    handle: cufftHandle,
}

unsafe impl Send for Plan1d {}

impl core::fmt::Debug for Plan1d {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Plan1d")
            .field("handle", &self.handle)
            .finish()
    }
}

impl Plan1d {
    /// Create a 1-D plan of length `nx` and `batch` parallel transforms.
    ///
    /// # Example
    ///
    /// A single forward R2C transform of length 1024.
    ///
    /// ```no_run
    /// use baracuda_driver::{Context, Device, DeviceBuffer};
    /// use baracuda_cufft::{Plan1d, Transform};
    /// use baracuda_types::Complex32;
    ///
    /// # fn demo() -> Result<(), Box<dyn std::error::Error>> {
    /// let ctx = Context::new(&Device::get(0)?)?;
    /// let n = 1024;
    ///
    /// let mut input:  DeviceBuffer<f32>       = DeviceBuffer::zeros(&ctx, n)?;
    /// let mut output: DeviceBuffer<Complex32> = DeviceBuffer::new(&ctx, n / 2 + 1)?;
    ///
    /// let plan = Plan1d::new(n as i32, Transform::R2C, 1)?;
    /// plan.exec_r2c(&mut input, &mut output)?;
    /// # Ok(()) }
    /// ```
    pub fn new(nx: i32, transform: Transform, batch: i32) -> Result<Self> {
        let c = cufft()?;
        let cu = c.cufft_plan_1d()?;
        let mut plan: cufftHandle = 0;
        check(unsafe { cu(&mut plan, nx, transform.raw(), batch) })?;
        Ok(Self { handle: plan })
    }

    /// Bind subsequent exec calls on this plan to `stream`.
    pub fn set_stream(&self, stream: &Stream) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_set_stream()?;
        check(unsafe { cu(self.handle, stream.as_raw() as _) })
    }

    /// Execute a real-to-complex transform.
    pub fn exec_r2c(
        &self,
        input: &mut DeviceBuffer<f32>,
        output: &mut DeviceBuffer<Complex32>,
    ) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_exec_r2c()?;
        check(unsafe {
            cu(
                self.handle,
                input.as_raw().0 as *mut f32,
                output.as_raw().0 as *mut cufftComplex,
            )
        })
    }

    /// Execute a complex-to-real transform.
    ///
    /// Plan must have been built with [`Transform::C2R`]. cuFFT inverse R2C
    /// transforms are unnormalised — divide by `n` to recover the original
    /// signal.
    ///
    /// # Example
    ///
    /// Round-trip a length-1024 real signal through R2C then C2R.
    ///
    /// ```no_run
    /// use baracuda_driver::{Context, Device, DeviceBuffer};
    /// use baracuda_cufft::{Plan1d, Transform};
    /// use baracuda_types::Complex32;
    ///
    /// # fn demo() -> Result<(), Box<dyn std::error::Error>> {
    /// let ctx = Context::new(&Device::get(0)?)?;
    /// let n = 1024;
    ///
    /// let mut signal: DeviceBuffer<f32>       = DeviceBuffer::zeros(&ctx, n)?;
    /// let mut spectrum: DeviceBuffer<Complex32> = DeviceBuffer::new(&ctx, n / 2 + 1)?;
    /// let mut recovered: DeviceBuffer<f32>    = DeviceBuffer::zeros(&ctx, n)?;
    ///
    /// let fwd = Plan1d::new(n as i32, Transform::R2C, 1)?;
    /// let inv = Plan1d::new(n as i32, Transform::C2R, 1)?;
    /// fwd.exec_r2c(&mut signal, &mut spectrum)?;
    /// inv.exec_c2r(&mut spectrum, &mut recovered)?;
    /// // `recovered` now holds signal * n; divide by n for the original.
    /// # Ok(()) }
    /// ```
    pub fn exec_c2r(
        &self,
        input: &mut DeviceBuffer<Complex32>,
        output: &mut DeviceBuffer<f32>,
    ) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_exec_c2r()?;
        check(unsafe {
            cu(
                self.handle,
                input.as_raw().0 as *mut cufftComplex,
                output.as_raw().0 as *mut f32,
            )
        })
    }

    /// Execute a complex-to-complex transform in the given direction.
    pub fn exec_c2c(
        &self,
        input: &mut DeviceBuffer<Complex32>,
        output: &mut DeviceBuffer<Complex32>,
        direction: Direction,
    ) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_exec_c2c()?;
        check(unsafe {
            cu(
                self.handle,
                input.as_raw().0 as *mut cufftComplex,
                output.as_raw().0 as *mut cufftComplex,
                direction.raw(),
            )
        })
    }

    /// Raw `cufftHandle`. Use with care.
    #[inline]
    pub fn as_raw(&self) -> cufftHandle {
        self.handle
    }
}

impl Drop for Plan1d {
    fn drop(&mut self) {
        if let Ok(c) = cufft() {
            if let Ok(cu) = c.cufft_destroy() {
                let _ = unsafe { cu(self.handle) };
            }
        }
    }
}

/// A 2-D cuFFT plan.
pub struct Plan2d {
    handle: cufftHandle,
}

unsafe impl Send for Plan2d {}

impl core::fmt::Debug for Plan2d {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Plan2d")
            .field("handle", &self.handle)
            .finish()
    }
}

impl Plan2d {
    /// Create a 2-D plan of dimensions `nx × ny`.
    ///
    /// # Example
    ///
    /// Forward 2-D C2C FFT of a 128×128 complex image.
    ///
    /// ```no_run
    /// use baracuda_driver::{Context, Device, DeviceBuffer};
    /// use baracuda_cufft::{Direction, Plan2d, Transform};
    /// use baracuda_types::Complex32;
    ///
    /// # fn demo() -> Result<(), Box<dyn std::error::Error>> {
    /// let ctx = Context::new(&Device::get(0)?)?;
    /// let (nx, ny) = (128, 128);
    ///
    /// let mut img:      DeviceBuffer<Complex32> = DeviceBuffer::new(&ctx, (nx * ny) as usize)?;
    /// let mut spectrum: DeviceBuffer<Complex32> = DeviceBuffer::new(&ctx, (nx * ny) as usize)?;
    ///
    /// let plan = Plan2d::new(nx, ny, Transform::C2C)?;
    /// plan.exec_c2c(&mut img, &mut spectrum, Direction::Forward)?;
    /// # Ok(()) }
    /// ```
    pub fn new(nx: i32, ny: i32, transform: Transform) -> Result<Self> {
        let c = cufft()?;
        let cu = c.cufft_plan_2d()?;
        let mut plan: cufftHandle = 0;
        check(unsafe { cu(&mut plan, nx, ny, transform.raw()) })?;
        Ok(Self { handle: plan })
    }

    /// Execute a complex-to-complex 2D transform.
    pub fn exec_c2c(
        &self,
        input: &mut DeviceBuffer<Complex32>,
        output: &mut DeviceBuffer<Complex32>,
        direction: Direction,
    ) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_exec_c2c()?;
        check(unsafe {
            cu(
                self.handle,
                input.as_raw().0 as *mut cufftComplex,
                output.as_raw().0 as *mut cufftComplex,
                direction.raw(),
            )
        })
    }

    /// Raw handle.
    #[inline]
    pub fn as_raw(&self) -> cufftHandle {
        self.handle
    }
}

impl Drop for Plan2d {
    fn drop(&mut self) {
        if let Ok(c) = cufft() {
            if let Ok(cu) = c.cufft_destroy() {
                let _ = unsafe { cu(self.handle) };
            }
        }
    }
}

/// Owned 3-D cuFFT plan.
pub struct Plan3d {
    handle: cufftHandle,
}

unsafe impl Send for Plan3d {}

impl core::fmt::Debug for Plan3d {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Plan3d")
            .field("handle", &self.handle)
            .finish()
    }
}

impl Plan3d {
    /// Create a 3-D plan of dimensions `nx × ny × nz`.
    pub fn new(nx: i32, ny: i32, nz: i32, transform: Transform) -> Result<Self> {
        let c = cufft()?;
        let cu = c.cufft_plan_3d()?;
        let mut plan: cufftHandle = 0;
        check(unsafe { cu(&mut plan, nx, ny, nz, transform.raw()) })?;
        Ok(Self { handle: plan })
    }

    /// Bind subsequent exec calls on this plan to `stream`.
    pub fn set_stream(&self, stream: &Stream) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_set_stream()?;
        check(unsafe { cu(self.handle, stream.as_raw() as _) })
    }

    /// Execute a 3-D complex-to-complex transform in the given direction.
    pub fn exec_c2c(
        &self,
        input: &mut DeviceBuffer<Complex32>,
        output: &mut DeviceBuffer<Complex32>,
        direction: Direction,
    ) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_exec_c2c()?;
        check(unsafe {
            cu(
                self.handle,
                input.as_raw().0 as *mut cufftComplex,
                output.as_raw().0 as *mut cufftComplex,
                direction.raw(),
            )
        })
    }

    /// Raw `cufftHandle`. Use with care.
    #[inline]
    pub fn as_raw(&self) -> cufftHandle {
        self.handle
    }
}

impl Drop for Plan3d {
    fn drop(&mut self) {
        if let Ok(c) = cufft() {
            if let Ok(cu) = c.cufft_destroy() {
                let _ = unsafe { cu(self.handle) };
            }
        }
    }
}

/// cuFFT library version, e.g. `11300` for cuFFT 11.3.0.
pub fn version() -> Result<i32> {
    let c = cufft()?;
    let cu = c.cufft_get_version()?;
    let mut v: core::ffi::c_int = 0;
    check(unsafe { cu(&mut v) })?;
    Ok(v)
}

// =======================================================================
// Double-precision exec + PlanMany (batched) + XT multi-GPU
// =======================================================================

macro_rules! exec_z_impls {
    ($plan:ty) => {
        impl $plan {
            /// Execute D → Z (double-precision R2C). Plan must have been
            /// built with `Transform::D2Z`.
            pub fn exec_d2z(
                &self,
                input: &mut DeviceBuffer<f64>,
                output: &mut DeviceBuffer<Complex64>,
            ) -> Result<()> {
                let c = cufft()?;
                let cu = c.cufft_exec_d2z()?;
                check(unsafe {
                    cu(
                        self.handle,
                        input.as_raw().0 as *mut f64,
                        output.as_raw().0 as *mut cufftDoubleComplex,
                    )
                })
            }

            /// Execute Z → D (double-precision C2R).
            pub fn exec_z2d(
                &self,
                input: &mut DeviceBuffer<Complex64>,
                output: &mut DeviceBuffer<f64>,
            ) -> Result<()> {
                let c = cufft()?;
                let cu = c.cufft_exec_z2d()?;
                check(unsafe {
                    cu(
                        self.handle,
                        input.as_raw().0 as *mut cufftDoubleComplex,
                        output.as_raw().0 as *mut f64,
                    )
                })
            }

            /// Execute Z → Z (double-precision C2C). Direction passed at exec time.
            pub fn exec_z2z(
                &self,
                input: &mut DeviceBuffer<Complex64>,
                output: &mut DeviceBuffer<Complex64>,
                direction: Direction,
            ) -> Result<()> {
                let c = cufft()?;
                let cu = c.cufft_exec_z2z()?;
                check(unsafe {
                    cu(
                        self.handle,
                        input.as_raw().0 as *mut cufftDoubleComplex,
                        output.as_raw().0 as *mut cufftDoubleComplex,
                        direction.raw(),
                    )
                })
            }
        }
    };
}

exec_z_impls!(Plan1d);
exec_z_impls!(Plan2d);

/// A batched / many-rank plan (`cufftPlanMany`). Handles arbitrary
/// rank + advanced-data-layout transforms.
#[derive(Debug)]
pub struct PlanMany {
    handle: cufftHandle,
}

impl PlanMany {
    /// Construct a batched plan. `n[rank]` is the transform shape;
    /// `inembed` / `onembed` are the actual memory layouts of in/out
    /// (pass `None` for packed). `istride`/`ostride` are element strides
    /// between successive elements; `idist`/`odist` are element strides
    /// between successive batches.
    ///
    /// # Example
    ///
    /// 32 packed 1-D R2C transforms of length 256 (e.g., a STFT frame).
    ///
    /// ```no_run
    /// use baracuda_driver::{Context, Device, DeviceBuffer};
    /// use baracuda_cufft::{PlanMany, Transform};
    /// use baracuda_types::Complex32;
    ///
    /// # fn demo() -> Result<(), Box<dyn std::error::Error>> {
    /// let ctx = Context::new(&Device::get(0)?)?;
    /// let n_per = 256i32;
    /// let batch = 32i32;
    /// let mut n = [n_per];
    ///
    /// // Packed contiguous layout: pass None for embeds, strides = 1, dist = transform length.
    /// let plan = PlanMany::new(
    ///     /* rank   */ 1,
    ///     /* n      */ &mut n,
    ///     /* inemb  */ None,
    ///     /* istr   */ 1,
    ///     /* idist  */ n_per,
    ///     /* onemb  */ None,
    ///     /* ostr   */ 1,
    ///     /* odist  */ n_per / 2 + 1,
    ///     /* type   */ Transform::R2C,
    ///     /* batch  */ batch,
    /// )?;
    ///
    /// let mut input:  DeviceBuffer<f32>       =
    ///     DeviceBuffer::zeros(&ctx, (n_per * batch) as usize)?;
    /// let mut output: DeviceBuffer<Complex32> =
    ///     DeviceBuffer::new(&ctx, ((n_per / 2 + 1) * batch) as usize)?;
    /// plan.exec_r2c(&mut input, &mut output)?;
    /// # Ok(()) }
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        rank: i32,
        n: &mut [i32],
        inembed: Option<&mut [i32]>,
        istride: i32,
        idist: i32,
        onembed: Option<&mut [i32]>,
        ostride: i32,
        odist: i32,
        ty: Transform,
        batch: i32,
    ) -> Result<Self> {
        let c = cufft()?;
        let cu = c.cufft_plan_many()?;
        let mut h: cufftHandle = 0;
        check(unsafe {
            cu(
                &mut h,
                rank,
                n.as_mut_ptr(),
                inembed.map_or(core::ptr::null_mut(), |s| s.as_mut_ptr()),
                istride,
                idist,
                onembed.map_or(core::ptr::null_mut(), |s| s.as_mut_ptr()),
                ostride,
                odist,
                ty.raw(),
                batch,
            )
        })?;
        Ok(Self { handle: h })
    }

    /// Raw `cufftHandle`. Use with care.
    #[inline]
    pub fn as_raw(&self) -> cufftHandle {
        self.handle
    }

    /// Bind the plan to a CUDA stream.
    pub fn set_stream(&self, stream: &Stream) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_set_stream()?;
        check(unsafe { cu(self.handle, stream.as_raw() as _) })
    }
}

impl Drop for PlanMany {
    fn drop(&mut self) {
        if let Ok(c) = cufft() {
            if let Ok(cu) = c.cufft_destroy() {
                let _ = unsafe { cu(self.handle) };
            }
        }
    }
}

exec_z_impls!(PlanMany);

impl PlanMany {
    /// Execute R → C (single-precision R2C).
    pub fn exec_r2c(
        &self,
        input: &mut DeviceBuffer<f32>,
        output: &mut DeviceBuffer<Complex32>,
    ) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_exec_r2c()?;
        check(unsafe {
            cu(
                self.handle,
                input.as_raw().0 as *mut f32,
                output.as_raw().0 as *mut cufftComplex,
            )
        })
    }

    /// Execute C → R (single-precision C2R).
    pub fn exec_c2r(
        &self,
        input: &mut DeviceBuffer<Complex32>,
        output: &mut DeviceBuffer<f32>,
    ) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_exec_c2r()?;
        check(unsafe {
            cu(
                self.handle,
                input.as_raw().0 as *mut cufftComplex,
                output.as_raw().0 as *mut f32,
            )
        })
    }

    /// Execute C → C.
    pub fn exec_c2c(
        &self,
        input: &mut DeviceBuffer<Complex32>,
        output: &mut DeviceBuffer<Complex32>,
        direction: Direction,
    ) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_exec_c2c()?;
        check(unsafe {
            cu(
                self.handle,
                input.as_raw().0 as *mut cufftComplex,
                output.as_raw().0 as *mut cufftComplex,
                direction.raw(),
            )
        })
    }
}

/// Sizing estimates (workspace bytes) for a plan shape.
pub fn estimate_1d(nx: i32, ty: Transform, batch: i32) -> Result<usize> {
    let c = cufft()?;
    let cu = c.cufft_estimate_1d()?;
    let mut s: usize = 0;
    check(unsafe { cu(nx, ty.raw(), batch, &mut s) })?;
    Ok(s)
}

/// 2-D workspace-size estimate (bytes) for a plan of the given shape.
/// Wraps `cufftEstimate2d`.
pub fn estimate_2d(nx: i32, ny: i32, ty: Transform) -> Result<usize> {
    let c = cufft()?;
    let cu = c.cufft_estimate_2d()?;
    let mut s: usize = 0;
    check(unsafe { cu(nx, ny, ty.raw(), &mut s) })?;
    Ok(s)
}

/// 3-D workspace-size estimate (bytes) for a plan of the given shape.
/// Wraps `cufftEstimate3d`.
pub fn estimate_3d(nx: i32, ny: i32, nz: i32, ty: Transform) -> Result<usize> {
    let c = cufft()?;
    let cu = c.cufft_estimate_3d()?;
    let mut s: usize = 0;
    check(unsafe { cu(nx, ny, nz, ty.raw(), &mut s) })?;
    Ok(s)
}

/// Multi-GPU (XT) extension helpers. Use these to distribute a cuFFT
/// plan across multiple GPUs via `cufftXtSetGPUs` + `cufftXtExec`.
pub mod xt {
    use super::*;

    /// Spread a plan across `which_gpus` (CUDA device ordinals).
    ///
    /// # Safety
    ///
    /// `plan` must be a fresh (unexecuted) handle; all ordinals in
    /// `which_gpus` must be live CUDA devices.
    pub unsafe fn set_gpus(plan: cufftHandle, which_gpus: &mut [i32]) -> Result<()> { unsafe {
        let c = cufft()?;
        let cu = c.cufft_xt_set_gpus()?;
        check(cu(plan, which_gpus.len() as i32, which_gpus.as_mut_ptr()))
    }}

    /// Allocate a multi-GPU `cudaLibXtDesc*` matching the plan.
    /// Returns an opaque pointer that must be freed with [`free`].
    ///
    /// # Safety
    ///
    /// `plan` must have been configured with [`set_gpus`] first.
    pub unsafe fn malloc(
        plan: cufftHandle,
        subformat: i32,
    ) -> Result<*mut core::ffi::c_void> { unsafe {
        let c = cufft()?;
        let cu = c.cufft_xt_malloc()?;
        let mut desc: *mut core::ffi::c_void = core::ptr::null_mut();
        check(cu(plan, &mut desc, subformat))?;
        Ok(desc)
    }}

    /// Free an XT descriptor from [`malloc`].
    ///
    /// # Safety
    ///
    /// `desc` must come from [`malloc`].
    pub unsafe fn free(desc: *mut core::ffi::c_void) -> Result<()> { unsafe {
        let c = cufft()?;
        let cu = c.cufft_xt_free()?;
        check(cu(desc))
    }}

    /// Multi-GPU memcpy between host / device / XT descriptors.
    ///
    /// # Safety
    ///
    /// Pointer kinds and `ty` must agree.
    pub unsafe fn memcpy(
        plan: cufftHandle,
        dst: *mut core::ffi::c_void,
        src: *mut core::ffi::c_void,
        ty: i32,
    ) -> Result<()> { unsafe {
        let c = cufft()?;
        let cu = c.cufft_xt_memcpy()?;
        check(cu(plan, dst, src, ty))
    }}

    /// Execute the plan on its XT descriptors.
    ///
    /// # Safety
    ///
    /// `input` / `output` must be `cudaLibXtDesc*` pointers matching the plan.
    pub unsafe fn exec_descriptor(
        plan: cufftHandle,
        input: *mut core::ffi::c_void,
        output: *mut core::ffi::c_void,
        direction: Direction,
    ) -> Result<()> { unsafe {
        let c = cufft()?;
        let cu = c.cufft_xt_exec_descriptor()?;
        check(cu(plan, input, output, direction.raw()))
    }}
}

/// Set a user-allocated scratch work area (`cufftSetWorkArea`).
///
/// # Safety
///
/// `plan` must have `SetAutoAllocation(false)` first; `work_area` must
/// be a live device pointer.
pub unsafe fn set_work_area(plan: cufftHandle, work_area: *mut core::ffi::c_void) -> Result<()> { unsafe {
    let c = cufft()?;
    let cu = c.cufft_set_work_area()?;
    check(cu(plan, work_area))
}}

/// Disable / re-enable automatic work-area allocation.
pub fn set_auto_allocation(plan: cufftHandle, auto: bool) -> Result<()> {
    let c = cufft()?;
    let cu = c.cufft_set_auto_allocation()?;
    check(unsafe { cu(plan, if auto { 1 } else { 0 }) })
}

/// Scratch bytes this plan currently needs.
pub fn get_size(plan: cufftHandle) -> Result<usize> {
    let c = cufft()?;
    let cu = c.cufft_get_size()?;
    let mut s: usize = 0;
    check(unsafe { cu(plan, &mut s) })?;
    Ok(s)
}

// ============================================================================
// Two-step plan creation: cufftCreate + cufftMakePlan*
// ============================================================================

/// Generic cuFFT plan that supports the modern two-step creation flow:
/// [`Plan::create`] gets you a fresh handle, then one of the
/// `make_plan_*` methods configures the rank and reports the workspace
/// size — useful when you want to allocate the work area yourself
/// (call [`set_auto_allocation(false)`] before `make_plan_*`).
///
/// Unlike [`Plan1d`] / [`Plan2d`] / [`Plan3d`], this type can hold any
/// rank, including the batched-many / 64-bit-many forms.
pub struct Plan {
    handle: cufftHandle,
}

unsafe impl Send for Plan {}

impl core::fmt::Debug for Plan {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Plan")
            .field("handle", &self.handle)
            .finish()
    }
}

impl Plan {
    /// Allocate a fresh empty plan. The plan is unusable until you
    /// finalize it with one of the `make_plan_*` methods.
    pub fn create() -> Result<Self> {
        let c = cufft()?;
        let cu = c.cufft_create()?;
        let mut plan: cufftHandle = 0;
        check(unsafe { cu(&mut plan) })?;
        Ok(Self { handle: plan })
    }

    /// Finalize as a 1-D plan of length `nx` and `batch` parallel
    /// transforms. Returns the workspace size in bytes.
    pub fn make_plan_1d(&self, nx: i32, transform: Transform, batch: i32) -> Result<usize> {
        let c = cufft()?;
        let cu = c.cufft_make_plan_1d()?;
        let mut size: usize = 0;
        check(unsafe { cu(self.handle, nx, transform.raw(), batch, &mut size) })?;
        Ok(size)
    }

    /// Finalize as a 2-D plan. Returns workspace size in bytes.
    pub fn make_plan_2d(&self, nx: i32, ny: i32, transform: Transform) -> Result<usize> {
        let c = cufft()?;
        let cu = c.cufft_make_plan_2d()?;
        let mut size: usize = 0;
        check(unsafe { cu(self.handle, nx, ny, transform.raw(), &mut size) })?;
        Ok(size)
    }

    /// Finalize as a 3-D plan. Returns workspace size in bytes.
    pub fn make_plan_3d(
        &self,
        nx: i32,
        ny: i32,
        nz: i32,
        transform: Transform,
    ) -> Result<usize> {
        let c = cufft()?;
        let cu = c.cufft_make_plan_3d()?;
        let mut size: usize = 0;
        check(unsafe { cu(self.handle, nx, ny, nz, transform.raw(), &mut size) })?;
        Ok(size)
    }

    /// Finalize as a generic strided/batched plan. Returns workspace
    /// size in bytes.
    ///
    /// # Safety
    ///
    /// `n`, `inembed`, `onembed` must be writable arrays of length
    /// `rank` (cuFFT mutates them in some versions). Pass null for
    /// `inembed` / `onembed` to use defaults.
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn make_plan_many(
        &self,
        rank: i32,
        n: &mut [i32],
        inembed: *mut i32,
        istride: i32,
        idist: i32,
        onembed: *mut i32,
        ostride: i32,
        odist: i32,
        transform: Transform,
        batch: i32,
    ) -> Result<usize> { unsafe {
        assert_eq!(n.len() as i32, rank, "n.len() must equal rank");
        let c = cufft()?;
        let cu = c.cufft_make_plan_many()?;
        let mut size: usize = 0;
        check(cu(
            self.handle,
            rank,
            n.as_mut_ptr(),
            inembed,
            istride,
            idist,
            onembed,
            ostride,
            odist,
            transform.raw(),
            batch,
            &mut size,
        ))?;
        Ok(size)
    }}

    /// 64-bit variant of [`make_plan_many`] — use this when any
    /// dimension or stride exceeds `i32::MAX`.
    ///
    /// # Safety
    ///
    /// Same as [`make_plan_many`].
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn make_plan_many64(
        &self,
        rank: i32,
        n: &mut [i64],
        inembed: *mut i64,
        istride: i64,
        idist: i64,
        onembed: *mut i64,
        ostride: i64,
        odist: i64,
        transform: Transform,
        batch: i64,
    ) -> Result<usize> { unsafe {
        assert_eq!(n.len() as i32, rank, "n.len() must equal rank");
        let c = cufft()?;
        let cu = c.cufft_make_plan_many64()?;
        let mut size: usize = 0;
        check(cu(
            self.handle,
            rank,
            n.as_mut_ptr(),
            inembed,
            istride,
            idist,
            onembed,
            ostride,
            odist,
            transform.raw(),
            batch,
            &mut size,
        ))?;
        Ok(size)
    }}

    /// Bind subsequent exec calls to `stream`.
    pub fn set_stream(&self, stream: &Stream) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_set_stream()?;
        check(unsafe { cu(self.handle, stream.as_raw() as _) })
    }

    /// Raw `cufftHandle`. Use with care.
    #[inline]
    pub fn as_raw(&self) -> cufftHandle {
        self.handle
    }
}

impl Drop for Plan {
    fn drop(&mut self) {
        if let Ok(c) = cufft() {
            if let Ok(cu) = c.cufft_destroy() {
                let _ = unsafe { cu(self.handle) };
            }
        }
    }
}

// ============================================================================
// Callback API (cufftXtSetCallback / Clear / SetCallbackSharedSize)
// ============================================================================

pub mod callback {
    //! Pre/post callbacks attached to a cuFFT plan via the `cufftXt*`
    //! callback entry points. The callback ABI is fixed by NVIDIA: each
    //! callback receives the input/output element index, a caller-info
    //! pointer, and the data; see the cuFFT reference for the exact
    //! signatures by callback type. We expose only the raw setters
    //! because the function-pointer types are PTX-shaped, not regular
    //! `extern "C"` functions — they ship as device-side `__device__`
    //! functions linked into the user's CUBIN.
    use super::*;

    /// Callback type values from the cuFFT header
    /// (`cufftXtCallbackType`).
    #[derive(Copy, Clone, Debug, Eq, PartialEq)]
    #[repr(i32)]
    pub enum CallbackType {
        /// Load callback for complex single precision.
        LoadComplex = 0,
        /// Load callback for complex double precision.
        LoadDoubleComplex = 1,
        /// Load callback for real single precision.
        LoadReal = 2,
        /// Load callback for real double precision.
        LoadDoubleReal = 3,
        /// Store callback for complex single precision.
        StoreComplex = 4,
        /// Store callback for complex double precision.
        StoreDoubleComplex = 5,
        /// Store callback for real single precision.
        StoreReal = 6,
        /// Store callback for real double precision.
        StoreDoubleReal = 7,
    }

    /// Attach a load/store callback to the plan. `callback_routine` is
    /// an array of device function pointers — one per GPU for
    /// multi-GPU plans, otherwise a single-element array.
    /// `caller_info` is parallel; pass null for "no caller info".
    ///
    /// # Safety
    ///
    /// `callback_routine[i]` must be a `__device__` function with the
    /// signature cuFFT expects for `cb_type` (see the cuFFT reference).
    /// The routine and `caller_info` must outlive every plan exec call.
    pub unsafe fn set(
        plan: cufftHandle,
        callback_routine: &mut [*mut core::ffi::c_void],
        cb_type: CallbackType,
        caller_info: &mut [*mut core::ffi::c_void],
    ) -> Result<()> { unsafe {
        assert_eq!(
            callback_routine.len(),
            caller_info.len(),
            "callback_routine and caller_info must have the same length"
        );
        let c = cufft()?;
        let cu = c.cufft_xt_set_callback()?;
        check(cu(
            plan,
            callback_routine.as_mut_ptr(),
            cb_type as i32,
            caller_info.as_mut_ptr(),
        ))
    }}

    /// Detach any previously set callback of `cb_type`.
    pub fn clear(plan: cufftHandle, cb_type: CallbackType) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_xt_clear_callback()?;
        check(unsafe { cu(plan, cb_type as i32) })
    }

    /// Reserve `shared_size` bytes of dynamic shared memory per kernel
    /// for the callback. Maximum permissible value is GPU-dependent.
    pub fn set_shared_size(
        plan: cufftHandle,
        cb_type: CallbackType,
        shared_size: usize,
    ) -> Result<()> {
        let c = cufft()?;
        let cu = c.cufft_xt_set_callback_shared_size()?;
        check(unsafe { cu(plan, cb_type as i32, shared_size) })
    }
}