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
use crate::{assign_to_lhs_scalar, Matrix};
use custos::{get_device, CPU, CDatatype};
use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign};

#[cfg(feature = "opencl")]
use custos::CLDevice;
#[cfg(feature = "opencl")]
use crate::cl_assign_scalar;

#[cfg(feature = "cuda")]
use custos::CudaDevice;
#[cfg(feature = "cuda")]
use crate::cu_assign_scalar;

pub trait ScalarAssign<T> {
    fn adds_assign(&self, lhs: &mut Matrix<T>, rhs: T);
    fn muls_assign(&self, lhs: &mut Matrix<T>, rhs: T);
    fn divs_assign(&self, lhs: &mut Matrix<T>, rhs: T);
    fn subs_assign(&self, lhs: &mut Matrix<T>, rhs: T);
}

impl<T> ScalarAssign<T> for CPU
where
    T: Copy + AddAssign + MulAssign + DivAssign + SubAssign,
{
    fn adds_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        assign_to_lhs_scalar(lhs, rhs, |x, y| *x += y);
    }

    fn muls_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        assign_to_lhs_scalar(lhs, rhs, |x, y| *x *= y);
    }

    fn divs_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        assign_to_lhs_scalar(lhs, rhs, |x, y| *x /= y);
    }

    fn subs_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        assign_to_lhs_scalar(lhs, rhs, |x, y| *x -= y);
    }
}

#[cfg(feature = "opencl")]
impl<T: CDatatype> ScalarAssign<T> for CLDevice {
    fn adds_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        cl_assign_scalar(self, lhs, rhs, "+").unwrap();
    }

    fn muls_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        cl_assign_scalar(self, lhs, rhs, "*").unwrap();
    }

    fn divs_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        cl_assign_scalar(self, lhs, rhs, "/").unwrap();
    }

    fn subs_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        cl_assign_scalar(self, lhs, rhs, "-").unwrap();
    }
}

#[cfg(feature = "cuda")]
impl<T: CDatatype> ScalarAssign<T> for CudaDevice {
    fn adds_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        cu_assign_scalar(self, lhs, rhs, "+").unwrap();
    }

    fn muls_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        cu_assign_scalar(self, lhs, rhs, "*").unwrap();
    }

    fn divs_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        cu_assign_scalar(self, lhs, rhs, "/").unwrap();
    }

    fn subs_assign(&self, lhs: &mut Matrix<T>, rhs: T) {
        cu_assign_scalar(self, lhs, rhs, "-").unwrap();
    }
}

impl<T: CDatatype> AddAssign<T> for Matrix<'_, T> {
    fn add_assign(&mut self, rhs: T) {
        get_device!(self.device(), ScalarAssign<T>).adds_assign(self, rhs);
    }
}

impl<T: CDatatype> MulAssign<T> for Matrix<'_, T> {
    fn mul_assign(&mut self, rhs: T) {
        get_device!(self.device(), ScalarAssign<T>).muls_assign(self, rhs);
    }
}

impl<T: CDatatype> DivAssign<T> for Matrix<'_, T> {
    fn div_assign(&mut self, rhs: T) {
        get_device!(self.device(), ScalarAssign<T>).divs_assign(self, rhs);
    }
}