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

NAME

6       MCE::Map - Parallel map model similar to the native map function
7

VERSION

9       This document describes MCE::Map version 1.889
10

SYNOPSIS

12        ## Exports mce_map, mce_map_f, and mce_map_s
13        use MCE::Map;
14
15        ## Array or array_ref
16        my @a = mce_map { $_ * $_ } 1..10000;
17        my @b = mce_map { $_ * $_ } \@list;
18
19        ## Important; pass an array_ref for deeply input data
20        my @c = mce_map { $_->[1] *= 2; $_ } [ [ 0, 1 ], [ 0, 2 ], ... ];
21        my @d = mce_map { $_->[1] *= 2; $_ } \@deeply_list;
22
23        ## File path, glob ref, IO::All::{ File, Pipe, STDIO } obj, or scalar ref
24        ## Workers read directly and not involve the manager process
25        my @e = mce_map_f { chomp; $_ } "/path/to/file"; # efficient
26
27        ## Involves the manager process, therefore slower
28        my @f = mce_map_f { chomp; $_ } $file_handle;
29        my @g = mce_map_f { chomp; $_ } $io;
30        my @h = mce_map_f { chomp; $_ } \$scalar;
31
32        ## Sequence of numbers (begin, end [, step, format])
33        my @i = mce_map_s { $_ * $_ } 1, 10000, 5;
34        my @j = mce_map_s { $_ * $_ } [ 1, 10000, 5 ];
35
36        my @k = mce_map_s { $_ * $_ } {
37           begin => 1, end => 10000, step => 5, format => undef
38        };
39

DESCRIPTION

41       This module provides a parallel map implementation via Many-Core
42       Engine.  MCE incurs a small overhead due to passing of data. A fast
43       code block will run faster natively. However, the overhead will likely
44       diminish as the complexity increases for the code.
45
46        my @m1 =     map { $_ * $_ } 1..1000000;               ## 0.127 secs
47        my @m2 = mce_map { $_ * $_ } 1..1000000;               ## 0.304 secs
48
49       Chunking, enabled by default, greatly reduces the overhead behind the
50       scene.  The time for mce_map below also includes the time for data
51       exchanges between the manager and worker processes. More
52       parallelization will be seen when the code incurs additional CPU time.
53
54        sub calc {
55           sqrt $_ * sqrt $_ / 1.3 * 1.5 / 3.2 * 1.07
56        }
57
58        my @m1 =     map { calc } 1..1000000;                  ## 0.367 secs
59        my @m2 = mce_map { calc } 1..1000000;                  ## 0.365 secs
60
61       Even faster is mce_map_s; useful when input data is a range of numbers.
62       Workers generate sequences mathematically among themselves without any
63       interaction from the manager process. Two arguments are required for
64       mce_map_s (begin, end). Step defaults to 1 if begin is smaller than
65       end, otherwise -1.
66
67        my @m3 = mce_map_s { calc } 1, 1000000;                ## 0.270 secs
68
69       Although this document is about MCE::Map, the MCE::Stream module can
70       write results immediately without waiting for all chunks to complete.
71       This is made possible by passing the reference to an array (in this
72       case @m4 and @m5).
73
74        use MCE::Stream;
75
76        sub calc {
77           sqrt $_ * sqrt $_ / 1.3 * 1.5 / 3.2 * 1.07
78        }
79
80        my @m4; mce_stream \@m4, sub { calc }, 1..1000000;
81
82           ## Completes in 0.272 secs. This is amazing considering the
83           ## overhead for passing data between the manager and workers.
84
85        my @m5; mce_stream_s \@m5, sub { calc }, 1, 1000000;
86
87           ## Completed in 0.176 secs. Like with mce_map_s, specifying a
88           ## sequence specification turns out to be faster due to lesser
89           ## overhead for the manager process.
90

OVERRIDING DEFAULTS

92       The following list options which may be overridden when loading the
93       module.
94
95        use Sereal qw( encode_sereal decode_sereal );
96        use CBOR::XS qw( encode_cbor decode_cbor );
97        use JSON::XS qw( encode_json decode_json );
98
99        use MCE::Map
100            max_workers => 4,                # Default 'auto'
101            chunk_size => 100,               # Default 'auto'
102            tmp_dir => "/path/to/app/tmp",   # $MCE::Signal::tmp_dir
103            freeze => \&encode_sereal,       # \&Storable::freeze
104            thaw => \&decode_sereal,         # \&Storable::thaw
105            init_relay => 0,                 # Default undef; MCE 1.882+
106            use_threads => 0,                # Default undef; MCE 1.882+
107        ;
108
109       From MCE 1.8 onwards, Sereal 3.015+ is loaded automatically if
110       available.  Specify "Sereal => 0" to use Storable instead.
111
112        use MCE::Map Sereal => 0;
113

