Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "../common/counting_iterator.hpp"
#include "../common/exception.hpp"
#include "../common/iterator_facade.hpp"
#include "../common/typing.hpp"

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -222,7 +223,8 @@ concept row_based_range = std::same_as<std::remove_cvref_t<RangeType>, std::span

template <non_owning_view_c Elements, class StructType, class AttributeType>
requires columnar_attribute_range<Elements> || row_based_range<Elements, StructType>
auto get_attribute_span(Elements const elements, AttributeType StructType::* member, std::string_view attribute_name) {
inline auto get_attribute_span(Elements const elements, AttributeType StructType::* member,
std::string_view attribute_name) {
if constexpr (columnar_attribute_range<Elements>) {
// this check is needed as the columnar branch never gets to use member
static_assert(std::same_as<typename Elements::Proxy::value_type, StructType>);
Expand Down Expand Up @@ -747,6 +749,77 @@ template <dataset_type_tag dataset_type_> class Dataset {
}
};

// non-owning, type-erased strided view over a single attribute of a component
// works for both row-based and columnar buffers
struct ConstAttributeView {
MetaAttribute const* meta_attribute{nullptr};
char const* first{nullptr};
Idx stride{};
Idx size{};

template <typename UnderlyingType> auto as() const {
if (size != 0 && meta_attribute->ctype != ctype_v<UnderlyingType>) {
throw UnreachableHit{"ConstAttributeView::as", "Requested type must match the attribute ctype"};
}
return IdxRange{size} | std::views::transform([first = first, stride = stride](Idx idx) {
return *reinterpret_cast<UnderlyingType const*>(first + idx * stride);
});
}
};

// empty if the component is absent or a columnar buffer has no buffer for the attribute
// scenario < 0 (default) returns the view over all scenarios
inline ConstAttributeView get_attribute_view(Dataset<const_dataset_t> const& dataset, std::string_view component,
std::string_view attribute,
Idx scenario = Dataset<const_dataset_t>::invalid_index) {
assert(scenario < dataset.batch_size());
if (!dataset.is_batch() && scenario > 0) {
throw DatasetError{"Cannot export a single dataset with specified scenario\n"};
}

ConstAttributeView result{};
if (Idx const component_idx = dataset.find_component(component, false); component_idx >= 0) {
auto const& info = dataset.get_component_info(component_idx);
auto const& buffer = dataset.get_buffer(component_idx);
auto const& meta_attribute = info.component->get_attribute(attribute);
result.meta_attribute = &meta_attribute;

if (dataset.is_row_based(buffer)) {
result.first = static_cast<char const*>(buffer.data) + meta_attribute.offset;
result.stride = narrow_cast<Idx>(info.component->size);
} else if (auto const it = std::ranges::find(buffer.attributes, &meta_attribute,
&AttributeBuffer<void const>::meta_attribute);
it != buffer.attributes.end()) {
result.first = static_cast<char const*>(it->data);
result.stride = narrow_cast<Idx>(meta_attribute.size);
}

// absent optional attributes (first == nullptr) remain an empty view
if (result.first != nullptr) {
if (scenario == Dataset<const_dataset_t>::invalid_index) {
result.size = info.total_elements;
} else if (info.elements_per_scenario == -1) { // -1 is used for non-uniform components, i.e., indptr-based
result.first += buffer.indptr[scenario] * result.stride;
result.size = buffer.indptr[scenario + 1] - buffer.indptr[scenario];
} else {
result.first += info.elements_per_scenario * scenario * result.stride;
result.size = info.elements_per_scenario;
}
}
assert(scenario == Dataset<const_dataset_t>::invalid_index || scenario >= 0);
assert(scenario == Dataset<const_dataset_t>::invalid_index || info.elements_per_scenario == -1 ||
info.elements_per_scenario >= 0);
assert(result.first != nullptr || result.size == 0);
}
return result;
}

// non-owning view over the ids of a component, for both row-based and columnar buffers
inline auto get_component_ids_view(Dataset<const_dataset_t> const& dataset, std::string_view component,
Idx scenario = Dataset<const_dataset_t>::invalid_index) {
return get_attribute_view(dataset, component, "id", scenario).as<ID>();
}

} // namespace meta_data

template <dataset_type_tag dataset_type> using Dataset = meta_data::Dataset<dataset_type>;
Expand Down
83 changes: 83 additions & 0 deletions tests/cpp_unit_tests/auxiliary/test_dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1256,4 +1256,87 @@ TEST_CASE("Test writable dataset") {
}
}

