62
|
1 |
" NAME Parallelism
|
67
|
2 |
AUTHOR tph@cs.man.ac.uk
|
|
3 |
FUNCTION throttled Futures; lazy eval; explicit pa'l'l procs
|
|
4 |
ST-VERSIONS stx
|
|
5 |
PREREQUISITES
|
|
6 |
CONFLICTS
|
|
7 |
DISTRIBUTION world
|
|
8 |
VERSION 1.1
|
|
9 |
DATE 22 Jan 1989
|
|
10 |
SUMMARY
|
62
|
11 |
Parallelism contains a number of explicitly parallel constructs,
|
|
12 |
including a new version of Future, Lazy evaluation, and explicit
|
|
13 |
parallel processes. Lots of code in here. Contains an early
|
|
14 |
version (read: doesn't work) of a ""throttled"" future mechanism.
|
|
15 |
New version RSN.(2.2).TPH
|
|
16 |
|
|
17 |
claus: I have separated the original Parallelism package into
|
|
18 |
individual ones: Lazy, Future, ThrottledFuture and ParallelEvaluation
|
|
19 |
"!
|
|
20 |
|
|
21 |
'From Smalltalk-80, version 2, of April 1, 1983 on 29 March 1987 at 5:13:27 pm'!
|
|
22 |
|
|
23 |
!Object methodsFor: 'parallel evaluation'!
|
|
24 |
|
|
25 |
touch
|
67
|
26 |
"Simply returns self. If the receiver is an uncompleted
|
|
27 |
Future or Lazy, this forces complete evaluation."
|
62
|
28 |
|
67
|
29 |
^self
|
62
|
30 |
! !
|
|
31 |
|
|
32 |
'From Smalltalk-80, version 2, of April 1, 1983 on 29 March 1987 at 5:09:10 pm'!
|
|
33 |
|
|
34 |
Object subclass: #Lazy
|
67
|
35 |
instanceVariableNames: 'result startSemaphore endSemaphore '
|
|
36 |
classVariableNames: ''
|
|
37 |
poolDictionaries: ''
|
|
38 |
category: 'Kernel-Processes'!
|
62
|
39 |
Lazy comment:
|
|
40 |
'I represent an execution which may not be required. I will
|
|
41 |
not start execution until at least one message has been
|
|
42 |
received. The messages sent to me are delayed until execution
|
|
43 |
has completed.'!
|
|
44 |
|
|
45 |
|
|
46 |
!Lazy methodsFor: 'synchronising'!
|
|
47 |
|
|
48 |
doesNotUnderstand: aMessage
|
67
|
49 |
"Any message to a Lazy will end up here."
|
62
|
50 |
|
67
|
51 |
startSemaphore signal. "Start the evaluation."
|
|
52 |
endSemaphore wait. "Wait until evaluation completed."
|
|
53 |
endSemaphore signal. "Wake up anything else."
|
|
54 |
^result perform: aMessage selector withArguments: aMessage arguments
|
|
55 |
"Perform the message, having re-synchronised."! !
|
62
|
56 |
|
|
57 |
!Lazy methodsFor: 'evaluating'!
|
|
58 |
|
|
59 |
block: aBlock
|
67
|
60 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
61 |
to me before execution of the block has terminated are
|
|
62 |
suspended until it has terminated. Do not start the evaluation
|
|
63 |
until at least one message has been sent to the receiver."
|
62
|
64 |
|
67
|
65 |
startSemaphore := Semaphore new.
|
|
66 |
endSemaphore := Semaphore new.
|
|
67 |
[startSemaphore wait.
|
|
68 |
result := aBlock value.
|
|
69 |
endSemaphore signal] fork!
|
62
|
70 |
|
|
71 |
block: aBlock value: aValue
|
67
|
72 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
73 |
to me before execution of the block has terminated are
|
|
74 |
suspended until it has terminated. Do not start the evaluation
|
|
75 |
until at least one message has been sent to the receiver."
|
62
|
76 |
|
67
|
77 |
startSemaphore := Semaphore new.
|
|
78 |
endSemaphore := Semaphore new.
|
|
79 |
[startSemaphore wait.
|
|
80 |
result := aBlock value: aValue.
|
|
81 |
endSemaphore signal] fork!
|
62
|
82 |
|
|
83 |
block: aBlock value: value1 value: value2
|
67
|
84 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
85 |
to me before execution of the block has terminated are
|
|
86 |
suspended until it has terminated. Do not start the evaluation
|
|
87 |
until at least one message has been sent to the receiver."
|
62
|
88 |
|
67
|
89 |
startSemaphore := Semaphore new.
|
|
90 |
endSemaphore := Semaphore new.
|
|
91 |
[startSemaphore wait.
|
|
92 |
result := aBlock value: value1 value: value2.
|
|
93 |
endSemaphore signal] fork!
|
62
|
94 |
|
|
95 |
block: aBlock value: value1 value: value2 value: value3
|
67
|
96 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
97 |
to me before execution of the block has terminated are
|
|
98 |
suspended until it has terminated. Do not start the evaluation
|
|
99 |
until at least one message has been sent to the receiver."
|
62
|
100 |
|
67
|
101 |
startSemaphore := Semaphore new.
|
|
102 |
endSemaphore := Semaphore new.
|
|
103 |
[startSemaphore wait.
|
|
104 |
result := aBlock value: value1 value: value2 value: value3.
|
|
105 |
endSemaphore signal] fork!
|
62
|
106 |
|
|
107 |
block: aBlock valueWithArguments: anArray
|
67
|
108 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
109 |
to me before execution of the block has terminated are
|
|
110 |
suspended until it has terminated. Do not start the evaluation
|
|
111 |
until at least one message has been sent to the receiver."
|
62
|
112 |
|
67
|
113 |
startSemaphore := Semaphore new.
|
|
114 |
endSemaphore := Semaphore new.
|
|
115 |
[startSemaphore wait.
|
|
116 |
result := aBlock valueWithArguments: anArray.
|
|
117 |
endSemaphore signal] fork! !
|
62
|
118 |
|
|
119 |
!Lazy class methodsFor: 'class initialization'!
|
|
120 |
|
|
121 |
initialize
|
67
|
122 |
"must avoid the checks"
|
62
|
123 |
|
67
|
124 |
superclass := nil
|
62
|
125 |
|
67
|
126 |
"Lazy initialize."! !
|
62
|
127 |
|
|
128 |
!Lazy class methodsFor: 'examples'!
|
|
129 |
|
|
130 |
example1
|
67
|
131 |
"Evaluates the factorial, starting only when the
|
|
132 |
result is actually required (when printString is sent)."
|
62
|
133 |
|
67
|
134 |
| fac |
|
|
135 |
fac := [100 factorial] futureValue.
|
351
|
136 |
Transcript showCR: 'Doing nothing. '.
|
67
|
137 |
(Delay forSeconds: 2) wait.
|
351
|
138 |
Transcript showCR: fac printString
|
62
|
139 |
|
67
|
140 |
"Lazy example1"!
|
62
|
141 |
|
|
142 |
example2
|
67
|
143 |
"Starts evaluating both factorials only when required (by the touch),
|
|
144 |
and waits until both blocks have finished before continuing."
|
62
|
145 |
|
67
|
146 |
| fac1 fac2 |
|
351
|
147 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 100 factorial] lazyValue.
|
|
148 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 120 factorial] lazyValue.
|
67
|
149 |
fac2 touch.
|
|
150 |
fac1 touch.
|
351
|
151 |
Transcript showCR: 'both completed.'.
|
62
|
152 |
|
67
|
153 |
"Lazy example2"!
|
62
|
154 |
|
|
155 |
example3
|
67
|
156 |
"Demonstrates how to pass arguments to a lazy evaluation block."
|
62
|
157 |
|
67
|
158 |
| temp |
|
|
159 |
temp := [:x :y :z | x * y * z] lazyValueWithArguments: #(2 3 4).
|
351
|
160 |
Transcript showCR: temp printString.
|
62
|
161 |
|
67
|
162 |
"Lazy example3"! !
|
62
|
163 |
|
|
164 |
Lazy initialize!
|
|
165 |
|
|
166 |
!Block methodsFor: 'parallel evaluation'!
|
|
167 |
|
|
168 |
lazyValue
|
67
|
169 |
"Fork a synchronised evaluation of myself. Only starts
|
|
170 |
the evaluation when the result is requested."
|
62
|
171 |
|
67
|
172 |
^Lazy new block: self!
|
62
|
173 |
|
|
174 |
lazyValue: aValue
|
67
|
175 |
"Fork a synchronised evaluation of myself. Only starts
|
|
176 |
the evaluation when the result is requested."
|
62
|
177 |
|
67
|
178 |
^Lazy new block: self value: aValue!
|
62
|
179 |
|
|
180 |
lazyValue: aValue value: anotherValue
|
67
|
181 |
"Fork a synchronised evaluation of myself. Only starts
|
|
182 |
the evaluation when the result is requested."
|
62
|
183 |
|
67
|
184 |
^Lazy new block: self value: aValue value: anotherValue!
|
62
|
185 |
|
|
186 |
lazyValue: aValue value: anotherValue value: bValue
|
67
|
187 |
"Fork a synchronised evaluation of myself. Only starts
|
|
188 |
the evaluation when the result is requested."
|
62
|
189 |
|
67
|
190 |
^Lazy new block: self value: aValue value: anotherValue value: bValue!
|
62
|
191 |
|
|
192 |
lazyValueWithArguments: anArray
|
67
|
193 |
"Fork a synchronised evaluation of myself. Only starts
|
|
194 |
the evaluation when the result is requested."
|
62
|
195 |
|
67
|
196 |
^Lazy new block: self valueWithArguments: anArray
|
62
|
197 |
! !
|
|
198 |
|
|
199 |
"COPYRIGHT.
|
|
200 |
The above file is a Manchester Goodie protected by copyright.
|
|
201 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
202 |
part of it which is separately transmitted or stored:
|
67
|
203 |
* You must ensure that every copy includes this notice, and that
|
|
204 |
source and author(s) of the material are acknowledged.
|
|
205 |
* These conditions must be imposed on anyone who receives a copy.
|
|
206 |
* The material shall not be used for commercial gain without the prior
|
|
207 |
written consent of the author(s).
|
62
|
208 |
Further information on the copyright conditions may be obtained by
|
|
209 |
sending electronic mail:
|
67
|
210 |
To: goodies-lib@cs.man.ac.uk
|
|
211 |
Subject: copyright
|
62
|
212 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
213 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
214 |
|
|
215 |
(C) Copyright 1992 University of Manchester
|
|
216 |
For more information about the Manchester Goodies Library (from which
|
|
217 |
this file was distributed) send e-mail:
|
67
|
218 |
To: goodies-lib@cs.man.ac.uk
|
|
219 |
Subject: help
|
62
|
220 |
"!
|