457
|
1 |
"{ Package: 'goodies/Parallelism-Futures' }"
|
|
2 |
|
62
|
3 |
" NAME Parallelism
|
67
|
4 |
AUTHOR tph@cs.man.ac.uk
|
|
5 |
FUNCTION throttled Futures; lazy eval; explicit pa'l'l procs
|
|
6 |
ST-VERSIONS stx
|
|
7 |
PREREQUISITES
|
|
8 |
CONFLICTS
|
|
9 |
DISTRIBUTION world
|
|
10 |
VERSION 1.1
|
|
11 |
DATE 22 Jan 1989
|
|
12 |
SUMMARY
|
62
|
13 |
Parallelism contains a number of explicitly parallel constructs,
|
|
14 |
including a new version of Future, Lazy evaluation, and explicit
|
|
15 |
parallel processes. Lots of code in here. Contains an early
|
|
16 |
version (read: doesn't work) of a ""throttled"" future mechanism.
|
|
17 |
New version RSN.(2.2).TPH
|
|
18 |
|
|
19 |
claus: I have separated the original Parallelism package into
|
|
20 |
individual ones: Lazy, Future, ThrottledFuture and ParallelEvaluation
|
|
21 |
"!
|
|
22 |
|
|
23 |
'From Smalltalk-80, version 2, of April 1, 1983 on 29 March 1987 at 5:13:27 pm'!
|
|
24 |
|
|
25 |
!Object methodsFor: 'parallel evaluation'!
|
|
26 |
|
|
27 |
touch
|
67
|
28 |
"Simply returns self. If the receiver is an uncompleted
|
|
29 |
Future or Lazy, this forces complete evaluation."
|
62
|
30 |
|
67
|
31 |
^self
|
62
|
32 |
! !
|
|
33 |
|
|
34 |
Object subclass: #Future
|
67
|
35 |
instanceVariableNames: 'result semaphore '
|
|
36 |
classVariableNames: ''
|
|
37 |
poolDictionaries: ''
|
|
38 |
category: 'Kernel-Processes'
|
62
|
39 |
!
|
|
40 |
|
|
41 |
Future comment:
|
|
42 |
'I represent an execution in progress. Any messages sent to me are delayed until
|
|
43 |
execution has completed.'
|
|
44 |
!
|
|
45 |
|
484
|
46 |
!Future class methodsFor:'documentation'!
|
|
47 |
|
|
48 |
copyright
|
|
49 |
"
|
|
50 |
This is a Manchester Goodie protected by copyright.
|
|
51 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
52 |
part of it which is separately transmitted or stored:
|
|
53 |
* You must ensure that every copy includes this notice, and that
|
|
54 |
source and author(s) of the material are acknowledged.
|
|
55 |
* These conditions must be imposed on anyone who receives a copy.
|
|
56 |
* The material shall not be used for commercial gain without the prior
|
|
57 |
written consent of the author(s).
|
|
58 |
Further information on the copyright conditions may be obtained by
|
|
59 |
sending electronic mail:
|
|
60 |
To: goodies-lib@cs.man.ac.uk
|
|
61 |
Subject: copyright
|
|
62 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
63 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
64 |
|
|
65 |
(C) Copyright 1992 University of Manchester
|
|
66 |
For more information about the Manchester Goodies Library (from which
|
|
67 |
this file was distributed) send e-mail:
|
|
68 |
To: goodies-lib@cs.man.ac.uk
|
|
69 |
Subject: help
|
|
70 |
"
|
|
71 |
!
|
|
72 |
|
|
73 |
documentation
|
|
74 |
"
|
|
75 |
I represent an execution in progress.
|
|
76 |
Any messages sent to me are delayed until execution has completed.'
|
|
77 |
"
|
|
78 |
!
|
|
79 |
|
|
80 |
examples
|
|
81 |
"
|
|
82 |
Starts evaluating the factorial immediately, but waits until
|
|
83 |
the result is available before printing the answer
|
|
84 |
[exBegin]
|
|
85 |
| fac |
|
|
86 |
|
|
87 |
fac := [100 factorial] futureValue.
|
|
88 |
Transcript showCR: 'evaluating factorial...'.
|
|
89 |
Transcript showCR: fac printString
|
|
90 |
[exEnd]
|
|
91 |
|
|
92 |
|
|
93 |
An example illustrating the use of multiple futures and
|
|
94 |
explicit resynchronisation.
|
|
95 |
|
|
96 |
Starts evaluating both factorials immediately, but waits until
|
|
97 |
both blocks have finished before continuing.
|
|
98 |
[exBegin]
|
|
99 |
| fac1 fac2 |
|
|
100 |
|
|
101 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 1000 factorial] futureValue.
|
|
102 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 2000 factorial] futureValue.
|
|
103 |
fac2 touch.
|
|
104 |
fac1 touch.
|
|
105 |
Transcript showCR: 'both completed.'.
|
|
106 |
[exEnd]
|
|
107 |
|
|
108 |
Example showing how arguments may be passed to futures.
|
|
109 |
[exBegin]
|
|
110 |
| temp |
|
|
111 |
|
|
112 |
temp := [:x :y | 10 * x * y] futureValue: 3 value: 4.
|
|
113 |
Transcript showCR: temp printString.
|
|
114 |
|
|
115 |
[exEnd]
|
|
116 |
|
|
117 |
Claus:
|
|
118 |
The above examples do not really show the power of Futures;
|
|
119 |
they can be useful, whenever some long-time computation is
|
|
120 |
to be done, and some other useful work can be done in the meanwhile.
|
|
121 |
for example:
|
|
122 |
|
|
123 |
Here, the input is read before - the readTime and view creation
|
|
124 |
times sum up:
|
|
125 |
[exBegin]
|
|
126 |
|p text v|
|
|
127 |
|
|
128 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
|
|
129 |
text := p contents.
|
|
130 |
p close.
|
|
131 |
v := TextView new openAndWait.
|
|
132 |
v contents:text
|
|
133 |
[exEnd]
|
|
134 |
|
|
135 |
The same here:
|
|
136 |
[exBegin]
|
|
137 |
|p text v|
|
|
138 |
|
|
139 |
v := TextView new openAndWait.
|
|
140 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
|
|
141 |
text := p contents.
|
|
142 |
p close.
|
|
143 |
v contents:text
|
|
144 |
[exEnd]
|
|
145 |
|
|
146 |
|
|
147 |
Here, the view creation and reading are done in parallel:
|
|
148 |
(if the user is slow when opening the view, the contents may
|
|
149 |
be already available)
|
|
150 |
[exBegin]
|
|
151 |
|p text v|
|
|
152 |
|
|
153 |
text := [ |p t|
|
|
154 |
|
|
155 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
|
|
156 |
t := p contents.
|
|
157 |
p close.
|
|
158 |
t
|
|
159 |
] futureValue.
|
|
160 |
v := TextView new openAndWait.
|
|
161 |
v contents:text
|
|
162 |
[exEnd]
|
|
163 |
"
|
|
164 |
!
|
|
165 |
|
|
166 |
version
|
|
167 |
^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.5 1997-01-16 13:45:49 cg Exp $'
|
|
168 |
! !
|
|
169 |
|
62
|
170 |
!Future methodsFor: 'synchronising'!
|
|
171 |
|
|
172 |
doesNotUnderstand: aMessage
|
67
|
173 |
"Any message to a Future will end up here."
|
62
|
174 |
|
67
|
175 |
semaphore wait. "Wait for evaluation to complete"
|
|
176 |
"(if not already completed)"
|
|
177 |
semaphore signal. "Wake up anything else that might be waiting"
|
|
178 |
^result perform: aMessage selector
|
|
179 |
withArguments: aMessage arguments
|
62
|
180 |
! !
|
|
181 |
|
|
182 |
!Future methodsFor: 'evaluating'!
|
|
183 |
|
|
184 |
block: aBlock
|
67
|
185 |
"Execute aBlock in parallel with whatever called me, but
|
|
186 |
ensure that any messages sent to me before execution
|
|
187 |
of the block has terminated are suspended until it has terminated."
|
62
|
188 |
|
67
|
189 |
semaphore := Semaphore new.
|
|
190 |
[result := aBlock value. semaphore signal] fork
|
62
|
191 |
!
|
|
192 |
|
|
193 |
block: aBlock value: aValue
|
67
|
194 |
"Execute aBlock in parallel with whatever called me, but
|
|
195 |
ensure that any messages sent to me before execution
|
|
196 |
of the block has terminated are suspended until it has terminated."
|
62
|
197 |
|
67
|
198 |
semaphore := Semaphore new.
|
|
199 |
[result := aBlock value: aValue. semaphore signal] fork
|
62
|
200 |
!
|
|
201 |
|
|
202 |
block: aBlock value: value1 value: value2
|
67
|
203 |
"Execute aBlock in parallel with whatever called me, but
|
|
204 |
ensure that any messages sent to me before execution
|
|
205 |
of the block has terminated are suspended until it has terminated."
|
62
|
206 |
|
67
|
207 |
semaphore := Semaphore new.
|
|
208 |
[result := aBlock value: value1 value: value2.
|
|
209 |
semaphore signal] fork
|
62
|
210 |
!
|
|
211 |
|
|
212 |
block: aBlock value: value1 value: value2 value: value3
|
67
|
213 |
"Execute aBlock in parallel with whatever called me, but
|
|
214 |
ensure that any messages sent to me before execution
|
|
215 |
of the block has terminated are suspended until it has terminated."
|
62
|
216 |
|
67
|
217 |
semaphore := Semaphore new.
|
|
218 |
[result := aBlock value: value1 value: value2 value: value3.
|
|
219 |
semaphore signal] fork
|
62
|
220 |
!
|
|
221 |
|
|
222 |
block: aBlock valueWithArguments: anArray
|
67
|
223 |
"Execute aBlock in parallel with whatever called me, but
|
|
224 |
ensure that any messages sent to me before execution
|
|
225 |
of the block has terminated are suspended until it has terminated."
|
62
|
226 |
|
67
|
227 |
semaphore := Semaphore new.
|
|
228 |
[result := aBlock valueWithArguments: anArray.
|
|
229 |
semaphore signal] fork
|
62
|
230 |
! !
|
|
231 |
|
|
232 |
!Future class methodsFor: 'class initialization'!
|
|
233 |
|
|
234 |
initialize
|
67
|
235 |
"must avoid the checks"
|
62
|
236 |
|
67
|
237 |
superclass := nil
|
62
|
238 |
|
67
|
239 |
"Future initialize."
|
62
|
240 |
! !
|
|
241 |
|
|
242 |
Future initialize!
|
|
243 |
|
|
244 |
!Block methodsFor: 'parallel evaluation'!
|
|
245 |
|
|
246 |
futureValue
|
67
|
247 |
"Fork a synchronised evaluation of myself. Starts the
|
|
248 |
evaluation in parallel immediately."
|
62
|
249 |
|
67
|
250 |
^Future new block: self
|
62
|
251 |
!
|
|
252 |
|
|
253 |
futureValue: aValue
|
67
|
254 |
"Fork a synchronised evaluation of myself. Starts the
|
|
255 |
evaluation in parallel immediately."
|
62
|
256 |
|
67
|
257 |
^Future new block: self value: aValue
|
62
|
258 |
!
|
|
259 |
|
|
260 |
futureValue: aValue value: anotherValue
|
67
|
261 |
"Fork a synchronised evaluation of myself. Starts the
|
|
262 |
evaluation in parallel immediately."
|
62
|
263 |
|
67
|
264 |
^Future new block: self value: aValue value: anotherValue
|
62
|
265 |
!
|
|
266 |
|
|
267 |
futureValue: aValue value: anotherValue value: bValue
|
67
|
268 |
"Fork a synchronised evaluation of myself. Starts the
|
|
269 |
evaluation in parallel immediately."
|
62
|
270 |
|
67
|
271 |
^Future new block: self value: aValue value: anotherValue value: bValue
|
62
|
272 |
!
|
|
273 |
|
|
274 |
futureValueWithArguments: anArray
|
67
|
275 |
"Fork a synchronised evaluation of myself. Starts the
|
|
276 |
evaluation in parallel immediately."
|
62
|
277 |
|
67
|
278 |
^Future new block: self valueWithArguments: anArray
|
62
|
279 |
!
|
|
280 |
|
|
281 |
parallelAnd: aBlock
|
67
|
282 |
"Executes the receiver in parallel with aBlock. Once both
|
|
283 |
have completed, perform a logical AND operation."
|
62
|
284 |
|
67
|
285 |
| first second |
|
|
286 |
first := self futureValue.
|
|
287 |
second := aBlock futureValue.
|
|
288 |
^first touch & second touch!
|
62
|
289 |
|
|
290 |
parallelEqv: aBlock
|
67
|
291 |
"Executes the receiver in parallel with aBlock. Once both
|
|
292 |
have completed, perform a logical equivalence (exclusive-NOR)
|
|
293 |
operation."
|
62
|
294 |
|
67
|
295 |
| first second |
|
|
296 |
first := self futureValue.
|
|
297 |
second := aBlock futureValue.
|
|
298 |
^first touch eqv: second touch!
|
62
|
299 |
|
|
300 |
parallelOr: aBlock
|
67
|
301 |
"Executes the receiver in parallel with aBlock. Once both
|
|
302 |
have completed, perform a logical OR operation."
|
62
|
303 |
|
67
|
304 |
| first second |
|
|
305 |
first := self futureValue.
|
|
306 |
second := aBlock futureValue.
|
|
307 |
^first touch | second touch!
|
62
|
308 |
|
|
309 |
parallelPerform: aSymbol with: aBlock
|
67
|
310 |
"Executes the receiver in parallel with aBlock. Once both
|
|
311 |
have completed, perform the operation given by aSymbol."
|
62
|
312 |
|
67
|
313 |
| first second |
|
|
314 |
first := self futureValue.
|
|
315 |
second := aBlock futureValue.
|
|
316 |
^first touch perform: aSymbol with: second touch!
|
62
|
317 |
|
|
318 |
parallelXor: aBlock
|
67
|
319 |
"Executes the receiver in parallel with aBlock. Once both
|
|
320 |
have completed, perform a logical equivalence (exclusive-NOR)
|
|
321 |
operation."
|
62
|
322 |
|
67
|
323 |
| first second |
|
|
324 |
first := self futureValue.
|
|
325 |
second := aBlock futureValue.
|
|
326 |
^first touch xor: second touch
|
62
|
327 |
! !
|
|
328 |
|
|
329 |
|
|
330 |
"COPYRIGHT.
|
|
331 |
The above file is a Manchester Goodie protected by copyright.
|
|
332 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
333 |
part of it which is separately transmitted or stored:
|
67
|
334 |
* You must ensure that every copy includes this notice, and that
|
|
335 |
source and author(s) of the material are acknowledged.
|
|
336 |
* These conditions must be imposed on anyone who receives a copy.
|
|
337 |
* The material shall not be used for commercial gain without the prior
|
|
338 |
written consent of the author(s).
|
62
|
339 |
Further information on the copyright conditions may be obtained by
|
|
340 |
sending electronic mail:
|
67
|
341 |
To: goodies-lib@cs.man.ac.uk
|
|
342 |
Subject: copyright
|
62
|
343 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
344 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
345 |
|
|
346 |
(C) Copyright 1992 University of Manchester
|
|
347 |
For more information about the Manchester Goodies Library (from which
|
|
348 |
this file was distributed) send e-mail:
|
67
|
349 |
To: goodies-lib@cs.man.ac.uk
|
|
350 |
Subject: help
|
62
|
351 |
"!
|