# Mathematica not using all cores available

I am trying to run a ParallelDo loop in Mathematica, that uses NSolve to solve an equation for a number of different input variables. I am running Mathematica on a computer with 16 physical cores (32 threads) and have instructed Mathematica to open up 16 sub-kernels. The problem is that, although it opens up 16 sub-kernels, it only ever uses 6 of them to do the calculation. Is there any way to force Mathematica to use all 16 kernels?

Edit: Here is an example of the code

dk = 1;

ParallelEvaluate[
λ = 2*10^-1;
g = 2*10^-1;
ϕ = 1;
m = 1;
f = 7;
lattsize = 50;
p[P_, α_, β_] := {P*Sin[α]*Cos[β], P*Sin[α]*Sin[β], P*Cos[α]};
q[Q_, a_] := {Q*Sin[a], 0, Q*Cos[a]};
k[X_] := {0, 0, X};
X = Interpolation[Table[{i, i}, {i, 0, lattsize, 10^-3}]];
ω[x_] := Sqrt[x.x + m^2];
(*x:=p, y:=q, z:=k, s:=k+(-)p+(-)q*)
A1[x_, y_, z_, s_] := (1 + (g*ϕ^2)/(8*ω[x]^2))*ω[x] + (1 + (g*ϕ^2)/(8*ω[y]^2))*ω[y] + (1 + (g*ϕ^2)/(8*ω[z]^2))*ω[z] + (1 + (g*ϕ^2)/(8*ω[s]^2))*ω[s];
]

ParallelDo[
solA1 = NSolve[A1[p[P, α, β], q[Q, a], k[X[i]], k[X[i]] - p[P, α, β] - q[Q, a]] == f, Q, Method -> {Automatic, "SymbolicProcessing" -> 0}];
If[solA1 != {},
solA1 = Select[Q /. solA1, Positive];
AppendTo[minA1, {{P, i, α, β, a}, Min[solA1] /.Infinity -> Null}];
AppendTo[maxA1, {{P, i, α, β, a}, Max[solA1] /.-Infinity -> Null}];,
AppendTo[minA1, {{P, i, α, β, a}, Null}];
AppendTo[maxA1, {{P, i, α, β, a}, Null}];
],
{P, 0, 5, dk}, {i, 0, 10, dk}, {α, 0, 3, dk}, {β, 0, 6, dk}, {a, 0, 3, dk},
Method -> "CoarsestGrained"
]

minA1Master = Join @@ ParallelEvaluate[minA1];
maxA1Master = Join @@ ParallelEvaluate[maxA1];

• Check SystemInformation["Kernel", "MaxLicenseSubprocesses"] and SystemInformation["Kernel", "MaxLicenseProcesses"] and see if you are running more than this number at the same time on the same computer. Check how many kernels are actually alive and working: 16 or less? If everything checks out, the behaviour you see is not usual. Post a reproducible example (sscce.org), along with your Mma version and description of the evidence that only 6 kernels are being used. – Szabolcs Feb 27 '17 at 15:20
• @Szabolcs I checked SystemInformation["Kernel", "MaxLicenseSubprocesses"] and there are 320, for SystemInformation["Kernel", "MaxLicenseProcesses"] there are 40, so I'm definitely not running more that this number. I also checked "Parallel Kernel Status" and it shows that 16 kernels are running idle (when I run the code 6 of them take the load). – user35305 Feb 27 '17 at 15:28
• Then the answer lies within the specific you are are running. We need to see that---preferably a significantly simplified version---before the question can be answered. – Szabolcs Feb 27 '17 at 15:33
• @Szabolcs I have edited my OP to include a (somewhat) simplified version of the code. Hopefully this will help (apologies, I don't know how to type the Greek characters on here). – user35305 Feb 27 '17 at 15:44

I think the code is not complete, but I think I can see the problem.

ParallelTable and ParallelDo only parallelize on the first iterator. Your first iterator is {P, 0, 5, dk}. It iterates over only 6 values, thus only 6 evaluations will be submitted. The other iterators (i, α, β, ...) are all grouped within the same evaluation.

One way to work around this is to use ParallelMap, and map a function over sets of parameters. A non-parallel illustration is:

Table[f[{i, j}], {i, 1, 5}, {j, 10, 15}]


use

Map[f, Tuples[{Range[1, 5], Range[10, 15]}]]


If the nesting structure of the result is important, use

Map[f, Table[{i, j}, {i, 1, 5}, {j, 10, 15}], {2}]

• Thanks for the answer. I'm not too familiar with ParallelMap, but I'll look into it. What is incomplete about the code? – user35305 Feb 27 '17 at 16:57
• @user35305 It doesn't run in a fresh kernel. One problem is that minA1 isn't initialized. But that doesn't matter, the example can be made much smaller. ParallelDo[Pause, {6}, {10}] is sufficient. You'll get 6 busy kernels and the rest will stay free. – Szabolcs Feb 27 '17 at 18:11
• Ah yes, whoops I forgot to add that. In my actual code minA1 is initialised such that minA1={}. I have since re-written the code to use ParallelMap and although all 16 kernels are now being used Mathematica still takes ~50 seconds to evaluate the problem. Without using ParallelMap i.e. just using Map, the problem takes ~6.5 minutes. I thought it would be quicker than it is with 16 kernels, more like ~25 seconds?! – user35305 Feb 27 '17 at 18:23
• @user35305 There are just too many things that can cause that. You could ask a new question about this problem, and show the code ... hopefully it is not too complicated. – Szabolcs Feb 27 '17 at 18:26
• Ok, fair enough. Maybe I'll do that then. – user35305 Feb 27 '17 at 18:33