Using openmp with odeint and adaptative step sizes

414 views Asked by At

I am trying to use openmp to parallelize my code. Everything works just fine when I use constant step sizes, however when I run the same code using an adaptative stepper I get errors that I don't understand.

Here are the essential parts of the code :

using namespace std;
using namespace boost::numeric::odeint;
const int jmax = 10;
typedef  double value_type;
typedef boost::array<value_type ,2*(jmax+1) > state_type;


//The step function
void rhs( const state_type A , state_type &dAdt , const  value_type t )

{

 value_type RHStemp0;
 value_type RHStemp1;
//We will write the RHS of the equations a  big sum
#pragma omp parallel for schedule(runtime)
for(int j = 0; j < jmax+1 ; j++ ) //Real part
{
    RHStemp0 = value_type(0.0);
    RHStemp1 = value_type(0.0);
    for (int k = 0; k< jmax+1 ;k++)
    {
        for (int l = max(0,j+k-jmax); l < 1 + min(jmax,j+k);l++)
        {
            RHStemp0 = RHStemp0 + S[j*SIZE_S*SIZE_S + k*SIZE_S  + l]*(-A[k+jmax+1]*A[l]*A[j+k-l] + A[k]*A[l+jmax+1]*A[j+k-l]
                                                  + A[k]*A[l]*A[j+k-l+jmax+1] +A[k+jmax+1]*A[l+jmax+1]*A[j+k-l+jmax+1]);
            RHStemp1 = RHStemp1 + S[j*SIZE_S*SIZE_S + k*SIZE_S  + l]*(A[k]*A[l]*A[j+k-l] - A[k]*A[l+jmax+1]*A[j+k-l+jmax+1]
                                                  + A[k+jmax+1]*A[l]*A[j+k-l+jmax+1] +A[k+jmax+1]*A[l+jmax+1]*A[j+k-l]);
        }
    }
    dAdt[j] = (-1/(value_type((2*(2*j+3)))))*RHStemp0;
    dAdt[j+jmax+1] = (1/(value_type((2*(2*j+3)))))*RHStemp1;
}

}

int main()
{
const state_type initial = loadInitialData();    //Initial condition
omp_set_num_threads(jmax+1);
int chunk_size = jmax/omp_get_max_threads();
omp_set_schedule( omp_sched_dynamic, chunk_size );

//I define my controlled error steppers
typedef runge_kutta_fehlberg78< state_type , value_type ,
                  state_type , value_type,openmp_range_algebra> error_stepper_type;

typedef controlled_runge_kutta< error_stepper_type > controlled_stepper_type;
controlled_stepper_type controlled_stepper;

int steps =  integrate_adaptive( controlled_stepper  ,rhs ,
                   initial, TINITIAL  , TFINAL,INITIAL_STEP ,  push_back_state_and_time( A_vec , times ) );

}

I do not show the definition of all variables but I doubt that they are the problem, since this works fine if I just remove the openmp_range_algebra option from the definition of error_stepper_type. This works also fine if I use openmp_range_algebra with a constant stepper size, like the Runge Kutta of order 4.

However, with this code I get the following errors :

invalid conversion from 'boost::range_iterator<const boost::array<double, 22ull>, void>::type {aka const double*}' to 'boost::range_iterator<boost::array<double, 22ull>, void>::type {aka double*}' [-fpermissive]|

So it seems that I try to allocate sonmething that is constant. This errors appears in the file openmp_range_algebra.hpp, in the following code :

template< class S >
static typename norm_result_type< S >::type norm_inf( const S &s )
{
    using std::max;
    using std::abs;
    typedef typename norm_result_type< S >::type result_type;
    result_type init = static_cast< result_type >( 0 );
    const size_t len = boost::size(s);
    typename boost::range_iterator<S>::type beg = boost::begin(s);
    #pragma omp parallel for reduction(max: init) schedule(dynamic)
    for( size_t i = 0 ; i < len ; ++i )
        init = max( init , abs( beg[i] ) );
    return init;
}

I hope I have been clear enough, I just would like to be able to use adaptative stepper with my parallelized code.

Thank you very much for your help.

1

There are 1 answers

2
mariomulansky On

This is a bug in odeint, I've filed it on github: https://github.com/headmyshoulder/odeint-v2/issues/166 and I will try to fix it asap. Thanks for posting.

edit: fixed, your program should now compile. Also, I've changed an example to use adaptive stepper: https://github.com/headmyshoulder/odeint-v2/blob/master/examples/openmp/lorenz_ensemble_simple.cpp