-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat.scm
executable file
·67 lines (58 loc) · 1.81 KB
/
mat.scm
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
(define-module (guile-gpu mat)
#:use-module (ice-9 match)
#:use-module (guile-gpu common)
#:use-module (guile-gpu gpu)
#:use-module (ffi cblas) ; from guile-ffi-cblas/mod
#:export (array-copy-bytevector
array-matrix-scale!
sv+!
sv-!
svvs*!
ref-saxpy!
ref-sgemv!))
(define (array-matrix-scale! a m)
(let ((len (array-length m)))
(do ((i 0 (1+ i))) ((= i len))
(cblas-sscal! a (array-cell-ref m i)))))
; copy from array to bytevector
(define (array-copy-bytevector dst src)
(match (array-dimensions dst)
((r c)
(do ((i 0 (1+ i))) ((= i r))
(do ((j 0 (1+ j))) ((= j c))
(array-set! dst (f32vector-ref src (+ (* i c) j)) i j))))
((r)
(do ((i 0 (1+ i))) ((= i r))
(array-set! dst (f32vector-ref src i) i)))))
;;;;
;;;; blas reference
;;;;
(define (ref-saxpy! a x y)
(match (array-dimensions x)
((r)
(do ((j 0 (+ j 1))) ((= j r))
(let* ((o (array-ref x j))
(e (array-ref y j)))
(array-set! y (+ e (* a o)) j))))))
; y := alpha*A*x + beta*y
(define (ref-sgemv! alpha A transA x beta y)
(match (array-dimensions A)
((r c)
(do ((i 0 (+ i 1))) ((= i r))
(let ((s 0))
(do ((j 0 (+ j 1))) ((= j c))
(set! s (+ s (* alpha (array-ref A i j)
(array-ref x j)))))
(array-set! y (+ s (* beta (array-ref y i))) i))))))
; blas style, consider replacing these with calls into blas
(define (sv+! dst src1 src2)
(array-map! dst (lambda (a b)
(+ a b))
src1 src2))
(define (sv-! dst src1 src2)
(array-map! dst (lambda (a b)
(- a b))
src1 src2))
(define (svvs*! dst vec sc)
(array-map! dst (lambda (v) (* v sc))
vec))