1MCE::Stream(3)        User Contributed Perl Documentation       MCE::Stream(3)
2
3
4

NAME

6       MCE::Stream - Parallel stream model for chaining multiple maps and
7       greps
8

VERSION

10       This document describes MCE::Stream version 1.862
11

SYNOPSIS

13        ## Exports mce_stream, mce_stream_f, mce_stream_s
14        use MCE::Stream;
15
16        my (@m1, @m2, @m3);
17
18        ## Default mode is map and processed from right-to-left
19        @m1 = mce_stream sub { $_ * 3 }, sub { $_ * 2 }, 1..10000;
20        mce_stream \@m2, sub { $_ * 3 }, sub { $_ * 2 }, 1..10000;
21
22        ## Native Perl
23        @m3 = map { $_ * $_ } grep { $_ % 5 == 0 } 1..10000;
24
25        ## Streaming grep and map in parallel
26        mce_stream \@m3,
27           { mode => 'map',  code => sub { $_ * $_ } },
28           { mode => 'grep', code => sub { $_ % 5 == 0 } }, 1..10000;
29
30        ## Array or array_ref
31        my @a = mce_stream sub { $_ * $_ }, 1..10000;
32        my @b = mce_stream sub { $_ * $_ }, \@list;
33
34        ## Important; pass an array_ref for deeply input data
35        my @c = mce_stream sub { $_->[1] *= 2; $_ }, [ [ 0, 1 ], [ 0, 2 ], ... ];
36        my @d = mce_stream sub { $_->[1] *= 2; $_ }, \@deeply_list;
37
38        ## File path, glob ref, IO::All::{ File, Pipe, STDIO } obj, or scalar ref
39        ## Workers read directly and not involve the manager process
40        my @e = mce_stream_f sub { chomp; $_ }, "/path/to/file"; # efficient
41
42        ## Involves the manager process, therefore slower
43        my @f = mce_stream_f sub { chomp; $_ }, $file_handle;
44        my @g = mce_stream_f sub { chomp; $_ }, $io;
45        my @h = mce_stream_f sub { chomp; $_ }, \$scalar;
46
47        ## Sequence of numbers (begin, end [, step, format])
48        my @i = mce_stream_s sub { $_ * $_ }, 1, 10000, 5;
49        my @j = mce_stream_s sub { $_ * $_ }, [ 1, 10000, 5 ];
50
51        my @k = mce_stream_s sub { $_ * $_ }, {
52           begin => 1, end => 10000, step => 5, format => undef
53        };
54

DESCRIPTION

56       This module allows one to stream multiple map and/or grep operations in
57       parallel. Code blocks run simultaneously from right-to-left. The
58       results are appended immediately when providing a reference to an
59       array.
60
61        ## Appends are serialized, even out-of-order ok, but immediately.
62        ## Out-of-order chunks are held temporarily until ordered chunks
63        ## arrive.
64
65        mce_stream \@a, sub { $_ }, sub { $_ }, sub { $_ }, 1..10000;
66
67        ##                                                    input
68        ##                                        chunk1      input
69        ##                            chunk3      chunk2      input
70        ##                chunk2      chunk2      chunk3      input
71        ##   append1      chunk3      chunk1      chunk4      input
72        ##   append2      chunk1      chunk5      chunk5      input
73        ##   append3      chunk5      chunk4      chunk6      ...
74        ##   append4      chunk4      chunk6      ...
75        ##   append5      chunk6      ...
76        ##   append6      ...
77        ##   ...
78        ##
79
80       MCE incurs a small overhead due to passing of data. A fast code block
81       will run faster natively when chaining multiple map functions. However,
82       the overhead will likely diminish as the complexity increases for the
83       code.
84
85        ## 0.334 secs -- baseline using the native map function
86        my @m1 = map { $_ * 4 } map { $_ * 3 } map { $_ * 2 } 1..1000000;
87
88        ## 0.427 secs -- this is quite amazing considering data passing
89        my @m2 = mce_stream
90              sub { $_ * 4 }, sub { $_ * 3 }, sub { $_ * 2 }, 1..1000000;
91
92        ## 0.355 secs -- appends to @m3 immediately, not after running
93        my @m3; mce_stream \@m3,
94              sub { $_ * 4 }, sub { $_ * 3 }, sub { $_ * 2 }, 1..1000000;
95
96       Even faster is mce_stream_s; useful when input data is a range of
97       numbers.  Workers generate sequences mathematically among themselves
98       without any interaction from the manager process. Two arguments are
99       required for mce_stream_s (begin, end). Step defaults to 1 if begin is
100       smaller than end, otherwise -1.
101
102        ## 0.278 secs -- numbers are generated mathematically via sequence
103        my @m4; mce_stream_s \@m4,
104              sub { $_ * 4 }, sub { $_ * 3 }, sub { $_ * 2 }, 1, 1000000;
105

