How can I make definitions inside ParallelTable to use them outside?
For example, if I execute:
ParallelTable[a[i] = i, {i, 1, 3}]
and after that, I try to access the value of a[1], I don't get 1 as I'd expect. Is there a way to make definitions inside ParallelTable so that I can access to them outside?
Note that what I want to do is more sophisticated than the trivial example above. Though there would probably be a way to do it without making definitions inside ParallelTable, it would really make my life easier if I could do it.
Answer
In this case (and for any Parallel Evaluation), you have to use SetSharedFunction before use ParallelTable.
SetSharedFunction[a];
ParallelTable[a[i] = i, {i, 1, 10}];
a /@ Range[10]
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
I use a lot the SetSharedVariable too, to count operations dynamically. Here is one example:
SetSharedFunction[a];
SetSharedVariable[n];
n = 0; Dynamic@n
ParallelTable[(n++; Pause[0.5]; a[i] = i), {i, 1, 10}]
SetSharedFunction works for distributed DownValues, and SetSharedVariables to distributed OwnValues. Another common mistake is to load some Package, and try to execute one of it functions in parallel without distribute it before. SetSharedFunction solves this too.
Comments
Post a Comment