forked from BlueBrain/HighFive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboost_ublas_double.cpp
64 lines (48 loc) · 1.66 KB
/
boost_ublas_double.cpp
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
/*
* Copyright (c), 2017, Adrien Devresse
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
*/
#include <iostream>
#undef H5_USE_BOOST
#define H5_USE_BOOST
// In some versions of Boost (starting with 1.64), you have to include the serialization header
// before ublas
#include <boost/serialization/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <highfive/H5File.hpp>
using namespace HighFive;
const std::string FILE_NAME("boost_ublas_double.h5");
const std::string DATASET_NAME("dset");
const size_t size_x = 10;
const size_t size_y = 10;
int main(void) {
try {
typedef typename boost::numeric::ublas::matrix<double> Matrix;
// create a 10x10 matrix
Matrix mat(size_x, size_y);
// fill it
for (std::size_t i = 0; i < size_x; ++i) {
mat(i, i) = static_cast<double>(i);
}
// Create a new HDF5 file
File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate);
// create a new dataset with the 10x10 Matrix dimension
DataSet dataset = file.createDataSet<double>(DATASET_NAME, DataSpace::From(mat));
// write it
dataset.write(mat);
// now, let read it back
Matrix result;
dataset.read(result);
// print what we read
std::cout << "Matrix result:\n" << result << std::endl;
} catch (Exception& err) {
// catch and print any HDF5 error
std::cerr << err.what() << std::endl;
}
return 0; // successfully terminated
}