ModelProxy.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @file kernel/pdevs/mpi/ModelProxy.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2015 ULCO http://www.univ-litoral.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 <paradevs/common/Model.hpp>
  28. #include <boost/mpi/communicator.hpp>
  29. namespace paradevs { namespace pdevs { namespace mpi {
  30. enum Tags
  31. {
  32. finish_send_tag,
  33. output_send_tag,
  34. post_event_send_tag,
  35. start_send_tag,
  36. transition_send_tag,
  37. output_receive_tag,
  38. tn_receive_tag
  39. };
  40. template < class Time >
  41. class ModelProxy : public common::Model < Time >
  42. {
  43. typedef common::Model < Time > parent_type;
  44. typedef ModelProxy < Time > type;
  45. public:
  46. ModelProxy(const std::string& name, int rank, bool atomic) :
  47. common::Model < Time >(name), _atomic(atomic), _rank(rank)
  48. { }
  49. virtual ~ModelProxy()
  50. { _communicator.send(_rank, finish_send_tag); }
  51. virtual bool is_atomic() const
  52. { return _atomic; }
  53. virtual std::string to_string(int level) const
  54. {
  55. (void) level;
  56. return std::string();
  57. }
  58. virtual void observation(std::ostream& file) const
  59. {
  60. (void) file;
  61. }
  62. virtual void output(typename Time::type t)
  63. {
  64. try {
  65. typename common::Bag < Time > bag;
  66. _communicator.send(_rank, output_send_tag, t);
  67. _communicator.recv(_rank, output_receive_tag, bag);
  68. dispatch_events(bag, t);
  69. } catch (const boost::mpi::exception& e) {
  70. std::cout << e.what() << std::endl;
  71. }
  72. }
  73. virtual void post_event(typename Time::type t,
  74. const common::ExternalEvent < Time >& event)
  75. {
  76. try {
  77. _communicator.send(_rank, post_event_send_tag, t);
  78. _communicator.send(_rank, post_event_send_tag, event);
  79. } catch (const boost::mpi::exception& e) {
  80. std::cout << e.what() << std::endl;
  81. }
  82. }
  83. virtual typename Time::type dispatch_events(common::Bag < Time > bag,
  84. typename Time::type t)
  85. {
  86. for (auto & event : bag) {
  87. event.set_model(this);
  88. }
  89. return dynamic_cast < common::Coordinator < Time >* >(
  90. parent_type::get_parent())->dispatch_events(bag, t);
  91. }
  92. virtual typename Time::type start(typename Time::type t)
  93. {
  94. try {
  95. _communicator.send(_rank, start_send_tag, t);
  96. typename Time::type tn;
  97. _communicator.recv(_rank, tn_receive_tag, tn);
  98. type::_tl = t;
  99. type::_tn = tn;
  100. } catch (const boost::mpi::exception& e) {
  101. std::cout << e.what() << std::endl;
  102. }
  103. return type::_tn;
  104. }
  105. virtual typename Time::type transition(typename Time::type t)
  106. {
  107. try {
  108. _communicator.send(_rank, transition_send_tag, t);
  109. typename Time::type tn;
  110. _communicator.recv(_rank, tn_receive_tag, tn);
  111. type::_tl = t;
  112. type::_tn = tn;
  113. } catch (const boost::mpi::exception& e) {
  114. std::cout << e.what() << std::endl;
  115. }
  116. return type::_tn;
  117. }
  118. private:
  119. bool _atomic;
  120. boost::mpi::communicator _communicator;
  121. int _rank;
  122. };
  123. } } } // namespace paradevs pdevs mpi
  124. #endif