3
$\begingroup$

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.

$\endgroup$
  • 4
    $\begingroup$ 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? $\endgroup$ – C. E. Feb 12 '15 at 7:46
  • $\begingroup$ Yes. I need Block or Module to be nested inside Table in order for the full computation to take place. $\endgroup$ – Mike Feb 12 '15 at 22:10
  • 2
    $\begingroup$ @Mike Having to use Block or Module still doesn't explain why you are using AppendTo. $\endgroup$ – Szabolcs Feb 12 '15 at 22:51
  • 2
    $\begingroup$ 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. $\endgroup$ – Marius Ladegård Meyer Jun 29 '15 at 22:01
1
$\begingroup$

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}]
$\endgroup$
  • 1
    $\begingroup$ Or replace SetSharedVariable[list]; with ParallelEvaluate[list = {}]; and then evaluate ParallelEvaluate[list] to get all values. $\endgroup$ – Karsten 7. Oct 28 '15 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.