summaryrefslogtreecommitdiff
path: root/lib/MCE/Channel.pm
blob: 92e72a1c916e0026896bc66896ec87731078bf09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
###############################################################################
## ----------------------------------------------------------------------------
## Queue-like and two-way communication capability.
##
###############################################################################

package MCE::Channel;

use strict;
use warnings;

no warnings qw( uninitialized once );

our $VERSION = '1.888';

## no critic (BuiltinFunctions::ProhibitStringyEval)
## no critic (TestingAndDebugging::ProhibitNoStrict)

use if $^O eq 'MSWin32', 'threads';
use if $^O eq 'MSWin32', 'threads::shared';

use Carp ();

$Carp::Internal{ (__PACKAGE__) }++;

my ( $freeze, $thaw );

BEGIN {
   if ( $] ge '5.008008' && ! $INC{'PDL.pm'} ) {
      local $@;
      eval 'use Sereal::Encoder 3.015; use Sereal::Decoder 3.015;';
      if ( ! $@ ) {
         my $encoder_ver = int( Sereal::Encoder->VERSION() );
         my $decoder_ver = int( Sereal::Decoder->VERSION() );
         if ( $encoder_ver - $decoder_ver == 0 ) {
            $freeze = \&Sereal::Encoder::encode_sereal;
            $thaw   = \&Sereal::Decoder::decode_sereal;
         }
      }
   }

   if ( ! defined $freeze ) {
      require Storable;
      $freeze = \&Storable::freeze;
      $thaw   = \&Storable::thaw;
   }
}

use MCE::Util ();

my $tid = $INC{'threads.pm'} ? threads->tid() : 0;

sub new {
   my ( $class, %argv ) = @_;
   my $impl = defined( $argv{impl} ) ? ucfirst( lc $argv{impl} ) : 'Mutex';

   # Replace 'fast' with 'Fast' in the implementation value.
   $impl =~ s/fast$/Fast/;

   $impl = 'Threads'     if ( $impl eq 'Mutex' && $^O eq 'MSWin32' );
   $impl = 'ThreadsFast' if ( $impl eq 'MutexFast' && $^O eq 'MSWin32' );
   $impl = 'Mutex'       if ( $impl eq 'Threads' && $^O eq 'cygwin' );
   $impl = 'MutexFast'   if ( $impl eq 'ThreadsFast' && $^O eq 'cygwin' );

   eval "require MCE::Channel::$impl; 1;" ||
      Carp::croak("Could not load Channel implementation '$impl': $@");

   my $pkg = 'MCE::Channel::'.$impl;
   no strict 'refs';

   $pkg->new(%argv);
}

sub CLONE {
   $tid = threads->tid if $INC{'threads.pm'};
}

sub DESTROY {
   my ( $pid, $self ) = ( $tid ? $$ .'.'. $tid : $$, @_ );

   if ( $self->{'init_pid'} && $self->{'init_pid'} eq $pid ) {
      MCE::Util::_destroy_socks($self, qw(c_sock c2_sock p_sock p2_sock));
      delete($self->{c_mutex}), delete($self->{p_mutex});
   }

   return;
}

sub impl {
   $_[0]->{'impl'} || 'Not defined';
}

sub _get_freeze { $freeze; }
sub _get_thaw   { $thaw;   }

sub _ended {
   warn "WARNING: ($_[0]) called on a channel that has been 'end'ed\n";

   return;
}

sub _read {
   my $bytes = MCE::Util::_sysread( $_[0], $_[1], my $len = $_[2] );
   my $read  = $bytes;

   while ( $bytes && $read != $len ) {
      $bytes = MCE::Util::_sysread( $_[0], $_[1], $len - $read, length($_[1]) );
      $read += $bytes if $bytes;
   }

   return;
}

sub _pid {
   $tid ? $$ .'.'. $tid : $$;
}

1;

__END__

###############################################################################
## ----------------------------------------------------------------------------
## Module usage.
##
###############################################################################

=head1 NAME

MCE::Channel - Queue-like and two-way communication capability

=head1 VERSION

This document describes MCE::Channel version 1.888

