0
$\begingroup$

I'm running a heavy simulation and try to use Parallel computing to run a Module case. I have been working on this parallel computation for more than three months, but still haven't come up with a solution with my code.

I most function I'm trying is Parallelize, DistributeDefinitions, I put every effort I can but still don't know how to make this works. I show a part of my code here, for instance, anyone can give me some advice? Many Thanks and happy new year.

LaunchKernels[];
sPa[s_, r_, n_Integer?Positive, tt_, nnn_, mm1_, mm2_, 
   lim_Integer?Positive] := Module[{sh = s/2, k, p0, t},

   numpt = n;
   Print[StringForm[" `1` .", numpt], DateString[]];

   nnn1 = nnn; mmm1 = mm1; mmm2 = mm2; tt1 = tt; n10 = 100; nh1 = 100;
   no1 = 100;
   nh = 0; no = 0; n8 = 0; n9 = 0; n12 = 0; n14 = 0; n16 = 0;
   npt = 1;
   rad = {};
   Do[rad0 = RandomVariate[NormalDistribution[2, 0.5]]; 
   If[rad0 >= 2 && rad0 <= 4, AppendTo[rad, rad0]]; 
   If[Length[rad] == numpt, Break[], Continue], {10 numpt}];

   p0 = {RandomReal[{-sh + r, 0 - r}], RandomReal[{-sh + r, sh - r}], 
   RandomReal[{-sh + r, sh - r}]};
   spl = {p0};

   k = 2;
   While[k <= numpt,
   Label[begin5]; 
   p0 = {RandomReal[{-sh + r, 0 - r}], RandomReal[{-sh + r, sh - r}],
   RandomReal[{-sh + r, sh - r}]};
   err1 = 0; err2 = 0;
   Do[dd1 = rad[[k]] + rad[[kk]]; dd = Max[dd1, 2.7]; 
   If[EuclideanDistance[p0, spl[[kk]]] >= dd, err1 = 0, err1 = 1]; 
   err2 += err1, {kk, 1, Length[spl]}];
   If[err2 == 0, AppendTo[spl, p0], Goto[begin5]]; 
   If[Mod[Length[spl], 100] == 
   0, {Print[StringForm[" `1` .", Length[spl]]]}]; ++k];
   Print[StringForm[" `1` Done. ", Length[spl]]];
   (**)

   hhpl = Table[{}, {tt}, {nnn}];
   hpl = Table[{}, {tt}];


   t = 1; While[t <= tt,
   Label[begin105];
   nn = 1; 
   While[nn <= nnn + 1 - t, 
   If[t > 1 && nn == 1, 
   Print[StringForm["hhpl[[t,1]] = `1` .", 
    Length[hhpl[[t - 1, nn]]]], DateString[]]];

   m = 1; Label[begin1]; 
   If[t > 1, {If[
    Length[hhpl[[t - 1, nn]]] == 
     0, {mmm1 = 0}, {mmm1 = Length[hhpl[[t - 1, nn]]]}]}]; 
   While[m <= mmm1, 
   If[t == 1, {ph = {-50, RandomReal[{-sh, sh}], 
      RandomReal[{-sh, sh}]}}, {hRandVector = 
     Normalize[RandomVariate[NormalDistribution[], 3]]; nh++; 
    If[nh > nh1, {If[
       nh > 3 nh1, {Print[StringForm["001 nh = `1` .", nh]]; 
        ph = hhpl[[t - 1, nn, m]]; 
        Goto[begin37]}, {If[nh == 2 nh1, 
         Print[StringForm["002 nh = `1` .", nh]]]; 
        ph = {hhpl[[t - 1, nn, m, 1]] + hRandVector[[1]], 
          hhpl[[t - 1, nn, m, 2]] + hRandVector[[2]], 
          hhpl[[t - 1, nn, m, 3]] + hRandVector[[3]]}}]}, {If[
       nh == nh1, Print[StringForm["003 nh = `1` .", nh]]]; 
      ph = {hhpl[[t - 1, nn, m, 1]] + Abs[hRandVector[[1]]], 
        hhpl[[t - 1, nn, m, 2]] + hRandVector[[2]], 
        hhpl[[t - 1, nn, m, 3]] + hRandVector[[3]]}}]}];

    err1 = 0; err2 = 0; k = 1; 
    While[k <= Length[spl], 
    If[EuclideanDistance[ph, spl[[k]]] >= rad[[k]] + 1, err1 = 0, 
    err1 = 1]; err2 += err1; If[err2 != 0, Goto[begin1]]; k++]; 
    Label[begin37]; AppendTo[hhpl[[t, nn]], ph]; nh = 0;
    m++];
   nn++];

   i = 1; 
   While[i <= t, iijj = t + 1 - i; k = 1; 
   While[k <= Length[hhpl[[i, iijj]]], 
   AppendTo[hpl[[t]], hhpl[[i, iijj, k]]]; k++]; i++];
   t++];

   proGraph = 
   Graphics3D[{Lighter[Green], Sphere[spl, rad], Magenta, 
    Sphere[#, 1.2]}, AxesLabel -> {"x", "y", "z"}, 
   Axes -> Automatic, 
   PlotRange -> {{-52, 2}, {-52, 52}, {-52, 52}}] & /@ hpl;
   (*-----------------------------Export Data-------------------------------
   ---------------------------------------------*)


  Export["spl.m", spl, "Package"];
  Label[begin27];
  Print[StringForm["All Done = `1` .", t - 1], DateString[]];
  ];
 sPa[s_, r_, n_Integer, tt_, nnn_, mm1_, mm2_] := 
 sPa[s, r, n, tt, nnn, mm1, mm2, Quotient[n, 1]]

 (*DistributeDefinitions[sPa,s,r, \
 tt,nnn,mm1,mm2,n,k,p0,t,sh,rad,spl,err1,err2];*)

 sPa[100, 1, 100, 50, 50, 12, 3]
 Parallelize[sPa]

There are some values in the last two lines, in my heavy code, I need to change some of them to be over 1000, but it will take more than a month, and sometimes the computer just dies because of no memory.

I really need some advice here to solve this problem. Please help me out. Thank you.

$\endgroup$
  • $\begingroup$ There are quite a few issues with your code. For example your second last line sPa[100, 1, 100, 50, 50, 12, 3] calls the function without any parallel kernels and your last line Parallelize[sPa] does nothing. I strongly suggest that you take some time to read the Parallel Computing Tools User Guide to learn how parallel computing is performed in the Wolfram Language. Also, your code is written procedural instead of functional which will make it slower; but that is another story. $\endgroup$ – Edmund Dec 28 '17 at 0:44
  • $\begingroup$ Thank you @Edmund. I've read the Parallel Computing Tools User Guide when first tried. There is not many examples showing parallel computation when Module used. Is it possible to apply parallel computation to a Module? $\endgroup$ – B.Bai Dec 29 '17 at 6:34

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.