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
use kernel::GemmKernel;
use archparam;
pub enum Gemm { }
pub type T = f64;
const MR: usize = 8;
const NR: usize = 4;
macro_rules! loop_m {
($i:ident, $e:expr) => { loop8!($i, $e) };
}
macro_rules! loop_n {
($j:ident, $e:expr) => { loop4!($j, $e) };
}
impl GemmKernel for Gemm {
type Elem = T;
#[inline(always)]
fn align_to() -> usize { 0 }
#[inline(always)]
fn mr() -> usize { MR }
#[inline(always)]
fn nr() -> usize { NR }
#[inline(always)]
fn always_masked() -> bool { true }
#[inline(always)]
fn nc() -> usize { archparam::D_NC }
#[inline(always)]
fn kc() -> usize { archparam::D_KC }
#[inline(always)]
fn mc() -> usize { archparam::D_MC }
#[inline(always)]
unsafe fn kernel(
k: usize,
alpha: T,
a: *const T,
b: *const T,
beta: T,
c: *mut T, rsc: isize, csc: isize) {
kernel(k, alpha, a, b, beta, c, rsc, csc)
}
}
#[inline(always)]
pub unsafe fn kernel(k: usize, alpha: T, a: *const T, b: *const T,
beta: T, c: *mut T, rsc: isize, csc: isize)
{
let mut ab: [[T; NR]; MR] = ::std::mem::uninitialized();
let mut a = a;
let mut b = b;
debug_assert_eq!(beta, 0.); loop_m!(i, loop_n!(j, ab[i][j] = 0.));
unroll_by!(4 => k, {
loop_m!(i, loop_n!(j, ab[i][j] += at(a, i) * at(b, j)));
a = a.offset(MR as isize);
b = b.offset(NR as isize);
});
macro_rules! c {
($i:expr, $j:expr) => (c.offset(rsc * $i as isize + csc * $j as isize));
}
loop_m!(i, loop_n!(j, *c![i, j] = alpha * ab[i][j]));
}
#[inline(always)]
unsafe fn at(ptr: *const T, i: usize) -> T {
*ptr.offset(i as isize)
}
#[test]
fn test_gemm_kernel() {
let mut a = [1.; 32];
let mut b = [0.; 16];
for (i, x) in a.iter_mut().enumerate() {
*x = i as f64;
}
for i in 0..4 {
b[i + i * 4] = 1.;
}
let mut c = [0.; 32];
unsafe {
kernel(4, 1., &a[0], &b[0],
0., &mut c[0], 1, 8);
}
assert_eq!(&a, &c);
}