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
use crate::ffi::{self, blas_int};
use crate::util::*;
use derive_builder::Builder;
use ndarray::prelude::*;

/* #region BLAS func */

pub trait GERFunc<F>
where
    F: BLASFloat,
{
    unsafe fn ger(
        m: *const blas_int,
        n: *const blas_int,
        alpha: *const F,
        x: *const F,
        incx: *const blas_int,
        y: *const F,
        incy: *const blas_int,
        a: *mut F,
        lda: *const blas_int,
    );
}

macro_rules! impl_func {
    ($type: ty, $func: ident) => {
        impl GERFunc<$type> for BLASFunc
        where
            $type: BLASFloat,
        {
            unsafe fn ger(
                m: *const blas_int,
                n: *const blas_int,
                alpha: *const $type,
                x: *const $type,
                incx: *const blas_int,
                y: *const $type,
                incy: *const blas_int,
                a: *mut $type,
                lda: *const blas_int,
            ) {
                ffi::$func(m, n, alpha, x, incx, y, incy, a, lda);
            }
        }
    };
}

impl_func!(f32, sger_);
impl_func!(f64, dger_);
impl_func!(c32, cgeru_);
impl_func!(c64, zgeru_);

/* #endregion */

/* #region BLAS driver */

pub struct GER_Driver<'x, 'y, 'a, F>
where
    F: BLASFloat,
{
    m: blas_int,
    n: blas_int,
    alpha: F,
    x: ArrayView1<'x, F>,
    incx: blas_int,
    y: ArrayView1<'y, F>,
    incy: blas_int,
    a: ArrayOut2<'a, F>,
    lda: blas_int,
}

impl<'x, 'y, 'a, F> BLASDriver<'a, F, Ix2> for GER_Driver<'x, 'y, 'a, F>
where
    F: BLASFloat,
    BLASFunc: GERFunc<F>,
{
    fn run_blas(self) -> Result<ArrayOut2<'a, F>, BLASError> {
        let Self { m, n, alpha, x, incx, y, incy, mut a, lda } = self;
        let x_ptr = x.as_ptr();
        let y_ptr = y.as_ptr();
        let a_ptr = a.get_data_mut_ptr();

        // assuming dimension checks has been performed
        // unconditionally return Ok if output does not contain anything
        if m == 0 || n == 0 {
            return Ok(a.clone_to_view_mut());
        }

        unsafe {
            BLASFunc::ger(&m, &n, &alpha, x_ptr, &incx, y_ptr, &incy, a_ptr, &lda);
        }
        return Ok(a.clone_to_view_mut());
    }
}

/* #endregion */

/* #region BLAS builder */

#[derive(Builder)]
#[builder(pattern = "owned", build_fn(error = "BLASError"), no_std)]
pub struct GER_<'x, 'y, 'a, F>
where
    F: BLASFloat,
{
    pub x: ArrayView1<'x, F>,
    pub y: ArrayView1<'y, F>,

    #[builder(setter(into, strip_option), default = "None")]
    pub a: Option<ArrayViewMut2<'a, F>>,
    #[builder(setter(into), default = "F::one()")]
    pub alpha: F,
}

impl<'x, 'y, 'a, F> BLASBuilder_<'a, F, Ix2> for GER_<'x, 'y, 'a, F>
where
    F: BLASFloat,
    BLASFunc: GERFunc<F>,
{
    fn driver(self) -> Result<GER_Driver<'x, 'y, 'a, F>, BLASError> {
        let Self { x, y, a, alpha } = self;

        // initialize intent(hide)
        let incx = x.stride_of(Axis(0));
        let incy = y.stride_of(Axis(0));
        let m = x.len_of(Axis(0));
        let n = y.len_of(Axis(0));

        // prepare output
        let a = match a {
            Some(a) => {
                blas_assert_eq!(a.dim(), (m, n), InvalidDim)?;
                if a.view().is_fpref() {
                    ArrayOut2::ViewMut(a)
                } else {
                    let a_buffer = a.view().to_col_layout()?.into_owned();
                    ArrayOut2::ToBeCloned(a, a_buffer)
                }
            },
            None => ArrayOut2::Owned(Array2::zeros((m, n).f())),
        };
        let lda = a.view().stride_of(Axis(1));

        // finalize
        let driver = GER_Driver {
            m: m.try_into()?,
            n: n.try_into()?,
            alpha,
            x,
            incx: incx.try_into()?,
            y,
            incy: incy.try_into()?,
            a,
            lda: lda.try_into()?,
        };
        return Ok(driver);
    }
}

/* #endregion */

/* #region BLAS wrapper */

pub type GER<'x, 'y, 'a, F> = GER_Builder<'x, 'y, 'a, F>;
pub type SGER<'x, 'y, 'a> = GER<'x, 'y, 'a, f32>;
pub type DGER<'x, 'y, 'a> = GER<'x, 'y, 'a, f64>;
pub type CGERU<'x, 'y, 'a> = GER<'x, 'y, 'a, c32>;
pub type ZGERU<'x, 'y, 'a> = GER<'x, 'y, 'a, c64>;

impl<'x, 'y, 'a, F> BLASBuilder<'a, F, Ix2> for GER_Builder<'x, 'y, 'a, F>
where
    F: BLASFloat,
    BLASFunc: GERFunc<F>,
{
    fn run(self) -> Result<ArrayOut2<'a, F>, BLASError> {
        // initialize
        let obj = self.build()?;

        if obj.a.as_ref().map(|a| a.view().is_fpref()) == Some(true) {
            // F-contiguous
            return obj.driver()?.run_blas();
        } else {
            // C-contiguous
            let a = obj.a.map(|a| a.reversed_axes());
            let obj = GER_ { a, x: obj.y, y: obj.x, ..obj };
            let a = obj.driver()?.run_blas()?;
            return Ok(a.reversed_axes());
        }
    }
}

/* #endregion */