# Why ParallelTable doesn't work in this simple code?

The code

Table[{a[i] = i^2, b[i] = a[i] + 100}, {i, 1, 4}]
a[1]
a[2]
a[3]
a[4]
b[1]
b[2]
b[3]
b[4]


returns (correctly) the following output:

{{1, 101}, {4, 104}, {9, 109}, {16, 116}}
1
4
9
16
101
104
109
116


But if I use ParallelTable instead, I get

{{1, 101}, {4, 104}, {9, 109}, {16, 116}}
a[1]
a[2]
a[3]
a[4]
b[1]
b[2]
b[3]
b[4]


What's hapanneing? I want to keep the definitions for a[i] and b[i], $i=1,2,3,4$, and, still, Parallelize the Table.

Ps.: actually, I want to keep the definition of b[i], $i=1,2,3,4$, only. I won't need the definitions of a[i], $i=1,2,3,4$.

Consider using SetSharedFunction:

In[1]:= LaunchKernels[4]

Out[1]= {"KernelObject"[1, "local"], "KernelObject"[2, "local"],
"KernelObject"[3, "local"], "KernelObject"[4, "local"]}

In[2]:= SetSharedFunction[a, b]

In[3]:= ParallelTable[{a[i] = i^2, b[i] = a[i] + 100}, {i, 1, 4}]
{a[1], a[2], a[3], a[4], b[1], b[2], b[3], b[4]}

Out[3]= {{1, 101}, {4, 104}, {9, 109}, {16, 116}}

Out[4]= {1, 4, 9, 16, 101, 104, 109, 116}

• Nice! I would ask if the problem could be related to DistributedContexts, but your solution works well. Nov 21 '17 at 19:14