123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- /**
- * @file tests/multithreading/simple/models.hpp
- * @author The ARTIS Development Team
- * See the AUTHORS or Authors.txt file
- */
- /*
- * ARTIS - the multimodeling and simulation environment
- * This file is a part of the ARTIS environment
- *
- * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef TESTS_MULTITHREADING_SIMPLE_MODELS_HPP
- #define TESTS_MULTITHREADING_SIMPLE_MODELS_HPP
- #include <artis-star/common/time/DoubleTime.hpp>
- #include <artis-star/kernel/pdevs/Dynamics.hpp>
- #include <random>
- #include <iostream>
- namespace artis {
- namespace tests {
- namespace multithreading {
- namespace simple {
- void delay()
- {
- for (unsigned int i = 0; i < 1000; ++i) {
- std::vector<int> v;
- for (unsigned int j = 1000; j > 0; --j) {
- v.push_back(j);
- }
- std::sort(v.begin(), v.end());
- }
- }
- struct State
- {
- enum values
- {
- STOP, UP, MAX, DOWN
- };
- };
- struct Vehicle
- {
- unsigned int index;
- double v_max;
- double acceleration;
- State::values state;
- artis::common::DoubleTime::type next_time;
- };
- struct GeneratorParameters
- {
- double v_max;
- double mean;
- double stddev;
- unsigned long seed;
- };
- class Generator
- : public artis::pdevs::Dynamics<artis::common::DoubleTime, Generator, GeneratorParameters>
- {
- public:
- struct outputs
- {
- enum values
- {
- OUT
- };
- };
- Generator(const std::string &name,
- const artis::pdevs::Context<artis::common::DoubleTime,
- Generator,
- GeneratorParameters> &context)
- :
- artis::pdevs::Dynamics<artis::common::DoubleTime, Generator, GeneratorParameters>(
- name, context),
- _v_max(context.parameters().v_max),
- _distribution(context.parameters().mean, context.parameters().stddev),
- _v_max_distribution(0.5, 1.),
- _port_distribution(0, 7)
- {
- _generator.seed(context.parameters().seed);
- output_ports({{outputs::OUT, "out_1"}});
- output_ports({{outputs::OUT + 1, "out_2"}});
- output_ports({{outputs::OUT + 2, "out_3"}});
- output_ports({{outputs::OUT + 3, "out_4"}});
- output_ports({{outputs::OUT + 4, "out_5"}});
- output_ports({{outputs::OUT + 5, "out_6"}});
- output_ports({{outputs::OUT + 6, "out_7"}});
- output_ports({{outputs::OUT + 7, "out_8"}});
- }
- ~Generator() override = default;
- void dint(const artis::common::DoubleTime::type &t) override
- {
- // std::cout << get_full_name() << " at " << t << " - dint" << std::endl;
- _last_time = t;
- _sigma = _distribution(_generator);
- _sigma = _sigma <= 0 ? 0.1 : _sigma;
- _next_v_max = _v_max * _v_max_distribution(_generator);
- _next_port = _port_distribution(_generator);
- ++_index;
- }
- void start(const artis::common::DoubleTime::type &t) override
- {
- // std::cout << get_full_name() << " at " << t << " - start" << std::endl;
- _last_time = t;
- _sigma = _distribution(_generator);
- _sigma = _sigma <= 0 ? 0.1 : _sigma;
- _next_v_max = _v_max * _v_max_distribution(_generator);
- _next_port = _port_distribution(_generator);
- _index = 1;
- }
- artis::common::DoubleTime::type
- ta(const artis::common::DoubleTime::type & /* t */) const override
- { return _sigma; }
- artis::common::Bag<artis::common::DoubleTime>
- lambda(const artis::common::DoubleTime::type &t) const override
- {
- artis::common::Bag<artis::common::DoubleTime> bag;
- // std::cout << get_full_name() << " at " << t << " - lambda" << std::endl;
- if (t > 0) {
- Vehicle vehicle = {_index, _next_v_max, 0.5, State::STOP, t};
- bag.push_back(
- artis::common::ExternalEvent<artis::common::DoubleTime>(
- outputs::OUT + _next_port, vehicle));
- }
- return bag;
- }
- common::DoubleTime::type
- lookahead(const common::DoubleTime::type & /* t */) const override
- {
- return _last_time + _sigma;
- }
- private:
- // parameters
- double _v_max;
- // state
- artis::common::DoubleTime::type _sigma;
- artis::common::DoubleTime::type _last_time;
- std::default_random_engine _generator;
- std::normal_distribution<double> _distribution;
- std::uniform_real_distribution<double> _v_max_distribution;
- std::uniform_int_distribution<int> _port_distribution;
- double _next_v_max;
- int _next_port;
- unsigned int _index;
- };
- class Counter
- : public artis::pdevs::Dynamics<artis::common::DoubleTime, Counter>
- {
- public:
- struct inputs
- {
- enum values
- {
- IN
- };
- };
- struct vars
- {
- enum values
- {
- COUNTER
- };
- };
- Counter(const std::string &name,
- const artis::pdevs::Context<artis::common::DoubleTime, Counter> &context)
- :
- artis::pdevs::Dynamics<artis::common::DoubleTime, Counter>(name,
- context)
- {
- input_port({inputs::IN, "in"});
- observable({vars::COUNTER, "counter"});
- }
- ~Counter() override = default;
- void dext(const artis::common::DoubleTime::type & /* t */,
- const artis::common::DoubleTime::type & /* e */,
- const artis::common::Bag<artis::common::DoubleTime> &bag) override
- {
- // std::cout << get_full_name() << " at " << t << " - dext" << std::endl;
- _counter += bag.size();
- }
- void start(const artis::common::DoubleTime::type & /* t */) override
- {
- // std::cout << get_full_name() << " at " << t << " - start" << std::endl;
- _counter = 0;
- }
- artis::common::DoubleTime::type
- ta(const artis::common::DoubleTime::type & /* t */) const override
- {
- return artis::common::DoubleTime::infinity;
- }
- artis::common::Value observe(const artis::common::DoubleTime::type & /* t */,
- unsigned int index) const override
- {
- if (index == vars::COUNTER) {
- return _counter;
- } else {
- return artis::common::Value();
- }
- }
- common::DoubleTime::type lookahead(const common::DoubleTime::type & /* t */) const override
- {
- return common::DoubleTime::infinity;
- }
- private:
- unsigned int _counter;
- };
- class Link :
- public artis::pdevs::Dynamics<common::DoubleTime, Link>
- {
- public :
- struct inputs
- {
- enum values
- {
- IN
- };
- };
- struct outputs
- {
- enum values
- {
- OUT
- };
- };
- Link(const std::string &name,
- const artis::pdevs::Context<common::DoubleTime, Link> &context)
- : artis::pdevs::Dynamics<common::DoubleTime, Link>(name, context)
- {
- input_port({inputs::IN, "in"});
- output_port({outputs::OUT, "out"});
- }
- ~ Link() override = default;
- void dint(const artis::common::DoubleTime::type &t) override
- {
- // delay();
- // std::cout << get_full_name() << " at " << t << " - dint" << std::endl;
- auto it = _vehicles.begin();
- while (it != _vehicles.end()) {
- if (it->next_time == t and it->state == State::STOP) {
- _vehicles.erase(it);
- it = _vehicles.begin();
- } else {
- ++it;
- }
- }
- for (auto &vehicle: _vehicles) {
- if (vehicle.next_time == t) {
- switch (vehicle.state) {
- case State::UP: {
- double duration = vehicle.v_max / vehicle.acceleration;
- double acceleration_distance = 0.5 * vehicle.acceleration * duration * duration;
- vehicle.state = State::MAX;
- vehicle.next_time = t + (_length - 2 * acceleration_distance) / vehicle.v_max;
- break;
- }
- case State::MAX: {
- vehicle.state = State::DOWN;
- vehicle.next_time = t + vehicle.v_max / vehicle.acceleration;
- break;
- }
- case State::DOWN: {
- vehicle.state = State::STOP;
- vehicle.next_time = t;
- break;
- }
- case State::STOP: {
- assert(false);
- break;
- }
- }
- }
- }
- update_sigma(t);
- }
- void dext(const artis::common::DoubleTime::type &t,
- const artis::common::DoubleTime::type & /* e */,
- const artis::common::Bag<artis::common::DoubleTime> &bag) override
- {
- // std::cout << get_full_name() << " at " << t << " - dext" << std::endl;
- std::for_each(bag.begin(), bag.end(),
- [this, t](const common::ExternalEvent<common::DoubleTime> &event) {
- if (event.on_port(inputs::IN)) {
- Vehicle vehicle;
- event.data()(vehicle);
- vehicle.next_time = t + vehicle.v_max / vehicle.acceleration;
- vehicle.state = State::UP;
- _vehicles.push_back(vehicle);
- }
- });
- update_sigma(t);
- }
- artis::common::DoubleTime::type ta(const artis::common::DoubleTime::type & /* t */) const override
- { return _sigma; }
- artis::common::Bag<artis::common::DoubleTime>
- lambda(const artis::common::DoubleTime::type &t) const override
- {
- artis::common::Bag<artis::common::DoubleTime> bag;
- // std::cout << get_full_name() << " at " << t << " - lambda" << std::endl;
- for (auto vehicle: _vehicles) {
- if (vehicle.next_time == t and vehicle.state == State::STOP) {
- bag.push_back(
- artis::common::ExternalEvent<artis::common::DoubleTime>(
- outputs::OUT, vehicle));
- }
- }
- return bag;
- }
- void start(const artis::common::DoubleTime::type & /* t */) override
- {
- // std::cout << get_full_name() << " at " << t << " - start" << std::endl;
- _sigma = artis::common::DoubleTime::infinity;
- }
- common::DoubleTime::type lookahead(const common::DoubleTime::type &t) const override
- {
- double eot = artis::common::DoubleTime::infinity;
- for (auto vehicle: _vehicles) {
- double eot_i = artis::common::DoubleTime::infinity;
- if (vehicle.next_time == t and vehicle.state == State::STOP) {
- eot_i = t;
- } else if (vehicle.state == State::DOWN) {
- eot_i = vehicle.next_time;
- } else if (vehicle.state == State::MAX) {
- eot_i = vehicle.next_time + vehicle.v_max / vehicle.acceleration;
- } else if (vehicle.state == State::UP) {
- double duration = vehicle.v_max / vehicle.acceleration;
- double acceleration_distance = 0.5 * vehicle.acceleration * duration * duration;
- eot_i = vehicle.next_time
- + (_length - 2 * acceleration_distance) / vehicle.v_max
- + vehicle.v_max / vehicle.acceleration;
- }
- if (eot_i < eot) {
- eot = eot_i;
- }
- }
- return eot;
- }
- private:
- void update_sigma(const artis::common::DoubleTime::type &t)
- {
- if (_vehicles.empty()) {
- _sigma = artis::common::DoubleTime::infinity;
- } else {
- _sigma = std::min_element(_vehicles.begin(), _vehicles.end(),
- [](const Vehicle &e1, const Vehicle &e2) {
- return e1.next_time < e2.next_time;
- })->next_time - t;
- }
- }
- const double _length = 500;
- std::deque<Vehicle> _vehicles;
- artis::common::DoubleTime::type _sigma;
- };
- }
- }
- }
- } // namespace artis tests multithreading simple
- #endif
|