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.879
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        ;
106
107       From MCE 1.8 onwards, Sereal 3.015+ is loaded automatically if
108       available.  Specify "Sereal => 0" to use Storable instead.
109
110        use MCE::Map Sereal => 0;
111

CUSTOMIZING MCE

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

API DOCUMENTATION

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

MANUAL SHUTDOWN

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

INDEX

235       MCE, MCE::Core
236

AUTHOR

238       Mario E. Roy, <marioeroy AT gmail DOT com>
239
240
241
242perl v5.36.0                      2022-07-22                       MCE::Map(3)
Impressum