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.
list = ParallelTable[{x,2 x^2},{x,1,2}]
would do what you ask? Do you have any particular reason for usingTable
the way you do? $\endgroup$ – C. E. Feb 12 '15 at 7:46Block
orModule
to be nested insideTable
in order for the full computation to take place. $\endgroup$ – Mike Feb 12 '15 at 22:10Block
orModule
still doesn't explain why you are usingAppendTo
. $\endgroup$ – Szabolcs Feb 12 '15 at 22:51AppendTo
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. $\endgroup$ – Marius Ladegård Meyer Jun 29 '15 at 22:01