Commit 2c2caa73 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

core: remove useless copy mechanism

parent e67dcef8
......@@ -4538,179 +4538,7 @@ struct simulation
});
}
template<typename Iterator>
status copy_connections(Iterator first_orig,
Iterator last_orig,
Iterator copy,
const input_port& orig_port,
const input_port& copy_port) noexcept
{
for (output_port_id output_id : orig_port.connections) {
auto* output = output_ports.try_to_get(output_id);
irt_return_if_fail(output, status::dynamics_unknown_port_id);
auto it = std::find(first_orig, last_orig, output->model);
if (it == last_orig) {
irt_return_if_bad(
connect(output_id, input_ports.get_id(copy_port)));
} else {
auto* orig_model = models.try_to_get(output->model);
irt_return_if_fail(orig_model, status::dynamics_unknown_id);
int index_port = -1;
irt_return_if_bad(
get_output_port_index(*orig_model, output_id, &index_port));
auto index_model = std::distance(first_orig, it);
auto to_change_id = *(copy + index_model);
auto* to_change_mdl = models.try_to_get(to_change_id);
irt_return_if_fail(to_change_mdl, status::dynamics_unknown_id);
output_port_id copy_port_id;
irt_return_if_bad(get_output_port_id(
*to_change_mdl, index_port, &copy_port_id));
connect(copy_port_id, input_ports.get_id(copy_port));
}
}
return status::success;
}
template<typename Iterator>
status copy_connections(Iterator first_orig,
Iterator last_orig,
Iterator copy,
const output_port& orig_port,
const output_port& copy_port) noexcept
{
for (input_port_id input_id : orig_port.connections) {
auto* input = input_ports.try_to_get(input_id);
irt_return_if_fail(input, status::dynamics_unknown_port_id);
auto it = std::find(first_orig, last_orig, input->model);
if (it == last_orig) {
irt_return_if_bad(
connect(output_ports.get_id(copy_port), input_id));
} else {
auto* orig_model = models.try_to_get(input->model);
irt_return_if_fail(orig_model, status::dynamics_unknown_id);
int index_port = -1;
irt_return_if_bad(
get_input_port_index(*orig_model, input_id, &index_port));
auto index_model = std::distance(first_orig, it);
auto to_change_id = *(copy + index_model);
auto* to_change_mdl = models.try_to_get(to_change_id);
irt_return_if_fail(to_change_mdl, status::dynamics_unknown_id);
input_port_id copy_port_id;
irt_return_if_bad(
get_input_port_id(*to_change_mdl, index_port, &copy_port_id));
connect(output_ports.get_id(copy_port), copy_port_id);
}
}
return status::success;
}
public:
template<typename InputIterator, typename OutputIterator>
status copy(InputIterator first, InputIterator last, OutputIterator out)
{
for (auto it = first, copy = out; it != last; ++it, ++copy) {
model_id model_id = *it;
const model* mdl = models.try_to_get(model_id);
irt_return_if_fail(mdl, status::dynamics_unknown_id);
auto ret = dispatch(
mdl->type,
[ this, &mdl,
copy ]<typename DynamicsM>(DynamicsM & dynamics_models)
->status {
using Dynamics = typename DynamicsM::value_type;
irt_return_if_fail(dynamics_models.can_alloc(1),
status::dynamics_not_enough_memory);
auto* dyn_ptr = dynamics_models.try_to_get(mdl->id);
irt_return_if_fail(dyn_ptr, status::dynamics_unknown_id);
auto& new_dyn = dynamics_models.alloc(*dyn_ptr);
auto new_dyn_id = dynamics_models.get_id(new_dyn);
if constexpr (is_detected_v<has_input_port_t, Dynamics>)
std::fill_n(new_dyn.x,
std::size(new_dyn.x),
static_cast<input_port_id>(0));
if constexpr (is_detected_v<has_output_port_t, Dynamics>)
std::fill_n(new_dyn.y,
std::size(new_dyn.y),
static_cast<output_port_id>(0));
irt_return_if_bad(
this->alloc(new_dyn, new_dyn_id, mdl->name.c_str()));
*copy = new_dyn.id;
return status::success;
});
irt_return_if_bad(ret);
}
size_t model_it = 0;
for (auto it = first; it != last; ++it, ++model_it) {
const model* mdl = models.try_to_get(*(first + model_it));
irt_return_if_fail(mdl, status::dynamics_unknown_id);
auto ret = dispatch(
mdl->type,
[ this, model_it, first, last,
out ]<typename DynamicsM>(DynamicsM & dynamics_models) {
using Dynamics = typename DynamicsM::value_type;
auto* mdl_src = this->models.try_to_get(*(first + model_it));
auto* mdl_dst = this->models.try_to_get(*(out + model_it));
irt_return_if_fail(mdl_src, status::dynamics_unknown_id);
irt_return_if_fail(mdl_dst, status::dynamics_unknown_id);
auto* dyn_src = dynamics_models.try_to_get(mdl_src->id);
auto* dyn_dst = dynamics_models.try_to_get(mdl_dst->id);
irt_return_if_fail(dyn_src, status::dynamics_unknown_id);
irt_return_if_fail(dyn_dst, status::dynamics_unknown_id);
if constexpr (is_detected_v<has_input_port_t, Dynamics>)
for (size_t i = 0, e = std::size(dyn_src->x); i != e; ++i)
copy_connections(first,
last,
out,
input_ports.get(dyn_src->x[i]),
input_ports.get(dyn_dst->x[i]));
if constexpr (is_detected_v<has_output_port_t, Dynamics>)
for (size_t i = 0, e = std::size(dyn_src->y); i != e; ++i)
copy_connections(first,
last,
out,
output_ports.get(dyn_src->y[i]),
output_ports.get(dyn_dst->y[i]));
return status::success;
});
irt_return_if_bad(ret);
}
return status::success;
}
status init(size_t model_capacity, size_t messages_capacity)
{
constexpr size_t ten{ 10 };
......
......@@ -788,116 +788,6 @@ main()
expect(cnt.number == 9_ul);
};
"generator_counter_and_copy_simluation"_test = [] {
irt::simulation sim;
expect(irt::is_success(sim.init(16lu, 256lu)));
expect(sim.generator_models.can_alloc(1));
expect(sim.counter_models.can_alloc(1));
auto& gen = sim.generator_models.alloc();
auto& cnt = sim.counter_models.alloc();
expect(sim.models.can_alloc(2));
expect(
irt::is_success(sim.alloc(gen, sim.generator_models.get_id(gen))));
expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
expect(sim.connect(gen.y[0], cnt.x[0]) == irt::status::success);
irt::model_id sources[2], destinations[2];
sources[0] = gen.id;
sources[1] = cnt.id;
destinations[0] = static_cast<irt::model_id>(0);
destinations[1] = static_cast<irt::model_id>(0);
expect(sim.copy(std::begin(sources),
std::end(sources),
std::begin(destinations)) == irt::status::success);
expect(destinations[0] != static_cast<irt::model_id>(0));
expect(destinations[1] != static_cast<irt::model_id>(0));
expect(destinations[0] != sources[0]);
expect(destinations[1] != sources[0]);
expect(destinations[0] != sources[1]);
expect(destinations[1] != sources[1]);
irt::model& gen_copy = sim.models.get(destinations[0]);
expect(gen_copy.type == irt::dynamics_type::generator);
irt::model& cnt_copy = sim.models.get(destinations[1]);
expect(cnt_copy.type == irt::dynamics_type::counter);
irt::time t = 0.0;
expect(sim.initialize(t) == irt::status::success);
expect(sim.models.size() == 4);
irt::status st;
do {
st = sim.run(t);
expect(irt::is_success(st));
expect(cnt.number <= static_cast<irt::i64>(t));
} while (t < 10.0);
expect(cnt.number == 9_ul);
irt::counter* dyn_cnt_copy = sim.counter_models.try_to_get(cnt_copy.id);
expect(dyn_cnt_copy != nullptr);
expect(dyn_cnt_copy->number == 9_ul);
};
"generators_counter"_test = [] {
irt::simulation sim;
expect(irt::is_success(sim.init(16lu, 256lu)));
expect(sim.generator_models.can_alloc(1));
expect(sim.counter_models.can_alloc(1));
auto& gen = sim.generator_models.alloc();
auto& cnt = sim.counter_models.alloc();
expect(sim.models.can_alloc(2));
expect(
irt::is_success(sim.alloc(gen, sim.generator_models.get_id(gen))));
expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
expect(sim.connect(gen.y[0], cnt.x[0]) == irt::status::success);
irt::model_id sources[1], destinations[1];
sources[0] = gen.id;
destinations[0] = static_cast<irt::model_id>(0);
expect(sim.copy(std::begin(sources),
std::end(sources),
std::begin(destinations)) == irt::status::success);
expect(destinations[0] != static_cast<irt::model_id>(0));
expect(destinations[0] != sources[0]);
destinations[0] = static_cast<irt::model_id>(0);
expect(sim.copy(std::begin(sources),
std::end(sources),
std::begin(destinations)) == irt::status::success);
expect(destinations[0] != static_cast<irt::model_id>(0));
expect(destinations[0] != sources[0]);
irt::time t = 0.0;
expect(sim.initialize(t) == irt::status::success);
expect(sim.models.size() == 4);
irt::status st;
do {
st = sim.run(t);
expect(irt::is_success(st));
expect(cnt.number <= static_cast<irt::i64>(3 * t));
} while (t < 10.0);
expect(cnt.number == 27_ul);
};
"time_func"_test = [] {
irt::simulation sim;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment