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.873
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 MCE::Shared::Sequence->new ( { options }, begin, end [, step, format ] )
67 MCE::Shared::Sequence->new ( begin, end [, step, format ] )
68 MCE::Shared->sequence ( { options }, begin, end [, step, format ] )
69 MCE::Shared->sequence ( begin, end [, step, format ] )
70 Constructs a new object. "step", if omitted, defaults to 1 if "begin"
71 is smaller than "end" or "-1" if "begin" is greater than "end". The
72 "format" string is passed to "sprintf" behind the scene (% may be
73 omitted).
74
75 $seq_n_formatted = sprintf( "%4.1f", $seq_n );
76
77 Two options "chunk_size" and "bounds_only" are supported, which default
78 to 1 and 0 respectively. Chunking reduces the number of IPC calls to
79 and from the shared-manager process for large sequences.
80
81 If "bounds_only => 1" is specified, the "next" method computes the
82 "begin" and "end" values only for the chunk and not the numbers in
83 between (hence boundaries only).
84
85 use MCE::Shared;
86
87 # demo 1
88
89 $seq1 = MCE::Shared->sequence(
90 { chunk_size => 10, bounds_only => 0 }, 1, 20
91 );
92
93 # @chunk = $seq1->next; # ( qw/ 1 2 3 4 5 6 7 8 9 10 / )
94 # @chunk = $seq1->next; # ( qw/ 11 12 13 14 15 16 17 18 19 20 / )
95
96 while ( my @chunk = $seq1->next ) {
97 ...
98 }
99
100 # demo 2
101
102 $seq2 = MCE::Shared->sequence(
103 { chunk_size => 10, bounds_only => 1 }, 1, 100
104 );
105
106 # ( $beg, $end ) = $seq2->next; # ( 1, 10 )
107 # ( $beg, $end ) = $seq2->next; # ( 11, 20 )
108 # ( $beg, $end ) = $seq2->next; # ( 21, 30 )
109 # ...
110 # ( $beg, $end ) = $seq2->next; # ( 81, 90 )
111 # ( $beg, $end ) = $seq2->next; # ( 91, 100 )
112
113 # The optional chunk_id value, starting at 1, applies to sequence
114 # objects configured with the bounds_only option set to a true
115 # value. API available since 1.834.
116
117 while ( my ( $beg, $end, $chunk_id ) = $seq2->next ) {
118 for my $i ( $beg .. $end ) {
119 ...
120 }
121 }
122
123 Parameters may be given later with "rewind" before calling "next".
124
125 # non-shared or local construction for use by a single process
126
127 use MCE::Shared::Sequence;
128
129 $seq = MCE::Shared::Sequence->new;
130 $seq->rewind( -1, 1, 0.1, "%4.1f" );
131
132 $seq = MCE::Shared::Sequence->new(
133 { chunk_size => 10, bounds_only => 1 }, 1, 100
134 );
135
136 # construction for sharing with other threads and processes
137
138 use MCE::Shared;
139
140 $seq = MCE::Shared->sequence;
141 $seq->rewind( 1, 100 );
142
143 $seq = MCE::Shared->sequence(
144 { chunk_size => 10, bounds_only => 1 }, 1, 100
145 );
146
147 next
148 Returns the next computed sequence(s). An undefined value is returned
149 when the computed "begin" value exceeds the value held by "end".
150
151 # default: { chunk_size => 1, bounds_only => 0 }
152 $seq = MCE::Shared->sequence( 1, 100 );
153
154 while ( defined ( my $num = $seq->next ) ) {
155 ...
156 }
157
158 # chunking
159
160 $seq = MCE::Shared->sequence(
161 { chunk_size => 10 }, 1, 100
162 );
163
164 while ( my @chunk = $seq->next ) {
165 ...
166 }
167
168 # chunking, boundaries only
169
170 $seq = MCE::Shared->sequence(
171 { chunk_size => 10, bounds_only => 1 }, 1, 100
172 );
173
174 while ( my ( $beg, $end, $chunk_id ) = $seq->next ) {
175 for my $i ( $beg .. $end ) {
176 ...
177 }
178 }
179
180 rewind ( { options }, begin, end [, step, format ] )
181 rewind ( begin, end [, step, format ] )
182 Sets the initial value back to the value held by "begin" when no
183 arguments are given. Otherwise, resets the sequence with given
184 criteria.
185
186 $seq->rewind;
187
188 $seq->rewind( { chunk_size => 10, bounds_only => 1 }, 1, 100 );
189
190 while ( my ( $beg, $end ) = $seq->next ) {
191 for my $i ( $beg .. $end ) {
192 ...
193 }
194 }
195
196 $seq->rewind( 1, 100 );
197
198 while ( defined ( my $num = $seq->next ) ) {
199 ...
200 }
201
203 MCE, MCE::Hobo, MCE::Shared
204
206 Mario E. Roy, <marioeroy AT gmail DOT com>
207
208
209
210perl v5.32.1 2021-01-27 MCE::Shared::Sequence(3)