# Why is the execution time getting smaller and smaller every time?

Why is the execution time to compute the value of sum getting smaller and smaller every time in the following code? And how to get consistency? Thank you very much for your help.

In[18]:= Quit[]

In[1]:= atime = AbsoluteTiming;

In[2]:= m = 1350; k = 10^4;

In[3]:= LaunchKernels[] // atime

Out[3]= {3.11965, {"KernelObject"[1, "local"],
"KernelObject"[2, "local"], "KernelObject"[3, "local"],
"KernelObject"[4, "local"], "KernelObject"[5, "local"],
"KernelObject"[6, "local"]}}

In[4]:= ClearSystemCache[]
sum = ParallelSum[
1/be Log[10^8 - (be - 1)^2/4] // N[#, k] &, {be, 2, m}]; // atime

Out[5]= {0.905762, Null}

In[6]:= ClearSystemCache[]
sum = ParallelSum[
1/be Log[10^8 - (be - 1)^2/4] // N[#, k] &, {be, 2, m}]; // atime

Out[7]= {0.359625, Null}

In[8]:= ClearSystemCache[]
sum = ParallelSum[
1/be Log[10^8 - (be - 1)^2/4] // N[#, k] &, {be, 2, m}]; // atime

Out[9]= {0.250268, Null}

In[10]:= ClearSystemCache[]
sum = ParallelSum[
1/be Log[10^8 - (be - 1)^2/4] // N[#, k] &, {be, 2, m}]; // atime

Out[11]= {0.167734, Null}

In[12]:= ClearSystemCache[]
sum = ParallelSum[
1/be Log[10^8 - (be - 1)^2/4] // N[#, k] &, {be, 2, m}]; // atime

Out[13]= {0.0718937, Null}

In[14]:= ClearSystemCache[]
sum = ParallelSum[
1/be Log[10^8 - (be - 1)^2/4] // N[#, k] &, {be, 2, m}]; // atime

Out[15]= {0.0620984, Null}

• have you tried ParallelEvaluate[ClearSystemCache[]] between your tries? – Albert Retey Oct 14 '16 at 17:13
• Thank you very much Albert for the quite helpful hint. It looks like ClearSystemCache[] clears the cache only for one of the kernels -- somehow it had never occurred to me to consider that possibility. – Iosif Pinelis Oct 14 '16 at 18:58
• It looks like it will clear the cache on the "master" kernel only, but that is the one which might not even have anything to clear as the actual computations are done in the parallel kernels. I think that this might well not be documented explicitly. It just seems self-evident when one sees your results and knows that the parallel kernels are just weekly interacting independent instances. Of course WRI could have implemented ClearSystemCache to handle the parallel kernels as well, but obviously they decided not to, maybe for good reasons... – Albert Retey Oct 17 '16 at 7:14

It looks like ClearSystemCache[] will only clear the cache of the master kernel, but not that of any of the parallel kernels. This could have been implemented differently but maybe there are good reasons to not automatically do this. The behavior is not too surprising if you bring to mind that the parallel kernels are more or less independent instances of the kernel which only do interact weakly with the provided Parallel* functions. Using:

ParallelEvaluate[ClearSystemCache[]]


will clear the caches of the parallel kernels and show the desired result (repeatable "slow" evaluation) for your example. For some cases, it might be necessary to clear the cache on both the parallel kernels and the master kernel to achieve repeatable timings.

I am posting the response that I have just received from Wolfram Technical Support, which appears to confirm Albert's suggestion:

Hello Prof. Pinelis,

There are two issues:

1) the subkernels also cache results 2) ParallelSum distributes the tasks across the subkernels in a "random" manner

The combination of these two issues is that the absolute timing appears to decrease each time you evaluate the expression, down to some lower limit. This lower limit is achieved when all subkernels have cached all intermediary results. They won't cache all intermediary results until ParallelSum has been evaluated enough times, such that the "random" distribution of tasks has covered the entire set of subkernels, i.e. each subkernel has seen each possible iterator and cached the results.

To get around the caching in the subkernels, you can evaluate the command ParallelEvaluate[ClearSystemCache[]]. This clears the cache in the subkernels memory.

Sincerely,

Kevin Daily

Wolfram Technical Support Wolfram Research, Inc. http://support.wolfram.com/