If an example would be helpful, here's a toy implementation of
`parallel-set-map` that uses `serial-lambda` to send the user-supplied
function across the place-channels:
#lang racket
(require web-server/lang/serial-lambda
racket/serialize
)
(define (place-printf pch fmt . args)
(place-channel-put pch (apply format fmt args)))
(define (spawn-worker id pch print-pch srl-proc)
(place/context _
(define proc
(deserialize srl-proc))
(let loop ()
(let* ([v (sync pch)]
[rslt (proc v)])
(place-printf print-pch "place ~a:\t~a -> ~a\n" id v rslt)
(place-channel-put pch rslt)
(loop)))))
(define/contract (parallel-set-map proc st send-print-pch)
(-> (and/c serializable? (-> any/c any/c))
(set/c any/c)
place-channel?
any/c)
(define-values {manager-pch worker-pch}
(place-channel))
(define workers
(for/list ([id (in-range 2)])
(spawn-worker id worker-pch send-print-pch (serialize proc))))
(define count
(for/sum ([v (in-set st)])
(place-channel-put manager-pch v)
1))
(define results
(let loop ([so-far (set)]
[i 1])
(define st
(set-add so-far (sync manager-pch)))
(if (= i count)
st
(loop st (add1 i)))))
(for-each place-kill workers)
results)
(define (try-it)
(define example-set
(set 1 2 3))
(define-values {get-print-pch send-print-pch}
(place-channel))
(thread (λ ()
(let loop ()
(write-string (sync get-print-pch) (current-output-port))
(loop))))
(place-printf send-print-pch
"~v\n"
(parallel-set-map (serial-lambda (x)
(+ x x))
example-set
send-print-pch))
(place-printf send-print-pch
"~v\n"
(parallel-set-map (serial-lambda (x)
(* x x))
example-set
send-print-pch)))
-Philip
On Sun, Apr 15, 2018 at 3:08 PM, Philip McGrath <[email protected]>
wrote:
> On Sun, Apr 15, 2018 at 2:51 PM, Zelphir Kaltstahl <
> [email protected]> wrote:
>
>> Having to write all things in terms of where things come from like in:
>>
>> > '([racket/base +] . [1 2])
>>
>> is not ergonomic at all.
>>
>
> Absolutely! To be clear, I was not suggesting that you use that format in
> practice: I was trying to illustrate part of the low-level mechanism by
> which `serial-lambda` (and `racket/serialize` in general) work.
>
> Using `serial-lambda` does all of the difficult accounting for you to make
> sure the right function from the right module is there when you deserialize
> it and to arrange for the serializable procedure to take its lexical
> environment along with it. You do have to be sure that you're dealing with
> pure functions and serializable data structures, but you can use it as a
> drop-in replacement for `lambda` and get surprisingly far before you have
> to think about any of the details.
>
--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.