1MCE::Shared::Sequence(3U)ser Contributed Perl DocumentatiMoCnE::Shared::Sequence(3)
2
3
4
6 MCE::Shared::Sequence - Sequence helper class
7
9 This document describes MCE::Shared::Sequence version 1.839
10
12 A number sequence class for use as a standalone or managed by
13 MCE::Shared.
14
16 # non-shared or local construction for use by a single process
17
18 use MCE::Shared::Sequence;
19
20 my $seq_a = MCE::Shared::Sequence->new( $begin, $end, $step, $fmt );
21
22 my $seq_b = MCE::Shared::Sequence->new(
23 { chunk_size => 10, bounds_only => 1 },
24 $begin, $end, $step, $fmt
25 );
26
27 # construction for sharing with other threads and processes
28
29 use MCE::Shared;
30
31 my $seq_a = MCE::Shared->sequence( 1, 100 );
32
33 my $seq_b = MCE::Shared->sequence(
34 { chunk_size => 10, bounds_only => 1 },
35 1, 100
36 );
37
38 # example
39
40 use MCE::Hobo;
41
42 sub parallel_a {
43 my ( $id ) = @_;
44 while ( defined ( my $num = $seq_a->next ) ) {
45 print "$id: $num\n";
46 }
47 }
48
49 sub parallel_b {
50 my ( $id ) = @_;
51 while ( my ( $beg, $end ) = $seq_b->next ) {
52 for my $num ( $beg .. $end ) {
53 print "$id: $num\n";
54 }
55 }
56 }
57
58 MCE::Hobo->new( \¶llel_a, $_ ) for 1 .. 2;
59 MCE::Hobo->new( \¶llel_b, $_ ) for 3 .. 4;
60
61 # ... do other work ...
62
63 MCE::Hobo->waitall();
64
66 new ( { options }, begin, end [, step, format ] )
67 new ( begin, end [, step, format ] )
68 Constructs a new object. "step", if omitted, defaults to 1 if
69 "begin" is smaller than "end" or "-1" if "begin" is greater than
70 "end". The "format" string is passed to "sprintf" behind the scene
71 (% may be omitted).
72
73 $seq_n_formatted = sprintf( "%4.1f", $seq_n );
74
75 Two options "chunk_size" and "bounds_only" are supported, which
76 default to 1 and 0 respectively. Chunking reduces the number of IPC
77 calls to and from the shared-manager process for large sequences.
78
79 If "bounds_only =" 1> is specified, the "next" method computes the
80 "begin" and "end" values only for the chunk and not the numbers in
81 between (hence boundaries only).
82
83 use MCE::Shared;
84
85 # demo 1
86
87 $seq1 = MCE::Shared->sequence(
88 { chunk_size => 10, bounds_only => 0 }, 1, 20
89 );
90
91 # @chunk = $seq1->next; # ( qw/ 1 2 3 4 5 6 7 8 9 10 / )
92 # @chunk = $seq1->next; # ( qw/ 11 12 13 14 15 16 17 18 19 20 / )
93
94 while ( my @chunk = $seq1->next ) {
95 ...
96 }
97
98 # demo 2
99
100 $seq2 = MCE::Shared->sequence(
101 { chunk_size => 10, bounds_only => 1 }, 1, 100
102 );
103
104 # ( $beg, $end ) = $seq2->next; # ( 1, 10 )
105 # ( $beg, $end ) = $seq2->next; # ( 11, 20 )
106 # ( $beg, $end ) = $seq2->next; # ( 21, 30 )
107 # ...
108 # ( $beg, $end ) = $seq2->next; # ( 81, 90 )
109 # ( $beg, $end ) = $seq2->next; # ( 91, 100 )
110
111 # The optional chunk_id value, starting at 1, applies to sequence
112 # objects configured with the bounds_only option set to a true
113 # value. API available since 1.834.
114
115 while ( my ( $beg, $end, $chunk_id ) = $seq2->next ) {
116 for my $i ( $beg .. $end ) {
117 ...
118 }
119 }
120
121 Parameters may be given later with "rewind" before calling "next".
122
123 # non-shared or local construction for use by a single process
124
125 use MCE::Shared::Sequence;
126
127 $seq = MCE::Shared::Sequence->new;
128 $seq->rewind( -1, 1, 0.1, "%4.1f" );
129
130 $seq = MCE::Shared::Sequence->new(
131 { chunk_size => 10, bounds_only => 1 }, 1, 100
132 );
133
134 # construction for sharing with other threads and processes
135
136 use MCE::Shared;
137
138 $seq = MCE::Shared->sequence;
139 $seq->rewind( 1, 100 );
140
141 $seq = MCE::Shared->sequence(
142 { chunk_size => 10, bounds_only => 1 }, 1, 100
143 );
144
145 next
146 Returns the next computed sequence(s). An undefined value is
147 returned when the computed "begin" value exceeds the value held by
148 "end".
149
150 # default: { chunk_size => 1, bounds_only => 0 }
151 $seq = MCE::Shared->sequence( 1, 100 );
152
153 while ( defined ( my $num = $seq->next ) ) {
154 ...
155 }
156
157 # chunking
158
159 $seq = MCE::Shared->sequence(
160 { chunk_size => 10 }, 1, 100
161 );
162
163 while ( my @chunk = $seq->next ) {
164 ...
165 }
166
167 # chunking, boundaries only
168
169 $seq = MCE::Shared->sequence(
170 { chunk_size => 10, bounds_only => 1 }, 1, 100
171 );
172
173 while ( my ( $beg, $end, $chunk_id ) = $seq->next ) {
174 for my $i ( $beg .. $end ) {
175 ...
176 }
177 }
178
179 rewind ( { options }, begin, end [, step, format ] )
180 rewind ( begin, end [, step, format ] )
181 Sets the initial value back to the value held by "begin" when no
182 arguments are given. Otherwise, resets the sequence with given
183 criteria.
184
185 $seq->rewind;
186
187 $seq->rewind( { chunk_size => 10, bounds_only => 1 }, 1, 100 );
188
189 while ( my ( $beg, $end ) = $seq->next ) {
190 for my $i ( $beg .. $end ) {
191 ...
192 }
193 }
194
195 $seq->rewind( 1, 100 );
196
197 while ( defined ( my $num = $seq->next ) ) {
198 ...
199 }
200
202 MCE, MCE::Hobo, MCE::Shared
203
205 Mario E. Roy, <marioeroy AT gmail DOT com>
206
207
208
209perl v5.28.0 2018-08-25 MCE::Shared::Sequence(3)