rocm-rs 0.4.2

Rust bindings for AMD ROCm libraries
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
// src/rocfft/utils.rs

use crate::error::Error::RocFFT;
use crate::error::Result;
use crate::hip::{DeviceMemory, Stream};
use crate::rocfft::{
    description::PlanDescription,
    error,
    execution::ExecutionInfo,
    plan::{ArrayType, PlacementType, Plan, Precision, TransformType},
};

/// Determines the size of the output for a real-to-complex transform
///
/// For a real FFT, the output size in the first dimension is `(input_size / 2) + 1`
/// due to conjugate symmetry, but unchanged in other dimensions.
///
/// # Arguments
/// * `input_lengths` - The sizes of each dimension in the input array
///
/// # Returns
/// * The sizes of each dimension in the output array
pub fn get_real_forward_output_length(input_lengths: &[usize]) -> Vec<usize> {
    let mut output_lengths = input_lengths.to_vec();

    if !output_lengths.is_empty() {
        // For real-to-complex FFT, output first dimension is (n/2)+1
        output_lengths[0] = (input_lengths[0] / 2) + 1;
    }

    output_lengths
}

/// Wrapper for forward complex-to-complex FFT
///
/// # Arguments
/// * `input` - Input data on GPU (complex interleaved)
/// * `output` - Optional output data on GPU (for out-of-place transform)
/// * `lengths` - Size of each dimension
/// * `precision` - Numerical precision
/// * `stream` - Optional GPU stream for execution
///
/// # Returns
/// * Result indicating success or error
pub unsafe fn complex_forward_transform<T>(
    input: &DeviceMemory<T>,
    output: Option<&DeviceMemory<T>>,
    lengths: &[usize],
    precision: Precision,
    stream: Option<&Stream>,
) -> Result<()> {
    let placement = match output {
        Some(_) => PlacementType::NotInPlace,
        None => PlacementType::InPlace,
    };

    let dimensions = lengths.len();
    if dimensions < 1 || dimensions > 3 {
        return Err(RocFFT(error::Error::InvalidDimensions));
    }

    // Create default plan
    let mut plan = Plan::new(
        placement,
        TransformType::ComplexForward,
        precision,
        dimensions,
        lengths,
        1, // Single transform
        None,
    )?;

    // Set up execution info if we have a stream
    let mut exec_info = match stream {
        Some(s) => {
            let mut info = ExecutionInfo::new()?;
            unsafe { info.set_stream(s.as_raw() as *mut std::ffi::c_void) }?;
            Some(info)
        }
        None => None,
    };

    // Setup input/output buffers
    let input_ptr = [input.as_ptr()];
    let output_ptrs = match output {
        Some(out) => vec![out.as_ptr()],
        None => vec![],
    };

    // Execute the plan
    plan.execute(&input_ptr, &output_ptrs, exec_info.as_mut())?;

    Ok(())
}

/// Wrapper for inverse complex-to-complex FFT
///
/// # Arguments
/// * `input` - Input data on GPU (complex interleaved)
/// * `output` - Optional output data on GPU (for out-of-place transform)
/// * `lengths` - Size of each dimension
/// * `precision` - Numerical precision
/// * `scale` - Whether to apply 1/N scaling
/// * `stream` - Optional GPU stream for execution
///
/// # Returns
/// * Result indicating success or error
pub fn complex_inverse_transform<T>(
    input: &DeviceMemory<T>,
    output: Option<&DeviceMemory<T>>,
    lengths: &[usize],
    precision: Precision,
    scale: bool,
    stream: Option<&Stream>,
) -> Result<()> {
    let placement = match output {
        Some(_) => PlacementType::NotInPlace,
        None => PlacementType::InPlace,
    };

    let dimensions = lengths.len();
    if dimensions < 1 || dimensions > 3 {
        return Err(RocFFT(error::Error::InvalidDimensions));
    }

    // Calculate total number of elements to determine scaling factor
    let total_elements: usize = lengths.iter().product();
    let scale_factor = if scale {
        1.0 / total_elements as f64
    } else {
        1.0
    };

    // Create plan description if we need scaling
    let description = if scale {
        let mut desc = PlanDescription::new()?;
        desc.set_scale_factor(scale_factor)?;
        Some(desc)
    } else {
        None
    };

    // Create plan
    let mut plan = Plan::new(
        placement,
        TransformType::ComplexInverse,
        precision,
        dimensions,
        lengths,
        1, // Single transform
        description.as_ref(),
    )?;

    // Set up execution info if we have a stream
    let mut exec_info = match stream {
        Some(s) => unsafe {
            let mut info = ExecutionInfo::new()?;
            info.set_stream(s.as_raw() as *mut std::ffi::c_void)?;
            Some(info)
        },
        None => None,
    };

    // Setup input/output buffers
    let input_ptr = [input.as_ptr()];
    let output_ptrs = match output {
        Some(out) => vec![out.as_ptr()],
        None => vec![],
    };

    // Execute the plan
    plan.execute(&input_ptr, &output_ptrs, exec_info.as_mut())?;

    Ok(())
}

