Skip to content
Merged
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 @@ -74,8 +74,13 @@ template <class StructType> constexpr MetaComponent get_meta_component(char cons
auto ptr = reinterpret_cast<StructType*>(buffer_ptr);
std::fill(ptr + pos, ptr + pos + size, StructType{});
},
.create_buffer = [](Idx size) -> RawDataPtr { return new StructType[size]; },
.destroy_buffer = [](RawDataConstPtr buffer_ptr) { delete[] reinterpret_cast<StructType const*>(buffer_ptr); },
.create_buffer = [](Idx size) -> RawDataPtr {
return new StructType[size]; // NOSONAR(S5025)
},
.destroy_buffer =
[](RawDataConstPtr buffer_ptr) {
delete[] reinterpret_cast<StructType const*>(buffer_ptr); // NOSONAR(S5025)
},
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class JobDispatch {
std::exception_ptr const ex_ptr = std::current_exception();
try {
std::rethrow_exception(ex_ptr);
} catch (std::exception const& ex) {
} catch (std::exception const& ex) { // NOSONAR(S1181)
messages[scenario_idx] = ex.what();
} catch (...) {
} catch (...) { // NOSONAR(S2738)
messages[scenario_idx] = "unknown exception";
}
};
Expand Down Expand Up @@ -178,11 +178,11 @@ class JobDispatch {
setup_(args...);
run_(args...);
winddown_();
} catch (...) {
} catch (...) { // NOSONAR(S2738)
handle_exception_(args...);
try {
winddown_();
} catch (...) {
} catch (...) { // NOSONAR(S2738)
recover_from_bad_();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ template <symmetry_tag sym> class MathSolver : public MathSolverBase<sym> {
all_const_y_{std::all_of(topo_ptr->load_gen_type.cbegin(), topo_ptr->load_gen_type.cend(),
[](LoadGenType x) { return x == LoadGenType::const_y; })} {}

MathSolver<sym>* clone() const final { return new MathSolver<sym>(*this); }
MathSolver<sym>* clone() const final {
return new MathSolver<sym>(*this); // NOSONAR(S5025)
}

SolverOutput<sym> run_power_flow(PowerFlowInput<sym> const& input, double err_tol, Idx max_iter, Logger& log,
CalculationMethod calculation_method, YBus<sym> const& y_bus) final {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MathSolverDispatcher {
template <template <class> class MathSolverType>
constexpr Config(math_solver_tag<MathSolverType> /* unused */)
: create{[](std::shared_ptr<MathModelTopology const> const& topo_ptr) -> MathSolverBase<sym>* {
return new MathSolverType<sym>{topo_ptr};
return new MathSolverType<sym>{topo_ptr}; // NOSONAR(S5025)
}} {}

std::add_pointer_t<MathSolverBase<sym>*(std::shared_ptr<MathModelTopology const> const&)> create;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ class TapPositionOptimizerImpl<std::tuple<TransformerTypes...>, StateCalculator,
auto result = optimize(state, order, method);
update_state(cache);
return result;
} catch (...) {
} catch (...) { // NOSONAR(S2738)
update_state(cache);
throw;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ enum PGM_SymmetryType {
*
*/
enum PGM_ErrorCode {
PGM_no_error = 0, /**< no error occurred */
PGM_regular_error = 1, /**< some error occurred which is not in the batch calculation */
PGM_batch_error = 2, /**< some error occurred which is in the batch calculation */
PGM_serialization_error = 3 /**< some error occurred which is in the (de)serialization process */
PGM_no_error = 0, /**< no error occurred */
PGM_regular_error = 1, /**< some error occurred which is not in the batch calculation */
PGM_batch_error = 2, /**< some error occurred which is in the batch calculation */
PGM_serialization_error = 3, /**< some error occurred which is in the (de)serialization process */
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ PGM_API void PGM_destroy_handle(PGM_Handle* handle);
/**
* @brief Get error code of last operation.
*
* The behavior is implementation-defined if the handle is NULL.
*
* @param handle The pointer to the handle you just used for an operation.
* @return The error code, see #PGM_ErrorCode .
*/
Expand All @@ -49,6 +51,8 @@ PGM_API PGM_Idx PGM_error_code(PGM_Handle const* handle);
* If the error code is PGM_batch_error.
* Use PGM_n_failed_scenarios(), PGM_failed_scenarios(), and PGM_batch_errors() to retrieve the detail.
*
* The behavior is implementation-defined if the handle is NULL.
*
* @param handle The pointer to the handle you just used for an operation.
* @return A char const* poiner to a zero terminated string.
* The pointer is not valid if you execute another operation.
Expand All @@ -59,6 +63,8 @@ PGM_API char const* PGM_error_message(PGM_Handle const* handle);
/**
* @brief Get the number of failed scenarios. Only applicable when you just executed a batch calculation.
*
* The behavior is implementation-defined if the handle is NULL.
*
* @param handle The pointer to the handle you just used for a batch calculation.
* @return The number of failed scenarios.
*/
Expand All @@ -67,6 +73,8 @@ PGM_API PGM_Idx PGM_n_failed_scenarios(PGM_Handle const* handle);
/**
* @brief Get the list of failed scenarios, Only applicable when you just execute a batch calculation.
*
* The behavior is implementation-defined if the handle is NULL.
*
* @param handle The pointer to the handle you just used for a batch calculation.
* @return A pointer to a PGM_Idx array with length returned by PGM_n_failed_scenarios().
* The pointer is not valid if you execute another operation.
Expand All @@ -77,6 +85,8 @@ PGM_API PGM_Idx const* PGM_failed_scenarios(PGM_Handle const* handle);
/**
* @brief Get the list of batch errors. Only applicable when you just execute a batch calculation.
*
* The behavior is implementation-defined if the handle is NULL.
*
* @param handle The pointer to the handle you just used for a batch calculation.
* @return A pointer to a char const* array with length returned by PGM_n_failed_scenarios().
* Each entry is a zero terminated string.
Expand All @@ -88,6 +98,8 @@ PGM_API char const** PGM_batch_errors(PGM_Handle const* handle);
/**
* @brief Clear and reset the handle.
*
* The behavior is implementation-defined if the handle is NULL.
*
* @param handle The pointer to the handle.
*/
PGM_API void PGM_clear_error(PGM_Handle* handle);
Expand Down
70 changes: 50 additions & 20 deletions power_grid_model_c/power_grid_model_c/src/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#include "power_grid_model_c/buffer.h"

#include "handle.hpp"
#include "input_sanitization.hpp"

#include <power_grid_model/auxiliary/meta_data.hpp>

#include <cstdlib>
Expand All @@ -16,20 +19,29 @@ using namespace power_grid_model;

using meta_data::RawDataConstPtr;
using meta_data::RawDataPtr;
using power_grid_model_c::call_with_catch;
using power_grid_model_c::safe_ptr;
using power_grid_model_c::safe_ptr_get;
using power_grid_model_c::safe_ptr_maybe_nullptr;
using power_grid_model_c::to_c_size;
} // namespace

// buffer control
RawDataPtr PGM_create_buffer(PGM_Handle* /* handle */, PGM_MetaComponent const* component, PGM_Idx size) {
// alignment should be maximum of alignment of the component and alignment of void*
size_t const alignment = std::max(component->alignment, sizeof(void*));
// total bytes should be multiple of alignment
size_t const requested_bytes = component->size * size;
size_t const rounded_bytes = ((requested_bytes + alignment - 1) / alignment) * alignment;
RawDataPtr PGM_create_buffer(PGM_Handle* handle, PGM_MetaComponent const* component, PGM_Idx size) {
return call_with_catch(handle, [component, size] {
auto const& safe_component = safe_ptr_get(component);

// alignment should be maximum of alignment of the component and alignment of void*
size_t const alignment = std::max(safe_component.alignment, sizeof(void*));
// total bytes should be multiple of alignment
size_t const requested_bytes = safe_component.size * size;
size_t const rounded_bytes = ((requested_bytes + alignment - 1) / alignment) * alignment;
#ifdef _WIN32
return _aligned_malloc(rounded_bytes, alignment);
return _aligned_malloc(rounded_bytes, alignment);
#else
return std::aligned_alloc(alignment, rounded_bytes);
return std::aligned_alloc(alignment, rounded_bytes);
#endif
});
}
void PGM_destroy_buffer(RawDataPtr ptr) {
#ifdef _WIN32
Expand All @@ -38,36 +50,54 @@ void PGM_destroy_buffer(RawDataPtr ptr) {
std::free(ptr); // NOLINT(hicpp-no-malloc)
#endif
}
void PGM_buffer_set_nan(PGM_Handle* /* handle */, PGM_MetaComponent const* component, void* ptr, PGM_Idx buffer_offset,

void PGM_buffer_set_nan(PGM_Handle* handle, PGM_MetaComponent const* component, void* ptr, PGM_Idx buffer_offset,
PGM_Idx size) {
component->set_nan(ptr, buffer_offset, size);
call_with_catch(handle, [component, ptr, buffer_offset, size] {
safe_ptr_get(component).set_nan(safe_ptr(ptr), buffer_offset, size);
});
}

namespace {
// template for get and set attribute
template <bool is_get, class BufferPtr, class ValuePtr>
void buffer_get_set_value(PGM_MetaAttribute const* attribute, BufferPtr buffer_ptr, ValuePtr value_ptr,
void buffer_get_set_value(PGM_MetaAttribute const& attribute, BufferPtr buffer_ptr, ValuePtr value_ptr,
PGM_Idx buffer_offset, PGM_Idx size, PGM_Idx stride) {
using RawValuePtr = std::conditional_t<is_get, char*, char const*>;

if (size <= 0) {
return;
}

// if stride is negative, use the size of the attributes as stride
if (stride < 0) {
stride = static_cast<PGM_Idx>(attribute->size);
stride = to_c_size(attribute.size);
}

auto const raw_value_ptr = reinterpret_cast<RawValuePtr>(safe_ptr(value_ptr));
auto const safe_buffer_ptr = safe_ptr(buffer_ptr);

for (Idx i = buffer_offset; i != size + buffer_offset; ++i) {
ValuePtr const shifted_value_ptr =
reinterpret_cast<std::conditional_t<is_get, char*, char const*>>(value_ptr) + stride * i;
ValuePtr const shifted_value_ptr = raw_value_ptr + stride * i;
if constexpr (is_get) {
attribute->get_value(buffer_ptr, shifted_value_ptr, i);
attribute.get_value(safe_buffer_ptr, shifted_value_ptr, i);
} else {
attribute->set_value(buffer_ptr, shifted_value_ptr, i);
attribute.set_value(safe_buffer_ptr, shifted_value_ptr, i);
}
}
}
} // namespace
void PGM_buffer_set_value(PGM_Handle* /* handle */, PGM_MetaAttribute const* attribute, RawDataPtr buffer_ptr,
void PGM_buffer_set_value(PGM_Handle* handle, PGM_MetaAttribute const* attribute, RawDataPtr buffer_ptr,
RawDataConstPtr src_ptr, PGM_Idx buffer_offset, PGM_Idx size, PGM_Idx src_stride) {
buffer_get_set_value<false>(attribute, buffer_ptr, src_ptr, buffer_offset, size, src_stride);
call_with_catch(handle, [attribute, buffer_ptr, src_ptr, buffer_offset, size, src_stride] {
buffer_get_set_value<false>(safe_ptr_get(attribute), safe_ptr_maybe_nullptr(buffer_ptr),
safe_ptr_maybe_nullptr(src_ptr), buffer_offset, size, src_stride);
});
}
void PGM_buffer_get_value(PGM_Handle* /* handle */, PGM_MetaAttribute const* attribute, RawDataConstPtr buffer_ptr,
void PGM_buffer_get_value(PGM_Handle* handle, PGM_MetaAttribute const* attribute, RawDataConstPtr buffer_ptr,
RawDataPtr dest_ptr, PGM_Idx buffer_offset, PGM_Idx size, PGM_Idx dest_stride) {
buffer_get_set_value<true>(attribute, buffer_ptr, dest_ptr, buffer_offset, size, dest_stride);
call_with_catch(handle, [attribute, buffer_ptr, dest_ptr, buffer_offset, size, dest_stride] {
buffer_get_set_value<true>(safe_ptr_get(attribute), safe_ptr_maybe_nullptr(buffer_ptr),
safe_ptr_maybe_nullptr(dest_ptr), buffer_offset, size, dest_stride);
});
}
Loading
Loading