hanzo-rocm 0.5.2

Rust bindings for AMD ROCm libraries
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
// src/rocsolver/lapack/orthogonal.rs
//! Orthogonal/Unitary matrix operations.
//!
//! This module provides safe wrappers for generating and applying orthogonal/unitary matrices:
//!
//! - [`orgqr`]/[`ungqr`] - Generate Q from QR factorization
//! - [`ormqr`]/[`unmqr`] - Apply Q from QR factorization
//!
//! Note: rocSOLVER does not provide batched variants for these operations.

use crate::rocblas::Handle;
use crate::rocblas::ffi as rocblas_ffi;
use crate::rocblas::types::{Operation, Side};
use crate::rocsolver::bindings;
use crate::rocsolver::error::{Error, Result};
use crate::rocsolver::types::{Complex32, Complex64};

// Type alias for handle - we use rocblas handle but need to cast for rocsolver bindings
type RocblasHandle = rocblas_ffi::rocblas_handle;
type RocblasStatus = rocblas_ffi::rocblas_status;

/// Cast rocblas handle to rocsolver bindings handle type.
#[inline]
fn cast_handle(handle: RocblasHandle) -> bindings::rocblas_handle {
    handle as bindings::rocblas_handle
}

// ============================================================================
// Type traits for real orthogonal operations
// ============================================================================

/// Trait for real types that support orgqr (generate orthogonal Q from QR).
pub trait OrgqrType: Sized + Copy {
    /// Generate the orthogonal matrix Q from QR factorization.
    unsafe fn orgqr(
        handle: RocblasHandle,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
    ) -> RocblasStatus;
}

/// Trait for real types that support ormqr (apply orthogonal Q from QR).
pub trait OrmqrType: Sized + Copy {
    /// Apply Q from QR factorization to a matrix C.
    unsafe fn ormqr(
        handle: RocblasHandle,
        side: rocblas_ffi::rocblas_side,
        trans: rocblas_ffi::rocblas_operation,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
        C: *mut Self,
        ldc: i32,
    ) -> RocblasStatus;
}

// ============================================================================
// Type traits for complex unitary operations
// ============================================================================

/// Trait for complex types that support ungqr (generate unitary Q from QR).
pub trait UngqrType: Sized + Copy {
    /// Generate the unitary matrix Q from QR factorization.
    unsafe fn ungqr(
        handle: RocblasHandle,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
    ) -> RocblasStatus;
}

/// Trait for complex types that support unmqr (apply unitary Q from QR).
pub trait UnmqrType: Sized + Copy {
    /// Apply Q from QR factorization to a matrix C.
    unsafe fn unmqr(
        handle: RocblasHandle,
        side: rocblas_ffi::rocblas_side,
        trans: rocblas_ffi::rocblas_operation,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
        C: *mut Self,
        ldc: i32,
    ) -> RocblasStatus;
}

// ============================================================================
// Trait implementations for f32
// ============================================================================

impl OrgqrType for f32 {
    unsafe fn orgqr(
        handle: RocblasHandle,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
    ) -> RocblasStatus {
        bindings::rocsolver_sorgqr(cast_handle(handle), m, n, k, A, lda, ipiv)
    }
}

impl OrmqrType for f32 {
    unsafe fn ormqr(
        handle: RocblasHandle,
        side: rocblas_ffi::rocblas_side,
        trans: rocblas_ffi::rocblas_operation,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
        C: *mut Self,
        ldc: i32,
    ) -> RocblasStatus {
        bindings::rocsolver_sormqr(
            cast_handle(handle),
            side,
            trans,
            m,
            n,
            k,
            A,
            lda,
            ipiv,
            C,
            ldc,
        )
    }
}

// ============================================================================
// Trait implementations for f64
// ============================================================================

impl OrgqrType for f64 {
    unsafe fn orgqr(
        handle: RocblasHandle,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
    ) -> RocblasStatus {
        bindings::rocsolver_dorgqr(cast_handle(handle), m, n, k, A, lda, ipiv)
    }
}

impl OrmqrType for f64 {
    unsafe fn ormqr(
        handle: RocblasHandle,
        side: rocblas_ffi::rocblas_side,
        trans: rocblas_ffi::rocblas_operation,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
        C: *mut Self,
        ldc: i32,
    ) -> RocblasStatus {
        bindings::rocsolver_dormqr(
            cast_handle(handle),
            side,
            trans,
            m,
            n,
            k,
            A,
            lda,
            ipiv,
            C,
            ldc,
        )
    }
}

