ModelProxy.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @file kernel/pdevs/mpi/ModelProxy.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_MODEL_PROXY
  26. #define PDEVS_MPI_MODEL_PROXY 1
  27. #include <artis-star/common/Coordinator.hpp>
  28. #include <artis-star/common/Model.hpp>
  29. #include <boost/mpi/communicator.hpp>
  30. namespace artis {
  31. namespace pdevs {
  32. namespace mpi {
  33. enum Tags {
  34. finish_send_tag,
  35. output_send_tag,
  36. post_event_send_tag,
  37. start_send_tag,
  38. transition_send_tag,
  39. output_receive_tag,
  40. tn_receive_tag
  41. };
  42. template<class Time>
  43. class ModelProxy : public common::Model<Time> {
  44. typedef common::Model<Time> parent_type;
  45. typedef ModelProxy<Time> type;
  46. public:
  47. ModelProxy(const std::string& name, int rank, bool atomic)
  48. :
  49. common::Model<Time>(name), _atomic(atomic), _rank(rank) { }
  50. virtual ~ModelProxy() { _communicator.send(_rank, finish_send_tag); }
  51. virtual bool is_atomic() const { return _atomic; }
  52. virtual std::string to_string(int level) const
  53. {
  54. (void) level;
  55. return std::string();
  56. }
  57. virtual common::Value observe(const typename Time::type& t,
  58. unsigned int index) const
  59. {
  60. (void) t;
  61. (void) index;
  62. assert(false);
  63. return common::Value();
  64. }
  65. virtual void output(const typename Time::type& t)
  66. {
  67. try {
  68. typename common::Bag<Time> bag;
  69. _communicator.send(_rank, output_send_tag, t);
  70. _communicator.recv(_rank, output_receive_tag, bag);
  71. dispatch_events(bag, t);
  72. }
  73. catch (const boost::mpi::exception& e) {
  74. std::cout << e.what() << std::endl;
  75. }
  76. }
  77. virtual void post_event(const typename Time::type& t,
  78. const common::ExternalEvent<Time>& event)
  79. {
  80. try {
  81. _communicator.send(_rank, post_event_send_tag, t);
  82. _communicator.send(_rank, post_event_send_tag, event);
  83. }
  84. catch (const boost::mpi::exception& e) {
  85. std::cout << e.what() << std::endl;
  86. }
  87. }
  88. virtual typename Time::type dispatch_events(common::Bag<Time>& bag,
  89. const typename Time::type& t)
  90. {
  91. for (auto& event : bag) {
  92. event.set_model(this);
  93. }
  94. return dynamic_cast < common::Coordinator<Time>* >(
  95. parent_type::get_parent())->dispatch_events(bag, t);
  96. }
  97. virtual typename Time::type start(const typename Time::type& t)
  98. {
  99. try {
  100. _communicator.send(_rank, start_send_tag, t);
  101. typename Time::type tn;
  102. _communicator.recv(_rank, tn_receive_tag, tn);
  103. type::_tl = t;
  104. type::_tn = tn;
  105. }
  106. catch (const boost::mpi::exception& e) {
  107. std::cout << e.what() << std::endl;
  108. }
  109. return type::_tn;
  110. }
  111. virtual typename Time::type transition(const typename Time::type& t)
  112. {
  113. try {
  114. _communicator.send(_rank, transition_send_tag, t);
  115. typename Time::type tn;
  116. _communicator.recv(_rank, tn_receive_tag, tn);
  117. type::_tl = t;
  118. type::_tn = tn;
  119. }
  120. catch (const boost::mpi::exception& e) {
  121. std::cout << e.what() << std::endl;
  122. }
  123. return type::_tn;
  124. }
  125. private:
  126. bool _atomic;
  127. boost::mpi::communicator _communicator;
  128. int _rank;
  129. };
  130. }
  131. }
  132. } // namespace artis pdevs mpi
  133. #endif