OVERRIDING DEFAULTS

107       The following list options which may be overridden when loading the
108       module.
109
110        use Sereal qw( encode_sereal decode_sereal );
111        use CBOR::XS qw( encode_cbor decode_cbor );
112        use JSON::XS qw( encode_json decode_json );
113
114        use MCE::Stream
115            max_workers => 8,                # Default 'auto'
116            chunk_size => 500,               # Default 'auto'
117            tmp_dir => "/path/to/app/tmp",   # $MCE::Signal::tmp_dir
118            freeze => \&encode_sereal,       # \&Storable::freeze
119            thaw => \&decode_sereal,         # \&Storable::thaw
120            default_mode => 'grep',          # Default 'map'
121            fast => 1                        # Default 0 (fast dequeue)
122        ;
123
124       From MCE 1.8 onwards, Sereal 3.015+ is loaded automatically if
125       available.  Specify "Sereal => 0" to use Storable instead.
126
127        use MCE::Stream Sereal => 0;
128

CUSTOMIZING MCE

130       MCE::Stream->init ( options )
131       MCE::Stream::init { options }
132
133       The init function accepts a hash of MCE options. The gather and
134       bounds_only options, if specified, are ignored due to being used
135       internally by the module (not shown below).
136
137        use MCE::Stream;
138
139        MCE::Stream::init {
140           chunk_size => 1, max_workers => 4,
141
142           user_begin => sub {
143              print "## ", MCE->wid, " started\n";
144           },
145
146           user_end => sub {
147              print "## ", MCE->wid, " completed\n";
148           }
149        };
150
151        my @a = mce_stream sub { $_ * $_ }, 1..100;
152
153        print "\n", "@a", "\n";
154
155        -- Output
156
157        ## 1 started
158        ## 2 started
159        ## 3 started
160        ## 4 started
161        ## 3 completed
162        ## 1 completed
163        ## 2 completed
164        ## 4 completed
165
166        1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361
167        400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156
168        1225 1296 1369 1444 1521 1600 1681 1764 1849 1936 2025 2116 2209
169        2304 2401 2500 2601 2704 2809 2916 3025 3136 3249 3364 3481 3600
170        3721 3844 3969 4096 4225 4356 4489 4624 4761 4900 5041 5184 5329
171        5476 5625 5776 5929 6084 6241 6400 6561 6724 6889 7056 7225 7396
172        7569 7744 7921 8100 8281 8464 8649 8836 9025 9216 9409 9604 9801
173        10000
174
175       Like with MCE::Stream::init above, MCE options may be specified using
176       an anonymous hash for the first argument. Notice how both max_workers
177       and task_name can take an anonymous array for setting values uniquely
178       per each code block.
179
180       Remember that MCE::Stream processes from right-to-left when setting the
181       individual values.
182
183        use MCE::Stream;
184
185        my @a = mce_stream {
186           task_name   => [ 'c', 'b', 'a' ],
187           max_workers => [  2,   4,   3, ],
188
189           user_end => sub {
190              my ($mce, $task_id, $task_name) = @_;
191              print "$task_id - $task_name completed\n";
192           },
193
194           task_end => sub {
195              my ($mce, $task_id, $task_name) = @_;
196              MCE->print("$task_id - $task_name ended\n");
197           }
198        },
199        sub { $_ * 4 },             ## 2 workers, named c
200        sub { $_ * 3 },             ## 4 workers, named b
201        sub { $_ * 2 }, 1..10000;   ## 3 workers, named a
202
203        -- Output
204
205        0 - a completed
206        0 - a completed
207        0 - a completed
208        0 - a ended
209        1 - b completed
210        1 - b completed
211        1 - b completed
212        1 - b completed
213        1 - b ended
214        2 - c completed
215        2 - c completed
216        2 - c ended
217
218       Note that the anonymous hash, for specifying options, also comes first
219       when passing an array reference.
220
221        my @a; mce_stream {
222           ...
223        }, \@a, sub { ... }, sub { ... }, 1..10000;
224

API DOCUMENTATION