// ============================================================================
// Trait implementations for Complex32
// ============================================================================

impl UngqrType for Complex32 {
    unsafe fn ungqr(
        handle: RocblasHandle,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
    ) -> RocblasStatus {
        bindings::rocsolver_cungqr(
            cast_handle(handle),
            m,
            n,
            k,
            A,
            lda,
            ipiv,
        )
    }
}

impl UnmqrType for Complex32 {
    unsafe fn unmqr(
        handle: RocblasHandle,
        side: rocblas_ffi::rocblas_side,
        trans: rocblas_ffi::rocblas_operation,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
        C: *mut Self,
        ldc: i32,
    ) -> RocblasStatus {
        bindings::rocsolver_cunmqr(
            cast_handle(handle),
            side,
            trans,
            m,
            n,
            k,
            A,
            lda,
            ipiv,
            C,
            ldc,
        )
    }
}

// ============================================================================
// Trait implementations for Complex64
// ============================================================================

impl UngqrType for Complex64 {
    unsafe fn ungqr(
        handle: RocblasHandle,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
    ) -> RocblasStatus {
        bindings::rocsolver_zungqr(
            cast_handle(handle),
            m,
            n,
            k,
            A,
            lda,
            ipiv,
        )
    }
}

impl UnmqrType for Complex64 {
    unsafe fn unmqr(
        handle: RocblasHandle,
        side: rocblas_ffi::rocblas_side,
        trans: rocblas_ffi::rocblas_operation,
        m: i32,
        n: i32,
        k: i32,
        A: *mut Self,
        lda: i32,
        ipiv: *mut Self,
        C: *mut Self,
        ldc: i32,
    ) -> RocblasStatus {
        bindings::rocsolver_zunmqr(
            cast_handle(handle),
            side,
            trans,
            m,
            n,
            k,
            A,
            lda,
            ipiv,
            C,
            ldc,
        )
    }
}

// ============================================================================
// Public API functions for real matrices
// ============================================================================

/// Generates the orthogonal matrix Q from QR factorization.
///
/// Generates an m-by-n real matrix Q with orthonormal columns,
/// defined as the first n columns of the product of k elementary reflectors:
///   Q = H(1) * H(2) * ... * H(k)
/// as returned by geqrf.
///
/// # Arguments
/// * `handle` - rocBLAS handle
/// * `m` - Number of rows of Q (m >= 0)
/// * `n` - Number of columns of Q (m >= n >= 0)
/// * `k` - Number of elementary reflectors (n >= k >= 0)
/// * `A` - Device pointer to matrix containing reflectors (modified to contain Q)
/// * `lda` - Leading dimension of A
/// * `ipiv` - Device pointer to Householder scalars from geqrf (k elements)
///
/// # Returns
/// `Ok(())` on success, or an error if the operation failed.
///
/// # Example
/// ```rust,no_run
/// use rocm_rs::{rocblas::Handle, rocsolver};
///
/// let handle = Handle::new().unwrap();
/// let m = 4i32;
/// let n = 3i32;
/// let k = 3i32;
/// // A and ipiv would be device pointers from geqrf
/// // rocsolver::orgqr(&handle, m, n, k, A, m, ipiv).unwrap();
/// ```
#[inline]
pub fn orgqr<T: OrgqrType>(
    handle: &Handle,
    m: i32,
    n: i32,
    k: i32,
    A: *mut T,
    lda: i32,
    ipiv: *mut T,
) -> Result<()> {
    let status = unsafe { T::orgqr(handle.as_raw(), m, n, k, A, lda, ipiv) };
    Error::from_status(status)
}

