LogicalProcessor.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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-2022 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<typename 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:
  55. return;
  56. case output_send_tag: {
  57. _communicator.recv(_parent, output_send_tag, t);
  58. _model->output(t);
  59. _communicator.send(_parent, output_receive_tag,
  60. _output_bag);
  61. _output_bag.clear();
  62. break;
  63. }
  64. case post_event_send_tag: {
  65. common::ExternalEvent <Time> event;
  66. _communicator.recv(_parent, post_event_send_tag, t);
  67. _communicator.recv(_parent, post_event_send_tag, event);
  68. _model->post_event(t, event);
  69. break;
  70. }
  71. case start_send_tag:
  72. _communicator.recv(_parent, start_send_tag, t);
  73. _communicator.send(_parent, tn_receive_tag,
  74. _model->start(t));
  75. break;
  76. case transition_send_tag:
  77. _communicator.recv(_parent, transition_send_tag, t);
  78. _communicator.send(_parent, tn_receive_tag,
  79. _model->transition(t));
  80. break;
  81. default:
  82. throw std::runtime_error("Invalid tag");
  83. }
  84. }
  85. };
  86. private:
  87. int _rank;
  88. int _parent;
  89. boost::mpi::communicator _communicator;
  90. common::Model <Time> *_model;
  91. common::Bag <Time> _output_bag;
  92. };
  93. }
  94. }
  95. } // namespace artis pdevs mpi
  96. #endif