0
$\begingroup$

I was trying to carry out the following calculations:

j = 0; SetSharedVariable[j];
resultlist1 = ParallelTable[j++; GaussianPotential[fieldlist[[ind2]], theta1, vz, v2, matrixsize, DisorderRangeRatio -> 0], {ind2, 1, num}];

At the same time, I am monitoring the progress by executing

Dynamic[Refresh[TableForm[{"ind1=", ind1, "ind2=", ind2, "j=", j}], UpdateInterval -> 1]]

There are two strange things:

(1) The behavior of j. The second command just quickly updates j from 1 to num, and stops there. I was expecting that the second command updates j each time it carries out a new calculation. How can I correct this?

(2) ParallelTable actually takes more time than Table. In contrast, when I execute the example in the official document,

Table[PrimeQ[x], {x, 10^1000, 10^1000 + 5000}]; // AbsoluteTiming
ParallelTable[PrimeQ[x], {x, 10^1000, 10^1000 + 5000}]; // AbsoluteTiming

The parallel version does save much time (5.87s vs 3.40s). What could be the reason for ParallelTable to be slower? Does it matter that the function GaussianPotential comes from a separate package I wrote?

$\endgroup$
  • 2
    $\begingroup$ A common reason why parallel operation might be slower is the overhead of transferring data between kernels. $\endgroup$ – Sascha Jan 20 '16 at 23:17
  • $\begingroup$ @Sascha Thanks! But for the computations I did, Table takes 110 seconds, while ParallelTable takes 113 seconds, which in my regard is not a trivial calculation at each step. Is the overhead still significant in such cases? $\endgroup$ – Xiao Jan 20 '16 at 23:55
  • $\begingroup$ The first run of the parallel kernels takes a lot of time. So, AbsoluteTimingwill show you the correct results just if you have initiated the kernels preliminary or in case when you run ParallelTable at second time.. $\endgroup$ – Rom38 Jan 21 '16 at 5:16
  • $\begingroup$ Have you tried to remove the j++; and the monitoring part? SetSharedVariable will require all the kernel to synchronize on that variable. Since j is used and modified in all the kernels, this variable will dramatically slow down the parallel computation. Please see the first example in the Possible Issue of the SetSharedVariable[] in the Mathematica document. $\endgroup$ – Louis Yang Sep 6 '16 at 1:30
2
$\begingroup$

Not a complete answer, but it may help to illustrate some of this behavior. If I run the simple example,

Dynamic[Refresh[TableForm[{"ind2=", ind2, "j=", j}], UpdateInterval -> 1]]

j = 0; num = 100; SetSharedVariable[j]; 
ParallelTable[ind2^2; Pause[2]; j++, {ind2, 1, num}]

I find that Dynamic does not display ind2. It does, however, display j, which increases in steps of 4 on my four-processor machine. The AbsoluteTiming is 50.2

On the other hand, with ParallelTable replaced by Table, both variables display, increasing in steps of 1. Here, AbsoluteTiming is 200.5. Thus, ParallelTable is four times as fast here, as expected.

Evidently, the OP finds ParallelTable slow, because it is moving large amount of data, as suggested by Sascha.

The actual ParallelTable output of this example is

{0, 4, 8, 14, 18, 22, 26, 30, 34, 1, 5, 9, 12, 16, 20, 24, 28, 32, 2, 
 6, 10, 13, 17, 21, 25, 29, 33, 3, 7, 11, 15, 19, 23, 27, 31, 35, 36, 
 40, 44, 48, 52, 56, 60, 65, 37, 41, 45, 49, 53, 57, 61, 64, 38, 42, 
 46, 50, 54, 58, 62, 66, 39, 43, 47, 51, 55, 59, 63, 67, 69, 73, 77, 
 81, 85, 89, 93, 97, 68, 72, 76, 80, 84, 88, 92, 96, 70, 74, 78, 82, 
 86, 90, 94, 98, 71, 75, 79, 83, 87, 91, 95, 99}

showing that ParallelTable assigns blocks of nine calculations at a time to each of the kernels in this case. Method can be used to specify how the computation of the table is distributed, and this sometimes affects timing.

Of course,

j = 0; num = 100; SetSharedVariable[j]; 
ParallelTable[Pause[2]; j++; ind2, {ind2, 1, num}]

simply returns a list of 1 to 100, as it should.

$\endgroup$

Your Answer

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

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