/// Wrapper for forward real-to-complex FFT
///
/// # Arguments
/// * `input` - Input real data on GPU
/// * `output` - Output complex data on GPU
/// * `lengths` - Size of each dimension of input
/// * `precision` - Numerical precision
/// * `stream` - Optional GPU stream for execution
///
/// # Returns
/// * Result indicating success or error
pub fn real_forward_transform<T, U>(
    input: &DeviceMemory<T>,  // Real input
    output: &DeviceMemory<U>, // Complex output (interleaved)
    lengths: &[usize],
    precision: Precision,
    stream: Option<&Stream>,
) -> Result<()> {
    let dimensions = lengths.len();
    if dimensions < 1 || dimensions > 3 {
        return Err(RocFFT(error::Error::InvalidDimensions));
    }

    // Create default plan for real-to-complex transform
    let mut plan = Plan::new(
        PlacementType::NotInPlace,
        TransformType::RealForward,
        precision,
        dimensions,
        lengths,
        1, // Single transform
        None,
    )?;

    // Set up execution info if we have a stream
    let mut exec_info = match stream {
        Some(s) => unsafe {
            let mut info = ExecutionInfo::new()?;
            info.set_stream(s.as_raw() as *mut std::ffi::c_void)?;
            Some(info)
        },
        None => None,
    };

    // Setup input/output buffers
    let input_ptr = [input.as_ptr()];
    let output_ptr = [output.as_ptr()];

    // Execute the plan
    plan.execute(&input_ptr, &output_ptr, exec_info.as_mut())?;

    Ok(())
}

/// Wrapper for inverse complex-to-real FFT
///
/// # Arguments
/// * `input` - Input complex data on GPU
/// * `output` - Output real data on GPU
/// * `lengths` - Size of each dimension of the output
/// * `precision` - Numerical precision
/// * `scale` - Whether to apply 1/N scaling
/// * `stream` - Optional GPU stream for execution
///
/// # Returns
/// * Result indicating success or error
pub fn complex_to_real_transform<T, U>(
    input: &DeviceMemory<T>,  // Complex input (interleaved)
    output: &DeviceMemory<U>, // Real output
    lengths: &[usize],
    precision: Precision,
    scale: bool,
    stream: Option<&Stream>,
) -> Result<()> {
    let dimensions = lengths.len();
    if dimensions < 1 || dimensions > 3 {
        return Err(RocFFT(error::Error::InvalidDimensions));
    }

    // Calculate total number of elements to determine scaling factor
    let total_elements: usize = lengths.iter().product();
    let scale_factor = if scale {
        1.0 / total_elements as f64
    } else {
        1.0
    };

    // Create plan description if we need scaling
    let description = if scale {
        let mut desc = PlanDescription::new()?;
        desc.set_scale_factor(scale_factor)?;
        Some(desc)
    } else {
        None
    };

    // Create plan
    let mut plan = Plan::new(
        PlacementType::NotInPlace,
        TransformType::RealInverse,
        precision,
        dimensions,
        lengths, // Pass the full output lengths
        1,       // Single transform
        description.as_ref(),
    )?;

    // Set up execution info if we have a stream
    let mut exec_info = match stream {
        Some(s) => unsafe {
            let mut info = ExecutionInfo::new()?;
            info.set_stream(s.as_raw() as *mut std::ffi::c_void)?;
            Some(info)
        },
        None => None,
    };

    // Setup input/output buffers
    let input_ptr = [input.as_ptr()];
    let output_ptr = [output.as_ptr()];

    // Execute the plan
    plan.execute(&input_ptr, &output_ptr, exec_info.as_mut())?;

    Ok(())
}

