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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use anyhow::{anyhow, ensure};
use itertools::izip;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cmp::min;
use std::convert::TryInto;
use std::path::Path;
use strength_reduce::StrengthReducedU64;
use super::super::chunk::{Chunk, ULE};
use super::types::*;
use super::{DatasetExt, DatasetExtReader};
use crate::extent::Extents;
use crate::filters::byteorder::Order as ByteOrder;
/// A HDF5 dataset (a single variable).
///
/// > Note to reader implementations: The official HDF5 library uses a 1MB dataset cache by default.
///
#[derive(Debug, Serialize, Deserialize)]
pub struct Dataset<'a, const D: usize> {
pub dtype: Datatype,
pub dsize: usize,
pub order: ByteOrder,
#[serde(borrow)]
#[serde(with = "super::super::serde::chunks_u64s")]
pub chunks: Cow<'a, [Chunk<D>]>,
#[serde(with = "super::super::serde::arr_u64")]
pub shape: [u64; D],
#[serde(with = "super::super::serde::arr_u64")]
pub chunk_shape: [u64; D],
#[serde(with = "super::super::serde::sr_u64")]
chunk_shape_reduced: [StrengthReducedU64; D],
#[serde(with = "super::super::serde::arr_u64")]
pub scaled_dim_sz: [u64; D],
#[serde(with = "super::super::serde::arr_u64")]
pub dim_sz: [u64; D],
#[serde(with = "super::super::serde::arr_u64")]
pub chunk_dim_sz: [u64; D],
pub shuffle: bool,
pub gzip: Option<u8>,
}
impl<const D: usize> Dataset<'_, D> {
pub fn index(ds: &hdf5::Dataset) -> Result<Dataset<'static, D>, anyhow::Error> {
use hdf5::filters::Filter;
ensure!(ds.ndim() == D, "Dataset rank does not match.");
let filters = ds.filters();
let mut shuffle = false;
let mut gzip = None;
for f in filters {
match f {
Filter::Shuffle => shuffle = true,
Filter::Deflate(z) => gzip = Some(z),
_ => return Err(anyhow!("{}: Unsupported filter", ds.name())),
}
}
let dtype = ds.dtype()?;
let order = dtype.byte_order();
let shape: [u64; D] = ds
.shape()
.into_iter()
.map(|u| u as u64)
.collect::<Vec<u64>>()
.as_slice()
.try_into()?;
let chunk_shape = ds.chunk().map_or_else(
|| shape,
|cs| {
cs.into_iter()
.map(|u| u as u64)
.collect::<Vec<u64>>()
.as_slice()
.try_into()
.unwrap()
},
);
let mut chunks: Vec<Chunk<D>> = match (ds.is_chunked(), ds.offset()) {
// Continuous
(false, Some(offset)) => Ok::<_, anyhow::Error>(vec![Chunk {
offset: [ULE::ZERO; D],
size: ULE::new(ds.storage_size()),
addr: ULE::new(offset),
}]),
// Chunked
(true, None) => {
let n = ds.num_chunks().expect("weird..");
let mut v = Vec::with_capacity(n);
ds.chunks_visit(|ci| {
v.push(Chunk {
offset: ci
.offset
.iter()
.copied()
.map(ULE::new)
.collect::<Vec<_>>()
.as_slice()
.try_into()
.unwrap(),
size: ULE::new(ci.size),
addr: ULE::new(ci.addr),
});
0
})?;
Ok(v)
}
_ => Err(anyhow!(
"{}: Unsupported data layout (chunked: {}, offset: {:?})",
ds.name(),
ds.is_chunked(),
ds.offset()
)),
}?;
chunks.sort();
let chunks = Cow::from(chunks);
{
let expected_chunks = shape
.iter()
.zip(&chunk_shape)
.map(|(s, c)| s.div_ceil(*c))
.product::<u64>() as usize;
ensure!(
chunks.len() == expected_chunks,
"{}: unexpected number of chunks given dataset size (is_chunked: {}, chunks: {} != {} (expected), shape: {:?}, chunk shape: {:?})",
ds.name(),
ds.is_chunked(),
chunks.len(),
expected_chunks,
shape,
chunk_shape);
}
Dataset::new(
dtype.into(),
order.into(),
shape,
chunks,
chunk_shape,
shuffle,
gzip,
)
}
pub fn new<'a, C>(
dtype: Datatype,
order: ByteOrder,
shape: [u64; D],
chunks: C,
chunk_shape: [u64; D],
shuffle: bool,
gzip: Option<u8>,
) -> Result<Dataset<'a, D>, anyhow::Error>
where
C: Into<Cow<'a, [Chunk<D>]>>,
{
let chunks = chunks.into();
let dsize = dtype.dsize();
// optimized divisor for chunk shape
let chunk_shape_reduced = chunk_shape
.iter()
.map(|c| StrengthReducedU64::new(*c))
.collect::<Vec<_>>()
.as_slice()
.try_into()?;
// scaled dimension size: dimension size of dataset in chunk offset coordinates.
// the dimension size is rounded up. when the dataset size is not a multiple of
// chunk size we have a partially filled chunk which is also present in the list of chunks.
let scaled_dim_sz: [u64; D] = {
let mut d = shape
.iter()
.zip(&chunk_shape)
.map(|(d, z)| d.div_ceil(*z))
.rev()
.scan(1, |p, c| {
let sz = *p;
*p *= c;
Some(sz)
})
.collect::<Vec<u64>>();
d.reverse();
d
}
.as_slice()
.try_into()?;
// size of dataset dimensions
let dim_sz: [u64; D] = {
let mut d = shape
.iter()
.rev()
.scan(1, |p, &c| {
let sz = *p;
*p *= c;
Some(sz)
})
.collect::<Vec<u64>>();
d.reverse();
d
}
.as_slice()
.try_into()?;
// size of chunk dimensions
let chunk_dim_sz: [u64; D] = {
let mut d = chunk_shape
.iter()
.rev()
.scan(1, |p, &c| {
let sz = *p;
*p *= c;
Some(sz)
})
.collect::<Vec<u64>>();
d.reverse();
d
}
.as_slice()
.try_into()?;
Ok(Dataset {
dtype,
dsize,
order,
chunks,
shape,
chunk_shape,
chunk_shape_reduced,
scaled_dim_sz,
dim_sz,
chunk_dim_sz,
shuffle,
gzip,
})
}
/// Number of elements in dataset.
#[must_use]
pub fn size(&self) -> usize {
self.shape.iter().product::<u64>() as usize
}
/// Dataset contains a single scalar value.
pub fn is_scalar(&self) -> bool {
self.shape.is_empty()
}
/// Test whether dataset and chunk layout is valid.
pub fn valid(&self) -> anyhow::Result<bool> {
for chunk in self.chunks.iter() {
let offset = chunk.offset.iter().map(|u| u.get()).collect::<Vec<_>>();
ensure!(
chunk.contains(&offset, &self.chunk_shape) == std::cmp::Ordering::Equal,
"chunk does not contain its offset"
);
}
Ok(true)
}
/// Returns an iterator over chunk, offset and size which if joined will make up the specified slice through the
/// variable.
pub fn chunk_slices<E>(&self, extents: E) -> impl Iterator<Item = (&Chunk<D>, u64, u64)>
where
E: TryInto<Extents>,
<E as TryInto<Extents>>::Error: std::fmt::Debug,
{
let extents = extents.try_into().unwrap();
let (indices, counts) = extents.get_start_count_sized(&self.shape).unwrap();
if indices
.iter()
.zip(counts.iter())
.map(|(i, c)| i + c)
.zip(self.shape.iter())
.any(|(l, &s)| l > s)
|| counts.iter().any(|&c| c == 0)
{
// Out of bounds or counts is zero in any dimension.
ChunkSlicer::empty(self)
} else {
ChunkSlicer::new(self, indices, counts)
}
}
/// Returns an Vec with chunks, offset and size grouped by chunk, with segments and
/// destination offset.
pub fn group_chunk_slices<E>(&self, extents: E) -> Vec<(&Chunk<D>, u64, u64, u64)>
where
E: TryInto<Extents>,
<E as TryInto<Extents>>::Error: std::fmt::Debug,
{
// Find chunks and calculate offset in destination vector.
let extents = extents.try_into().unwrap();
let mut chunks = self
.chunk_slices(&extents)
.scan(0u64, |offset, (c, start, end)| {
let slice_sz = end - start;
let current = *offset;
*offset += slice_sz;
Some((c, current, start, end))
})
.collect::<Vec<_>>();
// Sort by chunk file address, not destination address.
chunks.sort_unstable_by_key(|(c, _, _, _)| c.addr.get());
chunks
// XXX: A Vec of Vec's becomes very slow to de-allocate (a couple of seconds
// actually on a big file with about 380 chunks). So it is faster to have an
// expanded vector.
//
// Group by chunk
// let mut groups = Vec::<(&Chunk<D>, Vec<(u64, u64, u64)>)>::new();
// for (current, c, start, end) in chunks.iter() {
// match groups.last_mut() {
// Some((group_chunk, segments)) if *group_chunk == *c => {
// segments.push((*current, *start, *end));
// }
// _ => {
// groups.push((c, vec![(*current, *start, *end)]));
// }
// }
// }
// debug_assert!(groups.iter().map(|(c, _)| c).all_unique());
// debug_assert!(groups.iter().map(|(_, s)| s.iter().map(|(current, _, _)| current)).flatten().all_unique());
//
// groups
}
pub fn chunk_at_coord(&self, indices: &[u64]) -> &Chunk<D> {
debug_assert_eq!(indices.len(), self.chunk_shape.len());
debug_assert_eq!(indices.len(), self.scaled_dim_sz.len());
let offset = indices
.iter()
.zip(&self.chunk_shape_reduced)
.zip(&self.scaled_dim_sz)
.fold(0, |offset, ((&index, &ch_sh), &sz)| {
offset + index / ch_sh * sz
});
&self.chunks[offset as usize]
}
}
impl<const D: usize> DatasetExt for Dataset<'_, D> {
fn size(&self) -> usize {
self.size()
}
fn dtype(&self) -> Datatype {
self.dtype
}
fn dsize(&self) -> usize {
self.dsize
}
fn shape(&self) -> &[u64] {
self.shape.as_slice()
}
fn chunk_shape(&self) -> &[u64] {
self.chunk_shape.as_slice()
}
fn valid(&self) -> anyhow::Result<bool> {
self.valid()
}
fn as_par_reader(&self, p: &dyn AsRef<Path>) -> anyhow::Result<Box<dyn DatasetExtReader + '_>> {
use crate::reader::direct::Direct;
Ok(Box::new(Direct::with_dataset(self, p)?))
}
}
pub struct ChunkSlicer<'a, const D: usize> {
dataset: &'a Dataset<'a, D>,
/// The current offset in values from start.
offset: u64,
/// The coordinates of the current offset in values from the start of the slice.
offset_coords: [u64; D],
/// The slice start indices.
indices: [u64; D],
/// The size of the slice.
counts: [u64; D],
counts_reduced: [StrengthReducedU64; D],
/// The end of the slice in values from the start of the dataset. The product of
/// all the sizes in `counts`.
end: u64,
}
impl<'a, const D: usize> ChunkSlicer<'a, D> {
/// Empty slice returned for indices and counts that are out of bounds or of zero size.
pub fn empty(dataset: &'a Dataset<D>) -> ChunkSlicer<'a, D> {
ChunkSlicer {
dataset,
offset: 0,
offset_coords: [0; D],
indices: [0; D],
counts: [0; D],
counts_reduced: (0..D)
.map(|_| StrengthReducedU64::new(1))
.collect::<Vec<_>>()
.as_slice()
.try_into()
.unwrap(),
end: 0,
}
}
pub fn new(dataset: &'a Dataset<D>, indices: [u64; D], counts: [u64; D]) -> ChunkSlicer<'a, D> {
let end = if dataset.is_scalar() {
// scalar
assert!(indices.is_empty());
1
} else {
// size of slice dimensions
counts.iter().product::<u64>()
};
debug_assert_eq!(indices.len(), dataset.shape.len());
debug_assert_eq!(counts.len(), dataset.shape.len());
// Checked in `Dataset::chunk_slices`.
debug_assert!(izip!(&indices, &counts, &dataset.shape).all(|(i, c, z)| i + c <= *z));
ChunkSlicer {
dataset,
offset: 0,
offset_coords: [0; D],
indices,
counts_reduced: counts
.iter()
.map(|c| StrengthReducedU64::new(*c))
.collect::<Vec<_>>()
.as_slice()
.try_into()
.unwrap(),
counts,
end,
}
}
/// Offset in values from chunk offset coordinates. `dim_sz` is dimension size of chunk
/// dimensions.
fn chunk_start(coords: &[u64; D], chunk_offset: &[ULE; D], dim_sz: &[u64; D]) -> u64 {
debug_assert_eq!(coords.len(), chunk_offset.len());
debug_assert_eq!(coords.len(), dim_sz.len());
coords
.iter()
.zip(chunk_offset)
.zip(dim_sz)
.fold(0, |start, ((&coord, &offset), &sz)| {
start + (coord - offset.get()) * sz
})
}
}
impl<'a, const D: usize> Iterator for ChunkSlicer<'a, D> {
type Item = (&'a Chunk<D>, u64, u64);
fn next(&mut self) -> Option<Self::Item> {
if self.offset >= self.end {
return None;
}
// scalar
if self.dataset.is_scalar() {
debug_assert!(self.dataset.chunks.len() == 1);
debug_assert!(self.dataset.shape.is_empty());
self.offset += 1;
return Some((&self.dataset.chunks[0], 0, 1));
}
let mut start = [0; D];
for (s, i, o) in izip!(&mut start, &self.indices, &self.offset_coords) {
*s = i + o;
}
let chunk: &Chunk<D> = self.dataset.chunk_at_coord(&start);
debug_assert!(
chunk.contains(&start, &self.dataset.chunk_shape) == std::cmp::Ordering::Equal
);
// position in chunk of current offset
let chunk_start = Self::chunk_start(&start, &chunk.offset, &self.dataset.chunk_dim_sz);
// Number of values to advance in current chunk.
let mut advance = 0;
let mut carry = 0;
let mut di = 0;
for (idx, offset, count, count_sru, chunk_offset, chunk_sz, chunk_dim_sz, dataset_shape) in
izip!(
&self.indices,
&mut self.offset_coords,
&self.counts,
&self.counts_reduced,
&chunk.offset,
&self.dataset.chunk_shape,
&self.dataset.chunk_dim_sz,
&self.dataset.shape,
)
.rev()
{
// The chunk size may not align to the dataset size. If the chunk
// dimension is greater than the end of the dataset, it must be cut
// so that it ends at the end of the dataset.
//
// There are two possibilities:
// * a) Either the chunk is stored in full on disk, with some bogus data.
// * b) A cut-down chunk is stored on disk.
//
// "a" makes more sense. Let's try that.
//
// The dimension size should remain the same, since it only depends on the lower
// dimensions and we are working our way from the last one (`rev`).
//
// This does seem to create a lot of chunks.
let chunk_dim_end = chunk_offset.get() + chunk_sz;
let chunk_sz = if chunk_dim_end > *dataset_shape {
chunk_sz - (chunk_dim_end - *dataset_shape)
} else {
*chunk_sz
};
// If the chunk size in this dimension is 1, count must also be 1, and we will
// always carry over to the higher dimension. Unless the dimension size is 1, in which
// case the offset will be advanced with 1.
di += 1;
if chunk_sz == 1 && *chunk_dim_sz != 1 {
*offset += carry;
carry = *offset / *count_sru;
*offset = *offset % *count_sru;
continue;
} else {
// Carry over if previous dimension was exhausted.
*offset += carry;
carry = *offset / *count_sru;
*offset = *offset % *count_sru;
debug_assert!(*offset < *count);
// Advance to end of slice (`count`) or to end of dimension
let current = *offset;
let count_chunk_end = chunk_offset.get() + chunk_sz - idx;
*offset = min(*count, count_chunk_end);
let diff = (*offset - current) * chunk_dim_sz;
advance += diff;
self.offset += diff;
carry += *offset / *count_sru;
*offset = *offset % *count_sru;
if *idx == chunk_offset.get() && // slice starts at at chunk start (in this dimension)
*count == count_chunk_end && // slice ends at chunk end.
self.offset < self.end
// Reached end of dataset
{
continue;
} else {
debug_assert!(self.offset <= self.end);
break;
}
}
}
// Left-over carry?
//
// Calculate new offset and start coords:
let i = self.indices.len() - di;
for (offset, count) in izip!(&mut self.offset_coords[..i], &self.counts_reduced[..i]).rev()
{
*offset += carry;
carry = *offset / *count;
*offset = *offset % *count;
if carry == 0 {
break;
}
}
debug_assert!(
advance > 0,
"slice iterator not advancing: stuck indefinitely."
);
// position in chunk of new offset
let chunk_end = chunk_start + advance;
debug_assert!(
chunk_end as usize <= self.dataset.chunk_shape.iter().product::<u64>() as usize
);
Some((chunk, chunk_start, chunk_end))
}
}
#[cfg(test)]
mod tests {
use super::super::tests::test_dataset;
use super::*;
use divan::Bencher;
#[divan::bench]
fn chunk_start(b: Bencher) {
let dim_sz = [10, 1];
let coords = [20, 10];
let ch_offset = [ULE::new(20), ULE::new(10)];
b.bench_local(|| divan::black_box(ChunkSlicer::chunk_start(&coords, &ch_offset, &dim_sz)))
}
#[test]
fn serialize_d2() {
let d = test_dataset();
let s = bincode::serialize(&d).unwrap();
let md: Dataset<2> = bincode::deserialize(&s).unwrap();
for (a, b) in izip!(d.chunk_shape_reduced.iter(), md.chunk_shape_reduced.iter()) {
assert_eq!(a.get(), b.get());
}
}
}