LogicalProcessor.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. {
  35. typedef LogicalProcessor<Time> type;
  36. public:
  37. LogicalProcessor(common::Model<Time> *model, int rank, int parent)
  38. :
  39. _rank(rank),
  40. _parent(parent),
  41. _model(model)
  42. {}
  43. virtual ~LogicalProcessor()
  44. {}
  45. void dispatch_events_to_parent(common::Node<Time> node,
  46. const common::Value &content,
  47. typename Time::type t)
  48. {
  49. (void) t;
  50. _output_bag.push_back(
  51. common::ExternalEvent<Time>(node, content));
  52. }
  53. void loop()
  54. {
  55. typename Time::type t;
  56. for (;;) {
  57. boost::mpi::status msg = _communicator.probe();
  58. switch (msg.tag()) {
  59. case finish_send_tag:return;
  60. case output_send_tag: {
  61. _communicator.recv(_parent, output_send_tag, t);
  62. _model->output(t);
  63. _communicator.send(_parent, output_receive_tag,
  64. _output_bag);
  65. _output_bag.clear();
  66. break;
  67. }
  68. case post_event_send_tag: {
  69. common::ExternalEvent<Time> event;
  70. _communicator.recv(_parent, post_event_send_tag, t);
  71. _communicator.recv(_parent, post_event_send_tag, event);
  72. _model->post_event(t, event);
  73. break;
  74. }
  75. case start_send_tag:_communicator.recv(_parent, start_send_tag, t);
  76. _communicator.send(_parent, tn_receive_tag,
  77. _model->start(t));
  78. break;
  79. case transition_send_tag:_communicator.recv(_parent, transition_send_tag, t);
  80. _communicator.send(_parent, tn_receive_tag,
  81. _model->transition(t));
  82. break;
  83. default:throw std::runtime_error("Invalid tag");
  84. }
  85. }
  86. };
  87. private:
  88. int _rank;
  89. int _parent;
  90. boost::mpi::communicator _communicator;
  91. common::Model<Time> *_model;
  92. common::Bag<Time> _output_bag;
  93. };
  94. }
  95. }
  96. } // namespace artis pdevs mpi
  97. #endif