/// Applies Q from QR factorization to a matrix C.
///
/// Multiplies matrix C by the orthogonal matrix Q from geqrf:
/// - C = Q * C   (side=Left, trans=None)
/// - C = Q^T * C (side=Left, trans=Transpose)
/// - C = C * Q   (side=Right, trans=None)
/// - C = C * Q^T (side=Right, trans=Transpose)
///
/// # Arguments
/// * `handle` - rocBLAS handle
/// * `side` - Apply Q from left or right
/// * `trans` - Whether to transpose Q
/// * `m` - Number of rows of C
/// * `n` - Number of columns of C
/// * `k` - Number of elementary reflectors
/// * `A` - Device pointer to matrix containing reflectors
/// * `lda` - Leading dimension of A
/// * `ipiv` - Device pointer to Householder scalars
/// * `C` - Device pointer to matrix C (modified)
/// * `ldc` - Leading dimension of C
///
/// # Example
/// ```rust,no_run
/// use rocm_rs::{rocblas::{Handle, types::{Side, Operation}}, rocsolver};
///
/// let handle = Handle::new().unwrap();
/// // Apply Q from left with no transpose: C = Q * C
/// // rocsolver::ormqr(&handle, Side::Left, Operation::None, m, n, k, A, lda, ipiv, C, ldc).unwrap();
/// ```
#[inline]
pub fn ormqr<T: OrmqrType>(
    handle: &Handle,
    side: Side,
    trans: Operation,
    m: i32,
    n: i32,
    k: i32,
    A: *mut T,
    lda: i32,
    ipiv: *mut T,
    C: *mut T,
    ldc: i32,
) -> Result<()> {
    let status = unsafe {
        T::ormqr(
            handle.as_raw(),
            side.into(),
            trans.into(),
            m,
            n,
            k,
            A,
            lda,
            ipiv,
            C,
            ldc,
        )
    };
    Error::from_status(status)
}

// ============================================================================
// Public API functions for complex matrices
// ============================================================================

/// Generates the unitary matrix Q from QR factorization (complex version).
///
/// Generates an m-by-n complex matrix Q with orthonormal columns,
/// defined as the first n columns of the product of k elementary reflectors:
///   Q = H(1) * H(2) * ... * H(k)
/// as returned by geqrf.
///
/// # Arguments
/// * `handle` - rocBLAS handle
/// * `m` - Number of rows of Q (m >= 0)
/// * `n` - Number of columns of Q (m >= n >= 0)
/// * `k` - Number of elementary reflectors (n >= k >= 0)
/// * `A` - Device pointer to matrix containing reflectors (modified to contain Q)
/// * `lda` - Leading dimension of A
/// * `ipiv` - Device pointer to Householder scalars from geqrf (k elements)
///
/// # Returns
/// `Ok(())` on success, or an error if the operation failed.
#[inline]
pub fn ungqr<T: UngqrType>(
    handle: &Handle,
    m: i32,
    n: i32,
    k: i32,
    A: *mut T,
    lda: i32,
    ipiv: *mut T,
) -> Result<()> {
    let status = unsafe { T::ungqr(handle.as_raw(), m, n, k, A, lda, ipiv) };
    Error::from_status(status)
}

/// Applies Q from QR factorization to a matrix C (complex version).
///
/// Multiplies matrix C by the unitary matrix Q from geqrf:
/// - C = Q * C   (side=Left, trans=None)
/// - C = Q^H * C (side=Left, trans=ConjugateTranspose)
/// - C = C * Q   (side=Right, trans=None)
/// - C = C * Q^H (side=Right, trans=ConjugateTranspose)
///
/// Note: For complex matrices, use ConjugateTranspose instead of Transpose.
///
/// # Arguments
/// * `handle` - rocBLAS handle
/// * `side` - Apply Q from left or right
/// * `trans` - Whether to apply conjugate transpose of Q
/// * `m` - Number of rows of C
/// * `n` - Number of columns of C
/// * `k` - Number of elementary reflectors
/// * `A` - Device pointer to matrix containing reflectors
/// * `lda` - Leading dimension of A
/// * `ipiv` - Device pointer to Householder scalars
/// * `C` - Device pointer to matrix C (modified)
/// * `ldc` - Leading dimension of C
#[inline]
pub fn unmqr<T: UnmqrType>(
    handle: &Handle,
    side: Side,
    trans: Operation,
    m: i32,
    n: i32,
    k: i32,
    A: *mut T,
    lda: i32,
    ipiv: *mut T,
    C: *mut T,
    ldc: i32,
) -> Result<()> {
    let status = unsafe {
        T::unmqr(
            handle.as_raw(),
            side.into(),
            trans.into(),
            m,
            n,
            k,
            A,
            lda,
            ipiv,
            C,
            ldc,
        )
    };
    Error::from_status(status)
}