Hello, I am trying to speed up my code with ParallelDo, but I ended up with a super slow case. Here is an artificial example of the case:
inits = RandomReal[{0, 5}, 10000];
sols = Table[
NDSolve[{f'[t] == Sin[t], f[0] == inits[[j]]}, f[t], {t, 0, 10}],
{j, 1, Length[inits]}];
Now, the parallel part
ParallelDo[
qsol = sols[[1]];
f[t] /. qsol /. t -> 4
, {i, 1, 50}
] // AbsoluteTiming
this took {15.5051, Null}
qsol = sols[[1]];
ParallelDo[
f[t] /. qsol /. t -> 4
, {i, 1, 50}
] // AbsoluteTiming
this took {0.01021, Null}
The situation gets worse as the number of sols increases. What is the problem here?
I am guessing that inside the parallel block, sols is copied to each kernel and this copying slows down as the number of elements increases in sols. So, I tried
SetSharedVariable[sols[[1]]];
ParallelDo[
qsol = sols[[1]];
f[t] /. qsol /. t -> 4
, {i, 1, 50}
] // AbsoluteTiming
This took {2.07197, Null} which is an improvement.
But when I tried
SetSharedVariable[sols];
ParallelDo[
qsol = sols[[1]];
f[t] /. qsol /. t -> 4
, {i, 1, 50}
] // AbsoluteTiming
This took {125.364, Null}. This is ridiculusly long.