-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example of reading an in memory HDF5 file. (#652)
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,74 @@ | ||
/* | ||
* Copyright (c), 2022, Blue Brain Project | ||
* | ||
* 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> | ||
#include <vector> | ||
|
||
|
||
#include <highfive/H5File.hpp> | ||
#include <hdf5_hl.h> | ||
|
||
using namespace HighFive; | ||
|
||
class InMemoryFile: public HighFive::File { | ||
public: | ||
explicit InMemoryFile(std::vector<std::uint8_t> buffer) | ||
: _buffer(std::move(buffer)) { | ||
_hid = H5LTopen_file_image(_buffer.data(), | ||
sizeof(_buffer[0]) * _buffer.size(), | ||
H5LT_FILE_IMAGE_DONT_RELEASE | H5LT_FILE_IMAGE_DONT_COPY); | ||
} | ||
|
||
private: | ||
std::vector<std::uint8_t> _buffer; | ||
}; | ||
|
||
|
||
// Create a 2D dataset 10x3 of double with eigen matrix | ||
// and write it to a file | ||
int main(void) { | ||
const std::string FILE_NAME("inmemory_file.h5"); | ||
const std::string DATASET_NAME("dset"); | ||
|
||
try { | ||
auto data = std::vector<double>{1.0, 2.0, 3.0}; | ||
|
||
{ | ||
// We create an HDF5 file. | ||
File file(FILE_NAME, File::Truncate); | ||
file.createDataSet(DATASET_NAME, data); | ||
} | ||
|
||
// Simulate having an inmemory file by reading a file | ||
// byte-by-byte into RAM. | ||
auto buffer = std::vector<std::uint8_t>(1ul << 20); | ||
auto file = std::fopen(FILE_NAME.c_str(), "r"); | ||
auto nread = std::fread(buffer.data(), sizeof(buffer[0]), buffer.size(), file); | ||
std::cout << "Bytes read: " << nread << "\n"; | ||
|
||
// Create a file from a buffer. | ||
auto h5 = InMemoryFile(std::move(buffer)); | ||
|
||
// Read a dataset as usual. | ||
auto read_back = h5.getDataSet(DATASET_NAME).read<std::vector<double>>(); | ||
|
||
// Check if the values match. | ||
for (size_t i = 0; i < read_back.size(); ++i) { | ||
if (read_back[i] != data[i]) { | ||
throw std::runtime_error("Values don't match."); | ||
} else { | ||
std::cout << "read_back[" << i << "] = " << read_back[i] << "\n"; | ||
} | ||
} | ||
} catch (Exception& err) { | ||
// catch and print any HDF5 error | ||
std::cerr << err.what() << std::endl; | ||
} | ||
|
||
return 0; // successfully terminated | ||
} |