Skip to content

Commit 94e7f20

Browse files
upgrade to current hdf5-sys and rust
1 parent 14917d0 commit 94e7f20

File tree

3 files changed

+91
-83
lines changed

3 files changed

+91
-83
lines changed

Diff for: Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "numeric"
3-
version = "0.1.4"
3+
version = "0.2.0"
44
authors = ["Gustav Larsson <[email protected]>"]
55
description = "N-dimensional matrix class for Rust"
66
repository = "https://github.com/numeric-rust/numeric"
@@ -11,12 +11,12 @@ keywords = ["numeric", "tensor", "matrix", "vector", "hdf5"]
1111
license = "MIT"
1212

1313
[dependencies]
14-
blas = "0.9.1"
15-
lapack = "0.8.1"
16-
num = "0.1.29"
17-
rand = "0.3.12"
18-
libc = "0.2.4"
19-
hdf5-sys = "0.3.2"
14+
blas = "0.22.0"
15+
lapack = "0.19.0"
16+
num = "0.4.0"
17+
rand = "0.8.3"
18+
libc = "0.2.94"
19+
hdf5-sys = "0.7.1"
2020

2121
[[test]]
2222
name = "numeric"

Diff for: src/io/mod.rs

+79-72
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,18 @@ extern crate std;
4343

4444
use libc::{c_char, c_void};
4545
use std::path::Path;
46-
use hdf5_sys as ffi;
46+
// use hdf5_sys as ffi;
47+
use hdf5_sys::h5d;
48+
use hdf5_sys::h5t;
49+
use hdf5_sys::h5p;
50+
use hdf5_sys::h5f;
51+
use hdf5_sys::h5e;
52+
use hdf5_sys::h5s;
53+
use hdf5_sys::h5i;
4754

4855
use tensor::Tensor;
4956

50-
extern fn error_handler(_: ffi::hid_t, _: *const c_void) {
57+
extern fn error_handler(_: h5i::hid_t, _: *const c_void) {
5158
// Suppress errors. We will rely on return statuses alone.
5259
}
5360

@@ -71,30 +78,30 @@ macro_rules! add_save {
7178
let group = "data";
7279

7380
unsafe {
74-
let filename_cstr = try!(::std::ffi::CString::new(filename));
75-
let group_cstr = try!(::std::ffi::CString::new(group));
81+
let filename_cstr = ::std::ffi::CString::new(filename)?;
82+
let group_cstr = ::std::ffi::CString::new(group)?;
7683

77-
//ffi::H5Eset_auto2(0, error_handler, 0 as *const c_void);
84+
//h5e::H5Eset_auto2(0, error_handler, 0 as *const c_void);
7885

79-
let file = ffi::H5Fcreate(filename_cstr.as_ptr() as *const c_char,
80-
ffi::H5F_ACC_TRUNC, ffi::H5P_DEFAULT, ffi::H5P_DEFAULT);
86+
let file = h5f::H5Fcreate(filename_cstr.as_ptr() as *const c_char,
87+
h5f::H5F_ACC_TRUNC, h5p::H5P_DEFAULT, h5p::H5P_DEFAULT);
8188

8289
let mut shape: Vec<u64> = Vec::new();
8390
for s in self.shape().iter() {
8491
shape.push(*s as u64);
8592
}
8693

87-
let space = ffi::H5Screate_simple(shape.len() as i32, shape.as_ptr(),
94+
let space = h5s::H5Screate_simple(shape.len() as i32, shape.as_ptr(),
8895
std::ptr::null());
8996

90-
let dset = ffi::H5Dcreate2(file, group_cstr.as_ptr() as *const c_char,
97+
let dset = h5d::H5Dcreate2(file, group_cstr.as_ptr() as *const c_char,
9198
$h5type, space,
92-
ffi::H5P_DEFAULT,
93-
ffi::H5P_DEFAULT,
94-
ffi::H5P_DEFAULT);
99+
h5p::H5P_DEFAULT,
100+
h5p::H5P_DEFAULT,
101+
h5p::H5P_DEFAULT);
95102

96-
let status = ffi::H5Dwrite(dset, $h5type, ffi::H5S_ALL, ffi::H5S_ALL,
97-
ffi::H5P_DEFAULT, self.as_ptr() as * const c_void);
103+
let status = h5d::H5Dwrite(dset, $h5type, h5s::H5S_ALL, h5s::H5S_ALL,
104+
h5p::H5P_DEFAULT, self.as_ptr() as * const c_void);
98105

99106
if status < 0 {
100107
let msg = format!("Failed to write '{}': {:?}", group, path);
@@ -103,25 +110,25 @@ macro_rules! add_save {
103110
}
104111

105112

106-
ffi::H5Dclose(dset);
107-
ffi::H5Fclose(file);
113+
h5d::H5Dclose(dset);
114+
h5f::H5Fclose(file);
108115
}
109116
Ok(())
110117
}
111118
}
112119
)
113120
}
114121

