1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ First of all, take a look at point 3.2: Performance tuning in Mathematica $\endgroup$ – Kuba Feb 28 '17 at 13:02
  • $\begingroup$ @Kuba Thanks for the info. The problem is, I'm fairly new to Mathematica so I'm not sure how to write this differently to achieve the same result?! Should I use Table inside the ParallelMap command? $\endgroup$ – user35305 Feb 28 '17 at 13:06
  • $\begingroup$ @Kuba How would I go about using Reap and Sow to create a table of values minA1 with the conditions imposed as per my original code? $\endgroup$ – user35305 Feb 28 '17 at 13:18
  • 2
    $\begingroup$ I'm not answering questions about parallel features, sorry :) I just wanted to link some general guidelines. $\endgroup$ – Kuba Feb 28 '17 at 13:30
  • $\begingroup$ @Kuba Ok fair enough. $\endgroup$ – user35305 Feb 28 '17 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.