-
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.
- Loading branch information
Showing
9 changed files
with
242 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
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,90 @@ | ||
/* | ||
* Copyright (c) 2024 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) | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "bits/H5Inspector_decl.hpp" | ||
|
||
#include <span> | ||
|
||
namespace HighFive { | ||
namespace details { | ||
|
||
template <class T, std::size_t Extent> | ||
struct inspector<std::span<T, Extent>> { | ||
using type = std::span<T, Extent>; | ||
using value_type = unqualified_t<T>; | ||
using base_type = typename inspector<value_type>::base_type; | ||
using hdf5_type = typename inspector<value_type>::hdf5_type; | ||
|
||
static constexpr size_t ndim = 1; | ||
static constexpr size_t recursive_ndim = ndim + inspector<value_type>::recursive_ndim; | ||
static constexpr bool is_trivially_copyable = std::is_trivially_copyable<value_type>::value && | ||
inspector<value_type>::is_trivially_nestable; | ||
|
||
static constexpr bool is_trivially_nestable = false; | ||
|
||
static std::vector<size_t> getDimensions(const type& val) { | ||
std::vector<size_t> sizes(recursive_ndim, 1ul); | ||
sizes[0] = val.size(); | ||
if (!val.empty()) { | ||
auto s = inspector<value_type>::getDimensions(val[0]); | ||
assert(s.size() + ndim == sizes.size()); | ||
for (size_t i = 0; i < s.size(); ++i) { | ||
sizes[i + ndim] = s[i]; | ||
} | ||
} | ||
return sizes; | ||
} | ||
|
||
static void prepare(type& val, const std::vector<size_t>& expected_dims) { | ||
auto actual_dims = getDimensions(val); | ||
if (actual_dims.size() != expected_dims.size()) { | ||
throw DataSpaceException("Mismatching rank."); | ||
} | ||
|
||
for (size_t i = 0; i < actual_dims.size(); ++i) { | ||
if (actual_dims[i] != expected_dims[i]) { | ||
throw DataSpaceException("Mismatching dimensions."); | ||
} | ||
} | ||
} | ||
|
||
static hdf5_type* data(type& val) { | ||
return val.empty() ? nullptr : inspector<value_type>::data(val[0]); | ||
} | ||
|
||
static const hdf5_type* data(const type& val) { | ||
return val.empty() ? nullptr : inspector<value_type>::data(val[0]); | ||
} | ||
|
||
template <class It> | ||
static void serialize(const type& val, const std::vector<size_t>& dims, It m) { | ||
if (!val.empty()) { | ||
auto subdims = std::vector<size_t>(dims.begin() + ndim, dims.end()); | ||
size_t subsize = compute_total_size(subdims); | ||
for (const auto& e: val) { | ||
inspector<value_type>::serialize(e, subdims, m); | ||
m += subsize; | ||
} | ||
} | ||
} | ||
|
||
template <class It> | ||
static void unserialize(const It& vec_align, const std::vector<size_t>& dims, type& val) { | ||
std::vector<size_t> subdims(dims.begin() + ndim, dims.end()); | ||
size_t subsize = compute_total_size(subdims); | ||
for (size_t i = 0; i < dims[0]; ++i) { | ||
inspector<value_type>::unserialize(vec_align + i * subsize, subdims, val[i]); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace details | ||
} // namespace HighFive |
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,56 @@ | ||
/* | ||
* Copyright (c), 2024, 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) | ||
* | ||
*/ | ||
|
||
// This example demonstrates using `std::span`. An `std::span` is a pointer | ||
// with a size. | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include <highfive/highfive.hpp> | ||
|
||
#include <highfive/span.hpp> | ||
|
||
int main(void) { | ||
using namespace HighFive; | ||
|
||
std::string file_name = "read_write_span.h5"; | ||
std::string dataset_name = "array"; | ||
|
||
File file(file_name, File::Truncate); | ||
|
||
// Let's write to file. | ||
{ | ||
// Assume we have one-dimensional data in some unsupported format (we | ||
// use `std::vector` for simplicity). Further, assume that the data is | ||
// stored contiguously. Then one can create an `std::span`. | ||
std::vector<double> values{1.0, 2.0, 3.0}; | ||
auto view = std::span<double>(values.data(), values.size()); | ||
|
||
// Given the span, HighFive can deduce the shape of the dataset. Hence, | ||
// spans are fully supported when writing. For example: | ||
auto dataset = file.createDataSet(dataset_name, view); | ||
} | ||
|
||
// Let's read from file. | ||
{ | ||
auto dataset = file.getDataSet(dataset_name); | ||
|
||
// Since spans are views, HighFive can't (or wont) allocate memory. | ||
// Instead one must preallocate memory and then create a span for that | ||
// memory: | ||
auto values = std::vector<double>(dataset.getElementCount()); | ||
auto view = std::span<double>(values.data(), values.size()); | ||
|
||
// ... now we can read into the preallocated memory: | ||
dataset.read(view); | ||
} | ||
|
||
return 0; | ||
} |
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