2 * phase_chain_omp_state.cpp
4 * Example of OMP parallelization with odeint
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)
16 #include <boost/random.hpp>
17 #include <boost/timer/timer.hpp>
19 #include <boost/numeric/odeint.hpp>
20 #include <boost/numeric/odeint/external/openmp/openmp.hpp>
22 #include <boost/numeric/odeint.hpp>
25 using namespace boost::numeric::odeint
;
26 using boost::timer::cpu_timer
;
27 using boost::math::double_constants::pi
;
29 typedef openmp_state
<double> state_type
;
31 //[phase_chain_state_rhs
32 struct phase_chain_omp_state
34 phase_chain_omp_state( double gamma
= 0.5 )
35 : m_gamma( gamma
) { }
37 void operator()( const state_type
&x
, state_type
&dxdt
, double /* t */ ) const
39 const size_t N
= x
.size();
40 #pragma omp parallel for schedule(runtime)
41 for(size_t n
= 0 ; n
< N
; ++n
)
43 const size_t M
= x
[n
].size();
44 for(size_t m
= 1 ; m
< M
-1 ; ++m
)
46 dxdt
[n
][m
] = coupling_func( x
[n
][m
+1] - x
[n
][m
] ) +
47 coupling_func( x
[n
][m
-1] - x
[n
][m
] );
49 dxdt
[n
][0] = coupling_func( x
[n
][1] - x
[n
][0] );
52 dxdt
[n
][0] += coupling_func( x
[n
-1].back() - x
[n
].front() );
54 dxdt
[n
][M
-1] = coupling_func( x
[n
][M
-2] - x
[n
][M
-1] );
57 dxdt
[n
][M
-1] += coupling_func( x
[n
+1].front() - x
[n
].back() );
62 double coupling_func( double x
) const
64 return sin( x
) - m_gamma
* ( 1.0 - cos( x
) );
72 int main( int argc
, char **argv
)
74 //[phase_chain_state_init
75 const size_t N
= 131101;
76 vector
<double> x( N
);
77 boost::random::uniform_real_distribution
<double> distribution( 0.0 , 2.0*pi
);
78 boost::random::mt19937
engine( 0 );
79 generate( x
.begin() , x
.end() , boost::bind( distribution
, engine
) );
80 const size_t blocks
= omp_get_max_threads();
81 state_type
x_split( blocks
);
87 //[phase_chain_state_integrate
88 integrate_n_steps( runge_kutta4
<state_type
>() , phase_chain_omp_state( 1.2 ) ,
89 x_split
, 0.0 , 0.01 , 100 );
90 unsplit( x_split
, x
);
93 double run_time
= static_cast<double>(timer
.elapsed().wall
) * 1.0e-9;
94 std::cerr
<< run_time
<< "s" << std::endl
;
95 // copy(x.begin(), x.end(), ostream_iterator<double>(cout, "\n"));