CUSTOMIZING MCE

115       MCE::Map->init ( options )
116       MCE::Map::init { options }
117
118       The init function accepts a hash of MCE options. The gather option, if
119       specified, is ignored due to being used internally by the module.
120
121        use MCE::Map;
122
123        MCE::Map->init(
124           chunk_size => 1, max_workers => 4,
125
126           user_begin => sub {
127              print "## ", MCE->wid, " started\n";
128           },
129
130           user_end => sub {
131              print "## ", MCE->wid, " completed\n";
132           }
133        );
134
135        my @a = mce_map { $_ * $_ } 1..100;
136
137        print "\n", "@a", "\n";
138
139        -- Output
140
141        ## 2 started
142        ## 1 started
143        ## 3 started
144        ## 4 started
145        ## 1 completed
146        ## 4 completed
147        ## 2 completed
148        ## 3 completed
149
150        1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361
151        400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156
152        1225 1296 1369 1444 1521 1600 1681 1764 1849 1936 2025 2116 2209
153        2304 2401 2500 2601 2704 2809 2916 3025 3136 3249 3364 3481 3600
154        3721 3844 3969 4096 4225 4356 4489 4624 4761 4900 5041 5184 5329
155        5476 5625 5776 5929 6084 6241 6400 6561 6724 6889 7056 7225 7396
156        7569 7744 7921 8100 8281 8464 8649 8836 9025 9216 9409 9604 9801
157        10000
158

API DOCUMENTATION

160       MCE::Map->run ( sub { code }, list )
161       mce_map { code } list
162
163       Input data may be defined using a list or an array reference. Unlike
164       MCE::Loop, Flow, and Step, specifying a hash reference as input data
165       isn't allowed.
166
167        ## Array or array_ref
168        my @a = mce_map { $_ * 2 } 1..1000;
169        my @b = mce_map { $_ * 2 } \@list;
170
171        ## Important; pass an array_ref for deeply input data
172        my @c = mce_map { $_->[1] *= 2; $_ } [ [ 0, 1 ], [ 0, 2 ], ... ];
173        my @d = mce_map { $_->[1] *= 2; $_ } \@deeply_list;
174
175        ## Not supported
176        my @z = mce_map { ... } \%hash;
177
178       MCE::Map->run_file ( sub { code }, file )
179       mce_map_f { code } file
180
181       The fastest of these is the /path/to/file. Workers communicate the next
182       offset position among themselves with zero interaction by the manager
183       process.
184
185       "IO::All" { File, Pipe, STDIO } is supported since MCE 1.845.
186
187        my @c = mce_map_f { chomp; $_ . "\r\n" } "/path/to/file";  # faster
188        my @d = mce_map_f { chomp; $_ . "\r\n" } $file_handle;
189        my @e = mce_map_f { chomp; $_ . "\r\n" } $io;              # IO::All
190        my @f = mce_map_f { chomp; $_ . "\r\n" } \$scalar;
191
192       MCE::Map->run_seq ( sub { code }, $beg, $end [, $step, $fmt ] )
193       mce_map_s { code } $beg, $end [, $step, $fmt ]
194
195       Sequence may be defined as a list, an array reference, or a hash
196       reference.  The functions require both begin and end values to run.
197       Step and format are optional. The format is passed to sprintf (% may be
198       omitted below).
199
200        my ($beg, $end, $step, $fmt) = (10, 20, 0.1, "%4.1f");
201
202        my @f = mce_map_s { $_ } $beg, $end, $step, $fmt;
203        my @g = mce_map_s { $_ } [ $beg, $end, $step, $fmt ];
204
205        my @h = mce_map_s { $_ } {
206           begin => $beg, end => $end,
207           step => $step, format => $fmt
208        };
209
210       MCE::Map->run ( sub { code }, iterator )
211       mce_map { code } iterator
212
213       An iterator reference may be specified for input_data. Iterators are
214       described under section "SYNTAX for INPUT_DATA" at MCE::Core.
215
216        my @a = mce_map { $_ * 2 } make_iterator(10, 30, 2);
217

MANUAL SHUTDOWN

219       MCE::Map->finish
220       MCE::Map::finish
221
222       Workers remain persistent as much as possible after running. Shutdown
223       occurs automatically when the script terminates. Call finish when
224       workers are no longer needed.
225
226        use MCE::Map;
227
228        MCE::Map->init(
229           chunk_size => 20, max_workers => 'auto'
230        );
231
232        my @a = mce_map { ... } 1..100;
233
234        MCE::Map->finish;
235

INDEX

237       MCE, MCE::Core
238

AUTHOR

240       Mario E. Roy, <marioeroy AT gmail DOT com>
241
242
243
244perl v5.38.0                      2023-09-14                       MCE::Map(3)
Impressum