How to map function above list in parallel in racket?

The title of the question says it all: what is the best way to map a function over a list in parallel in a racket? Thanks.

+7
source share
2 answers

If you mean multiple processor cores, then the most common approach is to use Places .

Locations allow the development of parallel programs that take advantage of machines with multiple processors, cores, or hardware threads.

Location is a parallel task that is actually a separate instance of the Racket virtual machine. Places exchange place channels, which are the endpoints for two-way buffered communications.

You may be able to use another parallelization technique, Futures , but the conditions for its operation are relatively limited, for example, floating point operations, as described here .


EDIT : in response to the comment:

Is there an implementation of a parallel card somewhere somewhere?

First, I have to make a backup. You may not need a place. You can get concurrency using Racket streams. For example, here map/thread :

 #lang racket (define (map/thread f xs) ;; Make one channel for each element of xs. (define cs (for/list ([x xs]) (make-channel))) ;; Make one thread for each elemnet of xs. ;; Each thread calls (fx) and puts the result to its channel. (for ([x xs] [c cs]) (thread (thunk (channel-put c (fx))))) ;; Get the result from each channel. ;; Note: This will block on each channel if not yet ready. (for/list ([c cs]) (channel-get c))) ;; Use: (define xs '(1 2 3 4 5)) (map add1 xs) (map/thread add1 xs) 

If the work in progress involves blocking, for example. I / O requests, this will give you "parallelism" in the sense that you are not stuck on I / O. However, Racket threads are green threads, so only one will use the CPU at a time.

If you really need parallel use of multiple processor cores, you will need futures or places.

Due to the fact that Places are implemented - effectively, like several instances of Racket, I do not immediately see how to write a common map/place . For examples of using seats using the "custom" method, see:

+9
source

I do not know in Racket, but I have implemented the version in SISC Scheme .

 (define (map-parallel fn lst) (call-with-values (lambda () (apply parallel (map (lambda (e) (delay (fn e))) lst))) list)) 

Only parallel not R5RS.

Usage example:

Using a regular map :

 (time (map (lambda (a) (begin (sleep 1000) (+ 1 a))) '(1 2 3 4 5))) => ((2 3 4 5 6) (5000 ms)) 

Using map-parallel :

 (time (map-parallel (lambda (a) (begin (sleep 1000) (+ 1 a))) '(1 2 3 4 5))) => ((2 3 4 5 6) (1000 ms)) 
+2
source

All Articles