HomeAboutCodePastes
summaryrefslogtreecommitdiff
path: root/bits/matrix-multiply.scm
blob: a8f57d54f9418a300496b8f1bfd004a92fd94f88 (plain)
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
#!/usr/bin/chicken-scheme
;; AUTOCOMPILE: -O3 -c++ -I/home/dieggsy/src/numchi/

(foreign-declare "#include \"eigen/Core\"")
(foreign-declare "using namespace Eigen;")
(import srfi-179
        srfi-1
        chicken.fixnum
        chicken.flonum
        blas
        micro-benchmark
        srfi-48)

(define (n3 a b)
  (let* ((n (vector-ref
             (interval-upper-bounds->vector
              (array-domain a))
             0))
         (m (vector-ref
             (interval-upper-bounds->vector
              (array-domain a))
             1))
         (p (vector-ref
             (interval-upper-bounds->vector
              (array-domain b))
             1))
         (c (make-specialized-array (make-interval (vector n p))
                                    f64-storage-class)))
    (do ((i 0 (fx+ i 1)))
        ((fx= i n))
      (do ((j 0 (fx+ j 1)))
          ((fx= j p))
        (define sum
          (let loop ((sum 0)
                     (k 0))
            (if (fx= k m)
                sum
                (loop (+ sum
                         (* (array-ref a i k)
                            (array-ref b k j)))
                      (fx+ k 1)))))
        (array-set! c sum i j)))
    c))

(define (srfi-no-copy a b)
  (define (array-dot-product a b)
    (array-fold fp+ 0.0 (array-map fp* a b)))
  (let ((a-rows (array-curry a 1))
        (b-columns (array-curry (array-rotate b 1) 1)))
    (array-outer-product array-dot-product a-rows b-columns)))

(define (srfi a b)
  (define (array-dot-product a b)
    (array-fold fp+ 0.0 (array-map fp* a b)))
  (let ((a-rows (array-curry a 1))
        (b-columns (array-curry (array-rotate b 1) 1)))
    (array-copy (array-outer-product array-dot-product a-rows b-columns)
                f64-storage-class)))

(define (blas a b)
  (let* ((m (vector-ref
             (interval-upper-bounds->vector
              (array-domain a))
             0))
         (k (vector-ref
             (interval-upper-bounds->vector
              (array-domain a))
             1))
         (n (vector-ref
             (interval-upper-bounds->vector
              (array-domain b))
             1))
         (arr (make-specialized-array
               (make-interval (vector m n))
               f64-storage-class))
         (res (dgemm! RowMajor NoTrans NoTrans
                   m
                   n
                   k
                   1
                   (array-body a)
                   (array-body b)
                   1
                   (array-body arr))))
    arr))

(define (eigen a b)
  (let* ((m (vector-ref
             (interval-upper-bounds->vector
              (array-domain a))
             0))
         (k (vector-ref
             (interval-upper-bounds->vector
              (array-domain a))
             1))
         (n (vector-ref
             (interval-upper-bounds->vector
              (array-domain b))
             1))
         (arr (make-specialized-array
               (make-interval (vector m n))
               f64-storage-class))
         (res ((foreign-lambda* void ((f64vector a) (f64vector b) (f64vector out) (size_t m) (size_t k) (size_t n))
                 "Map<Matrix<double,Dynamic,Dynamic>>(out,m,n) = Map<Matrix<double,Dynamic,Dynamic>>(a,m,k) * Map<Matrix<double,Dynamic,Dynamic>>(b,k,n);")
               (array-body a)
               (array-body b)
               (array-body arr)
               m k n)))
    arr))

;; (dgemm RowMajor NoTrans NoTrans 2 2 2 1 #f64(1 2 3 4) #f64(1 2 3 4) 1 #f64(0 0 0 0))

;; (array-body (blas A A))

;; (define A
;;   (list->array '(1 2 3 4)
;;                (make-interval #(2 2))
;;                f64-storage-class))
(define A
  (list->array (iota 10000)
               (make-interval #(100 100))
               f64-storage-class))

(current-benchmark-iterations 25)

(let ((bench (benchmark-run (n3 A A))))
  (format #t "            n3: ~9,2F\n" (alist-ref 'mean bench)))

(let ((bench (benchmark-run (srfi A A))))
  (format #t "          srfi: ~9,2F\n" (alist-ref 'mean bench)))
(let ((bench (benchmark-run (srfi-no-copy A A))))
  (format #t "srfi (no copy): ~9,2F\n" (alist-ref 'mean bench)))

(let ((bench (benchmark-run (eigen A A))))
  (format #t "         eigen: ~9,2F\n" (alist-ref 'mean bench)))
(let ((bench (benchmark-run (blas A A))))
  (format #t "          blas: ~9,2F\n" (alist-ref 'mean bench)))