# Why can't I parallelize this code

I have a simple routine (I thought it was simple), that I cannot parallize. It goes like this

Clear[ParallelSow, Arep, Brep, xrep];
SetSharedFunction[ParallelSow];
ParallelSow[expr_] := Sow[expr];

Data = Reap[ParallelDo[
Arep = (-1 + 2 RandomInteger[]) Exp[-14 RandomReal[] ];
Brep = (-1 + 2 RandomInteger[]) Exp[-14 RandomReal[] ];
xrep = (-1 + 2 RandomInteger[]) Exp[-14 RandomReal[] ];
cosmo = {A + B, B - A} /. {A -> Arep, B -> Brep, x -> xrep};
If[Brep^2 + 4 Arep < 0, Continue[]];
If[! (-1 <= (1/2)*(Brep + Sqrt[Brep^2 + 4*Arep]) <=
1 || -1 <= (1/2)*(Brep - Sqrt[Brep^2 + 4*Arep]) <= 1),
Continue[], , Print["Error 1"]];
If[0.943 < cosmo[[1]] < 0.981 && cosmo[[2]] < 0.15,
ParallelSow[{Arep, Brep, xrep, cosmo}]
, Null, Print["Error 2"]]
, 100000]][[2]][[1]]


And in the end I get a

ParallelDo::nopar1: <<1>> cannot be parallelized; proceeding with sequential evaluation. >>


Where <<1>> refers to the expression above.

I looked into the documentation and googled it. I have no idea what is in my code I cannot parallelize...

• Please show a complete minimal example. Look at sscce.org for guidance. – Szabolcs Oct 13 '16 at 11:36
• Done, sorry about that. – romanovzky Oct 13 '16 at 11:42

## 1 Answer

The syntax for ParallelDo is incorrect for your version of Mathematica. There should be an error message that makes this clear:

Do::itform: Argument 100000 at position 2 does not have the correct form for an iterator. >>

Use ParallelDo[..., {n}] instead of ParallelDo[..., n]

In v10.3 your syntax works. I'm not sure in which exact version this syntax was introduced, but I know that in 10.0 it doesn't work. I don't have v10.2.

• I don't like this new syntax that omits the {...}, it causes ambiguities which lead to misunderstandings. If I had reliable WiFi, I'd link to the question, but right now my network barely works. – Szabolcs Oct 13 '16 at 11:48
• The error message was not informative enough. Thank you a lot! I should upgrade the Mathematica version, I have a university licence and can upgrade it at any time... I lost the entire morning going through many different ways of writing the same code! – romanovzky Oct 13 '16 at 11:49
• @romanovzky No need to upgrade just for this, just put in those braces. It's just a new syntax but it doesn't bring new functionality. Of course if you have access to v11, go ahead, it's nicer than 10.x :-) – Szabolcs Oct 13 '16 at 11:50
• True that. TBH I've been a bit annoyed on how many times Wolfram changes these little things in the last versions of Mathematica, it makes working the same notebook on different computers (lapton, uni desktop, etc) very frustrating... – romanovzky Oct 13 '16 at 11:51