Using reduce_sum with common terms between some of the elements

I have a toxicology model with 6 patients. For each patient I have data from 2 experiments (for a total of 12 experiments), and in the model we solve 1 ODE per experiment.

I’d like to use reduce_sum, which has the following signature:

real reduce_sum   (F f, T[] x, int grainsize, T1 s1, T2 s2, ...);

Ideally I’d try to solve all 12 ODEs in parallel. I have an array theta that stores the parameters for each patients, plus some other similar variables. The way I see it I have a few options:

  • I use x to index which patient we’re solving for and I pass to partial_sum all the variables (i.e. theta stays fixed accross calls to partial_sum)
  • I pass patient specific parameters through x and solve all 6 pairs of ODEs in parallel.
  • Somehow, I pass the patient specific parameters through x and the index indicating which experiment corresponds to each patient, and solve all 12 ODEs in parallel.

The first approach is the most straightforward and a direct translation of the for loop I currently have. But I’m concerned about passing all the parameters to each partial_sum call and wonder if this impacts autodiff performance.

Just go the simplest route here…it’s an ode so these extra memory overheads should not matter at all.