Last active
January 10, 2019 18:08
-
-
Save b4284/f16df10da2dc70005e241278f0f6fda2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use-modules (srfi srfi-1) | |
(ice-9 threads) | |
(ice-9 futures)) | |
(define (matrix-transpose a) | |
;; ((a1 a2 ... ax) ((a1 b1 c1) | |
;; (b1 b2 ... bx) => (a2 b2 c2) | |
;; (c1 c2 ... cx)) (a3 b3 c3)) | |
(let A ((a' a) (b '())) | |
(if (fold (lambda (a b) (if b b (null? a))) #f a') | |
(reverse b) | |
(A (map cdr a') (cons (map car a') b))))) | |
(define m '((a1 a2 a3) | |
(b1 b2 b3) | |
(c1 c2 c3))) | |
(matrix-transpose m) | |
(define* (matrix* a b #:key (f* *) (f+ +) (f0 0)) | |
(let ((b' (matrix-transpose b))) | |
(map | |
(lambda (row) | |
(map | |
(lambda (col) | |
(let A ((row' row) (col' col) (sum f0)) | |
(if (null? row') | |
sum | |
(A (cdr row') (cdr col') | |
(f+ (f* (car row') (car col')) sum))))) | |
b')) | |
a))) | |
(define* (par-matrix* a b #:key (f* *) (f+ +) (f0 0)) | |
(let ((b' (matrix-transpose b))) | |
(par-map | |
(lambda (row) | |
(par-map | |
(lambda (col) | |
(let A ((row' row) (col' col) (sum f0)) | |
(if (null? row') | |
sum | |
(A (cdr row') (cdr col') | |
(f+ (f* (car row') (car col')) sum))))) | |
b')) | |
a))) | |
(define* (par-matrix-2* a b #:key (f* *) (f+ +) (f0 0)) | |
(let ((b' (matrix-transpose b))) | |
(map | |
(lambda (row) | |
(map | |
(lambda (col) | |
(future | |
(let A ((row' row) (col' col) (sum f0)) | |
(if (null? row') | |
sum | |
(A (cdr row') (cdr col') | |
(f+ (f* (car row') (car col')) sum)))))) | |
b')) | |
a))) | |
(define m1000 (map (lambda (_) (iota 1000)) (iota 1000))) | |
;; matrix* = 77 secs | |
;; par-matrix* = 50 secs | |
;; par-matrix-2* = 16 secs | |
(define m1000*m1000 | |
(map (lambda (r) | |
(map (lambda (n) (touch n)) | |
r)) | |
(par-matrix-2* m1000 m1000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment