-
Notifications
You must be signed in to change notification settings - Fork 3
/
rational-scanning.lisp
523 lines (377 loc) · 21.4 KB
/
rational-scanning.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
;;; -*- SYNTAX: COMMON-LISP; MODE: LISP; BASE: 10; PACKAGE: *SIM-i; MUSER: YES-*-
(in-package :*sim-i)
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;;>
;;;> The Thinking Machines *Lisp Simulator is in the public domain.
;;;> You are free to do whatever you like with it, including but
;;;> not limited to distributing, modifying, and copying.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
;;; The documentation for this stuff
;;; has been moved to segment-sets-doc.lisp
(*defstruct segment-set
(start-bits nil :type boolean)
(end-bits nil :type boolean)
(processor-not-in-any-segment nil :type boolean)
(start-address 0 :type (signed-byte 32) :cm-type (pvar (signed-byte (1+ *log-number-of-processors-limit*))))
(end-address 0 :type (signed-byte 32) :cm-type (pvar (signed-byte (1+ *log-number-of-processors-limit*))))
)
(*proclaim '(ftype (function (&rest t) (pvar segment-set)) create-segment-set!!))
(defun-wcefi create-segment-set!! (&key start-bit end-bit)
(assert (or (pvarp start-bit) (pvarp end-bit)) () "You must provide either a start or end bit pvar")
(if start-bit (assert (pvarp start-bit)))
(if end-bit (assert (pvarp end-bit)))
(let!! (segment-set-pvar)
(declare (type (pvar segment-set) segment-set-pvar))
(*all
(*setf (segment-set-start-bits!! segment-set-pvar) nil!!)
(*setf (segment-set-end-bits!! segment-set-pvar) nil!!)
(*setf (segment-set-processor-not-in-any-segment!! segment-set-pvar) t!!)
(*setf (segment-set-start-address!! segment-set-pvar) (!! -1))
(*setf (segment-set-end-address!! segment-set-pvar) (!! -1))
)
;; The user provided us only with start bits.
(cond
((and start-bit (not end-bit))
(*let (start)
(declare (type (pvar boolean) start))
(*nocompile (*set start start-bit))
(let (first-start-bit first-active-processor last-active-processor)
(setq first-active-processor (*min (self-address!!)))
(setq last-active-processor (*max (self-address!!)))
;; if there are no active processors then no segments
;; can be defined because no start-bits can be on.
(when first-active-processor
(*when start (setq first-start-bit (*min (self-address!!))))
;; if there is no start-bit on in the css then no segments
;; can be defined.
(when first-start-bit
;; put the start bits into the structure.
(*setf (segment-set-start-bits!! segment-set-pvar) start)
;; figure out where the end bits must go.
;; by definition the last active processor is the end
;; of the segment defined by the last start-bit.
;; Otherwise, except for the first start-bit, the
;; processor before any processor containing a start-bit
;; is the end of the previous segment.
(*setf (pref (segment-set-end-bits!! segment-set-pvar) last-active-processor) t)
(*when (and!! start (/=!! (self-address!!) (!! (the fixnum first-start-bit))))
(*nocompile
(*pset :no-collisions t!! (alias!! (segment-set-end-bits!! segment-set-pvar)) (1-!! (self-address!!)))
))
;; Turn on exactly those processors inside our newly defined segments.
(*when (and!! (>=!! (self-address!!) (!! (the fixnum first-start-bit)))
(<=!! (self-address!!) (!! (the fixnum last-active-processor)))
)
;; copy-scan the start-of-segment's address to all processors
;; in the segment.
(*setf (segment-set-start-address!! segment-set-pvar)
(scan!! (self-address!!) 'copy!!
:segment-pvar
(segment-set-start-bits!! (the (pvar segment-set) segment-set-pvar))
))
;; copy-scan the end-of-segment's address to all processors
;; in the segment.
(*setf (segment-set-end-address!! segment-set-pvar)
(scan!! (self-address!!) 'copy!!
:direction :backward
:segment-pvar
(segment-set-end-bits!! segment-set-pvar)
))
;; Identity all these processors as belonging to this
;; segment set.
(*setf (segment-set-processor-not-in-any-segment!! segment-set-pvar) nil!!)
))))))
((and end-bit (not start-bit))
(*let (end)
(declare (type (pvar boolean) end))
(*nocompile (*set end end-bit))
(let (last-end-bit first-active-processor last-active-processor)
(declare (ignore last-active-processor))
(setq first-active-processor (*min (self-address!!)))
;; (setq last-active-processor (*max (self-address!!)))
;; if there are no active processors then no segments
;; can be defined because no start-bits can be on.
(when first-active-processor
(*when end (setq last-end-bit (*max (self-address!!))))
;; if there is no end-bit on in the css then no segments
;; can be defined.
(when last-end-bit
;; put the end bits into the structure.
(*setf (segment-set-end-bits!! segment-set-pvar) end)
;; Figure out where the start bits must go.
;; By definition the first active processor is the beginning
;; of the segment defined by the first end-bit.
;; Otherwise, except for the last end-bit, the
;; processor after any processor containing an end-bit
;; is the start of the next segment.
(*setf (pref (segment-set-start-bits!! segment-set-pvar) first-active-processor) t)
(*when (and!! end (/=!! (self-address!!) (!! (the fixnum last-end-bit))))
(*nocompile
(*pset :no-collisions t!! (alias!! (segment-set-start-bits!! segment-set-pvar)) (1+!! (self-address!!)))
))
;; Turn on exactly those processors inside our newly defined segments.
(*when (and!! (<=!! (self-address!!) (!! (the fixnum last-end-bit)))
(>=!! (self-address!!) (!! (the fixnum first-active-processor)))
)
;; copy-scan the start-of-segment's address to all processors
;; in the segment.
(*setf (segment-set-start-address!! segment-set-pvar)
(scan!! (self-address!!) 'copy!!
:segment-pvar (the boolean-pvar (segment-set-start-bits!! segment-set-pvar))
))
;; copy-scan the end-of-segment's address to all processors
;; in the segment.
(*setf (segment-set-end-address!! segment-set-pvar)
(scan!! (self-address!!) 'copy!!
:direction :backward
:segment-pvar (the boolean-pvar (segment-set-end-bits!! segment-set-pvar))
))
;; Identity all these processors as belonging to this
;; segment set.
(*setf (segment-set-processor-not-in-any-segment!! segment-set-pvar) nil!!)
))))))
((and start-bit end-bit)
(*let (start end)
(declare (type (pvar boolean) start end))
(*all (*set start nil!!) (*set end nil!!))
(*nocompile
(*set start start-bit)
(*set end end-bit)
)
(let (first-active-processor #|last-active-processor|# first-start-bit last-start-bit first-end-bit last-end-bit)
(setq first-active-processor (*min (self-address!!)))
#|(setq last-active-processor (*max (self-address!!)))|#
(when first-active-processor
(*when start
(setq first-start-bit (*min (self-address!!)))
(setq last-start-bit (*max (self-address!!)))
)
(*when end
(setq first-end-bit (*min (self-address!!)))
(setq last-end-bit (*max (self-address!!)))
)
(when (or first-start-bit first-end-bit)
;; by definition, if you specify both start and end bits, they
;; must come in pairs.
(cond
((null first-start-bit) (error "There are no start bits in the css, but there are end bits"))
((null first-end-bit) (error "There are no end bits in the css, but there are start bits"))
((> first-start-bit first-end-bit) (error "The first end bit is before the first start bit"))
((> last-start-bit last-end-bit) (error "The last end bit is before the last start bit"))
(t
;; make sure the start and stop bits are interleaved.
(*all
;; Turn on every processor between the first start bit and the
;; last end bit inclusive.
(*when (and!! (>=!! (self-address!!) (!! (the fixnum first-start-bit)))
(<=!! (self-address!!) (!! (the fixnum last-end-bit)))
)
(*let (sum-scan-result scan-source)
(declare (type (pvar (signed-byte (1+ *current-send-address-length*))) sum-scan-result))
(declare (type (pvar (signed-byte 8)) scan-source))
;; Make a pvar with 1 in every start processor which
;; is not also an end processor, -1 in every end processor
;; which is not also a start processor, and 0 elsewhere.
(*set scan-source (!! 0))
(*when (and!! start (not!! end)) (*set scan-source (!! 1)))
(*when (and!! end (not!! start)) (*set scan-source (!! -1)))
;; Now scan across all the active processors.
;; The idea is that if there is an end processor after
;; every start processor then the end processors
;; will sum out to 0, and the start processors
;; will be unchanged.
(*set sum-scan-result (scan!! scan-source '+!!))
(*when (and!! start (not!! end))
(when (*or (/=!! (!! 1) sum-scan-result))
(error "There is no end-bit between two start bits. They must be interleaved.")
))
(*when (and!! end (not!! start))
(when (*or (/=!! (!! 0) sum-scan-result))
(error "There is not start bit between two end bits. They must be interleaved.")
))
)
;; Ok, each start processor has an end processor
;; either coincident with it or after it and
;; before the next start processor. Therefore
;; the start and end bits are valid and we can
;; store them away into our structure.
(*setf (segment-set-start-bits!! segment-set-pvar) start)
(*setf (segment-set-end-bits!! segment-set-pvar) end)
;; We want to determine regions which are not within
;; these (start - end) segments.
;; The active set is currently all processors between the first
;; start bit and the last end bit.
(*let ((inside-defined-segment? t!!))
(declare (type (pvar boolean) inside-defined-segment?))
(*when end (*set inside-defined-segment? nil!!))
;; this will cause every processor after and including each end processor
;; and before the next start processor to have a nil in
;; inside-defined-segment?.
(*set inside-defined-segment?
(scan!! inside-defined-segment? 'and!! :segment-pvar start)
)
(*when end (*set inside-defined-segment? t!!))
(*setf (segment-set-processor-not-in-any-segment!! segment-set-pvar) (not!! inside-defined-segment?))
)
(*when (not!! (the boolean-pvar (segment-set-processor-not-in-any-segment!! segment-set-pvar)))
;; copy-scan the start-of-segment's address to all processors
;; in the segment.
(*setf (segment-set-start-address!! segment-set-pvar)
(scan!! (self-address!!) 'copy!!
:segment-pvar (the boolean-pvar (segment-set-start-bits!! segment-set-pvar))
))
;; copy-scan the end-of-segment's address to all processors
;; in the segment.
(*setf (segment-set-end-address!! segment-set-pvar)
(scan!! (self-address!!) 'copy!!
:direction :backward
:segment-pvar (the boolean-pvar (segment-set-end-bits!! segment-set-pvar))
))
))))))))
))
)
segment-set-pvar
))
(defun-wcefi segment-set-scan!!
;; What does this do? We want to be able to execute a scan
;; inside a set of segments (which are each a set of contiguous processors)
;; previously defined using the create-segment-set!! function,
;; and do it independently of the existing css. The scan
;; is performed over all the processors in each segment.
;; An option is provided which allows the user to do the
;; scan operation only in those processors both active
;; when the function is entered and inside one of the
;; segments defined by the segent set. This option is
;; enabled using the :activate-all-processors-in-segment-set
;; keyword by giving it a value of NIL.
;; Unless told to do so, the routine will not check for active
;; processors outside the segment set. These processors will
;; not participate in any way in the scan operation. If told
;; to check and processors are found outside the segment set's
;; scope, the routine will error out.
(pvar
scan-operator
segment-set-pvar
&key
(direction :forward)
(check-for-processors-not-in-segment-set nil)
(activate-all-processors-in-segment-set t)
)
(*locally
(declare (type (pvar segment-set) segment-set-pvar))
;; Save away the CSS.
(*let (css-upon-entering-rational-scan!!)
(declare (type (pvar boolean) css-upon-entering-rational-scan!!))
(declare (return-pvar-p t))
(*all (*set css-upon-entering-rational-scan!! nil!!))
(*set css-upon-entering-rational-scan!! t!!)
(all!!
(when check-for-processors-not-in-segment-set
(when (*or (and!! (segment-set-processor-not-in-any-segment!! segment-set-pvar)
css-upon-entering-rational-scan!!
))
(error "There is an active processor not within the segment set defined by the segment-set-pvar")
))
;; Select exactly those processors defined to belong
;; to the segments in the segment set.
(when!! (not!! (segment-set-processor-not-in-any-segment!! segment-set-pvar))
(if activate-all-processors-in-segment-set
;; Do the scan over each segment.
(scan!!
pvar
scan-operator
:direction direction
:segment-pvar (if (eq direction :forward)
(segment-set-start-bits!! segment-set-pvar)
(segment-set-end-bits!! segment-set-pvar)
)
:include-self t
)
;; We need to activate only those processors both in the old CSS
;; and belonging to the segments in the segment set.
;; So we initialize a pvar to be 0 except in processors which
;; were active under the old CSS and are in the segment set;
;; these we set to 1.
(let!! ((sum-scan-result (!! 0)))
(declare (type (pvar (unsigned-byte *log-number-of-processors-limit*)) sum-scan-result))
(*when css-upon-entering-rational-scan!!
(declare (return-pvar-p nil))
(*set sum-scan-result (!! 1))
)
;; We need to find the first (or last, if we are backward scanning)
;; processor active under the old CSS in each segment. So we do
;; a sum scan, and every processor but the first (last) will have
;; something greater than 1. Clever, eh?
(if (eq direction :forward)
(*set sum-scan-result
(scan!!
sum-scan-result
'+!!
:direction :forward
:segment-pvar (segment-set-start-bits!! segment-set-pvar)
))
(*set sum-scan-result
(scan!!
sum-scan-result
'+!!
:direction :backwards
:segment-pvar (segment-set-end-bits!! segment-set-pvar)
))
)
;; the processor in each segment which has value 1 is the first (or last) active
;; processor in that segment and within css-upon-entering-rational-scan!!.
;; Use a pvar which is T for these processors and NIL otherwise as
;; the segment pvar to the regular scan function.
(when!! css-upon-entering-rational-scan!!
(let!! ((segment-pvar (=!! sum-scan-result (!! 1))))
(declare (type (pvar boolean) segment-pvar))
(scan!!
pvar
scan-operator
:direction direction
:segment-pvar segment-pvar
)
)))))))))
;(defun test-segment-sets ()
;
; (*let ((segment-set
; (create-segment-set!!
; :start-bit (zerop!! (mod!! (self-address!!) (!! 7)))
; :end-bit (=!! (!! 3) (mod!! (self-address!!) (!! 7)))
; )))
;
; (ppp (segment-set-start-bits!! segment-set) :end 30)
; (ppp (segment-set-end-bits!! segment-set) :end 30)
; (ppp (segment-set-processor-not-in-any-segment!! segment-set) :end 30)
; (ppp (segment-set-start-address!! segment-set) :end 30)
; (ppp (segment-set-end-address!! segment-set) :end 30)
;
; (*let ((temp (!! 0)))
; (declare (type (pvar (unsigned-byte 32)) temp))
; (*set temp (segment-set-scan!! (self-address!!) 'copy!! segment-set :direction :forward))
; (ppp temp :end 50)
; (*set temp (!! 0))
; (*when (oddp!! (self-address!!))
; (*set temp
; (segment-set-scan!!
; (self-address!!) 'copy!! segment-set :direction :backward :activate-all-processors-in-segment-set nil
; )))
; (ppp temp :end 50)
; ))
;
; (*when (not!! (zerop!! (self-address!!)))
; (let ((segment-set
; (create-segment-set!! :end-bit (=!! (!! 4) (mod!! (self-address!!) (!! 6))))
; ))
;
; (ppp (alias!! (segment-set-start-bits!! segment-set)) :end 30)
; (ppp (alias!! (segment-set-end-bits!! segment-set)) :end 30)
; (ppp (alias!! (segment-set-processor-not-in-any-segment!! segment-set)) :end 30)
; (ppp (alias!! (segment-set-start-address!! segment-set)) :end 30)
; (ppp (alias!! (segment-set-end-address!! segment-set)) :end 30)
;
; (ppp (segment-set-scan!! (self-address!!) 'copy!! segment-set :direction :backward) :end 40)
;
; )))