I am experimenting with Parallelize and ParallelTable

This is the code I am using:

K[a_, b_, r_] = a*Exp[-r/b]/r;

T = D[K[10^5, 10^9, s], s]/299792^2 /. s -> 7.6

R = Range[0, 7, 0.01];
\[Theta] = Range[0, 2*Pi, 0.05];

RcG[d_, t_] = Sqrt[7.6^2 + d^2 - 2*7.6*d*Cos[t]];
f[a_?NumberQ, b_?NumberQ] := 
  1/299792^2*NIntegrate[K[10^5, 10^9, x], {x, 7.6 - a, b}];

tab = Parallelize[
      d*(K[10^5, 10^9, RcG[d, t]] - K[10^5, 10^9, 7.6] - d*T) + 
     f[d, RcG[d, t]], {d, R}, {t, \[Theta]}]];
p1 = ListPlot3D[tab, InterpolationOrder -> 4]

I want make a 3d plot using the complicated function defined in the table. When computing, Mathem. gives this error a lot of times:

NIntegrate::inumr: The integrand K[100000,1000000000,x] has evaluated to non-numerical values for all sampling points in the region with boundaries {{7.3,7.30039}}.

It doesn't happen without the Parallelize, so this means that I am using it wrongly, but I don't get where. I also used ParallelTable, with the same result.


1 Answer 1


1) K is a built-in symbol. Don't use this symbol.

2) Use DistributedContexts with ParallelTable

  tab = ParallelTable[1/299792^2*d*(K[10^5, 10^9, RcG[d, t]] - K[10^5, 10^9, 7.6] - d*T) + f[d, RcG[d, t]], {d, R}, {t, \[Theta]}, DistributedContexts -> All];
  • $\begingroup$ Thank you, it works now. Also I didn't know that K is built-in, thanks for that too. $\endgroup$
    – mattiav27
    May 22, 2014 at 13:04
  • $\begingroup$ @mattiav27 DistributeContexts is in fact not necessary here and when set to All it can potentially cause problems ... the problem was (1), not (2). $\endgroup$
    – Szabolcs
    May 22, 2014 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.