=head1 SYNOPSIS

 use MCE::Channel;

 ########################
 # Construction
 ########################

 # A single producer and many consumers supporting processes and threads

 my $c1 = MCE::Channel->new( impl => 'Mutex' );    # default implementation
 my $c2 = MCE::Channel->new( impl => 'Threads' );  # threads::shared locking

 # Set the mp flag if two or more workers (many producers) will be calling
 # enqueue/send or recv2/recv2_nb on the left end of the channel

 my $c3 = MCE::Channel->new( impl => 'Mutex', mp => 1 );
 my $c4 = MCE::Channel->new( impl => 'Threads', mp => 1 );

 # Tuned for one producer and one consumer, no locking

 my $c5 = MCE::Channel->new( impl => 'Simple' );

 ########################
 # Queue-like behavior
 ########################

 # Send data to consumers
 $c1->enqueue('item');
 $c1->enqueue(qw/item1 item2 item3 itemN/);

 # Receive data
 my $item  = $c1->dequeue();      # item
 my @items = $c1->dequeue(2);     # (item1, item2)

 # Receive, non-blocking
 my $item  = $c1->dequeue_nb();   # item
 my @items = $c1->dequeue_nb(2);  # (item1, item2)

 # Signal that there is no more work to be sent
 $c1->end();

 ########################
 # Two-way communication
 ########################

 # Producer(s) sending data
 $c3->send('message');
 $c3->send(qw/arg1 arg2 arg3/);

 # Consumer(s) receiving data
 my $mesg = $c3->recv();          # message
 my @args = $c3->recv();          # (arg1, arg2, arg3)

 # Alternatively, non-blocking
 my $mesg = $c3->recv_nb();       # message
 my @args = $c3->recv_nb();       # (arg1, arg2, arg3)

 # A producer signaling no more work to be sent
 $c3->end();

 # Consumers(s) sending data
 $c3->send2('message');
 $c3->send2(qw/arg1 arg2 arg3/);

 # Producer(s) receiving data
 my $mesg = $c3->recv2();         # message
 my @args = $c3->recv2();         # (arg1, arg2, arg3)

 # Alternatively, non-blocking
 my $mesg = $c3->recv2_nb();      # message
 my @args = $c3->recv2_nb();      # (arg1, arg2, arg3)

=head1 DESCRIPTION

A MCE::Channel object is a container for sending and receiving data using
socketpair handles. Serialization is provided by L<Sereal> if available.
Defaults to L<Storable> otherwise. Excluding the C<Simple> implementation,
both ends of the C<channel> support many workers concurrently (with mp => 1).

=head2 new ( impl => STRING, mp => BOOLEAN )

This creates a new channel. Three implementations are provided C<Mutex>,
C<Threads>, and C<Simple> indicating the locking mechanism to use
C<MCE::Mutex>, C<threads::shared>, and no locking respectively.

 $chnl = MCE::Channel->new();     # default: impl => 'Mutex', mp => 0
                                  # default: impl => 'Threads' on Windows

The C<Mutex> implementation supports processes and threads whereas the
C<Threads> implementation is suited for Windows and threads only.

 $chnl = MCE::Channel->new( impl => 'Mutex' );    # MCE::Mutex locking
 $chnl = MCE::Channel->new( impl => 'Threads' );  # threads::shared locking

 # on Windows, silently becomes impl => 'Threads' when specifying 'Mutex'

Set the C<mp> (m)any (p)roducers option to a true value if there will be two
or more workers calling C<enqueue>, <send>, C<recv2>, or C<recv2_nb> on the
left end of the channel. This is important to not incur a race condition.

 $chnl = MCE::Channel->new( impl => 'Mutex', mp => 1 );
 $chnl = MCE::Channel->new( impl => 'Threads', mp => 1 );

 # on Windows, silently becomes impl => 'Threads' when specifying 'Mutex'

The C<Simple> implementation is optimized for one producer and one consumer max.
It omits locking for maximum performance. This implementation is preferred for
parent to child communication not shared by another worker.

 $chnl = MCE::Channel->new( impl => 'Simple' );

=head1 QUEUE-LIKE BEHAVIOR

=head2 enqueue ( ITEM1 [, ITEM2, ... ] )

Appends a list of items onto the left end of the channel. This will block once
the internal socket buffer becomes full (i.e. awaiting workers to dequeue on the
other end). This prevents producer(s) from running faster than consumer(s).

