3
(* toy example *)
myfun[x_, y_: 0] := Block[
    {ans, myplot, summary},
    Print["The first value is ", x];
    Print["The second value is ", y];
    Print["Their sum is ", x + y];
    summary = {#1, "=", #2} & @@@ 
    Transpose[{{"x", "y"}, Round[{x, y}, 0.001]}];
    Print[Grid[summary, Alignment -> {Left, Right, Center}]];
    myplot = Plot[Sin[z], {z, -10, x}];
    Print[myplot];
];

myfun[1, 2]

Runs fine.

But I want to do Parallel computing, like this

Parallelize[
    {myfun[1, 2],
    myfun[1, 4],
    myfun[2, 5]
    }

]

Then the output becomes this:

enter image description here

Which does not make any sense.

How would I change the code, so that the output keeps in the same order (for each call of myfun), but executes each of the function calls still run in Parallel?

Update:

So if I want to avoid using Print, I tried a bit like this:

myfun[x_, y_: 0] := Block[{ans, myplot, summary, res},
Reap[

Sow[Print["The first value is ", x], res],
Sow[Print["The second value is ", y], res],
Sow[Print["Their sum is ", x + y], res]


];

summary = {#1, "=", #2} & @@@ 
Transpose[{{"x", "y"}, Round[{x, y}, 0.001]}];


Reap[
Sow[Grid[summary, Alignment -> {Left, Right, Center}], res]
];

myplot = Plot[Sin[z], {z, -10, x}, ImageSize -> Medium];

Reap[Sow[myplot, res]][[2]]
];

So Reap collects all the res term? I dont think I fully understand how this works in a bit piece of code.

Thanks!

Chen Stats Yu
  • 4,986
  • 2
  • 24
  • 50

1 Answers1

5

As Yves said replace Print with List output. Here is an example using Sow and Reap along with Block and Mathematica 10 notation for Composition. (It would be better to avoid Print from the beginning but I am trying to make this an easy substitution for you.)

myfun[x_, y_: 0] :=
  Block[{ans, myplot, summary, Print = Sow@*Row@*List},
    Print["The first value is ", x];
    Print["The second value is ", y];
    Print["Their sum is ", x + y];
    summary = {#1, "=", #2} & @@@ Transpose[{{"x", "y"}, Round[{x, y}, 0.001]}];
    Print[Grid[summary, Alignment -> {Left, Right, Center}]];
    myplot = Plot[Sin[z], {z, -10, x}];
    Print[myplot];] // Reap // Last

Now using TableForm:

Parallelize[{myfun[1, 2], myfun[1, 4], myfun[2, 5]}] // TableForm

enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Thanks! That's indeed the easiest way for me to apply to my current code as I have lots of Print code among the calculations. I will try to figure out how to use Sow and Reap to replace the Print. I dont really see the relevance of Composition? – Chen Stats Yu Dec 03 '14 at 17:21
  • @Chen I'm glad this helps. Sow@*Row@*List is another way to write Composition[Sow, Row, List] added in version 10. Let me know if you have any more questions. – Mr.Wizard Dec 03 '14 at 17:51
  • I changed a bit of the code, adopting to your solutions, it's works fine. However, just two tiny problems. In my actual code, I also Exports the myplot to a PDF file. In the parallel version, it only saves the first 12 PDF, among the 23 myfun calls. The final output in the notebook was correct. It's only the saved PDFs, there were 11 missing. I am running 8 cores. I can't seem to find out why. The last thing is, the TableForm makes the graph really small. Can I change something in myplot to make it bigger (as the normal size)? – Chen Stats Yu Dec 03 '14 at 18:12
  • @Chen I can't diagnose the Export problem without seeing some code. Are you giving every file a unique name? How are the names created, and which ones are missing? For the plot size you can either wrap it in Pane, i.e. Pane @ Plot[. . .], or specify an explicit ImageSize option. – Mr.Wizard Dec 03 '14 at 18:20
  • 1
    @Chen I used the Pane method in my answer to (11961) which incidentally you might find of interest. – Mr.Wizard Dec 03 '14 at 18:22
  • (Cant seem to @ you) Here is the bits of the code, Export[ToString[HoldForm[data]]<>" -- "<>model<>".pdf",plotPI]; , where data and model are input as strings. The data are entered in random order as names of variables. The data from the letter "L" are all missing. Seems strange. I will try to fiddle around. If no success, I will start a new post. Thanks again! – Chen Stats Yu Dec 03 '14 at 18:26
  • So I added a bit updated code, see OP. I think I understand how it works in a small example, where Reap collects the things in Sow with a tag . I dont think I fully understand how Reap and Sow works in a large piece of code, to replace Print. – Chen Stats Yu Dec 03 '14 at 21:57