title: OpenMP generate segfault in Rcpp code for the SEIR model
body: <p>I wrote a (probably-inefficient, but anyway..) Rcpp code using inline to simulate a stochastic <a href="http://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology#The_SEIR_model" rel="nofollow">SEIR model</a>.
  The serial version compiles and works perfectly, but since I need to simulate from it a large number of times and since it seems to me like an embarrassingly parallel problem (just need to simulate again for other parameter values and return a matrix with the results) I tried to add  <code>#pragma omp parallel for</code> and to compile with <code>-fopenmp -lgomp</code> but ... boom!
  I get a segfault even for very small examples!
  I tried to add <code>setenv("OMP_STACKSIZE","24M",1);</code> and values well over 24M but still the segfault happens.</p>
  
  <p>I'll explain briefly the code since it's a bit long (I tried to shorten it but the result change and I can't reproduce it..):
  I have two nested loops, the inner one execute the model for a given parameter set and the outer one changes the parameters.</p>
  
  <p>The only reason a race condition might happen is if the code were trying to execute set of instructions inside <em>inner</em> the loop in parallel (which cannot be done because of the model structure, on iteration <code>t</code> it depends on iteration <code>t-1</code>) and not to parallelize the <em>outer</em>, but if I'm not mistaken that is what the <code>parallel for</code> constructor does for default if put just outside the outer...</p>
  
  <p>This is basically the form of the code I'm trying to run:</p>
  
  <pre><code>mat result(n_param,T_MAX);
  #pragma omp parallel for
  for(int i=0,i&lt;n_param_set;i++){
      t=0;
      rowvec jnk(T_MAX);
  
      while(t &lt; T_MAX){
  
          ...
  
          jnk(t) = something(jnk(t-1));
  
          ...
  
          t++;
      }
  
      result.row(i)=jnk;
  }
  return wrap(result);
  </code></pre>
  
  <p>And my question is: How I tell the compiler that I just want to compute in parallel the outer loop (even distributing them statically like n_loops/n_threads for each thread) and not the inner one (which is actually non-parallelizable)?</p>
  
  <p>The real code is a bit more involved and I'll present it here for the sake of reproducibility if you're really willing, but I'm only asking about the behavior of OpenMP. Please notice that the only OpenMP instruction appears at line <code>122</code>.</p>
  
  <pre><code>library(Rcpp);library(RcppArmadillo);library(inline)
  
  misc='
  #include &lt;math.h&gt;
  #define _USE_MATH_DEFINES 
  
  #include &lt;omp.h&gt;
  
  using namespace arma;
  
  template &lt;typename T&gt; int sgn(T val) {
    return (T(0) &lt; val) - (val &lt; T(0));
  }
  
  
  uvec rmultinomial(int n,vec prob)
  {  
    int K = prob.n_elem;
    uvec rN = zeros&lt;uvec&gt;(K);
    double p_tot = sum(prob);
    double pp;
  
    for(int k = 0; k &lt; K-1; k++) {
      if(prob(k)&gt;0) {
        pp = prob[k] / p_tot;
        rN(k) = ((pp &lt; 1.) ? (rbinom(1,(double) n,  pp))(0) : n);
        n -= rN[k];
      } else
        rN[k] = 0;
      if(n &lt;= 0) /* we have all*/
        return rN;
      p_tot -= prob[k]; /* i.e. = sum(prob[(k+1):K]) */
    }
    rN[K-1] = n;
    return rN;
  }
  '
  
  model_and_summary='
  mat SEIR_sim_plus_summaries()
  {
    vec alpha;
    alpha &lt;&lt; 0.002 &lt;&lt; 0.0045;
    vec beta;
    beta &lt;&lt; 0.01 &lt;&lt; 0.01;
    vec gamma;
    gamma &lt;&lt; 1.0/14.0 &lt;&lt; 1.0/14.0;
    vec sigma;
    sigma &lt;&lt; 1.0/(3.5) &lt;&lt; 1.0/(3.5);
    vec phi;
    phi &lt;&lt; 0.8 &lt;&lt; 0.8;
    int S_0 = 800;
    int E_0 = 100;
    int I_0 = 100;
    int R_0 = 0;
    int pop = 1000;
    double tau = 0.01;
    double t_0 = 0;
    vec obs_time;
    obs_time &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3 &lt;&lt; 4 &lt;&lt; 5 &lt;&lt; 6 &lt;&lt; 7 &lt;&lt; 8 &lt;&lt; 9 &lt;&lt; 10 &lt;&lt; 11 &lt;&lt; 12 &lt;&lt; 13 &lt;&lt; 14 &lt;&lt; 15 &lt;&lt; 16 &lt;&lt; 17 &lt;&lt; 18 &lt;&lt; 19 &lt;&lt; 20 &lt;&lt; 21 &lt;&lt; 22 &lt;&lt; 23 &lt;&lt; 24;
  
    const int n_obs = obs_time.n_elem;
    const int n_part = alpha.n_elem;
    mat stat(n_part,6);
  
  //#pragma omp parallel for
    for(int k=0;k&lt;n_part;k++) {    
      ivec INC_i(n_obs);
      ivec INC_o(n_obs);
  
  // Event variables
      double alpha_t;
      int nX; //current number of people moving
      vec rates(8);
      uvec trans(4); // current transitions, e.g. from S to E,I,R,Universe
      vec r(4);  // rates e.g. from S to E, I, R, Univ.
  
  
  /*********************** Initialize **********************/
      int S_curr = S_0;
      int S_prev = S_0;
      int E_curr = E_0;
      int E_prev = E_0;
      int I_curr = I_0;
      int I_prev = I_0;
      int R_curr = R_0;
      int R_prev = R_0;
      int IncI_curr = 0;
      int IncI_prev = 0;
      int IncO_curr = 0;
      int IncO_prev = 0;
  
      double t_curr = t_0;
  
      int t_idx =0;
      while( t_idx &lt; n_obs ) {
  
  // next time preparation
        t_curr += tau;
  
        S_prev = S_curr;
        E_prev = E_curr;
        I_prev = I_curr;
        R_prev = R_curr;
        IncI_prev = IncI_curr;
        IncO_prev = IncO_curr;
  
  /*********************** description (rates) of the events **********************/
        alpha_t = alpha(k)*(1+phi(k)*sin(2*M_PI*(t_curr+0)/52)); //real contact rate, time expressed in weeks
  
        rates(0) = (alpha_t * ((double)I_curr / (double)pop ) * ((double)S_curr));   //e+1, s-1, r,i    one s get infected (goes in E, not yey infectous)
        rates(1) = (sigma(k) * E_curr);                               //e-1, i+1, r,s   one exposed become infectous (goes in I) INCIDENCE!!
        rates(2) = (gamma(k) * I_curr);                               //i-1, s,e, r+1    one i recover
        rates(3) = (beta(k) * I_curr);                                   //i-1, s, r,e        one i dies
        rates(4) = (beta(k) * R_curr);                                       //i,e, s, r-1        one r dies
        rates(5) = (beta(k) * E_curr);                                         //e-1, s, r,i      one e dies
        rates(6) = (beta(k) * S_curr);                                //s-1 e, i ,r   one s dies
        rates(7) = (beta(k) * pop);                                              //s+1    one susc is born
  
  
  
  // Let the events occour
  /*********************** S compartement **********************/
        if((rates(0)+rates(6))&gt;0){
          nX = rbinom(1,S_prev,1-exp(-(rates(0)+rates(6))*tau))(0);
  
          r(0) = rates(0)/(rates(0)+rates(6)); r(1) = 0.0; r(2) = 0; r(3) = rates(6)/(rates(0)+rates(6)); 
          trans = rmultinomial(nX, r);
  
          S_curr -= nX;
          E_curr += trans(0);
          I_curr += trans(1);
          R_curr += trans(2);
  //trans(3) contains dead individual, who disappear...we could avoid this using sequential conditional binomial
  
        }
  
  /*********************** E compartement **********************/
        if((rates(1)+rates(5))&gt;0){
  
          nX = rbinom(1,E_prev,1-exp(-(rates(1)+rates(5))*tau))(0);
  
          r(0) = 0.0; r(1) = rates(1)/(rates(1)+rates(5)); r(2) = 0.0; r(3) = rates(5)/(rates(1)+rates(5)); 
          trans = rmultinomial(nX, r);
  
          S_curr += trans(0);
          E_curr -= nX;
          I_curr += trans(1);
          R_curr += trans(2);
  
          IncI_curr += trans(1);
        }
  
  /*********************** I compartement **********************/
        if((rates(2)+rates(3))&gt;0){
          nX = rbinom(1,I_prev,1-exp(-(rates(2)+rates(3))*tau))(0);
  
          r(0) = 0.0; r(1) = 0.0; r(2) = rates(2)/(rates(2)+rates(3)); r(3) = rates(3)/(rates(2)+rates(3)); 
          trans = rmultinomial(nX, r);
  
          S_curr += trans(0);
          E_curr += trans(1);
          I_curr -= nX;
          R_curr += trans(2);
  
          IncO_curr += trans(2);
        }
  
  /*********************** R compartement **********************/
        if(rates(4)&gt;0){
          nX = rbinom(1,R_prev,1-exp(-rates(4)*tau))(0);
  
          r(0) = 0.0; r(1) = 0.0; r(2) = 0.0; r(3) = rates(4)/rates(4); 
          trans = rmultinomial(nX, r);
  
          S_curr += trans(0);
          E_curr += trans(1);
          I_curr += trans(2);
          R_curr -= nX;
        }
  
  /*********************** Universe **********************/
        S_curr += pop - (S_curr+E_curr+I_curr+R_curr); //it should be poisson, but since the pop is fixed...
  
  
  /*********************** Save &amp; Continue **********************/
  // Check if the time is interesting for us
        if(t_curr &gt; obs_time[t_idx]){
          INC_i(t_idx) = IncI_curr;
          INC_o(t_idx) = IncO_curr;
          IncI_curr = IncI_prev = 0;
          IncO_curr = IncO_prev = 0;
  
          t_idx++;
        }
  
  //else just go on...
  
      }
  
  /*********************** Finished - Starting w/ stats **********************/
  
  // INC_i is the useful variable, how can I change its reference withour copying it?
      ivec incidence = INC_i; //just so if I want to use INC_o i have to change just this...
  
  //Scan the epidemics to recover the summary stats (naively divide the data each 52 weeks)
      double n_years = ceil((double)obs_time(n_obs-1)/52.0);
      vec mu_attack(n_years);
      vec ratio_attack(n_years-1);
      vec peak(n_years);
  
      vec atk(52);
      peak(0)=0.0;
      vec tmpExplo(52); //explosiveness
      vec explo(n_years);
  
      int year=0;
      int week;
  
      for(week=0 ; week&lt;n_obs ; week++){   
  
        if(week - 52*year &gt; 51){
          mu_attack(year) = sum( atk )/(double)pop;
  
          if(year&gt;0)
            ratio_attack(year-1) = mu_attack(year)/mu_attack(year-1);
  
          for(int i=0;i&lt;52;i++){
            if(atk(i)&gt;(peak(year)/2.0)){
              tmpExplo(i) = 1.0;
            } else {
              tmpExplo(i) = 0.0;
            }
          }
          explo(year) = sum(tmpExplo);
  
          year++;
          peak(year)=0.0;
        }
  
        atk(week-52*year) = incidence(week);
        if( peak(year) &lt; incidence(week) )
          peak(year)=incidence(week);
  
      }
  
      if(week - 52*year &gt; 51){
        mu_attack(year) = sum( atk )/(double)pop;
      } else {
  
        ivec idx(52);
        for(int i=0;i&lt;52;i++)
          { idx(i) = i; } //take just the updated ones...
        vec tmp = atk.elem(find(idx&lt;(week - 52*year)));
  
        mu_attack(year) = sum( tmp )/((double)pop * (tmp.n_elem/52.0)); 
        ratio_attack(year-1) = mu_attack(year)/mu_attack(year-1);
  
        for(int i=0;i&lt;tmp.n_elem;i++){
          if(tmp(i)&gt;(peak(year)/2.0)){
            tmpExplo(i) = 1.0;
          } else {
            tmpExplo(i) = 0.0;
          }
        }
        for(int i=tmp.n_elem;i&lt;52;i++)
          tmpExplo(i) = 0.0; //to reset the others
        explo(year) = sum(tmpExplo);
  
      }
  
      double correlation2;
      double correlation4;
      vec autocorr = acf(peak);
  
  
  /***** ACF *****/
      if(n_years&lt;3){
        correlation2=0.0;
        correlation4=0.0;
      } else {
        if(n_years&lt;5){
          correlation2 = autocorr(1);
          correlation4 = 0.0;
        } else {
          correlation2 = autocorr(1);
          correlation4 = autocorr(3);
        }
      }
  
      rowvec jnk(6);
      jnk &lt;&lt; sum(mu_attack)/(year+1.0)
          &lt;&lt; (sum( log(ratio_attack)%log(ratio_attack) )/(n_years-1)) - (pow(sum( log(ratio_attack) )/(n_years-1),2))
          &lt;&lt; correlation2 &lt;&lt; correlation4 &lt;&lt; max(peak) &lt;&lt; sum(explo)/n_years;
  
      stat.row(k) = jnk;
  
    }
  
    return stat;
  }
  '
  
  
  main='
  std::cout &lt;&lt; "max_num_threads " &lt;&lt; omp_get_max_threads() &lt;&lt; std::endl;
  
  RNGScope scope;
  
  mat summaries =  SEIR_sim_plus_summaries();
  
  return wrap(summaries);
  '
  
  plug = getPlugin("RcppArmadillo")
  
  ## modify the plugin for Rcpp to support OpenMP
  plug$env$PKG_CXXFLAGS &lt;- paste('-fopenmp', plug$env$PKG_CXXFLAGS)
  plug$env$PKG_LIBS &lt;- paste('-fopenmp -lgomp', plug$env$PKG_LIBS)
  
  SEIR_sim_summary = cxxfunction(sig=signature(),main,settings=plug,inc = paste(misc,model_and_summary),verbose=TRUE)
  
  SEIR_sim_summary()
  </code></pre>
  
  <p>Thanks for the help!</p>
  
  <p>NB: before you ask, I slightly modified the Rcpp multinomial sampling function just because I liked that way more than the one using pointer...not any other particular reason! :)</p>