Object (de)serialization is handled automatically using L<Sereal> if available
or defaults to L<Storable> otherwise.

 $chnl->enqueue('item1');
 $chnl->enqueue(qw/item2 item3 .../);

 $chnl->enqueue([ array_ref1 ]);
 $chnl->enqueue([ array_ref2 ], [ array_ref3 ], ...);

 $chnl->enqueue({ hash_ref1 });
 $chnl->enqueue({ hash_ref2 }, { hash_ref3 }, ...);

=head2 dequeue

=head2 dequeue ( COUNT )

Removes the requested number of items (default 1) from the right end of the
channel. If the channel contains fewer than the requested number of items,
the method will block (i.e. until other producer(s) enqueue more items).

 $item  = $chnl->dequeue();       # item1
 @items = $chnl->dequeue(2);      # ( item2, item3 )

=head2 dequeue_nb

=head2 dequeue_nb ( COUNT )

Removes the requested number of items (default 1) from the right end of the
channel. If the channel contains fewer than the requested number of items,
the method will return what it was able to retrieve and return immediately.
If the channel is empty, then returns C<an empty list> in list context or
C<undef> in scalar context.

 $item  = $chnl->dequeue_nb();    # array_ref1
 @items = $chnl->dequeue_nb(2);   # ( array_ref2, array_ref3 )

=head2 end

This is called by a producer to signal that there is no more work to be sent.
Once ended, no more items may be sent by the producer. Calling C<end> by
multiple producers is not supported.

 $chnl->end;

=head1 TWO-WAY IPC - PRODUCER TO CONSUMER

=head2 send ( ARG1 [, ARG2, ... ] )

Append data onto the left end of the channel. Unlike C<enqueue>, the values
are kept together for the receiving consumer, similarly to calling a method.
Object (de)serialization is handled automatically.

 $chnl->send('item');
 $chnl->send([ list_ref ]);
 $chnl->send([ hash_ref ]);

 $chnl->send(qw/item1 item2 .../);
 $chnl->send($id, [ list_ref ]);
 $chnl->send($id, { hash_ref });