TEST_CASE("Test get_attribute_view") {
auto const& dataset_type = test_meta_data_all.datasets.front();
CAPTURE(std::string_view{dataset_type.name});

std::vector<A::InputType> const row_buffer{
{.id = 10, .a1 = 0.5}, {.id = 11, .a1 = 1.5}, {.id = 12, .a1 = 2.5}, {.id = 13, .a1 = 3.5}};
std::vector<ID> const ids{10, 11, 12, 13};
std::vector<double> const a1{0.5, 1.5, 2.5, 3.5};
std::vector<ID> const no_ids{};
Idx const total_elements = std::ssize(ids);

// checks the view over all scenarios and the view of each scenario s, which spans [offsets[s], offsets[s + 1])
auto const check_view = [](auto const& get_view, auto const& expected, std::vector<Idx> const& offsets) {
CHECK(std::ranges::equal(get_view(ConstDataset::invalid_index), expected));
for (Idx scenario = 0; scenario < std::ssize(offsets) - 1; ++scenario) {
CAPTURE(scenario);
auto const begin = offsets[scenario];
auto const end = offsets[scenario + 1];
CHECK(std::ranges::equal(get_view(scenario),
expected | std::views::drop(begin) | std::views::take(end - begin)));
}
};
auto const check_dataset = [&check_view, &a1](ConstDataset const& dataset, auto const& expected_ids,
std::vector<Idx> const& offsets) {
check_view([&dataset](Idx scenario) { return get_component_ids_view(dataset, A::name, scenario); },
expected_ids, offsets);
check_view(
[&dataset](Idx scenario) {
return get_attribute_view(dataset, A::name, A::InputType::a1_name, scenario).as<double>();
},
a1, offsets);
};
auto const check_layouts = [&](ConstDataset& dataset, Idx elements_per_scenario, Idx const* indptr,
std::vector<Idx> const& offsets) {
SUBCASE("Row-based buffer") {
dataset.add_buffer(A::name, elements_per_scenario, total_elements, indptr, row_buffer.data());
check_dataset(dataset, ids, offsets);
}
SUBCASE("Columnar buffer with ids") {
dataset.add_buffer(A::name, elements_per_scenario, total_elements, indptr, nullptr);
dataset.add_attribute_buffer(A::name, A::InputType::id_name, ids.data());
dataset.add_attribute_buffer(A::name, A::InputType::a1_name, a1.data());
check_dataset(dataset, ids, offsets);
}
SUBCASE("Columnar buffer without ids") {
dataset.add_buffer(A::name, elements_per_scenario, total_elements, indptr, nullptr);
dataset.add_attribute_buffer(A::name, A::InputType::a1_name, a1.data());
check_dataset(dataset, no_ids, offsets);
}
};

SUBCASE("Single dataset") {
auto dataset = test::create_dataset<ConstDataset>(false, 1, dataset_type);
check_layouts(dataset, total_elements, nullptr, {0, total_elements});
}
SUBCASE("Batch dataset") {
constexpr Idx batch_size{2};
auto dataset = test::create_dataset<ConstDataset>(true, batch_size, dataset_type);

SUBCASE("Uniform") { check_layouts(dataset, total_elements / batch_size, nullptr, {0, 2, total_elements}); }
SUBCASE("Non-uniform") {
std::vector<Idx> const indptr{0, 1, total_elements};
check_layouts(dataset, -1, indptr.data(), indptr);
}
}
SUBCASE("Mismatched attribute type") {
auto dataset = test::create_dataset<ConstDataset>(false, 1, dataset_type);

SUBCASE("Row-based buffer") {
dataset.add_buffer(A::name, total_elements, total_elements, nullptr, row_buffer.data());
CHECK_THROWS_AS(get_attribute_view(dataset, A::name, A::InputType::a1_name).as<ID>(), UnreachableHit);
}
SUBCASE("Columnar buffer") {
dataset.add_buffer(A::name, total_elements, total_elements, nullptr, nullptr);
dataset.add_attribute_buffer(A::name, A::InputType::a1_name, a1.data());
CHECK_THROWS_AS(get_attribute_view(dataset, A::name, A::InputType::a1_name).as<ID>(), UnreachableHit);
}
SUBCASE("Empty view does not check the type") {
CHECK_NOTHROW(get_attribute_view(dataset, A::name, A::InputType::a1_name).as<ID>());
}
}
}

} // namespace power_grid_model::meta_data
Loading