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
use crate::{cpu::row_op, row_op_slice_lhs, Matrix};
use custos::{cpu::CPU, get_device, number::Number, CDatatype};
#[cfg(feature = "opencl")]
use crate::{cl_to_cpu_lr, opencl};
#[cfg(feature = "opencl")]
use custos::CLDevice;
#[cfg(feature = "cuda")]
use crate::{cu_to_cpu_lr, cu_to_cpu_lr_mut};
#[cfg(feature = "cuda")]
use custos::CudaDevice;
impl<'a, T: CDatatype> Matrix<'a, T> {
pub fn add_row(&self, rhs: &Matrix<T>) -> Matrix<'a, T> {
let device = get_device!(self.device(), RowOp<T>);
device.add_row(self, rhs)
}
pub fn add_row_mut(&mut self, rhs: &Matrix<'a, T>) {
let device = get_device!(self.device(), RowOp<T>);
device.add_row_mut(self, rhs)
}
}
pub trait RowOp<T> {
fn add_row(&self, lhs: &Matrix<T>, rhs: &Matrix<T>) -> Matrix<T>;
fn add_row_mut(&self, lhs: &mut Matrix<T>, rhs: &Matrix<T>);
}
impl<T: Number> RowOp<T> for CPU {
fn add_row(&self, lhs: &Matrix<T>, rhs: &Matrix<T>) -> Matrix<T> {
row_op(self, lhs, rhs, |c, a, b| *c = a + b)
}
fn add_row_mut(&self, lhs: &mut Matrix<T>, rhs: &Matrix<T>) {
let (lhs_rows, lhs_cols) = lhs.dims();
row_op_slice_lhs(lhs, lhs_rows, lhs_cols, rhs, |c, a| *c += a)
}
}
#[cfg(feature = "opencl")]
impl<T: CDatatype> RowOp<T> for CLDevice {
fn add_row(&self, lhs: &Matrix<T>, rhs: &Matrix<T>) -> Matrix<T> {
cl_to_cpu_lr(self, lhs, rhs, |device, lhs, rhs| device.add_row(lhs, rhs))
}
fn add_row_mut(&self, lhs: &mut Matrix<T>, rhs: &Matrix<T>) {
opencl::cpu_exec_lhs_rhs_mut(self, lhs, rhs, |cpu, lhs, rhs| cpu.add_row_mut(lhs, rhs))
.unwrap();
}
}
#[cfg(feature = "cuda")]
impl<T: CDatatype> RowOp<T> for CudaDevice {
fn add_row(&self, lhs: &Matrix<T>, rhs: &Matrix<T>) -> Matrix<T> {
cu_to_cpu_lr(self, lhs, rhs, |device, lhs, rhs| device.add_row(lhs, rhs))
}
fn add_row_mut(&self, lhs: &mut Matrix<T>, rhs: &Matrix<T>) {
cu_to_cpu_lr_mut(self, lhs, rhs, |device, lhs, rhs| {
device.add_row_mut(lhs, rhs)
})
}
}