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
//! # tomoxide-cuda
//!
//! The CUDA backend. It re-uses tomocupy's battle-tested `.cu` kernels through
//! a thin C-ABI shim (see `ffi` and `cuda/shim.cpp`) rather than rewriting
//! them. Compiled only when the **`cuda` feature** is enabled and an NVIDIA
//! toolkit is present; otherwise [`CudaBackend::new`] reports the backend as
//! unavailable so the rest of the workspace still builds and runs (on CPU).
//!
//! ## M4 scope
//! GPU capabilities wired through the shim: the FBP **filter** (`cfunc_filter`,
//! cuFFT), parallel-beam **back-projection** (`cfunc_linerec`), **Fourier**
//! reconstruction (`cfunc_fourierrec`, via the `FourierReconstruct` capability),
//! and **elementwise** dark/flat + minus-log. The `AnalyticReconstruct`
//! capability fuses the analytic chain into a **device-resident** path:
//! `recon(Fbp/Linerec/Fourierrec, &CudaBackend)` uploads the sinogram once,
//! runs pad → filter → crop → back-projection (or pack → fourierrec → unpack)
//! all on the device, and downloads the volume once — no per-stage host copies.
//! A cuFFT-backed [`Fft`](crate::backend::Fft) capability additionally
//! composes every Fft-based method onto CUDA through the backend-agnostic code:
//! `gridrec`, `lprec`, and Paganin/GPaganin/Farago phase all run on the GPU.
//! Still CPU-only: stripe removal (`remove_stripe` takes no backend).
#![cfg_attr(not(feature = "cuda"), allow(dead_code))]
#[cfg(feature = "cuda")]
pub mod ffi;
use crate::backend::{Backend, DeviceKind};
use crate::dtype::Dtype;
#[cfg(not(feature = "cuda"))]
use crate::error::Error;
use crate::error::Result;
/// Handle to the CUDA backend.
#[derive(Clone, Copy, Debug, Default)]
pub struct CudaBackend;
impl CudaBackend {
/// Initialise the CUDA backend.
///
/// Without the `cuda` feature this always returns
/// [`Error::BackendUnavailable`]. With the feature it probes for a device
/// via the shim and fails if none is present.
pub fn new() -> Result<Self> {
#[cfg(not(feature = "cuda"))]
{
Err(Error::BackendUnavailable(
"compiled without the `cuda` feature".into(),
))
}
#[cfg(feature = "cuda")]
{
let count = unsafe { ffi::tomoxide_cuda_device_count() };
if count <= 0 {
return Err(crate::error::Error::BackendUnavailable(
"no CUDA device found".into(),
));
}
Ok(CudaBackend)
}
}
}
impl Backend for CudaBackend {
fn name(&self) -> &'static str {
"cuda"
}
fn device(&self) -> DeviceKind {
DeviceKind::Cuda
}
fn supports(&self, dt: Dtype) -> bool {
// tomocupy compiles f32 and f16 (`*fp16`) kernel variants.
matches!(dt, Dtype::F32 | Dtype::F16)
}
/// FBP filter on the GPU (`cfunc_filter`, cuFFT), applying the shared ramp
/// definition so the filtered sinogram matches the CPU path.
#[cfg(feature = "cuda")]
fn fbp_filter(&self) -> Option<&dyn crate::backend::FbpFilter> {
Some(self)
}
/// Parallel-beam back-projection on the GPU (`cfunc_linerec`).
#[cfg(feature = "cuda")]
fn backprojector(&self) -> Option<&dyn crate::backend::FilteredBackproject> {
Some(self)
}
/// Fourier-gridding reconstruction on the GPU (`cfunc_fourierrec`).
#[cfg(feature = "cuda")]
fn fourier_reconstruct(&self) -> Option<&dyn crate::backend::FourierReconstruct> {
Some(self)
}
/// Fused on-device analytic reconstruction (filter → back-projection /
/// fourierrec without per-stage host copies).
#[cfg(feature = "cuda")]
fn analytic_reconstruct(&self) -> Option<&dyn crate::backend::AnalyticReconstruct> {
Some(self)
}
/// Dark/flat correction + minus-log on the GPU.
#[cfg(feature = "cuda")]
fn elementwise(&self) -> Option<&dyn crate::backend::Elementwise> {
Some(self)
}
/// Batched C2C FFT (cuFFT). Implementing this composes every Fft-based
/// method (gridrec, lprec, Paganin/GPaganin/Farago phase, Fourier-wavelet
/// stripe) onto CUDA through the backend-agnostic code.
#[cfg(feature = "cuda")]
fn fft(&self) -> Option<&dyn crate::backend::Fft> {
Some(self)
}
}
#[cfg(feature = "cuda")]
mod cuda_impl {
use super::{ffi, CudaBackend};
use ndarray::{Array3, Axis};
use std::os::raw::c_void;
use crate::backend::{
make_fbp_filter, Elementwise, FbpFilter, FilteredBackproject, FourierReconstruct,
};
use crate::data::{Frames, Layout, Tomo, Volume};
use crate::error::{Error, Result};
use crate::geometry::{Beam, Geometry};
use crate::params::FilterName;
/// RAII wrapper over a `cudaMalloc` allocation (freed on drop).
struct DevBuf {
ptr: *mut c_void,
bytes: usize,
}
impl DevBuf {
fn new(bytes: usize) -> Result<Self> {
let ptr = unsafe { ffi::tomoxide_cuda_malloc(bytes) };
if ptr.is_null() {
return Err(Error::Backend(format!("cudaMalloc({bytes}) failed")));
}
Ok(DevBuf { ptr, bytes })
}
fn from_host_f32(data: &[f32]) -> Result<Self> {
let bytes = std::mem::size_of_val(data);
let buf = DevBuf::new(bytes)?;
let rc = unsafe {
ffi::tomoxide_cuda_memcpy_h2d(buf.ptr, data.as_ptr() as *const c_void, bytes)
};
if rc != 0 {
return Err(Error::Backend(format!("cudaMemcpy H2D failed ({rc})")));
}
Ok(buf)
}
fn zeroed(bytes: usize) -> Result<Self> {
let buf = DevBuf::new(bytes)?;
let rc = unsafe { ffi::tomoxide_cuda_memset(buf.ptr, 0, bytes) };
if rc != 0 {
return Err(Error::Backend(format!("cudaMemset failed ({rc})")));
}
Ok(buf)
}
fn to_host_f32(&self, out: &mut [f32]) -> Result<()> {
let bytes = std::mem::size_of_val(out);
debug_assert!(bytes <= self.bytes);
let rc = unsafe {
ffi::tomoxide_cuda_memcpy_d2h(out.as_mut_ptr() as *mut c_void, self.ptr, bytes)
};
if rc != 0 {
return Err(Error::Backend(format!("cudaMemcpy D2H failed ({rc})")));
}
Ok(())
}
}
impl Drop for DevBuf {
fn drop(&mut self) {
unsafe { ffi::tomoxide_cuda_free(self.ptr) };
}
}
impl FilteredBackproject for CudaBackend {
/// Parallel-beam voxel-driven back-projection via tomocupy's
/// `cfunc_linerec` (phi = π/2). The sinogram must already be filtered and
/// centred on the detector midpoint (`recon` does this through the shared
/// FBP filter), so the kernel assumes centre `n/2`. Output is
/// `[nz, n, n]` with the kernel's y-flip and `4/nproj` scaling (tomocupy
/// convention — a fixed handedness/scale vs the CPU back-projector, which
/// scale-invariant correlation tests account for).
fn backproject(
&self,
sino: &Tomo<f32>,
geom: &Geometry,
out: &mut Volume<f32>,
) -> Result<()> {
if geom.beam != Beam::Parallel {
return Err(Error::InvalidParam(
"cuda back-projection supports parallel beam only".into(),
));
}
let s = sino.to_layout(Layout::Sinogram); // [nz, nproj, ncols]
let nz = s.n_rows();
let nproj = s.n_angles();
let ncols = s.n_cols();
let (oz, ny, nx) = out.dims();
if oz != nz {
return Err(Error::ShapeMismatch {
expected: format!("{nz} sinogram rows"),
found: oz.to_string(),
});
}
if ny != ncols || nx != ncols {
return Err(Error::InvalidParam(format!(
"cuda back-projection needs a square grid = detector width {ncols}; got {ny}x{nx}"
)));
}
let theta = &geom.angles.0;
if theta.len() != nproj {
return Err(Error::ShapeMismatch {
expected: format!("{nproj} angles"),
found: theta.len().to_string(),
});
}
let sino_slice = s
.array
.as_slice()
.ok_or_else(|| Error::InvalidParam("non-contiguous sinogram".into()))?;
// Device buffers: filtered sinogram, theta, output volume.
let g = DevBuf::from_host_f32(sino_slice)?;
let theta_d = DevBuf::from_host_f32(theta)?;
let f = DevBuf::zeroed(nz * ncols * ncols * std::mem::size_of::<f32>())?;
// cfunc_linerec(nproj, nz, n, ncproj=nproj, ncz=nz): whole stack at once.
let handle = unsafe { ffi::tomoxide_linerec_new(nproj, nz, ncols, nproj, nz) };
if handle.is_null() {
return Err(Error::Backend("cfunc_linerec allocation failed".into()));
}
let phi = std::f32::consts::FRAC_PI_2; // parallel beam
unsafe {
ffi::tomoxide_linerec_backproject(
handle,
f.ptr,
g.ptr,
theta_d.ptr as *const f32,
phi,
0,
std::ptr::null_mut(),
);
}
let rc = unsafe { ffi::tomoxide_cuda_sync() };
unsafe { ffi::tomoxide_linerec_free(handle) };
if rc != 0 {
return Err(Error::Backend(format!("cuda kernel sync failed ({rc})")));
}
let mut host = vec![0.0f32; nz * ncols * ncols];
f.to_host_f32(&mut host)?;
out.array = Array3::from_shape_vec((nz, ncols, ncols), host)
.map_err(|e| Error::InvalidParam(format!("cuda volume shape: {e}")))?;
Ok(())
}
}
/// Complex FBP weight `w[z, k] = ramp[k]·exp(-2πi·k·δ_z/pad)/pad`, for
/// `k in 0..ne/2+1`, `δ_z = ncols/2 − center(z)` (half spectrum ⇒ `f_k = k ≥
/// 0`), interleaved re/im — folds the ramp, signed-frequency centre-shift
/// phase, and `1/ne` cuFFT-inverse normalization (matches the CPU filter).
fn build_filter_w(
filter: &[f32],
geom: &Geometry,
nz: usize,
ncols: usize,
pad: usize,
) -> Vec<f32> {
let nfreq = pad / 2 + 1;
let half = ncols as f32 / 2.0;
let inv_pad = 1.0f32 / pad as f32;
let mut w = vec![0.0f32; nz * nfreq * 2];
for z in 0..nz {
let delta = half - geom.center.at(z);
for (k, &fk) in filter[..nfreq].iter().enumerate() {
let ang = -std::f32::consts::TAU * k as f32 * delta / pad as f32;
let idx = z * nfreq + k;
w[2 * idx] = fk * ang.cos() * inv_pad;
w[2 * idx + 1] = fk * ang.sin() * inv_pad;
}
}
w
}
fn ck(rc: i32, what: &str) -> Result<()> {
if rc != 0 {
return Err(Error::Backend(format!("cuda {what} failed ({rc})")));
}
Ok(())
}
impl crate::backend::AnalyticReconstruct for CudaBackend {
/// Fused, device-resident analytic reconstruction: upload the raw
/// sinogram once, then pad → cuFFT filter → crop → back-projection
/// (`Fbp`/`Linerec` via `cfunc_linerec`) or pack → `cfunc_fourierrec` →
/// unpack (`Fourierrec`) — all on the device — and download the volume
/// once. No per-stage host round-trips. Square grid (`n == ncols`).
fn reconstruct(
&self,
sino: &Tomo<f32>,
geom: &Geometry,
algorithm: crate::params::Algorithm,
params: &crate::params::ReconParams,
) -> Result<Volume<f32>> {
use crate::backend::make_fbp_filter;
use crate::params::Algorithm;
if geom.beam != Beam::Parallel {
return Err(Error::InvalidParam(
"cuda analytic reconstruct supports parallel beam only".into(),
));
}
let s = sino.to_layout(Layout::Sinogram); // [nz, nproj, ncols]
let (nz, nproj, ncols) = s.array.dim();
let n = params.num_gridx.unwrap_or(ncols);
if n != ncols {
return Err(Error::InvalidParam(format!(
"cuda analytic reconstruct needs a square grid = detector width {ncols}; got {n}"
)));
}
let theta = &geom.angles.0;
if theta.len() != nproj {
return Err(Error::ShapeMismatch {
expected: format!("{nproj} angles"),
found: theta.len().to_string(),
});
}
let raw = s
.array
.as_slice()
.ok_or_else(|| Error::InvalidParam("non-contiguous sinogram".into()))?;
let filter = make_fbp_filter(params.filter_name, ncols)?;
let pad = filter.len();
let pad_side = pad / 2 - ncols / 2;
let w = build_filter_w(&filter, geom, nz, ncols, pad);
let fsz = std::mem::size_of::<f32>();
let null = std::ptr::null_mut::<c_void>();
// Upload the raw sinogram + filter weights + angles ONCE.
let sino_dev = DevBuf::from_host_f32(raw)?;
let w_dev = DevBuf::from_host_f32(&w)?;
let theta_dev = DevBuf::from_host_f32(theta)?;
// pad → filter → crop, all device-resident.
let gpad = DevBuf::zeroed(nz * nproj * pad * fsz)?;
ck(
unsafe {
ffi::tomoxide_pad(sino_dev.ptr, gpad.ptr, nz, nproj, ncols, pad, pad_side, null)
},
"pad",
)?;
let fh = unsafe { ffi::tomoxide_filter_new(nproj, nz, pad) };
if fh.is_null() {
return Err(Error::Backend("cfunc_filter allocation failed".into()));
}
unsafe { ffi::tomoxide_filter_apply(fh, gpad.ptr, w_dev.ptr, null) };
unsafe { ffi::tomoxide_filter_free(fh) };
let gf = DevBuf::zeroed(nz * nproj * ncols * fsz)?;
ck(
unsafe {
ffi::tomoxide_crop(gpad.ptr, gf.ptr, nz, nproj, ncols, pad, pad_side, null)
},
"crop",
)?;
let vol = match algorithm {
Algorithm::Fbp | Algorithm::Linerec => {
let f = DevBuf::zeroed(nz * n * n * fsz)?;
let h = unsafe { ffi::tomoxide_linerec_new(nproj, nz, n, nproj, nz) };
if h.is_null() {
return Err(Error::Backend("cfunc_linerec allocation failed".into()));
}
unsafe {
ffi::tomoxide_linerec_backproject(
h,
f.ptr,
gf.ptr,
theta_dev.ptr as *const f32,
std::f32::consts::FRAC_PI_2,
0,
null,
);
}
unsafe { ffi::tomoxide_linerec_free(h) };
ck(unsafe { ffi::tomoxide_cuda_sync() }, "sync")?;
let mut host = vec![0.0f32; nz * n * n];
f.to_host_f32(&mut host)?;
host
}
Algorithm::Fourierrec => {
if nz % 2 != 0 {
return Err(Error::InvalidParam(format!(
"cuda fourierrec needs an even slice count; got nz={nz}"
)));
}
let gc = DevBuf::zeroed(nz * nproj * ncols * fsz)?; // complex [nz/2,nproj,ncols]
ck(
unsafe { ffi::tomoxide_pack_pairs(gf.ptr, gc.ptr, nz, nproj, ncols, null) },
"pack",
)?;
let fc = DevBuf::zeroed(nz * n * n * fsz)?; // complex [nz/2,n,n]
let h = unsafe {
ffi::tomoxide_fourierrec_new(nproj, nz / 2, n, theta_dev.ptr as *const f32)
};
if h.is_null() {
return Err(Error::Backend("cfunc_fourierrec allocation failed".into()));
}
unsafe { ffi::tomoxide_fourierrec_backproject(h, fc.ptr, gc.ptr, null) };
unsafe { ffi::tomoxide_fourierrec_free(h) };
let vol_dev = DevBuf::zeroed(nz * n * n * fsz)?;
ck(
unsafe { ffi::tomoxide_unpack_pairs(fc.ptr, vol_dev.ptr, nz, n, null) },
"unpack",
)?;
ck(unsafe { ffi::tomoxide_cuda_sync() }, "sync")?;
let mut host = vec![0.0f32; nz * n * n];
vol_dev.to_host_f32(&mut host)?;
host
}
other => {
return Err(Error::InvalidParam(format!(
"cuda analytic reconstruct: unsupported algorithm {other:?}"
)))
}
};
Ok(Volume::new(
Array3::from_shape_vec((nz, n, n), vol)
.map_err(|e| Error::InvalidParam(format!("cuda volume shape: {e}")))?,
))
}
}
impl FbpFilter for CudaBackend {
/// Shared FBP filter definition (same ramp the CPU/wgpu backends build),
/// so the GPU filter applies an identical kernel.
fn make_filter(&self, name: FilterName, n: usize) -> Result<Vec<f32>> {
make_fbp_filter(name, n)
}
/// FBP filtering on the GPU via tomocupy's `cfunc_filter` (cuFFT R2C →
/// ×w → C2R). The complex weight `w` folds the same ramp, signed-
/// frequency centre-shift phase, and `1/ne` normalization the CPU
/// `FbpFilter` uses, so the result matches the CPU filter (to the FFT
/// f32 floor). Edge-replicate padding to `ne = filter.len()`.
fn apply(&self, sino: &mut Tomo<f32>, filter: &[f32], geom: &Geometry) -> Result<()> {
let pad = filter.len();
if pad == 0 {
return Err(Error::InvalidParam("empty filter".into()));
}
let orig = sino.layout;
let s = sino.to_layout(Layout::Sinogram); // [nz, nproj, ncols]
let (nz, nproj, ncols) = s.array.dim();
if pad < ncols {
return Err(Error::ShapeMismatch {
expected: format!(">= {ncols} (n_cols)"),
found: pad.to_string(),
});
}
let pad_side = pad / 2 - ncols / 2;
let src = s
.array
.as_slice()
.ok_or_else(|| Error::InvalidParam("non-contiguous sinogram".into()))?;
let w = build_filter_w(filter, geom, nz, ncols, pad);
// Padded real sinogram [nz, nproj, pad], edge-replicated borders.
let mut g = vec![0.0f32; nz * nproj * pad];
for z in 0..nz {
for p in 0..nproj {
let row = (z * nproj + p) * ncols;
let first = src[row];
let last = src[row + ncols - 1];
let dst = (z * nproj + p) * pad;
for x in 0..pad_side {
g[dst + x] = first;
}
for x in 0..ncols {
g[dst + pad_side + x] = src[row + x];
}
for x in (pad_side + ncols)..pad {
g[dst + x] = last;
}
}
}
let g_dev = DevBuf::from_host_f32(&g)?;
let w_dev = DevBuf::from_host_f32(&w)?;
let handle = unsafe { ffi::tomoxide_filter_new(nproj, nz, pad) };
if handle.is_null() {
return Err(Error::Backend("cfunc_filter allocation failed".into()));
}
unsafe {
ffi::tomoxide_filter_apply(handle, g_dev.ptr, w_dev.ptr, std::ptr::null_mut());
}
let rc = unsafe { ffi::tomoxide_cuda_sync() };
unsafe { ffi::tomoxide_filter_free(handle) };
if rc != 0 {
return Err(Error::Backend(format!("cuda filter sync failed ({rc})")));
}
g_dev.to_host_f32(&mut g)?;
// Crop the centred [pad_side, pad_side+ncols) window back to ncols.
let mut out = vec![0.0f32; nz * nproj * ncols];
for z in 0..nz {
for p in 0..nproj {
let dst = (z * nproj + p) * ncols;
let srcp = (z * nproj + p) * pad + pad_side;
out[dst..dst + ncols].copy_from_slice(&g[srcp..srcp + ncols]);
}
}
let arr = Array3::from_shape_vec((nz, nproj, ncols), out)
.map_err(|e| Error::InvalidParam(format!("cuda filter shape: {e}")))?;
*sino = Tomo::new(arr, Layout::Sinogram).to_layout(orig);
Ok(())
}
}
impl crate::backend::Fft for CudaBackend {
fn fft_1d(
&self,
buf: &mut [crate::dtype::Complex32],
len: usize,
batch: usize,
inverse: bool,
) -> Result<()> {
if len == 0 || batch == 0 {
return Ok(());
}
if buf.len() != len * batch {
return Err(Error::ShapeMismatch {
expected: (len * batch).to_string(),
found: buf.len().to_string(),
});
}
let mut flat = complex_to_flat(buf);
let d = DevBuf::from_host_f32(&flat)?;
ck(
unsafe { ffi::tomoxide_fft_1d(d.ptr, len, batch, inverse as i32) },
"fft_1d",
)?;
d.to_host_f32(&mut flat)?;
flat_to_complex(&flat, buf);
Ok(())
}
fn fft_2d(
&self,
buf: &mut [crate::dtype::Complex32],
rows: usize,
cols: usize,
batch: usize,
inverse: bool,
) -> Result<()> {
if rows == 0 || cols == 0 || batch == 0 {
return Ok(());
}
if buf.len() != rows * cols * batch {
return Err(Error::ShapeMismatch {
expected: (rows * cols * batch).to_string(),
found: buf.len().to_string(),
});
}
let mut flat = complex_to_flat(buf);
let d = DevBuf::from_host_f32(&flat)?;
ck(
unsafe { ffi::tomoxide_fft_2d(d.ptr, rows, cols, batch, inverse as i32) },
"fft_2d",
)?;
d.to_host_f32(&mut flat)?;
flat_to_complex(&flat, buf);
Ok(())
}
}
/// Interleave `[re, im, …]` from a complex slice for upload.
fn complex_to_flat(buf: &[crate::dtype::Complex32]) -> Vec<f32> {
let mut flat = vec![0.0f32; buf.len() * 2];
for (i, c) in buf.iter().enumerate() {
flat[2 * i] = c.re;
flat[2 * i + 1] = c.im;
}
flat
}
/// Write an interleaved `[re, im, …]` buffer back into a complex slice.
fn flat_to_complex(flat: &[f32], buf: &mut [crate::dtype::Complex32]) {
for (i, c) in buf.iter_mut().enumerate() {
c.re = flat[2 * i];
c.im = flat[2 * i + 1];
}
}
impl Elementwise for CudaBackend {
/// `(data − mean(dark)) / max(mean(flat) − mean(dark), 1e-6)` on the GPU
/// (tomocupy `darkflat_correction`). Frame averages and the clamped
/// denominator are computed host-side; the per-projection broadcast runs
/// on the device.
fn darkflat(
&self,
data: &mut Tomo<f32>,
flat: &Frames<f32>,
dark: &Frames<f32>,
) -> Result<()> {
let dark2d = dark
.array
.mean_axis(Axis(0))
.ok_or_else(|| Error::InvalidParam("empty dark stack".into()))?;
let flat2d = flat
.array
.mean_axis(Axis(0))
.ok_or_else(|| Error::InvalidParam("empty flat stack".into()))?;
let mut denom = &flat2d - &dark2d;
denom.mapv_inplace(|v| if v.abs() < 1e-6 { 1.0 } else { v });
let restore = data.layout == Layout::Sinogram;
if restore {
*data = data.to_layout(Layout::Projection);
}
let (nproj, nz, nx) = data.array.dim();
{
let host = data
.array
.as_slice()
.ok_or_else(|| Error::InvalidParam("non-contiguous data".into()))?;
let d_data = DevBuf::from_host_f32(host)?;
let d_dark = DevBuf::from_host_f32(
dark2d.as_slice().expect("contiguous dark2d"),
)?;
let d_denom = DevBuf::from_host_f32(
denom.as_slice().expect("contiguous denom"),
)?;
let rc = unsafe {
ffi::tomoxide_darkflat(
d_data.ptr,
d_dark.ptr,
d_denom.ptr,
nproj,
nz,
nx,
std::ptr::null_mut(),
)
};
if rc != 0 {
return Err(Error::Backend(format!("cuda darkflat failed ({rc})")));
}
let sync = unsafe { ffi::tomoxide_cuda_sync() };
if sync != 0 {
return Err(Error::Backend(format!("cuda darkflat sync failed ({sync})")));
}
let out = data
.array
.as_slice_mut()
.ok_or_else(|| Error::InvalidParam("non-contiguous data".into()))?;
d_data.to_host_f32(out)?;
}
if restore {
*data = data.to_layout(Layout::Sinogram);
}
Ok(())
}
/// In-place `−ln(max(x, 1e-6))` (non-finite → 0) on the GPU.
fn minus_log(&self, data: &mut Tomo<f32>) -> Result<()> {
let n = data.array.len();
let host = data
.array
.as_slice()
.ok_or_else(|| Error::InvalidParam("non-contiguous data".into()))?;
let d_data = DevBuf::from_host_f32(host)?;
let rc = unsafe { ffi::tomoxide_minuslog(d_data.ptr, n, std::ptr::null_mut()) };
if rc != 0 {
return Err(Error::Backend(format!("cuda minus_log failed ({rc})")));
}
let sync = unsafe { ffi::tomoxide_cuda_sync() };
if sync != 0 {
return Err(Error::Backend(format!("cuda minus_log sync failed ({sync})")));
}
let out = data
.array
.as_slice_mut()
.ok_or_else(|| Error::InvalidParam("non-contiguous data".into()))?;
d_data.to_host_f32(out)?;
Ok(())
}
}
impl FourierReconstruct for CudaBackend {
/// Fourier-gridding reconstruction via tomocupy's `cfunc_fourierrec`.
///
/// The kernel processes `nz/2` **complex** slice-pairs: slice `s` and
/// `s + nz/2` of the filtered sinogram are packed into the real/imag of
/// one complex slice (tomocupy `FourierRec.backprojection`), and the
/// output volume is de-interleaved the same way. Requires an even slice
/// count and a square grid `n == ncols`. The FBP filter (incl. the
/// rotation-centre shift) is applied by the caller, so the kernel is
/// centre-agnostic. Output carries tomocupy's grid convention (verified
/// by scale/flip-invariant correlation vs the CPU `fourierrec`).
fn reconstruct(
&self,
filtered: &Tomo<f32>,
geom: &Geometry,
n: usize,
) -> Result<Volume<f32>> {
let s = filtered.to_layout(Layout::Sinogram); // [nz, nproj, ncols]
let nz = s.n_rows();
let nproj = s.n_angles();
let ncols = s.n_cols();
if nz % 2 != 0 {
return Err(Error::InvalidParam(format!(
"cuda fourierrec needs an even slice count (complex pairing); got nz={nz}"
)));
}
if n != ncols {
return Err(Error::InvalidParam(format!(
"cuda fourierrec needs a square grid = detector width {ncols}; got {n}"
)));
}
let theta = &geom.angles.0;
if theta.len() != nproj {
return Err(Error::ShapeMismatch {
expected: format!("{nproj} angles"),
found: theta.len().to_string(),
});
}
let src = s
.array
.as_slice()
.ok_or_else(|| Error::InvalidParam("non-contiguous sinogram".into()))?;
// Pack slice pairs (s, s+nz/2) into interleaved complex: for each
// complex element [s, p, x], re = filtered[s], im = filtered[s+nz/2].
let half = nz / 2;
let mut g = vec![0.0f32; nz * nproj * ncols];
for sp in 0..half {
for p in 0..nproj {
for x in 0..ncols {
let idx = sp * nproj * ncols + p * ncols + x;
g[2 * idx] = src[sp * nproj * ncols + p * ncols + x];
g[2 * idx + 1] = src[(sp + half) * nproj * ncols + p * ncols + x];
}
}
}
let g_dev = DevBuf::from_host_f32(&g)?;
let theta_dev = DevBuf::from_host_f32(theta)?;
// Output: complex [nz/2, n, n] = nz*n*n floats.
let f_dev = DevBuf::zeroed(nz * n * n * std::mem::size_of::<f32>())?;
let handle = unsafe {
ffi::tomoxide_fourierrec_new(nproj, half, n, theta_dev.ptr as *const f32)
};
if handle.is_null() {
return Err(Error::Backend("cfunc_fourierrec allocation failed".into()));
}
unsafe {
ffi::tomoxide_fourierrec_backproject(
handle,
f_dev.ptr,
g_dev.ptr,
std::ptr::null_mut(),
);
}
let rc = unsafe { ffi::tomoxide_cuda_sync() };
unsafe { ffi::tomoxide_fourierrec_free(handle) };
if rc != 0 {
return Err(Error::Backend(format!("cuda fourierrec sync failed ({rc})")));
}
let mut fbuf = vec![0.0f32; nz * n * n];
f_dev.to_host_f32(&mut fbuf)?;
// De-interleave: re → slice sp, im → slice sp+nz/2.
let mut vol = vec![0.0f32; nz * n * n];
for sp in 0..half {
for y in 0..n {
for x in 0..n {
let idx = sp * n * n + y * n + x;
vol[sp * n * n + y * n + x] = fbuf[2 * idx];
vol[(sp + half) * n * n + y * n + x] = fbuf[2 * idx + 1];
}
}
}
Ok(Volume::new(
Array3::from_shape_vec((nz, n, n), vol)
.map_err(|e| Error::InvalidParam(format!("cuda fourierrec shape: {e}")))?,
))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(not(feature = "cuda"))]
fn unavailable_without_feature() {
assert!(matches!(
CudaBackend::new(),
Err(Error::BackendUnavailable(_))
));
}
#[test]
fn advertises_cuda_device() {
let b = CudaBackend::default();
assert_eq!(b.name(), "cuda");
assert_eq!(b.device(), DeviceKind::Cuda);
assert!(b.supports(Dtype::F16));
}
}