226       Scripts using MCE::Stream can be written using the long or short form.
227       The long form becomes relevant when mixing modes. Again, processing
228       occurs from right-to-left.
229
230        my @m3 = mce_stream
231           { mode => 'map',  code => sub { $_ * $_ } },
232           { mode => 'grep', code => sub { $_ % 5 == 0 } }, 1..10000;
233
234        my @m4; mce_stream \@m4,
235           { mode => 'map',  code => sub { $_ * $_ } },
236           { mode => 'grep', code => sub { $_ % 5 == 0 } }, 1..10000;
237
238       For multiple grep blocks, the short form can be used. Simply specify
239       the default mode for the module. The two valid values for default_mode
240       is 'grep' and 'map'.
241
242        use MCE::Stream default_mode => 'grep';
243
244        my @f = mce_stream_f sub { /ending$/ }, sub { /^starting/ }, $file;
245
246       The following assumes 'map' for default_mode in order to demonstrate
247       all the possibilities for providing input data.
248
249       MCE::Stream->run ( sub { code }, list )
250       mce_stream sub { code }, list
251
252       Input data may be defined using a list or an array reference. Unlike
253       MCE::Loop, Flow, and Step, specifying a hash reference as input data
254       isn't allowed.
255
256        ## Array or array_ref
257        my @a = mce_stream sub { $_ * 2 }, 1..1000;
258        my @b = mce_stream sub { $_ * 2 }, \@list;
259
260        ## Important; pass an array_ref for deeply input data
261        my @c = mce_stream sub { $_->[1] *= 2; $_ }, [ [ 0, 1 ], [ 0, 2 ], ... ];
262        my @d = mce_stream sub { $_->[1] *= 2; $_ }, \@deeply_list;
263
264        ## Not supported
265        my @z = mce_stream sub { ... }, \%hash;
266
267       MCE::Stream->run_file ( sub { code }, file )
268       mce_stream_f sub { code }, file
269
270       The fastest of these is the /path/to/file. Workers communicate the next
271       offset position among themselves with zero interaction by the manager
272       process.
273
274       "IO::All" { File, Pipe, STDIO } is supported since MCE 1.845.
275
276        my @c = mce_stream_f sub { chomp; $_ . "\r\n" }, "/path/to/file";  # faster
277        my @d = mce_stream_f sub { chomp; $_ . "\r\n" }, $file_handle;
278        my @e = mce_stream_f sub { chomp; $_ . "\r\n" }, $io;              # IO::All
279        my @f = mce_stream_f sub { chomp; $_ . "\r\n" }, \$scalar;
280
281       MCE::Stream->run_seq ( sub { code }, $beg, $end [, $step, $fmt ] )
282       mce_stream_s sub { code }, $beg, $end [, $step, $fmt ]
283
284       Sequence may be defined as a list, an array reference, or a hash
285       reference.  The functions require both begin and end values to run.
286       Step and format are optional. The format is passed to sprintf (% may be
287       omitted below).
288
289        my ($beg, $end, $step, $fmt) = (10, 20, 0.1, "%4.1f");
290
291        my @f = mce_stream_s sub { $_ }, $beg, $end, $step, $fmt;
292        my @g = mce_stream_s sub { $_ }, [ $beg, $end, $step, $fmt ];
293
294        my @h = mce_stream_s sub { $_ }, {
295           begin => $beg, end => $end, step => $step, format => $fmt
296        };
297
298       MCE::Stream->run ( { input_data => iterator }, sub { code } )
299       mce_stream { input_data => iterator }, sub { code }
300
301       An iterator reference may be specified for input_data. The only other
302       way is to specify input_data via MCE::Stream::init. This prevents
303       MCE::Stream from configuring the iterator reference as another user
304       task which will not work.
305
306       Iterators are described under section "SYNTAX for INPUT_DATA" at
307       MCE::Core.
308
309        MCE::Stream::init {
310           input_data => iterator
311        };
312
313        my @a = mce_stream sub { $_ * 3 }, sub { $_ * 2 };
314

MANUAL SHUTDOWN

316       MCE::Stream->finish
317       MCE::Stream::finish
318
319       Workers remain persistent as much as possible after running. Shutdown
320       occurs automatically when the script terminates. Call finish when
321       workers are no longer needed.
322
323        use MCE::Stream;
324
325        MCE::Stream::init {
326           chunk_size => 20, max_workers => 'auto'
327        };
328
329        my @a = mce_stream { ... } 1..100;
330
331        MCE::Stream::finish;
332

INDEX

334       MCE, MCE::Core
335

AUTHOR

337       Mario E. Roy, <marioeroy AT gmail DOT com>
338
339
340
341perl v5.30.0                      2019-09-19                    MCE::Stream(3)
Impressum