ModelProxy.hpp 4.2 KB

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