115-
add_save!(u8, ffi::H5T_NATIVE_UINT8);
116-
add_save!(u16, ffi::H5T_NATIVE_UINT16);
117-
add_save!(u32, ffi::H5T_NATIVE_UINT32);
118-
add_save!(u64, ffi::H5T_NATIVE_UINT64);
119-
add_save!(i8, ffi::H5T_NATIVE_INT8);
120-
add_save!(i16, ffi::H5T_NATIVE_INT16);
121-
add_save!(i32, ffi::H5T_NATIVE_INT32);
122-
add_save!(i64, ffi::H5T_NATIVE_INT64);
123-
add_save!(f32, ffi::H5T_NATIVE_FLOAT);
124-
add_save!(f64, ffi::H5T_NATIVE_DOUBLE);
122+
add_save!(u8, h5t::H5T_NATIVE_UINT8);
123+
add_save!(u16, h5t::H5T_NATIVE_UINT16);
124+
add_save!(u32, h5t::H5T_NATIVE_UINT32);
125+
add_save!(u64, h5t::H5T_NATIVE_UINT64);
126+
add_save!(i8, h5t::H5T_NATIVE_INT8);
127+
add_save!(i16, h5t::H5T_NATIVE_INT16);
128+
add_save!(i32, h5t::H5T_NATIVE_INT32);
129+
add_save!(i64, h5t::H5T_NATIVE_INT64);
130+
add_save!(f32, h5t::H5T_NATIVE_FLOAT);
131+
add_save!(f64, h5t::H5T_NATIVE_DOUBLE);
125132

126133

