# Within-chain parallelization in Stan

**URL:** <https://discourse.mc-stan.org/t/within-chain-parallelization-in-stan/22890>\
**Category:** Developers\
**Tags:** rstan, fitting-issues\
**Created:** [June 7, 2021, 2:17pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-in-stan/22890 "2021-06-07T14:17:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isa\_Nazar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/isa_nazar/32/9376_2.png) [@Isa\_Nazar](https://discourse.mc-stan.org/u/Isa_Nazar)\
**Post date:** [June 7, 2021, 2:17pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-in-stan/22890/1 "2021-06-07T14:17:12Z")

</div>

Hi everyone, I am a Stan newbie.  
I need to fit a hierarchical survival model using RSTAN for large data. But, I found sampling is very time-consuming (takes ~ 7 days even for 2000 iteration/500 warmup).

To improve sampling speed, I am going to use more than one CPU per chain and have within-chain parallelization in Stan. Could someone help me to do this within-chain parallelization, please?

My stan code is as below (N=35000, n\_grid=350, province=31, number=17,num=12).  
In my codes, x[j]'s (j=1,2, …, n\_grid) are white noise and It is used only in the calculation of a and there is no need to estimate it.

Thank you in advance!

```
fit < - stan(“final model.stan”,data = da,iter=iter, warmup=warmup, cores =4, thin = 1, init = “random”)

```

```
### Stan codes

data {
  int <lower=0> province;
  int <lower=0> n_grid;
  int <lower=0> N;
  int <lower=0> num;
  int <lower=0> number;
  vector <lower=0> [province] p;
  row_vector[n_grid] kernel[province];
  matrix [N,number] respon;
}
parameters{
  vector [num] beta;
  real <lower=0> sigma;
  vector [n_grid] x;
}
transformed parameters{
  real alpha = 1 / sigma;
  vector [num] time_ratio;
  vector [N] lambdaa;
  vector [n_grid] exp_x;
  vector[province] a; // `a` saved to output
  time_ratio = exp (beta);
  exp_x = exp(x);
     lambdaa = exp ((-1 * respon[,6:17] * beta) / sigma);
           { // `z` is not part of output
    vector[province] z;
      vector[province] landa;
for (k in 1 : province) {
     landa[k] = kernel[k] * exp_x * p[k];
           }
     a = landa / sum(landa); // assign `a`
           }
           }
model{
  real log_alpha;
  log_alpha = log (alpha);
     target += normal_lpdf(x|0,2);
    target += normal_lpdf(beta | 0, 1000);
    target += normal_lpdf(log_alpha | 0, 5);
for (i in 1:N) {
   vector [province] f_i;
  vector [province] s_i;
for (k in 1:province){
    f_i[k]= a[k]*((lambdaa [i]*alpha*respon[i,4]^(alpha-1))/((1+(lambdaa [i]*respon[i,4]^alpha))^2));
    s_i[k]= a[k]*(1- ((lambdaa [i]*respon[i,4]^alpha)/ (1+(lambdaa [i]*respon[i,4]^alpha))));
    }
    target += respon[i,5] * (log (f_i)-log(s_i)) + log(s_i)- log_alpha;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Isa\_Nazar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/isa_nazar/32/9376_2.png) [@Isa\_Nazar](https://discourse.mc-stan.org/u/Isa_Nazar)\
**Post date:** [June 10, 2021, 3:09pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-in-stan/22890/2 "2021-06-10T15:09:53Z")

</div>

In this model (survival model with mentioned RSTAN codes), I want to plot the predicted survival curve and then add the KM curve. Could anyone help me, please?

Thank you!

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [June 19, 2021, 12:32pm UTC](https://discourse.mc-stan.org/t/within-chain-parallelization-in-stan/22890/3 "2021-06-19T12:32:00Z")

</div>

Hi,  
it looks like your model could be amenable to the `reduce_sum` approach. Try following the instructions at [Reduce Sum: A Minimal Example](https://mc-stan.org/users/documentation/case-studies/reduce_sum_tutorial.html) and if you encoutner problems, feel free to ask here.

> [@Isa\_Nazar](#):
>
> In this model (survival model with mentioned RSTAN codes), I want to plot the predicted survival curve and then add the KM curve. Could anyone help me, please?

I unfortunately don’t understand the model well enough to help directly. What exactly is the model you are trying to implement mathematically?