/// Create and initialize 2D FFT plan with custom strides and offsets
///
/// # Arguments
/// * `width` - Width of the transform (columns)
/// * `height` - Height of the transform (rows)
/// * `in_row_stride` - Stride between elements in the same row
/// * `in_col_stride` - Stride between elements in the same column
/// * `transform_type` - Type of transform
/// * `precision` - Numerical precision
/// * `placement` - In-place or out-of-place
///
/// # Returns
/// * FFT plan with the specified parameters
pub fn create_2d_fft_plan_with_strides(
    width: usize,
    height: usize,
    in_row_stride: usize,
    in_col_stride: usize,
    transform_type: TransformType,
    precision: Precision,
    placement: PlacementType,
) -> Result<Plan> {
    let lengths = vec![width, height];
    let dimensions = 2;

    // Create a plan description for the custom strides
    let mut description = PlanDescription::new()?;

    // Set strides (row stride is distance between adjacent elements,
    // column stride is distance between rows)
    let in_strides = vec![in_row_stride, in_col_stride];

    // If real-to-complex transform, output size is different
    let (in_array_type, out_array_type) = match transform_type {
        TransformType::RealForward => (ArrayType::Real, ArrayType::ComplexInterleaved),
        TransformType::RealInverse => (ArrayType::ComplexInterleaved, ArrayType::Real),
        _ => (ArrayType::ComplexInterleaved, ArrayType::ComplexInterleaved),
    };

    // Calculate distance between batches (total size of one 2D array)
    let in_distance = height * in_col_stride;

    // For simplicity, we use the same strides for output
    // In a real application, these might differ
    let out_strides = in_strides.clone();
    let out_distance = in_distance;

    // Set data layout
    description.set_data_layout(
        in_array_type,
        out_array_type,
        Some(&[0]), // No offset
        Some(&[0]), // No offset
        Some(&in_strides),
        in_distance,
        Some(&out_strides),
        out_distance,
    )?;

    // Create the plan
    let plan = Plan::new(
        placement,
        transform_type,
        precision,
        dimensions,
        &lengths,
        1, // Single transform
        Some(&description),
    )?;

    Ok(plan)
}

