123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- /**
- * @file kernel/pdevs/multithreading/Coordinator.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-2021 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 PDEVS_MULTITHREADING_COORDINATOR
- #define PDEVS_MULTITHREADING_COORDINATOR
- #include <artis-star/common/utils/Multithreading.hpp>
- #include <artis-star/kernel/pdevs/Coordinator.hpp>
- #include <thread>
- namespace artis {
- namespace pdevs {
- namespace multithreading {
- template<class Time>
- struct start_message
- {
- explicit start_message(typename Time::type t)
- : _t(t)
- {}
- typename Time::type _t;
- };
- template<class Time>
- struct transition_message
- {
- explicit transition_message(typename Time::type t)
- : _t(t)
- {}
- typename Time::type _t;
- };
- template<class Time>
- struct done_start_message
- {
- explicit done_start_message(typename Time::type tn,
- common::Model <Time> *child)
- :
- _tn(tn), _child(child)
- {}
- typename Time::type _tn;
- common::Model <Time> *_child;
- };
- template<class Time>
- struct done_transition_message
- {
- explicit done_transition_message(typename Time::type tn,
- common::Model <Time> *child)
- :
- _tn(tn), _child(child)
- {}
- typename Time::type _tn;
- common::Model <Time> *_child;
- };
- template<class Time,
- class GraphManager,
- class Parameters = common::NoParameters,
- class GraphParameters = common::NoParameters>
- class Coordinator
- : public pdevs::Coordinator<Time, GraphManager, Parameters, GraphParameters>
- {
- typedef pdevs::Coordinator<Time, GraphManager,
- Parameters, GraphParameters> parent_type;
- typedef Coordinator<Time, GraphManager,
- Parameters, GraphParameters> type;
- typedef done_start_message<Time> done_start_message_type;
- typedef start_message<Time> start_message_type;
- typedef done_transition_message<Time> done_transition_message_type;
- typedef transition_message<Time> transition_message_type;
- public:
- Coordinator(const std::string &name, const Parameters ¶meters,
- const GraphParameters &graph_parameters)
- :
- common::Model<Time>(name),
- pdevs::Coordinator<Time, GraphManager, Parameters, GraphParameters>(
- name, parameters, graph_parameters)
- {
- type::_graph_manager.init();
- _thread = std::make_shared<std::thread>([&] { loop(); });
- }
- virtual ~Coordinator()
- {
- done();
- _thread->join();
- }
- void done()
- { get_sender().send(artis::common::Close()); }
- artis::common::Sender get_sender()
- { return _incoming.get_sender(); }
- void set_sender(common::Sender sender)
- { _sender = sender; }
- void loop()
- {
- try {
- while (true) {
- _incoming.wait()
- .template handle<start_message_type>(
- [this](start_message_type const &msg) {
- typename Time::type tn = start(msg._t);
- _sender.send(done_start_message_type(tn, this));
- })
- .
- template handle<done_start_message_type>(
- [this](done_start_message_type const &msg) {
- type::_event_table.init(msg._tn, msg._child);
- --_received;
- if (_received == 0) {
- std::unique_lock <std::mutex> lock(_received_mutex);
- _condition.notify_one();
- }
- })
- .
- template handle<transition_message_type>(
- [this](transition_message_type const &msg) {
- typename Time::type tn = transition(msg._t);
- _sender.send(done_transition_message_type(tn, this));
- })
- .
- template handle<done_transition_message_type>(
- [this](done_transition_message_type const &msg) {
- type::_event_table.put(msg._tn, msg._child);
- --_received;
- if (_received == 0) {
- std::unique_lock <std::mutex> lock(_received_mutex);
- _condition.notify_one();
- }
- });
- }
- }
- catch (artis::common::Close const &) {
- }
- }
- typename Time::type start(const typename Time::type &t)
- {
- _received = 0;
- for (auto &child : parent_type::_graph_manager.children()) {
- if (child->is_atomic()) {
- type::_event_table.init(child->start(type::_tn), child);
- } else {
- ++_received;
- }
- }
- if (_received > 0) {
- std::unique_lock <std::mutex> lock(_received_mutex);
- type::_graph_manager.start(t);
- _condition.wait(lock);
- }
- type::_tl = t;
- type::_tn = type::_event_table.get_current_time();
- return type::_tn;
- }
- // TODO: to remove
- virtual int get_receiver_number(typename Time::type t)
- {
- return type::_event_table.get_current_models(t).size();
- }
- typename Time::type transition(const typename Time::type &t)
- {
- assert(t >= type::_tl and t <= type::_tn);
- common::Models <Time> receivers = type::get_receivers();
- // common::Models<Time> IMM = type::_event_table.get_current_models(t);
- common::Models <Time> IMM =
- type::_event_table.get_current_models(t, type::_graph_manager.lookahead(t));
- _received = 0;
- for (auto &model : receivers) {
- if (model->is_atomic()) {
- type::_event_table.put(model->transition(t), model);
- } else {
- ++_received;
- }
- }
- for (auto &model : IMM) {
- if (std::find(receivers.begin(), receivers.end(), model) == receivers.end()) {
- if (model->is_atomic()) {
- type::_event_table.put(model->transition(t), model);
- } else {
- ++_received;
- }
- }
- }
- if (_received > 0) {
- std::unique_lock <std::mutex> lock(_received_mutex);
- type::_graph_manager.transition(receivers, t);
- type::_graph_manager.transition(IMM, t);
- _condition.wait(lock);
- }
- parent_type::update_event_table(t);
- type::_tl = t;
- type::_tn = type::_event_table.get_current_time();
- type::clear_bag();
- return type::_tn;
- }
- private:
- std::shared_ptr <std::thread> _thread;
- artis::common::Receiver _incoming;
- artis::common::Sender _sender;
- unsigned int _received;
- std::mutex _received_mutex;
- std::condition_variable _condition;
- };
- }
- }
- } // namespace artis pdevs multithreading
- #endif
|