# Parallelization does not use all cores fully

On a new laptop with a 6-core i7-8850H in Mma11.3, I'd like to use its full capabilities. Unicore code

prm = Prime /@ Range[1,1000];
f[i_] := Length[(First/@FactorInteger[i]) \[Intersection] prm];
Table[f[5^100+i],{i,0,10}] //AbsoluteTiming


returns {43.9328,{1,4,3,4,3,8,0,2,1,1,2}}. Task manager measures: The multicore code

prm = Prime /@ Range[1, 1000];
f[i_] := Length[(First/@FactorInteger[i]) \[Intersection] prm];
DistributeDefinitions[prm, f];
ParallelCombine[Table[f[5^100+i],{i,#}]&, Range[0,10], Join] //AbsoluteTiming


returns {26.1113,{1,4,3,4,3,8,0,2,1,1,2}}. Task manager measures: In Evaluation -> Parallel Kernel Configuration, I have:

Why is the speed-up less than 2x? Why does the multicore code only give 33% use of my CPU and unicore 16%? How can I use all 6 cores fully (or at least 5 cores to the max)?

• In my experience, Mathematica uses only one thread per core. How does the CPU utilization look (average utilization and time history) with six kernels launched, and with one kernel launched (i.e., not parallel)? Commented Oct 20, 2018 at 12:25
• @bbgodfrey I thought the number of Mma kernels should equal the number of CPU threads. Should it equal the number of CPU cores? Anyway, I tried the same two computations with 6 kernels, and the results are very similar (45sec 15%, 26sec 30%).
– Leo
Commented Oct 20, 2018 at 12:33
• try adding Method-> "CoarsestGrained" at the end of ParallelCombine. you can also use Labeled to show which $KernelId was used to obtain the results Commented Oct 20, 2018 at 12:34 • CoarsestGrained and FinestGrained both require 26sec. – Leo Commented Oct 20, 2018 at 12:41 • @Leon Please see my answer for responses to your comments. Basically, your problem is suited only for two parallel kernels, and the rest have almost nothing to do. By the way, what is the brand and model of your PC? It appears to be very powerful. Commented Oct 20, 2018 at 19:48 ## 1 Answer The following was perfomed on a 4-processor, 8-thread PC running $Version
(* "11.3.0 for Microsoft Windows (64-bit) (March 7, 2018)" *)


To begin,

ParallelCombine[Table[f[5^100 + i], {i, #}] &, Range[0, 10], Join] // AbsoluteTiming
(* {37.2452, {1, 4, 3, 4, 3, 8, 0, 2, 1, 1, 2}} *)


is equivalent to

ParallelTable[f[5^100 + i], {i, 0, 10}] // AbsoluteTiming
(* {34.8027, {1, 4, 3, 4, 3, 8, 0, 2, 1, 1, 2}} *)


which is a bit easier to understand and manipulate, in my view. I then tried both Methods and also tried different orders of i in ParallelTable Nothing mattered. In all cases, Task Manager showed that all four kernels on my computer each ran at about 18% of total CPU capacity. However, two finished almost immediately, while the other two continued for some time, after which another finished and the last continued a bit longer. The reason for this behavior, it turns out, is easily determined.

Table[f[5^100 + i] // AbsoluteTiming, {i, 0, 10}]
(* {{0.0000608395, 1}, {0.0108113, 4}, {0.0120675, 3}, {0.255642, 4},
{2.88463, 3}, {0.00794627, 8}, {0.0868866, 0}, {31.8475, 2},
{3.3773, 1}, {24.3363, 1}, {0.0163816, 2}} *)


i = 7 and i = 9 take most of the time, with two kernels computing the others quickly and then finishing. Given that i = 7 takes almost 32 seconds by itself, it no longer is surprising that ParallelTable takes 35 seconds to handle all i.

• Thank you for your answer! It didn't cross my mind that different i require very different amounts of time. Just one last thing, regarding your addendum, how come 70% is the max possible usage?!? Is there no way around this (in cases where f has the same time-requirements on all i)?
• @Leon I just tried ParallelTable[f[5^100 + i], {i, {7, 7, 7, 7}}], which runs the slowest i on all four kernels. Mathematica CPU usage was only about 65%. A nagging question for me, though, is "65% of what?", because I do not know precisely what is being measured. I should mention that, during one run, some system process kicked in and drove the CPU usage almost to 100% for a few seconds. So, I guess that 100% usage is possible, but apparently not by Mathematica. Commented Oct 21, 2018 at 14:20