# Evaluating values from a indexed ParametricNDSolve solution in parallel

I have a ParametricNDsolve solution for a differential equation for a huge system given by s1. I have no problem in evaluating the value using the solution. It's, in fact, speeding up the process using Parallel cores.

I have represented the variables by a, b, c and d, some of which are indexed by j, and the parameters are g, n1and n2. My attempt is to create a table of all the required variables and replace s1 to them as in Tab1and finally evaluate t shown by Tab2

Tab1 = Table[ Table[Table[{a[j, t][g, n1, n2], b[j, t][g, n1, n2],
c[g, n1, n2][t], d[g, n1, n2][t]}, {j, 1, 2}], {g, 1, 3, 1}], {n1, 1, 4}, {n2, 1, 4}] /. s1; (*first step*)

Tab2=Tab1/.t->12 (say)


So,Tab1 is only a replacement of the solution s1 to the given variables a, b, c and d, which takes me more time to evaluate than Tab2. Is there any way I can parallel map or parallelEvaluate Tab1 using all cores available such that s1 is mapped to the table of variables simultaneously in parallel

• Your nested tables are equivalent to a single table: Tab1 = Table[{a[j, t][g, n1, n2], b[j, t][g, n1, n2], c[g, n1, n2][t], d[g, n1, n2][t]}, {n1, 1, 4}, {n2, 1, 4}, {g, 1, 3, 1}, {j, 1, 2}]; Jun 25, 2020 at 20:48

Since your Table is 5 levels deep to an element we can do this:

Tab0 = Table[{a[j, t][g, n1, n2], b[j, t][g, n1, n2], c[g, n1, n2][t], d[g, n1, n2][t]},
{n1, 1, 4}, {n2, 1, 4}, {g, 1, 3, 1}, {j, 1, 2}];

Tab1 = ParallelMap[# /. s1 &, Tab0, {5}];


It might also work at 4 levels deep too - you'd want to experiment to choose the right level that offers the best performance.

I am assuming s1 is a proper rule in a list of the form {LHS -> RHS}. Of course there is no guarantee this will be faster.

We could also do the replacement inside the Table during construction without needing the Map at all, and this would save memory:

Tab1 = ParallelTable[{
a[j, t][g, n1, n2],
b[j, t][g, n1, n2],
c[g, n1, n2][t],
d[g, n1, n2][t]} /. s1,
{n1, 1, 4}, {n2, 1, 4}, {g, 1, 3, 1}, {j, 1, 2}];

• @finity Thank you for your time. I did think one of this idea before. So, when I perform the calculation for my Tab1 code of the question, it gives the value in 88 secs. Now, for both these suggestions you mentioned below the process keeps running and doesn't evaluate. (ParallelTable works for other random problems) Jun 25, 2020 at 22:42
• Without knowing more about what s1 and the functions a,b,c,d really are, I cannot know why it's taking so long. Jun 25, 2020 at 22:49
• Yes I can understand. I will try to create a sample problem to see if it works. If same issue persists I will update my question. Thank you Jun 25, 2020 at 23:32
• @Rupesh, did this solve your problem? Did you get better performance? Jul 8, 2020 at 12:44
• I did a separate solutions s1 for a and b and s2 for c and d. Also, for s1 I had to do soln[g1_, n1_, n2_][t_] := Through[s1[g1, n1, n2]@t]. For s2 it wasn't required. It works this way. Jul 8, 2020 at 14:01