1

This is a followup question to this one. Following the comment, I used the code in this answer in this way:

ClearAll["Global`*"]
j = Reap[Do[{Sow[{i, i}, a]}; Sow[{i, i^2}, b], {i, 100}], _, Rule];
lista1 = a /. Last@j;
lista2 = b /. Last@j;
ListPlot[{lista1, lista2}]

ParallelEvaluate[foo = {}];
ParallelEvaluate[f1 = {}];
sow[x_] := (foo = {foo, x});
sow1[x_] := (f1 = {f1, x});
ParallelDo[sow[{i, i}]; sow1[{i, i^2}], {i, 100}];
lista11 = Join @@ ParallelEvaluate[Flatten@foo];
lista22 = Join @@ ParallelEvaluate[Flatten@f1];
ListPlot[{lista11, lista22}]

The problem is that the plots I obtain in the two cases are different. What did I do wrong?

I suspect it has to do with the use of Flatten, but I could not get it to work properly.

SHuisman
  • 3,258
  • 8
  • 12
mattiav27
  • 6,677
  • 3
  • 28
  • 64

1 Answers1

2

You mixed the 'x' and 'y' values once you did the Flatten in the ParallelEvaluate:

I unraveled the linked list by using Cases to look for explicit {x,y} pairs:

ClearAll["Global`*"]
j=Reap[Do[{Sow[{i,i},a]};Sow[{i,i^2},b],{i,100}],_,Rule];
lista1=a/.Last@j;
lista2=b/.Last@j;
ListPlot[{lista1,lista2}]

ParallelEvaluate[foo={}];
ParallelEvaluate[f1={}];
sow[x_]:=(foo={foo,x});
sow1[x_]:=(f1={f1,x});
ParallelDo[sow[{i,i}];sow1[{i,i^2}],{i,100}];
lista11=Join@@ParallelEvaluate[Cases[foo,{_Integer,_Integer},\[Infinity]]];
lista22=Join@@ParallelEvaluate[Cases[f1,{_Integer,_Integer},\[Infinity]]];
ListPlot[{lista11,lista22}]

It can also be done using Flatten, and then Partition[…,2]:

ClearAll["Global`*"]
j=Reap[Do[{Sow[{i,i},a]};Sow[{i,i^2},b],{i,100}],_,Rule];
lista1=a/.Last@j;
lista2=b/.Last@j;
ListPlot[{lista1,lista2}]

ParallelEvaluate[foo={}];
ParallelEvaluate[f1={}];
sow[x_]:=(foo={foo,x});
sow1[x_]:=(f1={f1,x});
ParallelDo[sow[{i,i}];sow1[{i,i^2}],{i,100}];
lista11=Join@@ParallelEvaluate[Partition[Flatten[foo],2]];
lista22=Join@@ParallelEvaluate[Partition[Flatten[f1],2]];
ListPlot[{lista11,lista22}]
SHuisman
  • 3,258
  • 8
  • 12