# 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
a
a
a
b
b
b
b


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
a
a
a
b
b
b
b


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:= LaunchKernels

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

In:= SetSharedFunction[a, b]

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

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

Out= {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