~old/guile-parallel

Parallelism for Guile

c55229b Use new build system

~old pushed to ~old/guile-parallel git

1 year, 9 days ago

e83091b Swith license to LGPL-2.1-or-later

~old pushed to ~old/guile-parallel git

1 year, 2 months ago

Guix: builds.sr.ht status

#Guile parallel

High level structures and procedures for parallelism.

#(parallel)

Misc things.

(use-modules
 (parallel))

(define (get-value x)
  (sleep x)
  (format #t "getting value: ~a\n" x)
  x)

(pcall + (get-value 1) (get-value 2))
(pcall + (get-value 2) (get-value 1))

#(parallel atomic)

Extension of (ice-9 atomic), offering more operations.

(use-modules
 (parallel atomic))

(define (make-atomic-counter)
  (let ((counter (make-atomic-box 0)))
    (lambda ()
      (atomic-box-fetch-and-add! counter 1))))

#(parallel barrier)

Pthread like barrier.

(use-modules
 (ice-9 threads)
 (parallel barrier))

(letrec ((barrier (make-barrier 2))
         (thread (begin-thread
                  (barrier)
                  (display "thread")
                  (newline))))
  (sleep 1)
  (display "not in thread")
  (newline)
  (barrier)
  (join-thread thread))

#(parallel futures)

Same as (ice-9 futures) but with work stealing workers.

(use-modules
 (parallel futures))

(pk
 (touch (future~ (+ 1 1))))

#(parallel queue)

Wrapper around (ice-9 q).

(use-modules
 (ice-9 threads)
 (srfi srfi-26)
 (parallel queue))

(define Q (make-queue))
(define thread
  (begin-thread
   (let loop ()
     (let ((elem (pop-queue! Q)))
       (display elem)
       (newline))
     (loop))))

(for-each
 (cut push-queue! Q <>)
 (iota 100))

#(parallel scheduler)

Thread pool scheduler. One can create as many pool wanted. There's also the eventually~ syntax that schedules expression on a globally defined pool.

(use-modules
 (ice-9 threads)
 (parallel schedule))

(eventually~
 (display "foo")
 (newline))

(let ((scheduler
       (make-scheduler
        #:name "my-scheduler"
        #:max-thread (/ (current-process-count) 2)
        #:port-if-error (open-output-file "my-scheduler.out"))))
  (schedule-job~ scheduler
                 (lambda ()
                   (display "my job")
                   (newline)))
  (scheduler-job/soon~ scheduler
                       (lambda ()
                         (display "Maybe this will run before my job!")
                         (newline))))

#(parallel semaphore)

Pthread like semaphore.

(use-modules
 (ice-9 threads)
 (parallel semaphore))

(define sem (make-semaphore 0))
(define thread
  (begin-thread
   (wait-semaphore sem)
   (display "wait!")
   (newline)))

(display "post!")
(newline)
(post-semaphore sem)

(join-thread thread)

#(parallel vector)

Do computation on vector like with SRFI-43, but in parallel.

(use-modules
 ((rnrs sorting) #:select (vector-sort))
 (srfi srfi-43)
 (parallel vector))

(define vector (vector-unfold values 100))

(pk
 (parallel-vector-count even? vector))

(pk
 (equal?
  (vector-map (lambda (_ x) (1+ x)) vector)
  (parallel-vector-map 1+ vector)))

(pk
 (=
  (/ (* 100 99) 2)
  (parallel-vector-map-reduce + vector)))

(pk
 (let ((v (vector-map (lambda _ (random 100)) vector)))
   (equal?
    (vector-sort < v)
    (paralle-vector-sort < v))))