/// Utility function to apply a 1D convolution using FFT
///
/// # Arguments
/// * `signal` - Input signal on device
/// * `kernel` - Convolution kernel on device
/// * `output` - Output buffer (must be same size as signal)
/// * `precision` - Numerical precision
/// * `stream` - Optional GPU stream
///
/// # Returns
/// * Result indicating success or error
pub fn fft_convolution_1d<T>(
    signal: &DeviceMemory<T>,
    kernel: &DeviceMemory<T>,
    output: &mut DeviceMemory<T>,
    precision: Precision,
    stream: Option<&Stream>,
) -> Result<()>
where
    T: Copy
        + Default
        + std::ops::Mul<Output = T>
        + std::ops::Neg<Output = T>
        + std::ops::Add<Output = T>,
{
    // Create work buffers for FFT
    let signal_size = signal.count();
    let kernel_size = kernel.count();

    if signal_size < kernel_size {
        return Err(RocFFT(error::Error::InvalidArgValue));
    }

    // Create padded work buffers (signal length + kernel length - 1)
    let padded_size = signal_size + kernel_size - 1;
    let lengths = vec![padded_size];

    // Allocate device memory for padded input and kernel
    let mut padded_signal = DeviceMemory::<T>::new(padded_size * 2)?; // *2 for complex
    let mut padded_kernel = DeviceMemory::<T>::new(padded_size * 2)?; // *2 for complex
    let mut fft_result = DeviceMemory::<T>::new(padded_size * 2)?; // *2 for complex

    // Create plans
    let mut forward_plan = Plan::new(
        PlacementType::NotInPlace,
        TransformType::ComplexForward,
        precision,
        1, // 1D
        &lengths,
        1, // Single transform
        None,
    )?;

    // Create inverse plan with scaling
    let mut desc = PlanDescription::new()?;
    desc.set_scale_factor(1.0 / padded_size as f64)?;

    let mut inverse_plan = Plan::new(
        PlacementType::NotInPlace,
        TransformType::ComplexInverse,
        precision,
        1, // 1D
        &lengths,
        1, // Single transform
        Some(&desc),
    )?;

    // Create execution info if we have a stream
    let mut exec_info = match stream {
        Some(s) => unsafe {
            let mut info = ExecutionInfo::new()?;
            info.set_stream(s.as_raw() as *mut std::ffi::c_void)?;
            Some(info)
        },
        None => None,
    };

    // Host buffers for preparing data
    let mut host_padded_signal = vec![T::default(); padded_size * 2];
    let mut host_padded_kernel = vec![T::default(); padded_size * 2];

    // Copy signal to host buffer (interleaved complex: real, imag, real, imag, ...)
    let mut host_signal = vec![T::default(); signal_size];
    signal.copy_to_host(&mut host_signal)?;

    for i in 0..signal_size {
        host_padded_signal[i * 2] = host_signal[i]; // Real part
        // Imaginary part is already initialized to zero
    }

    // Copy kernel to host buffer
    let mut host_kernel = vec![T::default(); kernel_size];
    kernel.copy_to_host(&mut host_kernel)?;

    for i in 0..kernel_size {
        host_padded_kernel[i * 2] = host_kernel[i]; // Real part
        // Imaginary part is already initialized to zero
    }

    // Copy data to device
    padded_signal.copy_from_host(&host_padded_signal)?;
    padded_kernel.copy_from_host(&host_padded_kernel)?;

    // Perform forward FFTs
    let input_ptr = [padded_signal.as_ptr()];
    let kernel_ptr = [padded_kernel.as_ptr()];
    let result_ptr = [fft_result.as_ptr()];

    // FFT of signal
    forward_plan.execute(&input_ptr, &result_ptr, exec_info.as_mut())?;

    // Copy result to padded_signal for reuse
    let mut host_fft_signal = vec![T::default(); padded_size * 2];
    fft_result.copy_to_host(&mut host_fft_signal)?;

    // FFT of kernel
    forward_plan.execute(&kernel_ptr, &result_ptr, exec_info.as_mut())?;

    // Copy kernel FFT result
    let mut host_fft_kernel = vec![T::default(); padded_size * 2];
    fft_result.copy_to_host(&mut host_fft_kernel)?;

    // Perform pointwise multiplication in frequency domain
    let mut host_mult_result = vec![T::default(); padded_size * 2];

    // Simple example assuming T is f32 or f64
    // In a real implementation you'd need to handle complex multiplication properly
    // This is a simplification!
    for i in 0..padded_size {
        let idx = i * 2;
        let s_real = host_fft_signal[idx];
        let s_imag = host_fft_signal[idx + 1];
        let k_real = host_fft_kernel[idx];
        let k_imag = host_fft_kernel[idx + 1];

        // Complex multiplication (s_real + i*s_imag) * (k_real + i*k_imag)
        // This assumes T can be multiplied and added, which may not be true for all types
        // In a real implementation, you'd need proper complex number handling
        host_mult_result[idx] = multiply_add(s_real, k_real, multiply_neg(s_imag, k_imag)); // Real part
        host_mult_result[idx + 1] = multiply_add(s_real, k_imag, multiply(s_imag, k_real)); // Imaginary part
    }

    // Copy multiplication result back to device
    fft_result.copy_from_host(&host_mult_result)?;

    // Create a buffer for the inverse FFT result
    let ifft_result = DeviceMemory::<T>::new(padded_size * 2)?;
    let ifft_ptr = [ifft_result.as_ptr()];

    // Perform inverse FFT
    inverse_plan.execute(&result_ptr, &ifft_ptr, exec_info.as_mut())?;

    // Copy result back to host
    let mut host_ifft_result = vec![T::default(); padded_size * 2];
    ifft_result.copy_to_host(&mut host_ifft_result)?;

    // Extract real part of the result (first signal_size elements)
    let mut host_output = vec![T::default(); signal_size];
    for i in 0..signal_size {
        host_output[i] = host_ifft_result[i * 2]; // Take only real part
    }

    // Copy result to output
    output.copy_from_host(&host_output)?;

    Ok(())
}

// These helper functions would need proper implementations for the generic type T
// Here we just use placeholders

fn multiply<T: std::ops::Mul<Output = T>>(a: T, b: T) -> T {
    a * b
}

fn multiply_neg<T: std::ops::Mul<Output = T> + std::ops::Neg<Output = T>>(a: T, b: T) -> T {
    -multiply(a, b)
}

fn multiply_add<T: std::ops::Mul<Output = T> + std::ops::Add<Output = T>>(a: T, b: T, c: T) -> T {
    multiply(a, b) + c
}