Skip to content

Commit

Permalink
Example of reading an in memory HDF5 file. (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
1uc committed Feb 1, 2023
1 parent a2e84d3 commit 4e5e971
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/highfive/H5File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class File: public Object, public NodeTraits<File>, public AnnotateTraits<File>
return details::get_plist<FileAccessProps>(*this, H5Fget_access_plist);
}

protected:
File() = default;

private:
using Object::Object;

Expand Down
5 changes: 5 additions & 0 deletions src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ function(compile_example exemple_source)
add_executable(${example_name} ${exemple_source})
target_link_libraries(${example_name} HighFive)

if(${example_name} MATCHES ".*hl_hdf5.*")
find_package(HDF5 REQUIRED C HL)
target_link_libraries(${example_name} ${HDF5_HL_LIBRARIES})
endif()

endfunction()

file(GLOB list_example "*.cpp")
Expand Down
74 changes: 74 additions & 0 deletions src/examples/hl_hdf5_inmemory_files.cpp
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
}

0 comments on commit 4e5e971

Please sign in to comment.