Skip to content
Closed
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
1 change: 1 addition & 0 deletions power_grid_model_c/power_grid_model_c/src/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace {
using namespace power_grid_model;

using meta_data::MetaAttribute;
using meta_data::RawDataConstPtr;
using meta_data::RawDataPtr;
using power_grid_model_c::call_with_catch;
Expand Down
2 changes: 2 additions & 0 deletions power_grid_model_c/power_grid_model_c/src/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <power_grid_model/auxiliary/meta_data.hpp>
#include <power_grid_model/common/typing.hpp>

namespace {
using namespace power_grid_model;
using namespace power_grid_model::meta_data;
using power_grid_model_c::call_with_catch;
Expand All @@ -27,6 +28,7 @@ using power_grid_model_c::safe_ptr_maybe_nullptr;
using power_grid_model_c::safe_str_view;
using power_grid_model_c::to_c_bool;
using power_grid_model_c::to_c_size;
} // namespace

// dataset info

Expand Down
45 changes: 27 additions & 18 deletions power_grid_model_c/power_grid_model_c/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class BadCalculationRequest : public PowerGridError {
explicit BadCalculationRequest(std::string msg) : PowerGridError{std::move(msg)} {}
};

void calculate_single_batch_dimension_impl(MainModel& model, PGM_Options const& opt,
void calculate_single_batch_dimension_impl(MainModel& model, MainModel::Options const& options,
MutableDataset const& output_dataset, ConstDataset const* batch_dataset) {
// check dataset integrity
if ((batch_dataset != nullptr) && (!batch_dataset->is_batch() || !output_dataset.is_batch())) {
Expand All @@ -168,13 +168,6 @@ void calculate_single_batch_dimension_impl(MainModel& model, PGM_Options const&
? safe_ptr_get(batch_dataset)
: ConstDataset{false, 1, "update", output_dataset.meta_data()};

check_calculate_valid_options(opt);
auto const options = extract_calculation_options(opt);

if (opt.experimental_features == PGM_experimental_features_disabled) {
check_no_experimental_features_used(model, options);
}

model.calculate(options, output_dataset, exported_update_dataset);
}

Expand Down Expand Up @@ -241,29 +234,33 @@ class MDBatchExceptionHandler : public power_grid_model_c::DefaultExceptionHandl

Idx get_batch_dimension(ConstDataset const* batch_dataset) {
Idx dimension = 0;
while (batch_dataset != nullptr) {
ConstDataset const* safe_batch_dataset = safe_ptr_maybe_nullptr(batch_dataset);
while (safe_batch_dataset != nullptr) {
++dimension;
batch_dataset = batch_dataset->get_next_cartesian_product_dimension();
safe_batch_dataset =
safe_ptr_maybe_nullptr(safe_ptr_get(safe_batch_dataset).get_next_cartesian_product_dimension());
}
return dimension;
}

Idx get_stride_size(ConstDataset const* batch_dataset) {
Idx size = 1;
ConstDataset const* current = batch_dataset->get_next_cartesian_product_dimension();
ConstDataset const* current =
safe_ptr_maybe_nullptr(safe_ptr_get(batch_dataset).get_next_cartesian_product_dimension());
while (current != nullptr) {
size *= current->batch_size();
current = current->get_next_cartesian_product_dimension();
auto const& safe_current = safe_ptr_get(current);
size *= safe_current.batch_size();
current = safe_current.get_next_cartesian_product_dimension();
}
return size;
}

// run calculation
void calculate_multi_dimensional_impl(MainModel& model, PGM_Options const& opt, MutableDataset const& output_dataset,
void calculate_multi_dimensional_impl(MainModel& model, MainModel::Options const& options, MutableDataset const& output_dataset,
ConstDataset const* batch_dataset) {
// for dimension < 2 (one-time or 1D batch), call implementation directly
if (auto const batch_dimension = get_batch_dimension(batch_dataset); batch_dimension < 2) {
calculate_single_batch_dimension_impl(model, opt, output_dataset, batch_dataset);
calculate_single_batch_dimension_impl(model, options, output_dataset, batch_dataset);
return;
}

Expand All @@ -280,7 +277,7 @@ void calculate_multi_dimensional_impl(MainModel& model, PGM_Options const& opt,
// a new handle
call_with_catch(
&local_handle,
[&model, &opt, &output_dataset, &safe_batch_dataset, i, stride_size] {
[&model, &options, &output_dataset, &safe_batch_dataset, i, stride_size] {
// create sliced datasets for the rest of dimensions
ConstDataset const single_update_dataset = safe_batch_dataset.get_individual_scenario(i);
MutableDataset const sliced_output_dataset =
Expand All @@ -293,7 +290,7 @@ void calculate_multi_dimensional_impl(MainModel& model, PGM_Options const& opt,
local_model.update_components<permanent_update_t>(single_update_dataset);

// recursive call
calculate_multi_dimensional_impl(local_model, opt, sliced_output_dataset,
calculate_multi_dimensional_impl(local_model, options, sliced_output_dataset,
safe_batch_dataset.get_next_cartesian_product_dimension());
},
MDBatchExceptionHandler{i * stride_size, stride_size});
Expand All @@ -305,6 +302,18 @@ void calculate_multi_dimensional_impl(MainModel& model, PGM_Options const& opt,
}
}

void calculate_impl(MainModel& model, PGM_Options const& options, MutableDataset const& output_dataset,
ConstDataset const* batch_dataset) {
check_calculate_valid_options(options);
auto const extracted_options = extract_calculation_options(options);

if (options.experimental_features == PGM_experimental_features_disabled) {
check_no_experimental_features_used(model, extracted_options);
}

calculate_multi_dimensional_impl(model, extracted_options, output_dataset, batch_dataset);
}

} // namespace

// run calculation
Expand All @@ -313,7 +322,7 @@ void PGM_calculate(PGM_Handle* handle, PGM_PowerGridModel* model, PGM_Options co
call_with_catch(
handle,
[model, opt, output_dataset, batch_dataset] {
calculate_multi_dimensional_impl(safe_ptr_get(cast_to_cpp(model)), safe_ptr_get(opt),
calculate_impl(safe_ptr_get(cast_to_cpp(model)), safe_ptr_get(opt),
safe_ptr_get(cast_to_cpp(output_dataset)),
safe_ptr_maybe_nullptr(cast_to_cpp(batch_dataset)));
},
Expand Down
7 changes: 7 additions & 0 deletions power_grid_model_c/power_grid_model_c/src/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ struct SerializationExceptionHandler : public power_grid_model_c::DefaultExcepti
constexpr SerializationExceptionHandler serialization_exception_handler{};
} // namespace

struct PGM_Serializer : public power_grid_model::meta_data::Serializer {
using Serializer::Serializer;
};
struct PGM_Deserializer : public power_grid_model::meta_data::Deserializer {
using Deserializer::Deserializer;
};

PGM_Deserializer* PGM_create_deserializer_from_binary_buffer(PGM_Handle* handle, char const* data, PGM_Idx size,
PGM_Idx serialization_format) {
return call_with_catch(
Expand Down
Loading