LogicalProcessor.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @file kernel/pdevs/mpi/LogicalProcessor.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef PDEVS_MPI_LOGICAL_PROCESSOR
  26. #define PDEVS_MPI_LOGICAL_PROCESSOR
  27. #include <artis-star/kernel/pdevs/mpi/ModelProxy.hpp>
  28. #include <boost/mpi/communicator.hpp>
  29. namespace artis {
  30. namespace pdevs {
  31. namespace mpi {
  32. template<class Time>
  33. class LogicalProcessor {
  34. typedef LogicalProcessor<Time> type;
  35. public:
  36. LogicalProcessor(common::Model<Time> *model, int rank, int parent)
  37. :
  38. _rank(rank),
  39. _parent(parent),
  40. _model(model) {}
  41. virtual ~LogicalProcessor() {}
  42. void dispatch_events_to_parent(common::Node<Time> node,
  43. const common::Value &content,
  44. typename Time::type t) {
  45. (void) t;
  46. _output_bag.push_back(
  47. common::ExternalEvent<Time>(node, content));
  48. }
  49. void loop() {
  50. typename Time::type t;
  51. for (;;) {
  52. boost::mpi::status msg = _communicator.probe();
  53. switch (msg.tag()) {
  54. case finish_send_tag:return;
  55. case output_send_tag: {
  56. _communicator.recv(_parent, output_send_tag, t);
  57. _model->output(t);
  58. _communicator.send(_parent, output_receive_tag,
  59. _output_bag);
  60. _output_bag.clear();
  61. break;
  62. }
  63. case post_event_send_tag: {
  64. common::ExternalEvent<Time> event;
  65. _communicator.recv(_parent, post_event_send_tag, t);
  66. _communicator.recv(_parent, post_event_send_tag, event);
  67. _model->post_event(t, event);
  68. break;
  69. }
  70. case start_send_tag:_communicator.recv(_parent, start_send_tag, t);
  71. _communicator.send(_parent, tn_receive_tag,
  72. _model->start(t));
  73. break;
  74. case transition_send_tag:_communicator.recv(_parent, transition_send_tag, t);
  75. _communicator.send(_parent, tn_receive_tag,
  76. _model->transition(t));
  77. break;
  78. default:throw std::runtime_error("Invalid tag");
  79. }
  80. }
  81. };
  82. private:
  83. int _rank;
  84. int _parent;
  85. boost::mpi::communicator _communicator;
  86. common::Model<Time> *_model;
  87. common::Bag<Time> _output_bag;
  88. };
  89. }
  90. }
  91. } // namespace artis pdevs mpi
  92. #endif