-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchunk.rs
422 lines (366 loc) Β· 12.6 KB
/
chunk.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::cmp::Ordering;
use std::convert::TryInto;
use std::hash::{Hash, Hasher};
use byteorder::LittleEndian as LE;
use zerocopy::byteorder::U64;
pub type ULE = U64<LE>;
/// A HDF5 chunk. A chunk is read and written in its entirety by the HDF5 library. This is
/// usually necessary since the chunk can be compressed and filtered.
///
///
/// Reference: [HDF5 chunking](https://support.hdfgroup.org/HDF5/doc/Advanced/Chunking/index.html).
#[derive(Debug, Eq, Clone)]
#[repr(C)]
pub struct Chunk<const D: usize> {
// WARNING: Do not alter repr, order or type of this struct or fields without verifying against
// slice operations.
/// Address or offset (bytes) in file where chunk starts.
pub addr: ULE,
/// Chunk size in bytes (storage size).
pub size: ULE,
/// Coordinates of offset in dataspace where the chunk begins.
pub offset: [ULE; D],
}
impl<const D: usize> Chunk<D> {
pub fn new(addr: u64, size: u64, offset: [u64; D]) -> Chunk<D> {
Chunk {
addr: ULE::new(addr),
size: ULE::new(size),
offset: offset
.iter()
.cloned()
.map(ULE::new)
.collect::<Vec<_>>()
.as_slice()
.try_into()
.unwrap(),
}
}
/// Is the point described by the indices inside the chunk (`Equal`), before (`Less`) or after
/// (`Greater`).
#[must_use]
pub fn contains(&self, i: &[u64], shape: &[u64]) -> Ordering {
assert!(i.len() == shape.len());
assert!(i.len() == self.offset.len());
for j in 0..i.len() {
if i[j] < self.offset[j].get() {
return Ordering::Less;
} else if i[j] >= self.offset[j].get() + shape[j] {
return Ordering::Greater;
}
}
Ordering::Equal
}
/// Reinterpret the Chunk as a slice of `u64`'s. This is ridiculously unsafe and I am not sure
/// if I know what I am doing.
///
/// Expressions in const-generics are not yet allowed.
pub fn as_u64s(&self) -> &[ULE] {
let ptr = self as *const Chunk<D>;
let slice: &[ULE] = unsafe {
let ptr = ptr as *const ULE;
std::slice::from_raw_parts(
ptr,
std::mem::size_of::<Self>() / std::mem::size_of::<ULE>(),
)
};
assert_eq!(
slice.len(),
std::mem::size_of::<Self>() / std::mem::size_of::<ULE>()
);
slice
}
/// Reinterpret a slice of `u64`s as a Chunk.
pub fn from_u64s(slice: &[ULE]) -> &Chunk<D> {
assert_eq!(
slice.len(),
std::mem::size_of::<Self>() / std::mem::size_of::<ULE>()
);
unsafe { &*(slice.as_ptr() as *const Chunk<D>) }
}
/// Reintepret a slice of `Chunk<D>`s to a slice of `u64`. This is efficient, but relies
/// on unsafe code.
pub fn slice_as_u64s(chunks: &[Chunk<D>]) -> &[ULE] {
let ptr = chunks.as_ptr() as *const Chunk<D>;
let slice: &[ULE] = unsafe {
let ptr = ptr as *const ULE;
std::slice::from_raw_parts(
ptr,
chunks.len() * std::mem::size_of::<Self>() / std::mem::size_of::<ULE>(),
)
};
assert_eq!(
slice.len(),
chunks.len() * std::mem::size_of::<Self>() / std::mem::size_of::<ULE>()
);
slice
}
/// Reintepret a slice of `u64`s to a slice of `Chunk<D>`. This is efficient, but relies
/// on unsafe code.
pub fn slice_from_u64s(slice: &[ULE]) -> &[Chunk<D>] {
assert_eq!(
slice.len() % (std::mem::size_of::<Self>() / std::mem::size_of::<ULE>()),
0
);
let n = slice.len() / (std::mem::size_of::<Self>() / std::mem::size_of::<ULE>());
let ptr = slice.as_ptr() as *const _;
let chunks: &[Chunk<D>] = unsafe { std::slice::from_raw_parts(ptr, n) };
assert_eq!(
slice.len(),
chunks.len() * std::mem::size_of::<Self>() / std::mem::size_of::<ULE>()
);
chunks
}
}
impl<const D: usize> Hash for Chunk<D> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.addr.hash(state);
}
}
impl<const D: usize> Ord for Chunk<D> {
fn cmp(&self, other: &Self) -> Ordering {
for (aa, bb) in self.offset.iter().zip(&other.offset) {
match aa.get().cmp(&bb.get()) {
Ordering::Greater => return Ordering::Greater,
Ordering::Less => return Ordering::Less,
Ordering::Equal => (),
}
}
Ordering::Equal
}
}
impl<const D: usize> PartialOrd for Chunk<D> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<const D: usize> PartialEq for Chunk<D> {
fn eq(&self, other: &Self) -> bool {
self.addr == other.addr
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn alignment() {
assert_eq!(std::mem::align_of::<Chunk<1>>(), 1);
assert_eq!(std::mem::align_of::<Chunk<2>>(), 1);
assert_eq!(std::mem::align_of::<Chunk<3>>(), 1);
assert_eq!(std::mem::align_of::<Chunk<8>>(), 1);
}
#[test]
fn ordering() {
let mut v = vec![
Chunk::new(5, 10, [10, 0, 0]),
Chunk::new(50, 10, [0, 0, 0]),
Chunk::new(1, 1, [10, 10, 0]),
Chunk::new(1, 1, [0, 10, 0]),
Chunk::new(1, 1, [0, 0, 10]),
];
v.sort();
assert_eq!(v[0].offset, [ULE::new(0), ULE::new(0), ULE::new(0)]);
assert_eq!(v[1].offset, [ULE::new(0), ULE::new(0), ULE::new(10)]);
assert_eq!(v[2].offset, [ULE::new(0), ULE::new(10), ULE::new(0)]);
assert_eq!(v[3].offset, [ULE::new(10), ULE::new(0), ULE::new(0)]);
assert_eq!(v[4].offset, [ULE::new(10), ULE::new(10), ULE::new(0)]);
}
#[test]
fn contains() {
let shape = [10, 10];
let c = Chunk::new(0, 10, [10, 10]);
assert_eq!(c.contains(&[0, 0], &shape), Ordering::Less);
assert_eq!(c.contains(&[5, 0], &shape), Ordering::Less);
assert_eq!(c.contains(&[10, 0], &shape), Ordering::Less);
assert_eq!(c.contains(&[10, 10], &shape), Ordering::Equal);
assert_eq!(c.contains(&[5, 10], &shape), Ordering::Less);
assert_eq!(c.contains(&[10, 15], &shape), Ordering::Equal);
assert_eq!(c.contains(&[15, 15], &shape), Ordering::Equal);
assert_eq!(c.contains(&[15, 10], &shape), Ordering::Equal);
assert_eq!(c.contains(&[20, 20], &shape), Ordering::Greater);
assert_eq!(c.contains(&[25, 20], &shape), Ordering::Greater);
assert_eq!(c.contains(&[25, 10], &shape), Ordering::Greater);
assert_eq!(c.contains(&[10, 25], &shape), Ordering::Greater);
assert_eq!(c.contains(&[5, 25], &shape), Ordering::Less);
assert_eq!(c.contains(&[25, 5], &shape), Ordering::Greater);
}
mod serde {
use super::*;
use test::Bencher;
#[test]
fn as_u64s() {
let c = Chunk::new(2, 7, [10, 10]);
let s = c.as_u64s();
println!("{:?} -> {:?}", c, s);
assert_eq!(s, [ULE::new(2), ULE::new(7), ULE::new(10), ULE::new(10)]);
// odd number of dims
let c = Chunk::new(2, 7, [10, 5, 10]);
let s = c.as_u64s();
println!("{:?} -> {:?}", c, s);
assert_eq!(
s,
[
ULE::new(2),
ULE::new(7),
ULE::new(10),
ULE::new(5),
ULE::new(10)
]
);
let c = Chunk::new(2, 7, [10, 5, 3, 10]);
let s = c.as_u64s();
println!("{:?} -> {:?}", c, s);
assert_eq!(
s,
[
ULE::new(2),
ULE::new(7),
ULE::new(10),
ULE::new(5),
ULE::new(3),
ULE::new(10)
]
);
}
#[test]
fn from_u64s() {
let s = [ULE::new(2), ULE::new(7), ULE::new(10), ULE::new(10)];
let c = Chunk::<2>::from_u64s(&s);
println!("{:?} -> {:?}", s, c);
assert_eq!(c, &Chunk::new(2, 7, [10, 10]));
// odd number of dims
let s = [
ULE::new(2),
ULE::new(7),
ULE::new(10),
ULE::new(5),
ULE::new(10),
];
let c = Chunk::<3>::from_u64s(&s);
println!("{:?} -> {:?}", s, c);
assert_eq!(c, &Chunk::new(2, 7, [10, 5, 10]));
let s = [
ULE::new(2),
ULE::new(7),
ULE::new(10),
ULE::new(5),
ULE::new(3),
ULE::new(10),
];
let c = Chunk::<4>::from_u64s(&s);
println!("{:?} -> {:?}", s, c);
assert_eq!(c, &Chunk::new(2, 7, [10, 5, 3, 10]));
}
#[test]
fn roundtrip() {
let c = Chunk::new(2, 7, [10, 5, 10]);
let s = c.as_u64s();
assert_eq!(
s,
[
ULE::new(2),
ULE::new(7),
ULE::new(10),
ULE::new(5),
ULE::new(10)
]
);
let c2 = Chunk::<3>::from_u64s(s);
assert_eq!(&c, c2);
}
#[test]
fn slice_as_u64s() {
let cs = [Chunk::new(2, 7, [10, 10]), Chunk::new(3, 17, [20, 20])];
let s = Chunk::<2>::slice_as_u64s(&cs);
println!("{:?} -> {:?}", cs, s);
assert_eq!(
s,
[
ULE::new(2),
ULE::new(7),
ULE::new(10),
ULE::new(10),
ULE::new(3),
ULE::new(17),
ULE::new(20),
ULE::new(20)
]
);
let cs = [
Chunk::new(2, 7, [10, 10, 15]),
Chunk::new(3, 17, [20, 20, 30]),
];
let s = Chunk::<3>::slice_as_u64s(&cs);
println!("{:?} -> {:?}", cs, s);
assert_eq!(
s,
[
ULE::new(2),
ULE::new(7),
ULE::new(10),
ULE::new(10),
ULE::new(15),
ULE::new(3),
ULE::new(17),
ULE::new(20),
ULE::new(20),
ULE::new(30)
]
);
}
#[test]
fn slice_from_u64s() {
let s = [
ULE::new(2),
ULE::new(7),
ULE::new(10),
ULE::new(10),
ULE::new(3),
ULE::new(17),
ULE::new(20),
ULE::new(20),
];
let cs = [Chunk::new(2, 7, [10, 10]), Chunk::new(3, 17, [20, 20])];
let dcs = Chunk::<2>::slice_from_u64s(&s);
println!("{:?} -> {:?}", s, dcs);
assert_eq!(dcs, cs);
let s = [
ULE::new(2),
ULE::new(7),
ULE::new(10),
ULE::new(10),
ULE::new(15),
ULE::new(3),
ULE::new(17),
ULE::new(20),
ULE::new(20),
ULE::new(30),
];
let cs = [
Chunk::new(2, 7, [10, 10, 15]),
Chunk::new(3, 17, [20, 20, 30]),
];
let dcs = Chunk::<3>::slice_from_u64s(&s);
println!("{:?} -> {:?}", s, dcs);
assert_eq!(dcs, cs);
}
#[bench]
fn slice_from_u64s_10k_3d(b: &mut Bencher) {
let chunks: Vec<Chunk<3>> = (0..10000)
.map(|i| Chunk::new(i * 10, 300, [i * 10, i * 100, i * 10000]))
.collect();
assert_eq!(chunks.len(), 10000);
let slice = Chunk::<3>::slice_as_u64s(chunks.as_slice());
assert_eq!(
slice.len(),
10000 * std::mem::size_of::<Chunk<3>>() / std::mem::size_of::<u64>()
);
let dechunks = Chunk::<3>::slice_from_u64s(&slice);
assert_eq!(dechunks.len(), chunks.len());
assert_eq!(dechunks, chunks.as_slice());
b.iter(|| {
test::black_box(Chunk::<3>::slice_from_u64s(&slice));
});
}
}
}