-
Notifications
You must be signed in to change notification settings - Fork 25
/
sgemm_kernel.rs
117 lines (98 loc) · 2.74 KB
/
sgemm_kernel.rs
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
// Copyright 2016 bluss
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use kernel::GemmKernel;
use archparam;
pub enum Gemm { }
pub type T = f32;
const MR: usize = 4;
const NR: usize = 8;
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::S_NC }
#[inline(always)]
fn kc() -> usize { archparam::S_KC }
#[inline(always)]
fn mc() -> usize { archparam::S_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)
}
}
/// matrix multiplication kernel
///
/// This does the matrix multiplication:
///
/// C ← α A B + β C
///
/// + k: length of data in a, b
/// + a, b are packed
/// + c has general strides
/// + rsc: row stride of c
/// + csc: col stride of c
/// + if beta is 0, then c does not need to be initialized
#[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 = [[0.; NR]; MR];
let mut a = a;
let mut b = b;
debug_assert_eq!(beta, 0.); // always masked
// Compute matrix multiplication into ab[i][j]
unroll_by_8!(k, {
let v0: [_; MR] = [at(a, 0), at(a, 1), at(a, 2), at(a, 3)];
let v1: [_; NR] = [at(b, 0), at(b, 1), at(b, 2), at(b, 3),
at(b, 4), at(b, 5), at(b, 6), at(b, 7)];
loop4!(i, loop8!(j, ab[i][j] += v0[i] * v1[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));
}
// set C = α A B
for j in 0..NR {
loop4!(i, *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.; 16];
let mut b = [0.; 32];
for (i, x) in a.iter_mut().enumerate() {
*x = i as f32;
}
for i in 0..4 {
b[i + i * 8] = 1.;
}
let mut c = [0.; 16];
unsafe {
kernel(4, 1., &a[0], &b[0], 0., &mut c[0], 1, 4);
// col major C
}
assert_eq!(&a, &c);
}