For convenience I use an indexed variables syntax to hold results of long computations for different integer parameters:
zeros[30] = longComputation[30]
zeros[40] = longComputation[40]
(* etc *)
I need to execute these computations for several values of the parameter. Since computations are completely independent, I thought that I could speed it up somewhat by evaluating these computations in parallel:
SetSharedVariable[zeros]
ParallelDo[
zeros[n] = longComputation[n],
{n, 30, 70, 10}
]
This does not work, however. I'm getting error messages like this one from each kernel:
Set::write: Tag Null in Null[30] is Protected.
and zeros[n]
have weird values assigned to them:
In[171]:= zeros[30]
Out[171]= Null[30]
What's going on here and how to do what I want?
longComputation(n)
is not correct Mathematica syntax for a function call. In this case you would needSetSharedFunction
as you are dealing withDownValues
. However, the communication overhead you introduce with this is likely going to negate any benefits or parallelization. Try to write parallel code that never requires write access to the same variable from different threads. Reformulate your problm in terms of ParallelMap or ParallelTable. $\endgroup$longComputation[n]
:( $\endgroup$