This is a follow-up question to my previously posted question on how to get Mathematica to use all available cores to do a parallel calculation. I resolved this by rewriting the code in terms of a ParallelMap command, and in doing so Mathematica started to use all available cores, 16 physical cores in total. On a single core using Map the calculation takes ~6.5 minutes, and on 16 cores using ParallelMap it takes ~43 seconds. Why is it not calculating faster than this? I would've expected the calculation to take ~390/16 = 24 seconds.
Here is a simplified version of my code:
LaunchKernels[16];
dk = 1; λ = 1; g = 1; m = 1; f = 7;
lattsize = 10; t = 1; dt = 2*10^-1;
ϕ = 1;
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];
minA1 = {};
maxA1 = {};
variablesTable = Flatten[Table[{P, i, α, β, a}, {P, 0, 1, dk}, {i, 0, 1, dk}, {α, 0, 1, dk}, {β, 0, 1, dk}, {a, 0, 1, dk}], 4];
func := (solA1 = NSolve[A1[p[#[[1]], #[[3]], #[[4]]], q[Q, #[[5]]], k[X[#[[2]]]], k[X[#[[2]]]] - p[#[[1]], #[[3]], #[[4]]] - q[Q, #[[5]]]] == f, Q, Method -> {Automatic, "SymbolicProcessing" -> 0}]) &;
ParallelMap[
( func[#];
If[solA1 != {},
solA1 = Select[Q /. solA1, Positive];
AppendTo[minA1, {#, Min[solA1] /. Infinity -> Null}];
AppendTo[maxA1, {#, Max[solA1] /. -Infinity -> Null}];,
AppendTo[minA1, {#, Null}];
AppendTo[maxA1, {#, Null}];
]
) &,
variablesTable,
Method -> "CoarsestGrained"
];
minA1Master = Join@@ParallelEvaluate[minA1]
maxA1Master = Join@@ParallelEvaluate[maxA1]
Any help would be much appreciated.
Tableinside theParallelMapcommand? – user35305 Feb 28 '17 at 13:06ReapandSowto create a table of valuesminA1with the conditions imposed as per my original code? – user35305 Feb 28 '17 at 13:18NSolvedoes not have a"SymbolicProcessing"suboption (NDSolvedoes). Anyway, maybe this is somethingFindRootwould do faster? – Daniel Lichtblau Feb 28 '17 at 15:14#[[1]], #[[2]]. Define a separate function with readable argument names. – Szabolcs Feb 28 '17 at 15:44Xis an interpolating function that does effectively nothing. Avoid it if you can. There are known problems with interpolating functions and parallelizationNSolveis really meant for "nice" (mostly polynomial) symbolic functions, not numerical black boxes. I'm not sure if using it with an interpolating function is a good idea (though it does seem to work). As Daniel said, try FindRoot. Hope this helps. – Szabolcs Feb 28 '17 at 15:48NSolveisn't complaining about me using"SymbolicProcessing"). I'll try FindRoot and see if this improves things. – user35305 Feb 28 '17 at 15:51minA1 = {}; maxA1 = {}; ParallelEvaluate[minA1 = {}; maxA1 = {};];at the start. I benchmarked it withMapandParallelMap, when mapped toTake[variablesTable, 500]. I got 5.8 vs 4.4 seconds. That's a small speedup, but still a speedup. I use M11.0 with 4 kernels (not 16). Do you observe the slowdown only when mapping over a longer list? Do you observe it only when using 16 kernels instead of fewer? – Szabolcs Feb 28 '17 at 15:54fun[x_] := (a=x^2;), then runfun[2], then read the result by checking global variablea. This is generally bad, and with parallelization I would expect it to break things.fun[x_] := x^2returns the result directly and doesn't manipulate global variables. This is good. – Szabolcs Feb 28 '17 at 15:58funcisfunc[{a1_, a2_, a3_, a4_, a5_, a6_}] := NSolve[ A1[p[a1, a3, a4], q[Q, a5], k[X[a2]], k[X[a2]] - p[a1, a3, a4] - q[Q, a5], a6] == f, Q](named arguments for readability, no messing with global variables, and directly mappable over a list of parameter lists) – Szabolcs Feb 28 '17 at 16:01minA1 = {}; maxA1 = {}on the master kernel as well as on the sub-kernels? To be fair I'm not sure, I'll try a smaller list now (and fewer kernels). So instead offunc := (solA1 = ...)&would it be better to writesolA1 := (...)&? – user35305 Feb 28 '17 at 16:02