-
Notifications
You must be signed in to change notification settings - Fork 95
Chunks iterator #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Chunks iterator #214
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
74a815a
Add chunk iterator
mulimoen fca779a
Make ChunkInfoBorrowed visible
mulimoen a4e1605
Move ChunkInfo to chunks file
mulimoen 76172a5
Add convenience methods on ChunkInfoBorrowed
mulimoen 307b309
Fix 1.14 function diff
mulimoen 41a64ed
Apply suggestions from code review
mulimoen 5bc25e5
Fixup types and ChunkInfoRef
mulimoen 6b67126
Document ChunkInfo
mulimoen e3ffa48
Add to changelog
mulimoen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod attribute; | ||
pub mod chunks; | ||
pub mod container; | ||
pub mod dataset; | ||
pub mod dataspace; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
use crate::internal_prelude::*; | ||
|
||
#[cfg(feature = "1.10.5")] | ||
use hdf5_sys::h5d::{H5Dget_chunk_info, H5Dget_num_chunks}; | ||
|
||
#[cfg(feature = "1.10.5")] | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
/// Information on a chunk in a Dataset | ||
pub struct ChunkInfo { | ||
aldanor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Array with a size equal to the dataset’s rank whose elements contain 0-based | ||
/// logical positions of the chunk’s first element in each dimension. | ||
pub offset: Vec<hsize_t>, | ||
/// Filter mask that indicates which filters were used with the chunk when written. | ||
/// | ||
/// A zero value indicates that all enabled filters are applied on the chunk. | ||
/// A filter is skipped if the bit corresponding to the filter’s position in | ||
/// the pipeline (0 ≤ position < 32) is turned on. | ||
pub filter_mask: u32, | ||
/// Chunk address in the file. | ||
pub addr: haddr_t, | ||
/// Chunk size in bytes. | ||
pub size: hsize_t, | ||
} | ||
|
||
#[cfg(feature = "1.10.5")] | ||
impl ChunkInfo { | ||
pub(crate) fn new(ndim: usize) -> Self { | ||
let offset = vec![0; ndim]; | ||
Self { offset, filter_mask: 0, addr: 0, size: 0 } | ||
} | ||
|
||
/// Returns positional indices of disabled filters. | ||
pub fn disabled_filters(&self) -> Vec<usize> { | ||
(0..32).filter(|i| self.filter_mask & (1 << i) != 0).collect() | ||
} | ||
} | ||
|
||
#[cfg(feature = "1.10.5")] | ||
pub(crate) fn chunk_info(ds: &Dataset, index: usize) -> Option<ChunkInfo> { | ||
if !ds.is_chunked() { | ||
return None; | ||
} | ||
h5lock!(ds.space().map_or(None, |s| { | ||
let mut chunk_info = ChunkInfo::new(ds.ndim()); | ||
h5check(H5Dget_chunk_info( | ||
ds.id(), | ||
s.id(), | ||
index as _, | ||
chunk_info.offset.as_mut_ptr(), | ||
&mut chunk_info.filter_mask, | ||
&mut chunk_info.addr, | ||
&mut chunk_info.size, | ||
)) | ||
.map(|_| chunk_info) | ||
.ok() | ||
})) | ||
} | ||
|
||
#[cfg(feature = "1.10.5")] | ||
pub(crate) fn get_num_chunks(ds: &Dataset) -> Option<usize> { | ||
if !ds.is_chunked() { | ||
return None; | ||
} | ||
h5lock!(ds.space().map_or(None, |s| { | ||
let mut n: hsize_t = 0; | ||
h5check(H5Dget_num_chunks(ds.id(), s.id(), &mut n)).map(|_| n as _).ok() | ||
})) | ||
} | ||
|
||
#[cfg(feature = "1.14.0")] | ||
mod v1_14_0 { | ||
use super::*; | ||
use hdf5_sys::h5d::H5Dchunk_iter; | ||
|
||
/// Borrowed version of [ChunkInfo](crate::dataset::ChunkInfo) | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct ChunkInfoRef<'a> { | ||
pub offset: &'a [hsize_t], | ||
pub filter_mask: u32, | ||
pub addr: haddr_t, | ||
pub size: hsize_t, | ||
} | ||
|
||
impl<'a> ChunkInfoRef<'a> { | ||
/// Returns positional indices of disabled filters. | ||
pub fn disabled_filters(&self) -> Vec<usize> { | ||
(0..32).filter(|i| self.filter_mask & (1 << i) != 0).collect() | ||
} | ||
} | ||
|
||
impl<'a> From<ChunkInfoRef<'a>> for ChunkInfo { | ||
fn from(val: ChunkInfoRef<'a>) -> Self { | ||
Self { | ||
offset: val.offset.to_owned(), | ||
filter_mask: val.filter_mask, | ||
addr: val.addr, | ||
size: val.size, | ||
} | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
struct RustCallback<F> { | ||
pub ndims: hsize_t, | ||
pub callback: F, | ||
} | ||
|
||
extern "C" fn chunks_callback<F>( | ||
offset: *const hsize_t, filter_mask: c_uint, addr: haddr_t, size: hsize_t, | ||
op_data: *mut c_void, | ||
) -> herr_t | ||
where | ||
F: FnMut(ChunkInfoRef) -> i32, | ||
{ | ||
unsafe { | ||
std::panic::catch_unwind(|| { | ||
let data: *mut RustCallback<F> = op_data.cast::<RustCallback<F>>(); | ||
let ndims = (*data).ndims; | ||
let callback = &mut (*data).callback; | ||
|
||
let offset = std::slice::from_raw_parts(offset, ndims as usize); | ||
|
||
let info = ChunkInfoRef { offset, filter_mask, addr, size }; | ||
|
||
callback(info) | ||
}) | ||
.unwrap_or(-1) | ||
} | ||
} | ||
|
||
pub(crate) fn visit<F>(ds: &Dataset, callback: F) -> Result<()> | ||
where | ||
F: for<'a> FnMut(ChunkInfoRef<'a>) -> i32, | ||
{ | ||
let mut data = RustCallback::<F> { ndims: ds.ndim() as _, callback }; | ||
|
||
h5try!(H5Dchunk_iter( | ||
ds.id(), | ||
H5P_DEFAULT, | ||
Some(chunks_callback::<F>), | ||
std::ptr::addr_of_mut!(data).cast() | ||
)); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn chunks_visit() { | ||
with_tmp_file(|f| { | ||
let ds = f.new_dataset::<i16>().no_chunk().shape((4, 4)).create("nochunk").unwrap(); | ||
assert_err_re!(visit(&ds, |_| 0), "not a chunked dataset"); | ||
|
||
let ds = | ||
f.new_dataset::<i16>().shape([3, 2]).chunk([1, 1]).create("chunk").unwrap(); | ||
ds.write(&ndarray::arr2(&[[1, 2], [3, 4], [5, 6]])).unwrap(); | ||
|
||
let mut i = 0; | ||
let f = |c: ChunkInfoRef| { | ||
match i { | ||
0 => assert_eq!(c.offset, [0, 0]), | ||
1 => assert_eq!(c.offset, [0, 1]), | ||
2 => assert_eq!(c.offset, [1, 0]), | ||
3 => assert_eq!(c.offset, [1, 1]), | ||
4 => assert_eq!(c.offset, [2, 0]), | ||
5 => assert_eq!(c.offset, [2, 1]), | ||
_ => unreachable!(), | ||
} | ||
assert_eq!(c.size, std::mem::size_of::<i16>() as u64); | ||
i += 1; | ||
0 | ||
}; | ||
visit(&ds, f).unwrap(); | ||
assert_eq!(i, 6); | ||
}) | ||
} | ||
} | ||
} | ||
#[cfg(feature = "1.14.0")] | ||
pub use v1_14_0::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.