phase_chain.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * phase_chain.cpp
  3. *
  4. * Example of MPI parallelization with odeint
  5. *
  6. * Copyright 2013 Karsten Ahnert
  7. * Copyright 2013 Mario Mulansky
  8. * Copyright 2013 Pascal Germroth
  9. * Distributed under the Boost Software License, Version 1.0. (See
  10. * accompanying file LICENSE_1_0.txt or copy at
  11. * http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #include <iostream>
  14. #include <vector>
  15. #include <boost/random.hpp>
  16. #include <boost/timer/timer.hpp>
  17. //[phase_chain_mpi_header
  18. #include <boost/numeric/odeint.hpp>
  19. #include <boost/numeric/odeint/external/mpi/mpi.hpp>
  20. //]
  21. using namespace std;
  22. using namespace boost::numeric::odeint;
  23. using boost::timer::cpu_timer;
  24. using boost::math::double_constants::pi;
  25. //[phase_chain_state
  26. typedef mpi_state< vector<double> > state_type;
  27. //]
  28. //[phase_chain_mpi_rhs
  29. struct phase_chain_mpi_state
  30. {
  31. phase_chain_mpi_state( double gamma = 0.5 )
  32. : m_gamma( gamma ) { }
  33. void operator()( const state_type &x , state_type &dxdt , double /* t */ ) const
  34. {
  35. const size_t M = x().size();
  36. const bool have_left = x.world.rank() > 0,
  37. have_right = x.world.rank() < x.world.size()-1;
  38. double x_left, x_right;
  39. boost::mpi::request r_left, r_right;
  40. if( have_left )
  41. {
  42. x.world.isend( x.world.rank()-1 , 0 , x().front() ); // send to x_right
  43. r_left = x.world.irecv( x.world.rank()-1 , 0 , x_left ); // receive from x().back()
  44. }
  45. if( have_right )
  46. {
  47. x.world.isend( x.world.rank()+1 , 0 , x().back() ); // send to x_left
  48. r_right = x.world.irecv( x.world.rank()+1 , 0 , x_right ); // receive from x().front()
  49. }
  50. for(size_t m = 1 ; m < M-1 ; ++m)
  51. {
  52. dxdt()[m] = coupling_func( x()[m+1] - x()[m] ) +
  53. coupling_func( x()[m-1] - x()[m] );
  54. }
  55. dxdt()[0] = coupling_func( x()[1] - x()[0] );
  56. if( have_left )
  57. {
  58. r_left.wait();
  59. dxdt()[0] += coupling_func( x_left - x().front() );
  60. }
  61. dxdt()[M-1] = coupling_func( x()[M-2] - x()[M-1] );
  62. if( have_right )
  63. {
  64. r_right.wait();
  65. dxdt()[M-1] += coupling_func( x_right - x().back() );
  66. }
  67. }
  68. double coupling_func( double x ) const
  69. {
  70. return sin( x ) - m_gamma * ( 1.0 - cos( x ) );
  71. }
  72. double m_gamma;
  73. };
  74. //]
  75. int main( int argc , char **argv )
  76. {
  77. //[phase_chain_mpi_init
  78. boost::mpi::environment env( argc , argv );
  79. boost::mpi::communicator world;
  80. const size_t N = 131101;
  81. vector<double> x;
  82. if( world.rank() == 0 )
  83. {
  84. x.resize( N );
  85. boost::random::uniform_real_distribution<double> distribution( 0.0 , 2.0*pi );
  86. boost::random::mt19937 engine( 0 );
  87. generate( x.begin() , x.end() , boost::bind( distribution , engine ) );
  88. }
  89. state_type x_split( world );
  90. split( x , x_split );
  91. //]
  92. cpu_timer timer;
  93. //[phase_chain_mpi_integrate
  94. integrate_n_steps( runge_kutta4<state_type>() , phase_chain_mpi_state( 1.2 ) ,
  95. x_split , 0.0 , 0.01 , 100 );
  96. unsplit( x_split , x );
  97. //]
  98. if( world.rank() == 0 )
  99. {
  100. double run_time = static_cast<double>(timer.elapsed().wall) * 1.0e-9;
  101. std::cerr << run_time << "s" << std::endl;
  102. // copy(x.begin(), x.end(), ostream_iterator<double>(cout, "\n"));
  103. }
  104. return 0;
  105. }