The following is a comparison demonstrating 2 workers incrementing a shared counter variable. The OO interface for MCE::Shared allows for zero-locking at the application level. All 3 snippets involve set and get while incrementing the variable.

1) ipc_shareable.pl

use strict; use warnings; use IPC::Shareable; my $options = { create => 1, exclusive => 0, mode => 0644, destroy => 1, }; tie my $cntr, 'IPC::Shareable', $options; my $val; $cntr = 0; if ( ! defined ( my $pid = fork() ) ) { die "Cannot fork!: $!"; } elsif ( $pid == 0 ) { # Child for ( 1 .. 10000 ) { ( tied $cntr )->shlock; $val = $cntr++; ( tied $cntr )->shunlock; } } else { # Parent for ( 1 .. 10000 ) { ( tied $cntr )->shlock; $val = $cntr++; ( tied $cntr )->shunlock; } waitpid $pid, 0; print "counter: ", $cntr, "\n"; }

2) mce_shared.pl

use strict; use warnings; use MCE::Mutex; use MCE::Shared; my $mutex = MCE::Mutex->new(); tie my $cntr, 'MCE::Shared', 0; my $val; if ( ! defined ( my $pid = fork() ) ) { die "Cannot fork!: $!"; } elsif ( $pid == 0 ) { # Child for ( 1 .. 10000 ) { $mutex->lock; $val = $cntr++; $mutex->unlock; } } else { # Parent for ( 1 .. 10000 ) { $mutex->lock; $val = $cntr++; $mutex->unlock; } waitpid( $pid, 0 ); print "counter: ", $cntr, "\n"; }

3) mce_shared_oo.pl

use strict; use warnings; use MCE::Shared; my $cntr = MCE::Shared->scalar( 0 ); my $val; if ( ! defined ( my $pid = fork() ) ) { die "Cannot fork!: $!"; } elsif ( $pid == 0 ) { # Child for ( 1 .. 10000 ) { $val = $cntr->incr; } } else { # Parent for ( 1 .. 10000 ) { $val = $cntr->incr; } waitpid( $pid, 0 ); print "counter: ", $cntr->get, "\n"; }

Results

time in seconds : FreeBSD Linux Mac OSX ipc_shareable.pl : 1.80 1.87 1.79 mce_shared.pl : 0.68 0.97 0.58 mce_shared_oo.pl : 0.38 0.73 0.41 counter: 20000

Fetches require more time on Linux than BSD-based OS in regards to sockets. This is seen not only with Perl/MCE::Shared but also with other languages. I'm not sure why.

MCE::Shared is fully wantarray-aware and performs fetches only when requested. Sadly, that is not the case via the TIE interface. Perhaps, this is an unnecessary overhead IMHO in regards to the TIE implementation inside Perl.

The following results were obtained by omitting fetches: e.g. $cntr++, $cntr->incr. Fortunately, stores perform similarly between BSD and Linux via the OO interface.

time in seconds : FreeBSD Linux Mac OSX ipc_shareable.pl : 1.77 1.86 1.61 mce_shared.pl : 0.68 0.95 0.58 mce_shared_oo.pl : 0.19 0.17 0.16 counter: 20000

The above is only a glimpse of what is possible with MCE::Shared. The use of parallel data-channels (enabled automatically) for MCE, MCE::Hobo, and threads further reduce latency for the shared-manager process. Calling MCE::Shared->init(), inside the worker, enables the optimization for fork and other parallel modules.

This has been an interesting exercise. Thank you Bloehdian for the initial post.

Tests were conducted on a Macbook Pro laptop (Haswell architecture) OS X 10.11.6 running at 2.6 GHz 1600 MHz RAM. FreeBSD and Linux are virtual machines managed by Parallels Desktop 11.2.1.

Regards, Mario.


In reply to Re^2: IPC:Shareable: Not an array reference by marioroy
in thread IPC:Shareable: Not an array reference by Bloehdian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.