kcott has asked for the wisdom of the Perl Monks concerning the following question:
G'day All,
I haven't use MCE previously and decided to get my head around it. I believe some of my current personal projects could benefit from it; there are also some $work applications.
I ran the very first example code in "MCE::Core - SYNOPSIS" and that worked fine.
[Note: In the following, "lcores" refers to "logical CPU cores"; MCE doco uses the same abbreviation.]
I decided that it would be useful to allow users to specify what percentage of lcores were used by an application. For instance, at any given time, 50% might be allowed for one MCE process, 25% for another MCE process, leaving the remaining 25% for other general use on the computer.
My first thought was that "max_workers => 'auto'" was unsuitable for this; even with its arithmetic modifiers (see "MCE::Core - MCE->new()" and "MCE::Util - get_ncpu()" for more about that). It uses an arbitrary eight or all lcores, whichever is the smallest: not useful for determining a percentage of lcores. So, I came up with the following proof-of-concept (POC) code:
#!/usr/bin/env perl use 5.016; use warnings; use MCE; my @percentages = qw{0 20 25 33.333 40 50 60 66.667 75 80 100 200}; for my $specified_percentage (@percentages) { say "Specified percentage of logical cores to use: $specified_perc +entage%"; my $modified_percentage = $specified_percentage <= 0 ? 1 : $specified_percentage >= 100 ? 100 : $specified_percentage; say "Modified percentage of logical cores to use: $modified_percen +tage%"; say 'Total number of logical cores ', MCE::Util::get_ncpu(); my $max_workers = int(0.5 + MCE::Util::get_ncpu() * $modified_percentage / 100 +) || 1; say "Maximum workers to use: $max_workers"; my @wids; my $mce = MCE::->new( max_workers => $max_workers, gather => \@wids, user_func => sub { my ($mce) = @_; MCE->gather($mce->wid); }, ); $mce->run(); say "Worker ID(s): @{[sort { $a <=> $b } @wids]}"; }
[Note: The use 5.016 is $work related: the minimum version to use. 5.012 will cover strict and say; MCE only requires 5.008001.]
The output from this looked fine. I've put it inside the spoiler.
Specified percentage of logical cores to use: 0% Modified percentage of logical cores to use: 1% Total number of logical cores 12 Maximum workers to use: 1 Worker ID(s): 1 Specified percentage of logical cores to use: 20% Modified percentage of logical cores to use: 20% Total number of logical cores 12 Maximum workers to use: 2 Worker ID(s): 1 2 Specified percentage of logical cores to use: 25% Modified percentage of logical cores to use: 25% Total number of logical cores 12 Maximum workers to use: 3 Worker ID(s): 1 2 3 Specified percentage of logical cores to use: 33.333% Modified percentage of logical cores to use: 33.333% Total number of logical cores 12 Maximum workers to use: 4 Worker ID(s): 1 2 3 4 Specified percentage of logical cores to use: 40% Modified percentage of logical cores to use: 40% Total number of logical cores 12 Maximum workers to use: 5 Worker ID(s): 1 2 3 4 5 Specified percentage of logical cores to use: 50% Modified percentage of logical cores to use: 50% Total number of logical cores 12 Maximum workers to use: 6 Worker ID(s): 1 2 3 4 5 6 Specified percentage of logical cores to use: 60% Modified percentage of logical cores to use: 60% Total number of logical cores 12 Maximum workers to use: 7 Worker ID(s): 1 2 3 4 5 6 7 Specified percentage of logical cores to use: 66.667% Modified percentage of logical cores to use: 66.667% Total number of logical cores 12 Maximum workers to use: 8 Worker ID(s): 1 2 3 4 5 6 7 8 Specified percentage of logical cores to use: 75% Modified percentage of logical cores to use: 75% Total number of logical cores 12 Maximum workers to use: 9 Worker ID(s): 1 2 3 4 5 6 7 8 9 Specified percentage of logical cores to use: 80% Modified percentage of logical cores to use: 80% Total number of logical cores 12 Maximum workers to use: 10 Worker ID(s): 1 2 3 4 5 6 7 8 9 10 Specified percentage of logical cores to use: 100% Modified percentage of logical cores to use: 100% Total number of logical cores 12 Maximum workers to use: 12 Worker ID(s): 1 2 3 4 5 6 7 8 9 10 11 12 Specified percentage of logical cores to use: 200% Modified percentage of logical cores to use: 100% Total number of logical cores 12 Maximum workers to use: 12 Worker ID(s): 1 2 3 4 5 6 7 8 9 10 11 12
The percentage needed to be capped at 100. If I comment out ": $specified_percentage >= 100 ? 100", the last block of output, which was
Specified percentage of logical cores to use: 200% Modified percentage of logical cores to use: 100% Total number of logical cores 12 Maximum workers to use: 12 Worker ID(s): 1 2 3 4 5 6 7 8 9 10 11 12
becomes
Specified percentage of logical cores to use: 200% Modified percentage of logical cores to use: 200% Total number of logical cores 12 Maximum workers to use: 24 Worker ID(s): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 + 23 24
I could put a version of that code into a module with a function returning $max_workers.
I decided to revisit 'auto' and see if I could come up with something better. Here's that POC solution:
#!/usr/bin/env perl use 5.016; use warnings; use MCE; my $total_logical_cores = MCE::Util::get_ncpu(); my $auto_max_workers; { my $mce_temp = MCE::->new(max_workers => 'auto'); $auto_max_workers = $mce_temp->max_workers(); $mce_temp->shutdown; } my $auto_to_ncpu_mult = $total_logical_cores / $auto_max_workers; my @percentages = qw{0 20 25 33.333 40 50 60 66.667 75 80 100 200}; for my $percentage (@percentages) { say "Specified percentage of logical cores to use: $percentage%"; $percentage = 1 if $percentage <= 0; say "Modified percentage of logical cores to use: $percentage%"; say 'Total number of logical cores ', MCE::Util::get_ncpu(); my $auto_max_workers = 'auto*' . $auto_to_ncpu_mult * $percentage +/ 100; say "Auto max_workers: $auto_max_workers"; my @wids; my $mce = MCE::->new( max_workers => $auto_max_workers, gather => \@wids, user_func => sub { my ($mce) = @_; MCE->gather($mce->wid); }, ); $mce->run(); say "Worker ID(s): @{[sort { $a <=> $b } @wids]}"; }
The output looks fine; again, it's in the spoiler.
Specified percentage of logical cores to use: 0% Modified percentage of logical cores to use: 1% Total number of logical cores 12 Auto max_workers: auto*0.015 Worker ID(s): 1 Specified percentage of logical cores to use: 20% Modified percentage of logical cores to use: 20% Total number of logical cores 12 Auto max_workers: auto*0.3 Worker ID(s): 1 2 Specified percentage of logical cores to use: 25% Modified percentage of logical cores to use: 25% Total number of logical cores 12 Auto max_workers: auto*0.375 Worker ID(s): 1 2 3 Specified percentage of logical cores to use: 33.333% Modified percentage of logical cores to use: 33.333% Total number of logical cores 12 Auto max_workers: auto*0.499995 Worker ID(s): 1 2 3 4 Specified percentage of logical cores to use: 40% Modified percentage of logical cores to use: 40% Total number of logical cores 12 Auto max_workers: auto*0.6 Worker ID(s): 1 2 3 4 5 Specified percentage of logical cores to use: 50% Modified percentage of logical cores to use: 50% Total number of logical cores 12 Auto max_workers: auto*0.75 Worker ID(s): 1 2 3 4 5 6 Specified percentage of logical cores to use: 60% Modified percentage of logical cores to use: 60% Total number of logical cores 12 Auto max_workers: auto*0.9 Worker ID(s): 1 2 3 4 5 6 7 Specified percentage of logical cores to use: 66.667% Modified percentage of logical cores to use: 66.667% Total number of logical cores 12 Auto max_workers: auto*1.000005 Worker ID(s): 1 2 3 4 5 6 7 8 Specified percentage of logical cores to use: 75% Modified percentage of logical cores to use: 75% Total number of logical cores 12 Auto max_workers: auto*1.125 Worker ID(s): 1 2 3 4 5 6 7 8 9 Specified percentage of logical cores to use: 80% Modified percentage of logical cores to use: 80% Total number of logical cores 12 Auto max_workers: auto*1.2 Worker ID(s): 1 2 3 4 5 6 7 8 9 10 Specified percentage of logical cores to use: 100% Modified percentage of logical cores to use: 100% Total number of logical cores 12 Auto max_workers: auto*1.5 Worker ID(s): 1 2 3 4 5 6 7 8 9 10 11 12 Specified percentage of logical cores to use: 200% Modified percentage of logical cores to use: 200% Total number of logical cores 12 Auto max_workers: auto*3 Worker ID(s): 1 2 3 4 5 6 7 8 9 10 11 12
Because 'auto' caps the maximum number of workers at the maximum number of lcores, I didn't need to do anything special with that. Here's the last block of output for comparison with what I showed above.
Specified percentage of logical cores to use: 200% Modified percentage of logical cores to use: 200% Total number of logical cores 12 Auto max_workers: auto*3 Worker ID(s): 1 2 3 4 5 6 7 8 9 10 11 12
However, I did need to set the minimum percentage. If I comment out "$percentage = 1 if $percentage <= 0;, the first block of output, which was
Specified percentage of logical cores to use: 0% Modified percentage of logical cores to use: 1% Total number of logical cores 12 Auto max_workers: auto*0.015 Worker ID(s): 1
becomes
Specified percentage of logical cores to use: 0% Modified percentage of logical cores to use: 0% Total number of logical cores 12 Auto max_workers: auto*0 Worker ID(s): 1 2 3 4 5 6 7 8
Curious: 'auto*0' eq 'auto*1' — is that a bug?
I could also put a version of that code into a module with a function returning $auto_max_workers.
So, finally the questions :-)
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: MCE - max_workers
by marioroy (Prior) on Nov 15, 2021 at 15:29 UTC | |
by kcott (Archbishop) on Nov 16, 2021 at 04:43 UTC | |
by marioroy (Prior) on Nov 16, 2021 at 09:56 UTC | |
by kcott (Archbishop) on Nov 16, 2021 at 12:27 UTC | |
by marioroy (Prior) on Nov 17, 2021 at 01:48 UTC | |
|
Re: MCE - max_workers [Benchmark]
by kcott (Archbishop) on Jul 01, 2021 at 20:55 UTC | |
|
Re: MCE - max_workers
by karlgoethebier (Abbot) on Jul 01, 2021 at 05:48 UTC | |
by kcott (Archbishop) on Jul 01, 2021 at 06:41 UTC | |
by karlgoethebier (Abbot) on Jul 01, 2021 at 19:36 UTC |