LogicalProcessor.hpp 3.5 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-2018 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 { namespace pdevs { namespace mpi {
  30. template < class Time >
  31. class LogicalProcessor
  32. {
  33. typedef LogicalProcessor < Time > type;
  34. public:
  35. LogicalProcessor(common::Model < Time >* model, int rank, int parent) :
  36. _rank(rank),
  37. _parent(parent),
  38. _model(model)
  39. { }
  40. virtual ~LogicalProcessor()
  41. { }
  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. {
  60. _communicator.recv(_parent, output_send_tag, t);
  61. _model->output(t);
  62. _communicator.send(_parent, output_receive_tag,
  63. _output_bag);
  64. _output_bag.clear();
  65. break;
  66. }
  67. case post_event_send_tag:
  68. {
  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:
  76. _communicator.recv(_parent, start_send_tag, t);
  77. _communicator.send(_parent, tn_receive_tag,
  78. _model->start(t));
  79. break;
  80. case transition_send_tag:
  81. _communicator.recv(_parent, transition_send_tag, t);
  82. _communicator.send(_parent, tn_receive_tag,
  83. _model->transition(t));
  84. break;
  85. default:
  86. throw std::runtime_error("Invalid tag");
  87. }
  88. }
  89. };
  90. private:
  91. int _rank;
  92. int _parent;
  93. boost::mpi::communicator _communicator;
  94. common::Model < Time >* _model;
  95. common::Bag < Time > _output_bag;
  96. };
  97. } } } // namespace artis pdevs mpi
  98. #endif