1 /*
2  *  Copyright (c), 2017, Adrien Devresse
3  *
4  *  Distributed under the Boost Software License, Version 1.0.
5  *    (See accompanying file LICENSE_1_0.txt or copy at
6  *          http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 #include <iostream>
10 
11 #undef H5_USE_BOOST
12 #define H5_USE_BOOST
13 
14 // In some versions of Boost (starting with 1.64), you have to include the serialization header before ublas
15 #include <boost/serialization/vector.hpp>
16 
17 #include <boost/numeric/ublas/io.hpp>
18 #include <boost/numeric/ublas/matrix.hpp>
19 #include <highfive/H5File.hpp>
20 
21 using namespace HighFive;
22 
23 const std::string FILE_NAME("boost_ublas_double.h5");
24 const std::string DATASET_NAME("dset");
25 
26 const size_t size_x = 10;
27 const size_t size_y = 10;
28 
main(void)29 int main(void) {
30 
31     try {
32         typedef typename boost::numeric::ublas::matrix<double> Matrix;
33 
34         // create a 10x10 matrix
35         Matrix mat(size_x, size_y);
36 
37         // fill it
38         for (std::size_t i = 0; i < size_x; ++i) {
39             mat(i, i) = static_cast<double>(i);
40         }
41 
42         // Create a new HDF5 file
43         File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate);
44 
45         // create a new dataset with the 10x10 Matrix dimension
46         DataSet dataset =
47             file.createDataSet<double>(DATASET_NAME, DataSpace::From(mat));
48 
49         // write it
50         dataset.write(mat);
51 
52         // now, let read it back
53         Matrix result;
54         dataset.read(result);
55 
56         // print what we read
57         std::cout << "Matrix result:\n" << result << std::endl;
58 
59     } catch (Exception& err) {
60         // catch and print any HDF5 error
61         std::cerr << err.what() << std::endl;
62     }
63 
64     return 0; // successfully terminated
65 }
66