Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Where does the 6th process come from?

by marioroy (Prior)
on Apr 22, 2017 at 16:38 UTC ( [id://1188627]=note: print w/replies, xml ) Need Help??


in reply to Where does the 6th process come from? [SOLVED]

For shared objects, the OO interface has the least overhead. MCE::Shared provides on-demand dereferencing for array and hash-like behavior. In that case, a mutex is needed whenever dereferencing involves a fetch and store (equals 2 IPC trips to the shared-manager). Regarding the OO interface, there is a single entry point to the shared-manager process. This is why a mutex isn't needed when accessing shared objects via the OO interface, unless wanting a mutex around updating two or more shared objects.

sub task { our $mutex; $mutex->lock; # not necessary when accessing shared-objects via th +e OO interface $result->push(shift); $mutex->unlock; $mutex->lock; # necessary if dereferencing involves fetch and stor +e behind TIE push @{ $result }, shift; $mutex->unlock; }

Replies are listed 'Best First'.
Re^2: Where does the 6th process come from?
by marioroy (Prior) on Apr 22, 2017 at 17:32 UTC

    Please find below various demonstrations. Increase the loop iteration from 1e5 to 1e6 and watch top or task manager.

    #!/usr/bin/env perl use strict; use warnings; use MCE::Hobo; use MCE::Shared; use Time::HiRes qw(time); use feature qw(say); my $number = MCE::Shared->scalar(0); my $start = time; MCE::Hobo->create('task', $_) for 1 .. 4; MCE::Hobo->waitall; printf "duration: %0.3f seconds\n", time - $start; say "number: ", $number->get(); sub task { my ($id) = @_; say "Hobo $id started"; $number->incr() for 1 .. 100000; say "Hobo $id ended"; } __END__ Hobo 1 started Hobo 2 started Hobo 3 started Hobo 4 started Hobo 4 ended Hobo 2 ended Hobo 1 ended Hobo 3 ended duration: 0.872 seconds number: 400000

    Fetching data involves extra latency due to workers waiting for a response from the shared-manager process. No worries, it runs reasonably well for being a plain-Perl module.

    sub task { my ($id, $val) = @_; say "Hobo $id started"; $val = $number->incr() for 1 .. 100000; say "Hobo $id ended"; } __END__ Hobo 1 started Hobo 2 started Hobo 3 started Hobo 4 started Hobo 4 ended Hobo 3 ended Hobo 1 ended Hobo 2 ended duration: 2.183 seconds number: 400000

    Dereferencing involving a fetch and store requires a mutex. Overhead goes up the roof. There's now a mutex lock, TIE STORE + FETCH (2 IPCs for that), and finally a mutex unlock.

    #!/usr/bin/env perl use strict; use warnings; use MCE::Hobo; use MCE::Mutex; use MCE::Shared; use Time::HiRes qw(time); use feature qw(say); my $mutex = MCE::Mutex->new( impl => 'Channel' ); my $number = MCE::Shared->scalar(0); my $start = time; MCE::Hobo->create('task', $_) for 1 .. 4; MCE::Hobo->waitall; printf "duration: %0.3f seconds\n", time - $start; say "number: ", $number->get(); sub task { my ($id, $val) = @_; say "Hobo $id started"; for ( 1 .. 100000 ) { $mutex->lock; $val = ++${ $number }; $mutex->unlock; } say "Hobo $id ended"; } __END__ Hobo 1 started Hobo 2 started Hobo 3 started Hobo 4 started Hobo 2 ended Hobo 1 ended Hobo 4 ended Hobo 3 ended duration: 20.343 seconds number: 400000

    Let's remove dereferencing. Thus, mutex and OO only.

    sub task { my ($id, $val) = @_; say "Hobo $id started"; for ( 1 .. 100000 ) { $mutex->lock; $val = $number->incr(); $mutex->unlock; } say "Hobo $id ended"; } __END__ Hobo 1 started Hobo 2 started Hobo 3 started Hobo 4 started Hobo 3 ended Hobo 4 ended Hobo 1 ended Hobo 2 ended duration: 9.702 seconds number: 400000

    Summary

    Performance is possible via the OO interface. Dereferencing is nice if you need it. Mutex is possible as well for synchronization. Any fetch from the shared-manager takes extra time. MCE::Shared is fully wantarray aware. Meaning, the MCE::Shared engine will only do extra stuff whenever needed, not more.

    Regards, Mario.

Re^2: Where does the 6th process come from?
by karlgoethebier (Abbot) on Apr 22, 2017 at 17:06 UTC

    Thank you marioroy!

    «The Crux of the Biscuit is the Apostrophe»

    Furthermore I consider that Donald Trump must be impeached as soon as possible

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1188627]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2024-04-25 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found