4
$\begingroup$

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: enter image description here 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: enter image description here In Evaluation -> Parallel Kernel Configuration, I have: enter image description here

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)?

$\endgroup$
  • 1
    $\begingroup$ 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)? $\endgroup$ – bbgodfrey Oct 20 '18 at 12:25
  • $\begingroup$ @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%). $\endgroup$ – Leon Oct 20 '18 at 12:33
  • 1
    $\begingroup$ try adding Method-> "CoarsestGrained" at the end of ParallelCombine. you can also use Labeled to show which $KernelId was used to obtain the results $\endgroup$ – Alucard Oct 20 '18 at 12:34
  • $\begingroup$ CoarsestGrained and FinestGrained both require 26sec. $\endgroup$ – Leon Oct 20 '18 at 12:41
  • 1
    $\begingroup$ @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. $\endgroup$ – bbgodfrey Oct 20 '18 at 19:48
5
$\begingroup$

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.

Addendum

In my experience, a computation that lends itself well to parallelization will launch four kernels on my PC, each using 16% - 19% of total CPU capacity as measured by Task Manager. Thus, the best I have seen is total CPU utilization of about 70%. Note that "the number of kernels available for parallel computation typically corresponds to the number of CPU cores", according to https://reference.wolfram.com/language/workflow/RunAComputationInParallel.html. Using more typically just adds overhead.

$\endgroup$
  • $\begingroup$ 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)? $\endgroup$ – Leon Oct 21 '18 at 11:20
  • $\begingroup$ @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. $\endgroup$ – bbgodfrey Oct 21 '18 at 14:20
  • $\begingroup$ When I did that, my CPU ran at 81%, the slowest core needed 32sec, the fastest 28sec. Hopefully they'll fix this in future editions of Mma, so we can get 100%. $\endgroup$ – Leon Oct 21 '18 at 19:59
  • $\begingroup$ @Leon That's great. Thanks for the info. $\endgroup$ – bbgodfrey Oct 21 '18 at 20:02

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.