The fast channel implementations, introduced in MCE 1.877, support one item
for C<send>. If you want to pass multiple arguments, simply join the arguments
into a string. That means the receiver will need to split the string.

 $chnl = MCE::Channel->new(impl => "SimpleFast");

 $chnl->send(join(" ", qw/item1 item2 item3/);
 my ($item1, $item2, $item3) = split " ", $chnl->recv();

=head2 recv

=head2 recv_nb

Blocking and non-blocking fetch methods from the right end of the channel.
For the latter and when the channel is empty, returns C<an empty list> in
list context or C<undef> in scalar context.

 $item      = $chnl->recv();
 $array_ref = $chnl->recv();
 $hash_ref  = $chnl->recv();

 ($item1, $item2)  = $chnl->recv_nb();
 ($id, $array_ref) = $chnl->recv_nb();
 ($id, $hash_ref)  = $chnl->recv_nb();

=head1 TWO-WAY IPC - CONSUMER TO PRODUCER

=head2 send2 ( ARG1 [, ARG2, ... ] )

Append data onto the right end of the channel. Unlike C<enqueue>, the values
are kept together for the receiving producer, similarly to calling a method.
Object (de)serialization is handled automatically.

 $chnl->send2('item');
 $chnl->send2([ list_ref ]);
 $chnl->send2([ hash_ref ]);

 $chnl->send2(qw/item1 item2 .../);
 $chnl->send2($id, [ list_ref ]);
 $chnl->send2($id, { hash_ref });

The fast channel implementations, introduced in MCE 1.877, support one item
for C<send2>. If you want to pass multiple arguments, simply join the arguments
into a string. Not to forget, the receiver must split the string as well.

 $chnl = MCE::Channel->new(impl => "MutexFast");

 $chnl->send2(join(" ", qw/item1 item2 item3/);
 my ($item1, $item2, $item3) = split " ", $chnl->recv();

=head2 recv2

=head2 recv2_nb

Blocking and non-blocking fetch methods from the left end of the channel.
For the latter and when the channel is empty, returns C<an empty list> in
list context or C<undef> in scalar context.

 $item      = $chnl->recv2();
 $array_ref = $chnl->recv2();
 $hash_ref  = $chnl->recv2();

 ($item1, $item2)  = $chnl->recv2_nb();
 ($id, $array_ref) = $chnl->recv2_nb();
 ($id, $hash_ref)  = $chnl->recv2_nb();

=head1 DEMONSTRATIONS

=head2 Example 1 - threads

C<MCE::Channel> was made to work efficiently with L<threads>. The reason
comes from using L<threads::shared> for locking versus L<MCE::Mutex>.

 use strict;
 use warnings;

 use threads;
 use MCE::Channel;

 my $queue = MCE::Channel->new( impl => 'Threads' );
 my $num_consumers = 10;

 sub consumer {
    my $count = 0;

    # receive items
    while ( my ($item1, $item2) = $queue->dequeue(2) ) {
       $count += 2;
    }

    # send result
    $queue->send2( threads->tid => $count );
 }

 threads->create('consumer') for 1 .. $num_consumers;

 ## producer

 $queue->enqueue($_, $_ * 2) for 1 .. 40000;
 $queue->end;

 my %results;
 my $total = 0;

 for ( 1 .. $num_consumers ) {
    my ($id, $count) = $queue->recv2;
    $results{$id} = $count;
    $total += $count;
 }

 $_->join for threads->list;

 print $results{$_}, "\n" for keys %results;
 print "$total total\n\n";

 __END__

 # output

 8034
 8008
 8036
 8058
 7990
 7948
 8068
 7966
 7960
 7932
 80000 total

=head2 Example 2 - MCE::Child

The following is similarly threads-like for Perl lacking threads support.
It spawns processes instead, thus requires the C<Mutex> channel implementation
which is the default if omitted.

 use strict;
 use warnings;

 use MCE::Child;
 use MCE::Channel;

 my $queue = MCE::Channel->new( impl => 'Mutex' );
 my $num_consumers = 10;

 sub consumer {
    my $count = 0;

    # receive items
    while ( my ($item1, $item2) = $queue->dequeue(2) ) {
       $count += 2;
    }

    # send result
    $queue->send2( MCE::Child->pid => $count );
 }

 MCE::Child->create('consumer') for 1 .. $num_consumers;

 ## producer

 $queue->enqueue($_, $_ * 2) for 1 .. 40000;
 $queue->end;

 my %results;
 my $total = 0;

 for ( 1 .. $num_consumers ) {
    my ($id, $count) = $queue->recv2;
    $results{$id} = $count;
    $total += $count;
 }

 $_->join for MCE::Child->list;

 print $results{$_}, "\n" for keys %results;
 print "$total total\n\n";

=head2 Example 3 - Consumer requests item

Like the previous example, but have the manager process await a notification
from the consumer before inserting into the queue. This allows the producer
to end the channel early (i.e. exit loop).

 use strict;
 use warnings;

 use MCE::Child;
 use MCE::Channel;

 my $queue = MCE::Channel->new( impl => 'Mutex' );
 my $num_consumers = 10;

 sub consumer {
    # receive items
    my $count = 0;

    while () {
       # Notify the manager process to send items. This allows the
       # manager process to enqueue only when requested. The benefit
       # is being able to end the channel immediately.

       $queue->send2( MCE::Child->pid ); # channel is bi-directional

       my ($item1, $item2) = $queue->dequeue(2);
       last unless ( defined $item1 );   # channel ended

       $count += 2;
    }

    # result
    return ( MCE::Child->pid => $count );
 }

 MCE::Child->create('consumer') for 1 .. $num_consumers;

 ## producer

 for my $num (1 .. 40000) {
    # Await worker notification before inserting (blocking).
    my $consumer_pid = $queue->recv2;
    $queue->enqueue($num, $num * 2);
 }

 $queue->end;

 my %results;
 my $total = 0;

 for my $child ( MCE::Child->list ) {
    my ($id, $count) = $child->join;
    $results{$id} = $count;
    $total += $count;
 }

 print $results{$_}, "\n" for keys %results;
 print "$total total\n\n";

=head2 Example 4 - Many producers

Running with 2 or more producers requires setting the C<mp> option. Internally,
this enables locking support for the left end of the channel. The C<mp> option
applies to C<Mutex> and C<Threads> channel implementations only.

Here, using the MCE facility for gathering the final count.

 use strict;
 use warnings;

 use MCE::Flow;
 use MCE::Channel;

 my $queue = MCE::Channel->new( impl => 'Mutex', mp => 1 );
 my $num_consumers = 10;

 sub consumer {
    # receive items
    my $count = 0;
    while ( my ( $item1, $item2 ) = $queue->dequeue(2) ) {
       $count += 2;
    }
    # send result
    MCE->gather( MCE->wid => $count );
 }

 sub producer {
    $queue->enqueue($_, $_ * 2) for 1 .. 20000;
 }

 ## run 2 producers and many consumers

 MCE::Flow->init(
    max_workers => [ 2, $num_consumers ],
    task_name   => [ 'producer', 'consumer' ],
    task_end    => sub {
       my ($mce, $task_id, $task_name) = @_;
       if ( $task_name eq 'producer' ) {
          $queue->end;
       }
    }
 );

 # consumers call gather above (i.e. send a key-value pair),
 # have MCE append to a hash

 my %results = mce_flow \&producer, \&consumer;

 MCE::Flow->finish;

 my $total = 0;

 for ( keys %results ) {
    $total += $results{$_};
    print $results{$_}, "\n";
 }

 print "$total total\n\n";

=head2 Example 5 - Many channels

This demonstration configures a channel per consumer. Plus, a common channel
for consumers to request the next input item. The C<Simple> implementation is
specified for the individual channels whereas locking may be necessary for the
C<$ready> channel. However, consumers do not incur reading and what is written
is very small (i.e. atomic write is guaranteed by the OS). Thus, am safely
choosing the C<Simple> implementation versus C<Mutex>.

 use strict;
 use warnings;

 use MCE::Flow;
 use MCE::Channel;

 my $prog_name  = $0; $prog_name =~ s{^.*[\\/]}{}g;
 my $input_size = shift || 3000;

 unless ($input_size =~ /\A\d+\z/) {
    print {*STDERR} "usage: $prog_name [ size ]\n";
    exit 1;
 }

 my $consumers = 4;

 my @chnls = map { MCE::Channel->new( impl => 'Simple' ) } 1 .. $consumers;

 my $ready =       MCE::Channel->new( impl => 'Simple' );

 sub producer {
    my $id = 0;

    # send the next input item upon request
    for ( 0 .. $input_size - 1 ) {
       my $chnl_num = $ready->recv2;
       $chnls[ $chnl_num ]->send( ++$id, $_ );
    }

    # signal no more work
    $_->send( 0, undef ) for @chnls;
 }

 sub consumer {
    my $chnl_num = MCE->task_wid - 1;

    while () {
       # notify the producer ready for input
       $ready->send2( $chnl_num );

       # retrieve input data
       my ( $id, $item ) = $chnls[ $chnl_num ]->recv;

       # leave loop if no more work
       last unless $id;

       # compute and send the result to the manager process
       # ordered output requires an id (must be 1st argument)
       MCE->gather( $id, [ $item, sqrt($item) ] );
    }
 }

 # A custom 'ordered' output iterator for MCE's gather facility.
 # It returns a closure block, expecting an ID for 1st argument.

 sub output_iterator {
    my %tmp; my $order_id = 1;

    return sub {
       my ( $id, $result ) = @_;
       $tmp{ $id } = $result;

       while () {
          last unless exists $tmp{ $order_id };
          $result = delete $tmp{ $order_id };
          printf "n: %d sqrt(n): %f\n", $result->[0], $result->[1];
          $order_id++;
       }
    };
 }

 # Run one producer and many consumers.
 # Output to be sent orderly to STDOUT.

 MCE::Flow->init(
    gather => output_iterator(),
    max_workers => [ 1, $consumers ],
 );

 MCE::Flow->run( \&producer, \&consumer );
 MCE::Flow->finish;

 __END__

 # Output

 n: 0 sqrt(n): 0.000000
 n: 1 sqrt(n): 1.000000
 n: 2 sqrt(n): 1.414214
 n: 3 sqrt(n): 1.732051
 n: 4 sqrt(n): 2.000000
 n: 5 sqrt(n): 2.236068
 n: 6 sqrt(n): 2.449490
 n: 7 sqrt(n): 2.645751
 n: 8 sqrt(n): 2.828427
 n: 9 sqrt(n): 3.000000
 ...

=head1 SEE ALSO

=over 3

=item * L<https://github.com/marioroy/mce-examples/tree/master/chameneos>

=item * L<threads::lite>

=back

=head1 AUTHOR

Mario E. Roy, S<E<lt>marioeroy AT gmail DOT comE<gt>>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2019-2023 by Mario E. Roy

MCE::Channel is released under the same license as Perl.

See L<https://dev.perl.org/licenses/> for more information.

=cut