127134
macro_rules! add_load {
@@ -137,38 +144,38 @@ macro_rules! add_load {
137144
},
138145
};
139146
unsafe {
140-
let filename_cstr = try!(::std::ffi::CString::new(filename));
141-
let group_cstr = try!(::std::ffi::CString::new(group));
147+
let filename_cstr = ::std::ffi::CString::new(filename)?;
148+
let group_cstr = ::std::ffi::CString::new(group)?;
142149

143-
ffi::H5Eset_auto2(0, error_handler, 0 as *const c_void);
150+
h5e::H5Eset_auto2(0, error_handler, 0 as *const c_void);
144151

145-
let file = ffi::H5Fopen(filename_cstr.as_ptr() as *const c_char,
146-
ffi::H5F_ACC_RDONLY, ffi::H5P_DEFAULT);
152+
let file = h5f::H5Fopen(filename_cstr.as_ptr() as *const c_char,
153+
h5f::H5F_ACC_RDONLY, h5p::H5P_DEFAULT);
147154

148155
if file < 0 {
149156
let msg = format!("File not found: {:?}", path);
150157
let err = std::io::Error::new(std::io::ErrorKind::NotFound, msg);
151158
return Err(err);
152159
}
153160

154-
let dset = ffi::H5Dopen2(file, group_cstr.as_ptr() as *const c_char,
155-
ffi::H5P_DEFAULT);
161+
let dset = h5d::H5Dopen2(file, group_cstr.as_ptr() as *const c_char,
162+
h5p::H5P_DEFAULT);
156163

157164
if dset < 0 {
158165
let msg = format!("Group '{}' not found: {}", group, filename);
159166
let err = std::io::Error::new(std::io::ErrorKind::NotFound, msg);
160167
return Err(err);
161168
}
162169

163-
let datatype = ffi::H5Dget_type(dset);
170+
let datatype = h5d::H5Dget_type(dset);
164171

165-
let space = ffi::H5Dget_space(dset);
166-
let ndims = ffi::H5Sget_simple_extent_ndims(space);
172+
let space = h5d::H5Dget_space(dset);
173+
let ndims = h5s::H5Sget_simple_extent_ndims(space);
167174

168-
let mut shape: Tensor<ffi::hsize_t> = Tensor::zeros(&[ndims as usize]);
175+
let mut shape: Tensor<h5d::hsize_t> = Tensor::zeros(&[ndims as usize]);
169176

170-
if ffi::H5Sget_simple_extent_dims(space, shape.as_mut_ptr(),
171-
0 as *mut ffi::hsize_t) != ndims {
177+
if h5s::H5Sget_simple_extent_dims(space, shape.as_mut_ptr(),
178+
0 as *mut h5d::hsize_t) != ndims {
172179
let msg = format!("Could not read shape of tesor: {}", filename);
173180
let err = std::io::Error::new(std::io::ErrorKind::InvalidData, msg);
174181
return Err(err);
@@ -179,65 +186,65 @@ macro_rules! add_load {
179186
let unsigned_shape = &unsigned_tensor.data();
180187

181188
let data: Tensor<$t> = {
182-
if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_UINT8) == 1 {
189+
if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_UINT8) == 1 {
183190
let mut native_data: Tensor<u8> = Tensor::empty(&unsigned_shape[..]);
184191
// Finally load the actual data
185-
ffi::H5Dread(dset, ffi::H5T_NATIVE_UINT8, ffi::H5S_ALL, ffi::H5S_ALL,
186-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
192+
h5d::H5Dread(dset, h5t::H5T_NATIVE_UINT8, h5s::H5S_ALL, h5s::H5S_ALL,
193+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
187194
native_data.convert::<$t>()
188-
} else if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_INT8) == 1 {
195+
} else if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_INT8) == 1 {
189196
let mut native_data: Tensor<i8> = Tensor::empty(&unsigned_shape[..]);
190197
// Finally load the actual data
191-
ffi::H5Dread(dset, ffi::H5T_NATIVE_INT8, ffi::H5S_ALL, ffi::H5S_ALL,
192-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
198+
h5d::H5Dread(dset, h5t::H5T_NATIVE_INT8, h5s::H5S_ALL, h5s::H5S_ALL,
199+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
193200
native_data.convert::<$t>()
194-
} else if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_UINT16) == 1 {
201+
} else if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_UINT16) == 1 {
195202
let mut native_data: Tensor<u16> = Tensor::empty(&unsigned_shape[..]);
196203
// Finally load the actual data
197-
ffi::H5Dread(dset, ffi::H5T_NATIVE_UINT16, ffi::H5S_ALL, ffi::H5S_ALL,
198-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
204+
h5d::H5Dread(dset, h5t::H5T_NATIVE_UINT16, h5s::H5S_ALL, h5s::H5S_ALL,
205+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
199206
native_data.convert::<$t>()
200-
} else if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_INT16) == 1 {
207+
} else if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_INT16) == 1 {
201208
let mut native_data: Tensor<i16> = Tensor::empty(&unsigned_shape[..]);
202209
// Finally load the actual data
203-
ffi::H5Dread(dset, ffi::H5T_NATIVE_INT16, ffi::H5S_ALL, ffi::H5S_ALL,
204-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
210+
h5d::H5Dread(dset, h5t::H5T_NATIVE_INT16, h5s::H5S_ALL, h5s::H5S_ALL,
211+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
205212
native_data.convert::<$t>()
206-
} else if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_UINT32) == 1 {
213+
} else if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_UINT32) == 1 {
207214
let mut native_data: Tensor<u32> = Tensor::empty(&unsigned_shape[..]);
208215
// Finally load the actual data
209-
ffi::H5Dread(dset, ffi::H5T_NATIVE_UINT32, ffi::H5S_ALL, ffi::H5S_ALL,
210-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
216+
h5d::H5Dread(dset, h5t::H5T_NATIVE_UINT32, h5s::H5S_ALL, h5s::H5S_ALL,
217+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
211218
native_data.convert::<$t>()
212-
} else if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_INT32) == 1 {
219+
} else if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_INT32) == 1 {
213220
let mut native_data: Tensor<i32> = Tensor::empty(&unsigned_shape[..]);
214221
// Finally load the actual data
215-
ffi::H5Dread(dset, ffi::H5T_NATIVE_INT32, ffi::H5S_ALL, ffi::H5S_ALL,
216-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
222+
h5d::H5Dread(dset, h5t::H5T_NATIVE_INT32, h5s::H5S_ALL, h5s::H5S_ALL,
223+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
217224
native_data.convert::<$t>()
218-
} else if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_UINT64) == 1 {
225+
} else if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_UINT64) == 1 {
219226
let mut native_data: Tensor<u64> = Tensor::empty(&unsigned_shape[..]);
220227
// Finally load the actual data
221-
ffi::H5Dread(dset, ffi::H5T_NATIVE_UINT64, ffi::H5S_ALL, ffi::H5S_ALL,
222-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
228+
h5d::H5Dread(dset, h5t::H5T_NATIVE_UINT64, h5s::H5S_ALL, h5s::H5S_ALL,
229+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
223230
native_data.convert::<$t>()
224-
} else if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_INT64) == 1 {
231+
} else if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_INT64) == 1 {
225232
let mut native_data: Tensor<i64> = Tensor::empty(&unsigned_shape[..]);
226233
// Finally load the actual data
227-
ffi::H5Dread(dset, ffi::H5T_NATIVE_INT64, ffi::H5S_ALL, ffi::H5S_ALL,
228-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
234+
h5d::H5Dread(dset, h5t::H5T_NATIVE_INT64, h5s::H5S_ALL, h5s::H5S_ALL,
235+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
229236
native_data.convert::<$t>()
230-
} else if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_FLOAT) == 1 {
237+
} else if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_FLOAT) == 1 {
231238
let mut native_data: Tensor<f32> = Tensor::empty(&unsigned_shape[..]);
232239
// Finally load the actual data
233-
ffi::H5Dread(dset, ffi::H5T_NATIVE_FLOAT, ffi::H5S_ALL, ffi::H5S_ALL,
234-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
240+
h5d::H5Dread(dset, h5t::H5T_NATIVE_FLOAT, h5s::H5S_ALL, h5s::H5S_ALL,
241+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
235242
native_data.convert::<$t>()
236-
} else if ffi::H5Tequal(datatype, ffi::H5T_NATIVE_DOUBLE) == 1 {
243+
} else if h5t::H5Tequal(datatype, h5t::H5T_NATIVE_DOUBLE) == 1 {
237244
let mut native_data: Tensor<f64> = Tensor::empty(&unsigned_shape[..]);
238245
// Finally load the actual data
239-
ffi::H5Dread(dset, ffi::H5T_NATIVE_DOUBLE, ffi::H5S_ALL, ffi::H5S_ALL,
240-
ffi::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
246+
h5d::H5Dread(dset, h5t::H5T_NATIVE_DOUBLE, h5s::H5S_ALL, h5s::H5S_ALL,
247+
h5p::H5P_DEFAULT, native_data.as_mut_ptr() as *mut c_void);
241248
native_data.convert::<$t>()
242249
} else {
243250
let msg = format!("Unable to convert '{}' to {}: {}",
@@ -247,9 +254,9 @@ macro_rules! add_load {
247254
}
248255
};
249256

250-
ffi::H5Tclose(datatype);
251-
ffi::H5Dclose(dset);
252-
ffi::H5Fclose(file);
257+
h5t::H5Tclose(datatype);
258+
h5d::H5Dclose(dset);
259+
h5f::H5Fclose(file);
253260

254261
Ok(data)
255262
}

Diff for: src/random/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
//! // 0.702227 0.346673 0.737954
1717
//! // [Tensor<f64> of shape 3x3]
1818
//! ```
19-
use rand::{Rng, SeedableRng, StdRng};
20-
use rand::distributions::range::SampleRange;
19+
use rand::{Rng, SeedableRng};
20+
use rand::rngs::StdRng;
21+
use rand::distributions::uniform::SampleRange;
2122
use num::traits::Float;
2223
use std::f64;
2324

@@ -40,7 +41,7 @@ impl RandomState {
4041
/// Generates a tensor by independently drawing samples from a uniform distribution in the
4142
/// range [`low`, `high`). This is appropriate for integer types as well.
4243
pub fn uniform<T>(&mut self, low: T, high: T, shape: &[usize]) -> Tensor<T>
43-
where T: NumericTrait + SampleRange {
44+
where T: NumericTrait + SampleRange<T> {
4445
let mut t = Tensor::zeros(shape);
4546
{
4647
let n = t.size();
@@ -54,7 +55,7 @@ impl RandomState {
5455

5556
/// Generates a tensor by independently drawing samples from a standard normal.
5657
pub fn normal<T>(&mut self, shape: &[usize]) -> Tensor<T>
57-
where T: NumericTrait + SampleRange + Float {
58+
where T: NumericTrait + SampleRange<T> + Float {
5859
let u1 = self.uniform(T::zero(), T::one(), shape);
5960
let u2 = self.uniform(T::zero(), T::one(), shape);
6061

0 commit comments

Comments
 (0)