LogicalProcessor.hpp 4.1 KB

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