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
use log::debug;
extern "C" {
fn matMulVecPacked(out: *mut u32, a: *const u32, b: *const u32, a_rows: usize, a_cols: usize);
fn matMulVecPacked2(
out: *mut u32,
a: *const u32,
b_full: *const u32,
a_rows: usize,
a_cols: usize,
);
fn matMulVecPacked4(
out: *mut u32,
a: *const u32,
b_full: *const u32,
a_rows: usize,
a_cols: usize,
);
fn matMulVecPacked8(
out: *mut u32,
a: *const u32,
b_full: *const u32,
a_rows: usize,
a_cols: usize,
);
}
/// Returns `out = a * b` where `a` is a matrix of shape `(a_rows, a_cols)` and
/// `b` is a vector of shape `(b_rows, b_cols)`. `out` is a vector of shape
/// `(a_rows, b_cols)`.
///
/// `a_cols * 4` must equal `b_rows`. `b_cols` must be 1. `out` must have length
/// greater than or equal to `a_rows + 8`.
pub fn matmul_vec_packed(
out: &mut [u32],
a: &[u32],
b: &[u32],
a_rows: usize,
a_cols: usize,
b_rows: usize,
b_cols: usize,
) {
debug!(
"matmul_vec_packed: a_rows: {}, a_cols: {}, b_rows: {}, b_cols: {}",
a_rows, a_cols, b_rows, b_cols
);
assert_eq!(a.len(), a_rows * a_cols);
assert_eq!(b.len(), b_rows * b_cols);
assert_eq!(a_cols * 4, b_rows);
assert!(out.len() >= a_rows + 8);
unsafe {
if b_cols == 1 {
matMulVecPacked(out.as_mut_ptr(), a.as_ptr(), b.as_ptr(), a_rows, a_cols);
} else if b_cols == 2 {
matMulVecPacked2(out.as_mut_ptr(), a.as_ptr(), b.as_ptr(), a_rows, a_cols);
} else if b_cols == 4 {
matMulVecPacked4(out.as_mut_ptr(), a.as_ptr(), b.as_ptr(), a_rows, a_cols);
// } else if b_cols == 6 {
// matMulVecPacked6(out.as_mut_ptr(), a.as_ptr(), b.as_ptr(), a_rows, a_cols);
} else if b_cols == 8 {
matMulVecPacked8(out.as_mut_ptr(), a.as_ptr(), b.as_ptr(), a_rows, a_cols);
} else {
panic!("b_cols must be 1, 2, 4, or 8");
}
}
}
#[cfg(test)]
mod test {
use std::time::Instant;
use super::*;
#[test]
#[ignore]
fn test_matmul_vec_packed() {
let a_rows = 32768;
let a_cols = 8192;
let b_rows = a_cols * 4;
let mut a = vec![0u32; a_rows * a_cols];
for i in 0..a.len() {
a[i] = fastrand::u32(..);
}
let mut b = vec![0u32; b_rows];
for i in 0..b.len() {
b[i] = fastrand::u32(..);
}
let mut out = vec![0u32; a_rows + 8];
let now = Instant::now();
matmul_vec_packed(&mut out, &a, &b, a_rows, a_cols, b_rows, 1);
debug!("matmul_vec_packed: {:?}", now.elapsed());
let now = Instant::now();
matmul_vec_packed(&mut out, &a, &b, a_rows, a_cols, b_rows, 1);
debug!("matmul_vec_packed: {:?}", now.elapsed());
}
#[test]
#[ignore]
fn test_matmul_vec_packed_8() {
// 32 GB:
// matmul_vec_packed: a_rows: 131072, a_cols: 65536, b_rows: 262144, b_cols: 8
// let a_rows = 131072;
// let a_cols = 65536;
// 10922 x 3642
// let a_rows = 131072;
// let a_cols = 131072 / 4;
//185363, m=185365
let a_rows = 184320; //131072;
let a_true_cols = 184320 + 1000; //262144;
let b_cols = 8;
let a_cols = (a_true_cols) / 4;
let b_rows = a_cols * 4;
// let mut a_rows = 131072; //(185363 / 8) * 8;
// let mut a_cols = (131072 + 131072 / 16) / 4; //(131072 + 511) / 4; //(185363 / 4 / 8) * 8;
// debug!("a_rows: {}, a_cols: {}", a_rows, a_cols);
// a_rows = a_rows / 8 * 8;
// a_cols = a_cols / 8 * 8;
debug!("a_rows: {}, a_cols: {}", a_rows, a_cols);
// let a_rows = ((131072.0 * (4.0 / 3.0)) as usize / 8) * 8;
// let a_cols = ((131072.0 / 4.0 * (4.0 / 3.0)) as usize / 8) * 8;
let mut a = vec![0u32; a_rows * a_cols];
for i in 0..a.len() {
a[i] = fastrand::u32(..);
}
let mut b = vec![0u32; b_rows * b_cols];
for i in 0..b.len() {
b[i] = fastrand::u32(..);
}
let mut out = vec![0u32; (a_rows + 8) * b_cols];
let now = Instant::now();
matmul_vec_packed(&mut out, &a, &b, a_rows, a_cols, b_rows, b_cols);
debug!("matmul_vec_packed: {:?}", now.elapsed());
// let now = Instant::now();
// matmul_vec_packed(&mut out, &a, &b, a_rows, a_cols, b_rows, b_cols);
// debug!("matmul_vec_packed: {:?}", now.elapsed());
}
}