# Appending elements to a list in parallel table

Is there any way to store values in a list that is being computed in parallel? For example, if ParallelTable is computing some {x,y} values, how would I store those computed values to a list? The order does not matter. I can always use Sort later.

For example, the following calculation can be done using Table:

list = {};
Table[Block[{x = x2, y},
y = 2 x^2;
AppendTo[list, {x, y}];
];,
{x2, 1, 2}]


This simply returns {{1,2},{2,8}}. But if I want to do this computation in parallel the calculation can't be stored in a list. This is obviously a simplified version of my algorithm which is very lengthy and I would like it to store in parallel.

Take same exmaple:

list = {};
ParallelTable[Block[{x = x2, y},
y = 2 x^2;
AppendTo[list, {x, y}];
];,
{x2, 1, 2}]


This returns an empty list. It makes sense that appending to a list is not a operation that can be done it parallel since the kernal needs to keep track of the order in the list, therefore multiple kernals can't manipulate it simultaneously. But I feel like there is some way around that by avoiding the ordered property of the list.

• Do you realize that list = ParallelTable[{x,2 x^2},{x,1,2}] would do what you ask? Do you have any particular reason for using Table the way you do? – C. E. Feb 12 '15 at 7:46
• Yes. I need Block or Module to be nested inside Table in order for the full computation to take place. – Mike Feb 12 '15 at 22:10
• @Mike Having to use Block or Module still doesn't explain why you are using AppendTo. – Szabolcs Feb 12 '15 at 22:51
• What @Szabolcs said. AppendTo should be a last resort. If the list becomes long it will probably cost you more than you gain from doing it in parallel. Try to post a minimal but sufficiently complicated version of what you actually need to calculate, so we can help you with the actual problem. – Marius Ladegård Meyer Jun 29 '15 at 22:01

## 1 Answer

If you want to use AppendTo, you could just make list a shared variable and change your ParallelTable to a ParallelDo:

list = {};
SetSharedVariable[list];
ParallelDo[Block[{x = x2, y}, y = 2 x^2;
AppendTo[list, {x, y}];];, {x2, 1, 4}]

• Or replace SetSharedVariable[list]; with ParallelEvaluate[list = {}]; and then evaluate ParallelEvaluate[list] to get all values. – Karsten 7. Oct 28 '15 at 0:04