# “ParallelDo” does not calculate the function well

Now, I'm solving utility maximization problem.

(* parameters of objective fuction *)
μx = Exp[16.*(-0.0089)] - 1.(*(107/100)^16 -1*);
μy = Exp[16.*0.0972] - 1.(*(107/100)^16-1*);
σx = (1276./10000.)*4.;
σy = (1583./10000.)*4.;
ρ = 4666./10000.;
rf = Exp[16*0.0254] - 1(*(103/100)^16-1*);
L0 = 1.427120697866550*10.^15;
A0 = 0.512324118564831*10.^15;
n0 = 0.017535911*10.^15;
n1 = -0.070768118*10.^15;
k = 0.4243;
ω = 20.;
c = 11.378271026200800;
l = c/(k*10.^15);
constant0 = N[L0 - n1 - n0 - n1 (1 + rf) - (A0 + n0)*(1 + rf)^2];
constant1 = N[rf*(n1 - (A0 + n0)*(1 + rf))];
constant2 = N[-n1 - (A0 + n0)*(1 + rf)];
constant3 = N[-(1 + rf) (A0 + n0)];
constant4 = N[-(A0 + n0)];
impact[x_] :=
Which[x >= 0, k (1. - Exp[-l x]), True, -k (1. - Exp[l x])];
p[x_, y_] :=
PDF[BinormalDistribution[{μx, μy}, {σx, σy}, ρ], {x, y}];

(* Utility fuction *)
u[{aa_, bb_, cc_, dd_}] :=
Block[{λ0 = aa, λ1 = bb, η0 = cc, η1 = dd},

T1[x1_, x2_, y1_, y2_] := Block[{V0 = λ0 impact[λ0 n0] - (λ0 + η0) rf + ( λ0 x1 + η0 y1) ,
V1 = impact[λ1 n1] λ1 + λ1 x2 + η1 \ y2 - rf*(λ1 + η1 )},
N[Max[0, constant0 + constant2*V1 + constant3*V0 + constant4*V0*V1]]];

Block[{sum, inc}, sum = 0.; inc = 0.;
ParallelDo[
inc =
N[NIntegrate[
T1[x1, x2, y1, y2] (ω/2. *T1[x1, x2, y1, y2] + 1.) p[x1, y1] p[x2, y2],
{x1, -Infinity,Infinity}, {y1, -Infinity, Infinity}, {x2, -Infinity, Infinity}, {y2, -Infinity, Infinity},
Method -> "AdaptiveMonteCarlo"]]; sum = sum + inc, {i, 10}];

-sum/10]
];


The problem is

u[{0.1, 0.1, 0.2, 0.1}] // AbsoluteTiming


works well when "ParallelDo" is replaced by "Do".

However, when I use "ParallelDo", the time for calculation is pretty good, but the function value of u is fixed with 0.

I have no idea why ParallelDo does not calculate the function value precisely...

• inc is not a shared variable. Each subkernel has its own version and the main kernel's inc remains set to zero. Try ParallelTable and perform your sum calculation afterwards on the 10 item list. – Felix Mar 7 '17 at 5:59
• Thank you!! I tried ParallelTable and now it works well – Byung-june Kim Mar 7 '17 at 8:30