-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcogen-memo-client.scm
424 lines (381 loc) · 13.6 KB
/
cogen-memo-client.scm
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
;;; $Id $
;;; memoization client for distributed PE
;;;
;;; memo function stuff
(define-syntax start-memo
(syntax-rules ()
((_ level fn bts args)
(start-memo-internal level 'fn fn bts args))
((_ level fn bts args new-goal)
(start-memo-internal level 'fn fn bts args))))
(define (nextlevel memo-template args . new-goal)
(let ((level (list-ref memo-template 1))
(goal-proc (list-ref memo-template 3))
(bts (cadr (list-ref memo-template 4))))
(apply start-memo-internal level
goal-proc
(eval goal-proc (interaction-environment))
bts
args
new-goal)))
(define (start-memo-internal level fname fct bts args . new-goal)
(clear-residual-program!)
(clear-memolist!)
(clear-support-code!)
(gensym-reset!)
(let* ((result
(with-fresh-gensym-local
(lambda ()
(reset (multi-memo level fname fct bts args)))))
(result (if (and (pair? result) (eq? (car result) 'LET))
(car (cdaadr result))
result))
(goal-proc (car *residual-program*))
(defn-template (take 2 goal-proc))
;; kludge alert
(defn-template
(if (null? new-goal)
defn-template
(list (car defn-template)
(cons (car new-goal) (cdadr defn-template)))))
(defn-body (list-tail goal-proc 2)))
(set-residual-program!
(list (append defn-template
(cdr *residual-program*)
defn-body)))
result))
;;; the memo-function
;;; - fn is the name of the function to memoize
;;; - args are the free variables of the function's body
;;; - bts are their binding times
(define (multi-memo level fname fct bts args)
(let* ((full-pp (cons fname args))
(pp (top-project-static full-pp bts))
(dynamics (top-project-dynamic full-pp bts))
(actuals (apply append dynamics))
(entry (local-cache-enter! full-pp pp bts fct))
(res-name (entry->name entry)))
(I-register-memo-point! full-pp res-name bts fct)
(if (= level 1)
;; generate call to fn with actual arguments
(_complete-serious
(apply make-residual-call res-name actuals))
;; reconstruct multi-memo
(_complete-serious
(make-residual-call 'MULTI-MEMO
(- level 1)
`',res-name
res-name
`',(binding-times dynamics)
`(LIST ,@actuals))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; distributed stuff
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This assumes all clients are running on different Kalis!
(define client-server-aspace)
(define (I-am-unemployed)
(remote-run! client-server-aspace
client-is-unemployed
(local-aspace)))
(define (I-register-memo-point! program-point local-name bts fct)
(remote-run! client-server-aspace
client-registers-memo-point!
(local-aspace) program-point local-name bts fct))
(define (can-I-work-on? local-name) ;synchronous
(remote-apply client-server-aspace
can-client-work-on? (local-aspace) local-name))
(define (I-am-working-on local-name) ;asynchronous
(remote-run! client-server-aspace
client-working-on (local-aspace) local-name))
(define (client-initialize! aspace)
(set! client-server-aspace aspace)
(local-cache-initialize!)
(local-pending-initialize!)
(local-resid-initialize!)
(set! client-status-lock (make-lock))
(I-am-unemployed))
;; messages sent from client to master
(define (client-working-on aspace local-name) ;async
; make sure that local-name is already registered with the master
(if (can-client-work-on? aspace local-name)
'nothing-to-do
(remote-run! aspace spec-client-process-kill local-name)))
(define (can-client-work-on? aspace local-name) ;sync
(let loop ((master-cache *master-cache*) (final? null?))
(cond
((master-cache-lookup-by-local-name local-name final?)
=> (lambda (entry)
(master-entry->add-local-name! entry local-name aspace)
(with-lock master-cache-lock
(lambda ()
(if (master-entry->mark entry)
#f
(begin
(master-entry->mark! entry #t)
#t))))))
(else ;wait until local name registered
(relinquish-timeslice)
(loop *master-cache* (lambda (item) (eq? item master-cache)))))))
;; finfo-table is a mapping from function names to the binding times
;; of their arguments and the function itself, e.g. a list of lists:
;; (list 'fname '(0 1 0) fname)
;; !!! THIS STRUCTURE DOES NOT EXIST SO FAR !!!
;; [does it pay off?]
(define *finfo-table* (make-proxy #f))
(define (proxy-assoc key proxy-alist)
(assoc key (proxy-local-ref proxy-alist)))
(define (finfo->bts finfo)
(cadr finfo))
(define (finfo->fct finfo)
(caddr finfo))
(define (spec-client-process master-entry)
(let* ((res-name (master-entry->name master-entry))
(program-point (master-entry->program-point master-entry))
(fname (car program-point))
(finfo (proxy-assoc fname *finfo-table*))
(bts (finfo->bts finfo))
(fct (finfo->fct finfo))
(static-skeleton (top-project-static program-point bts)))
;;assume master-cache knows about this specialization
(let loop ((entry
(local-cache-insert! res-name program-point static-skeleton bts fct)))
(specialize-entry entry)
(let inner-loop ()
(if (local-pending-empty?)
'terminate
(let* ((entry (local-cache-advance!))
(token (can-i-work-on? (entry->name entry))))
(if token
(loop entry)
(inner-loop))))))))
(define *client-status* #f)
(define client-status-lock)
(define (set-client-status! status)
(with-lock client-status-lock
(lambda () (set! *client-status* status))))
(define (spec-client-process-async entry)
(set-client-status! #f)
(let loop ((entry entry)) ;assume master-cache knows about this specialization
(specialize-entry entry)
(let inner-loop ()
(set-client-status! #f)
(if (local-pending-empty?)
'terminate
(let* ((entry (local-cache-advance!))
(local-name (entry->name entry)))
(set-client-status! (make-status local-name (current-thread) inner-loop))
(i-am-working-on local-name)
(specialize-entry entry)
(inner-loop))))))
(define (spec-client-process-kill local-name)
(obtain-lock client-status-lock)
(if *client-status*
(if (eq? local-name (status->name *client-status*))
(begin
(kill-thread! (status->thread *client-status*))
(release-lock client-status-lock)
((status->thunk *client-status*)))
(release-lock client-status-lock))
(release-lock client-status-lock)))
(define (specialize-entry entry)
(let* ((full-pp (entry->pp entry))
(res-name (entry->name entry))
(bts (entry->bts entry))
(fct (entry->fct entry)))
(with-fresh-gensym-local
(lambda ()
(let* ((cloned-pp (top-clone-dynamic full-pp bts))
(new-formals (apply append (top-project-dynamic cloned-pp bts))))
(make-residual-definition! res-name
new-formals
(reset (apply fct (cdr cloned-pp)))))))))
;;; cache entries:
;;; ->pp - the full program point
;;; ->static - the static skeleton of pp
;;; ->name - the generated unique name
;;; ->bts - the list of toplevel binding times
;;; ->fct - the function to be applied
;;; ->mark - local mark for advance
(define *local-cache*) ;no proxy necessary
(define *local-pending*)
(define (local-cache-initialize!)
(set! *local-cache* '()))
(define (local-pending-initialize!)
(set! *local-pending* '()))
(define (local-pending-empty?)
(null? *local-pending*))
(define (local-cache-enter! full-pp pp bts fct)
(let* ((fname (car full-pp)) ;name of the function; a symbol
(found
(or (assoc pp *local-cache*)
(assoc pp *local-pending*)
(let ((new-item (cons pp (make-entry full-pp pp (gensym fname) bts fct #f))))
(set! *local-pending*
(cons new-item *local-pending*))
new-item))))
(cdr found)))
(define (local-cache-insert! res-name full-pp pp bts fct)
(let ((entry (make-entry full-pp pp res-name bts fct #t)))
(set! *local-cache*
(cons entry *local-cache*))
entry))
(define (local-cache-advance!)
(let ((pending *local-pending*))
(if (null? pending)
'terminate-this-client
(let* ((item (car pending))
(entry (cdr item)))
(set! *local-pending* (cdr pending))
(set! *local-cache* (cons item *local-cache*))
entry))))
;;; residual code
(define *local-resid* (make-proxy #f))
(define (local-resid-initialize!)
(proxy-local-set! *local-resid* '()))
(define (make-residual-definition! name formals body)
(let ((item `(DEFINE (,name ,@formals) ,body)))
(proxy-local-set! *local-resid* (cons item (proxy-local-ref *local-resid*)))))
;;; export interface:
;;; spec-client-initialize!
;;; spec-client-process
;;; spec-client-process-async
;;; utilities
(define (encap proc)
(let ((aspace (local-aspace)))
(lambda args
(apply remote-apply aspace proc args))))
(define (with-lock lock thunk)
(obtain-lock lock)
(let ((value (thunk)))
(release-lock lock)
value))
;;; the pending list
(define *master-pending* 'undefined)
(define *master-pending-placeholders*)
(define (master-pending-initialize!)
(set! *master-pending* (cons '***HEAD*** '()))
(set! *master-pending-placeholders* '()))
(define (master-pending-empty?)
(null? (cdr *master-pending*)))
(define (master-pending-add! key entry)
(if (null? *master-pending-placeholders*)
(set-cdr! *master-pending*
(cons (cons key entry) (cdr *master-pending*)))
(begin ;locking?
(let ((placeholder (car *master-pending-placeholders*)))
(set! *master-pending-placeholders*
(cdr *master-pending-placeholders*))
(placeholder-set! placeholder entry)))))
(define (master-pending-lookup! key)
(let loop ((previous *master-pending*)
(current (cdr *master-pending*)))
(if (equal? key (caar current))
(begin
(set-cdr! previous (cdr current))
(cdar current))
(loop current (cdr current)))))
(define (master-pending-advance!)
(if (master-pending-empty?)
#f
(let ((entry (cdadr *master-pending*)))
(set-cdr! *master-pending* (cddr *master-pending*))
entry)))
(define (master-pending-sign-up! placeholder)
(set! *master-pending-placeholders*
(cons placeholder *master-pending-placeholders*))
(length *master-pending-placeholders*))
;;; the cache
(define *master-cache* 'undefined)
(define (master-cache-initialize!)
(set! *master-cache* '()))
(define (master-cache-add! key entry)
(set! *master-cache* (cons (cons key entry) *master-cache*)))
(define (master-cache-lookup key)
(cond ((assoc key *master-cache*) => cdr)
(else #f)))
(define (master-cache-lookup-by-local-name local-name final?)
(let loop ((master-cache *master-cache*))
(and (not (final? master-cache))
(let* ((master-entry (cdar master-cache))
(names+aspaces (master-entry->names+aspaces master-entry)))
(or (and (assoc local-name names+aspaces) master-entry)
(loop (cdr master-cache)))))))
(define master-cache-lock (make-lock))
(define master-pending-lock (make-lock))
(define master-placeholders-lock (make-lock)) ;necessary?
(define *unemployed-clients*)
(define (start-specialization client-aspaces
level fname fct bts args)
(master-cache-initialize!)
(master-pending-initialize!)
(set! *unemployed-clients* (make-placeholder))
(for-each (lambda (id aspace)
(remote-run! aspace client-initialize! aspace))
client-aspaces)
(let* ((program-point (cons fname args))
(static-skeleton (top-project-static program-point bts))
(new-name (gensym fname)))
(client-registers-memo-point! (car client-aspaces) program-point new-name bts fct))
;; detect termination
(let loop ((nr-unemployed (placeholder-value *unemployed-clients*))
(nr-of-clients (length client-aspaces)))
(if (not (= nr-unemployed nr-of-clients))
(loop (placeholder-value *unemployed-clients*)
nr-of-clients)))
;; collect residual program
(apply append
(map (lambda (aspace) (remote-apply aspace proxy-local-ref *local-resid*))
client-aspaces)))
(define (client-registers-memo-point! client-aspace
program-point local-name
bts fct)
(let ((static-skeleton (top-project-static program-point bts)))
(cond
((master-cache-lookup static-skeleton)
=> (lambda (entry)
(master-entry->add-local-name! entry local-name client-aspace)))
(else
(let ((entry
(make-master-entry program-point ; key
local-name ; global name
(list (list local-name client-aspace)) ; an alist
#f ; processed?
)))
(with-lock
master-cache-lock
(lambda () (master-cache-add! static-skeleton entry)))
(with-lock
master-pending-lock
(lambda () (master-pending-add! static-skeleton entry))))))))
(define (client-is-unemployed client-aspace)
(let ((entry (with-lock master-pending-lock
(lambda () (master-pending-advance!)))))
(if entry
(remote-run! client-aspace
spec-client-process
entry)
(let* ((entry-placeholder (make-placeholder))
(unemployed-clients
(with-lock master-pending-lock
(lambda () (master-pending-sign-up!
entry-placeholder)))))
(placeholder-set! *unemployed-clients* unemployed-clients)
(set! *unemployed-clients* (make-placeholder))
;; wait for next task
(remote-run! client-aspace
spec-client-process
(placeholder-value entry-placeholder))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; data structures
;;; entries in the local cache
(define-record entry
(pp static name bts fct mark))
;;; entries in the global cache
(define-record master-entry
(program-point name names+aspaces mark))
(define (master-entry->add-local-name! master-entry local-name aspace)
(let ((names+aspaces (master-entry->names+aspaces master-entry)))
(master-entry->names+aspaces! master-entry (cons (list local-name aspace) names+aspaces))))
;;; a status record
(define-record status
(name thread thunk))