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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299/** Algorithms for converting 2D coordinates to and from the Hilbert index.
Here the Hilbert curve has been scaled and discretized, so that the
range {0, 1, ..., n^2 - 1} is mapped to coordinates
{0, 1, ..., n-1} x {0, 1, ..., n-1}. In the classical Hilbert curve,
the continuous interval [0,1] is mapped to the unit square [0,1]^2.
*/
use rand::{distributions::Uniform, Rng};
use rand_chacha::ChaCha8Rng;
use std::collections::VecDeque;
pub type Coordinates = (usize, usize);
type Matrix = Vec<i32>;
pub type Vector = Vec<i32>;
#[inline]
pub fn log2(n: usize) -> usize {
(n as f64).log2().floor() as usize
}
/// Create a matrix.
/// note that the representation Vec of Vec is not optimal.
pub fn make_matrix<R: rand::Rng>(n: usize, low: i32, high: i32, rng: &mut R) -> Matrix {
let range = Uniform::new(low, high);
(0..(n * n)).map(|_| rng.sample(&range)).collect()
}
/// Naive product
#[allow(non_snake_case)]
pub fn naive_matrix_vector_product(A: &Matrix, v: &Vector, output: &mut Vector, n: usize) {
// // TODO: put asserts here to make sure no bounds checking happens.
// assert_eq!(output.len(), n);
// assert_eq!(A.len(), n * n);
// assert_eq!(v.len(), n);
for i in 0..n {
for j in 0..n {
output[i] += A[flat_index(i, j, n)] * v[j];
}
}
}
/// Converts [i][j] into [n*i+j]
#[inline]
fn flat_index(i: usize, j: usize, n: usize) -> usize {
n * i + j
}
/// Flatten matrix A according to the provided Hilbert coordinates.
#[allow(non_snake_case)]
pub fn flatten_matrix(depth: usize, A: Vec<i32>, n: usize) -> Vector {
let mut flattened_A = vec![0; n * n];
for (t, (i, j)) in HilbertIter::new(depth) {
flattened_A[t] = A[flat_index(i, j, n)];
}
flattened_A
}
#[allow(non_snake_case)]
pub fn hilbert_matrix_vector_product(
flattened_A: &Vector,
v: &Vector,
output: &mut Vector,
hilbert_iter: &Vec<(usize, Coordinates)>,
) {
for (t, (i, j)) in hilbert_iter {
output[*i] += flattened_A[*t] * v[*j];
}
}
/// `hilbert_matrix_vector_product` but Hilbert index is an iterator.
#[allow(non_snake_case)]
pub fn hilbert_matrix_vector_product_iter(
flattened_A: &Vector,
v: &Vector,
output: &mut Vector,
depth: usize,
) {
for (t, (i, j)) in HilbertIter::new(depth) {
output[i] += flattened_A[t] * v[j];
}
}
struct HilbertIter {
/// Number of steps remaining
n: usize,
index: usize,
i: usize,
j: usize,
queue: VecDeque<(char, usize)>,
buffer: Option<(usize, Coordinates)>,
}
impl HilbertIter {
pub fn new(depth: usize) -> Self {
let n = 2usize.pow(depth as u32);
let n = n * n + 1;
let queue = VecDeque::from([('H', depth)]);
Self {
n,
index: 1,
i: 0,
j: 0,
queue,
buffer: Some((0, (0, 0))),
}
}
fn step(&mut self) {
while self.buffer.is_none() && !self.queue.is_empty() {
let (symbol, depth) = self.queue.pop_front().unwrap();
if depth == 0 {
let non_terminal = match symbol {
'โ' => {
self.i += 1;
true
}
'โ' => {
self.i -= 1;
true
}
'โ' => {
self.j += 1;
true
}
'โ' => {
self.j -= 1;
true
}
_ => false,
};
if non_terminal {
self.buffer = Some((self.index, (self.i, self.j)));
self.index += 1;
}
}
if depth > 0 {
match symbol {
'H' => {
self.queue.push_back(('A', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('H', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('H', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('B', depth - 1));
}
'A' => {
self.queue.push_back(('H', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('A', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('A', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('C', depth - 1));
}
'B' => {
self.queue.push_back(('C', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('B', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('B', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('H', depth - 1));
}
'C' => {
self.queue.push_back(('B', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('C', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('C', depth - 1));
self.queue.push_back(('โ', depth - 1));
self.queue.push_back(('A', depth - 1));
}
_ => {
// # terminal up/down/left/right symbols
// # must be preserved until the end
self.queue.push_back((symbol, depth - 1));
}
};
}
}
}
}
impl Iterator for HilbertIter {
type Item = (usize, Coordinates);
fn next(&mut self) -> Option<Self::Item> {
if self.n > 0 {
self.n -= 1;
} else {
panic!("Hilbert iteration is out of bounds");
}
self.step();
self.buffer.take()
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.n, Some(self.n))
}
}
/// Generate (A, v) as inputs for matrix multiplication
pub fn setup_inputs(n: usize, rng: &mut ChaCha8Rng) -> (Vec<i32>, Vec<i32>) {
let range = Uniform::new(1, 11);
#[allow(non_snake_case)]
let A = make_matrix(n, 1, 11, rng);
let v: Vec<_> = (0..n).map(|_| rng.sample(&range)).collect();
assert_eq!(v.len(), n);
(A, v)
}
/// Setup (coordinates, flattened_A) for Hilbert multiplication
#[allow(non_snake_case)]
pub fn setup_hilbert(n: usize, A: Vec<i32>) -> (Vec<(usize, Coordinates)>, Vec<i32>) {
assert_eq!(n * n, A.len());
let depth: usize = log2(n);
let hilbert_iter: Vec<_> = HilbertIter::new(depth).collect();
println!("Hilbert matrix size: {}", hilbert_iter.len());
#[allow(non_snake_case)]
let flattened_A = flatten_matrix(depth, A, n);
(hilbert_iter, flattened_A)
}
/// Setup (depth, flattened_A) for Hilbert multiplication
#[allow(non_snake_case)]
pub fn setup_hilbert_iter(n: usize, A: Vec<i32>) -> (usize, Vec<i32>) {
assert_eq!(n * n, A.len());
let depth: usize = log2(n);
#[allow(non_snake_case)]
let flattened_A = flatten_matrix(depth, A, n);
(depth, flattened_A)
}
#[cfg(test)]
mod test {
use std::time::{self};
use insta::assert_yaml_snapshot;
use rand::distributions::Uniform;
use rand::Rng;
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use timeit::timeit_loops;
use crate::{
hilbert_matrix_vector_product, hilbert_matrix_vector_product_iter, make_matrix,
naive_matrix_vector_product,
};
#[test]
fn test_equality_of_implementation() {
let mut rng = ChaCha8Rng::seed_from_u64(10);
let range = Uniform::new(1, 11);
let n: usize = 2usize.pow(8);
let start = time::Instant::now();
// A = [[random.randint(1, 10) for _ in range(n)] for _ in range(n)]
#[allow(non_snake_case)]
let A = make_matrix(n, 1, 11, &mut rng);
// v = [random.randint(1, 10) for _ in range(n)]
let v: Vec<_> = (0..n).map(|_| rng.sample(&range)).collect();
assert_eq!(v.len(), n);
let mut output1 = vec![0; n];
let mut output2 = vec![0; n];
let mut output3 = vec![0; n];
let end = time::Instant::now();
println!("Initial data generation: {}s", (end - start).as_secs_f32());
// Naive
let timeit_count = 3;
let _ = timeit_loops! {timeit_count,
{ naive_matrix_vector_product(&A, &v, &mut output1, n); }
};
// reorder data
#[allow(non_snake_case)]
let (depth, flattened_A) = super::setup_hilbert_iter(n, A.clone());
// Hilbert Product
let _ = timeit_loops! {timeit_count,
{hilbert_matrix_vector_product_iter(&flattened_A,&v, &mut output2, depth);}
};
// non-iterative version
let (hilbert_iter, flattened_a) = super::setup_hilbert(n, A);
let _ = timeit_loops! {timeit_count,
{hilbert_matrix_vector_product(&flattened_a,&v, &mut output3, &hilbert_iter);}
};
assert_eq!(output1, output2);
assert_eq!(output1, output3);
assert_yaml_snapshot!(output2);
}
}