2 (in-package :clem-test
)
5 (defmacro with-benchmark
(&body body
)
6 (let ((start-var (gensym))
8 `(let ((,start-var
(get-internal-run-time)))
12 (let ((,end-var
(get-internal-run-time)))
13 (- ,end-var
,start-var
))))))
15 (defun matrix-bench-1 ()
16 (let ((x (random-matrix 256 256 :matrix-class
'double-float-matrix
:limit
255d0
)))
17 (print-range x
0 3 0 3)
18 (let ((xb1 (time (copy-to-ub8-matrix (gaussian-blur x
:k
1 :sigma
1))))
19 (xb2 (gaussian-blur x
:k
1 :sigma
1)))
20 (time (dotimes (i 10) (setf xb2
(gaussian-blur xb2
:k
1 :sigma
1))))
21 (print-range xb1
0 3 0 3)
22 (print-range xb2
0 3 0 3)
23 (print (cons (class-of xb1
) (class-of xb2
)))
24 (let ((ub2 (copy-to-ub8-matrix (clem::subset-matrix xb2
10 267 10 267))))
25 (print-range ub2
0 3 0 3)
28 (let ((q (time (mat-subtr xb1 ub2
:matrix-class
'sb16-matrix
))))
33 (defun matrix-bench-2 ()
35 (x (copy-to-ub8-matrix (normalize (random-matrix r c
)))))
41 (defun matrix-bench-3 ()
42 (let* ((x (random-double-float-matrix 512 512 :max
255d0
))
43 (y (random-double-float-matrix 5 5 :max
255d0
)))
45 (let ((conv (discrete-convolve x y
:truncate t
)))))))
47 (defun matrix-bench-4 ()
48 (let* ((x (random-ub8-matrix 512 512 :max
255))
49 (y (random-ub8-matrix 5 5 :max
255)))
51 (let ((conv (discrete-convolve x y
:matrix-class
'ub32-matrix
:truncate t
:norm-v nil
)))))
54 (defun matrix-bench-5 ()
55 (let* ((x (random-double-float-matrix 512 512 :max
1.0d0
))
56 (y (random-double-float-matrix 5 5 :max
1.0d0
)))
58 (let ((conv (discrete-convolve x y
:matrix-class
'double-float-matrix
:truncate t
:norm-v nil
)))))
62 (defun matrix-bench-6 ()
63 (let ((x (random-matrix 512 512 :matrix-class
'double-float-matrix
:limit
255d0
)))
64 (print-range x
0 5 0 5)
65 (let ((xb1 (time (gaussian-blur x
:k
3 :sigma
1))))
66 (print-range xb1
0 5 0 5)
67 (print (cons (dim x
) (dim xb1
))))))
69 (defun matrix-bench-7 ()
70 (let ((x (make-instance 'double-float-matrix
:rows
512 :cols
512 :initial-element
2d0
))
71 (y (make-instance 'double-float-matrix
:rows
512 :cols
512 :initial-element
3d0
))
72 (z (make-instance 'double-float-matrix
:rows
512 :cols
512 :initial-element
0d0
)))
73 (print-range x
0 5 0 5)
74 (let ((xb1 (time (gaussian-blur x
:k
3 :sigma
1))))
75 (print-range xb1
0 5 0 5)
76 (print (cons (dim x
) (dim xb1
))))))
78 (defun matrix-bench-8 ()
79 (let ((x (make-instance 'double-float-matrix
:rows
1024 :cols
1024 :initial-element
2d0
))
80 (y (make-instance 'double-float-matrix
:rows
1024 :cols
1024 :initial-element
3d0
))
81 (z (make-instance 'double-float-matrix
:rows
1024 :cols
1024 :initial-element
0d0
)))
82 (print-range x
0 5 0 5)
83 (let ((xb1 (time (clem::mat-mult3 x y z
))))
84 (print-range xb1
0 5 0 5)
85 (print (cons (dim x
) (dim xb1
))))))
87 (defun matrix-bench-9 ()
88 (let ((x (make-instance 'double-float-matrix
:rows
8 :cols
16 :initial-element
2d0
))
89 (y (make-instance 'double-float-matrix
:rows
16 :cols
8 :initial-element
3d0
))
90 (z (make-instance 'double-float-matrix
:rows
8 :cols
8 :initial-element
0d0
)))
91 (print-range x
0 5 0 5)
92 (let ((xb1 (time (clem::mat-mult3 x y z
))))
93 (print-range xb1
0 5 0 5)
94 (print (cons (dim x
) (dim xb1
))))))
96 (defun matrix-bench-10 ()
97 (let ((x (make-instance 'double-float-matrix
:rows
1024 :cols
1024 :initial-element
2d0
))
98 (y (make-instance 'double-float-matrix
:rows
1024 :cols
1024 :initial-element
3d0
))
99 (z (make-instance 'double-float-matrix
:rows
1024 :cols
1024 :initial-element
0d0
)))
100 (print-range x
0 5 0 5)
101 (let ((xb1 (time (clem::mat-mult-3-block x y z
))))
102 (print-range xb1
0 5 0 5)
103 (print (cons (dim x
) (dim xb1
))))))
106 (defun matrix-bench-13 ()
107 (declare (optimize (speed 3)
110 (let ((x (make-instance 'single-float-matrix
:rows
1024 :cols
1024 :initial-element
1f0
)))
111 (declare (type single-float-matrix x
))
115 (v (clem::matrix-vals x
)))
116 (declare (type (simple-array single-float
(* *)) v
)
117 (type single-float acc
)
118 (type fixnum rows cols
))
120 (declare (type fixnum i
))
122 (declare (type fixnum j
))
123 (setf acc
(+ acc
(the single-float
(aref v i j
))))))
126 (defun matrix-bench/ub8-matrix-add-mref
()
127 (let ((x (make-instance 'ub8-matrix
:rows
2048 :cols
2048 :initial-element
1)))
128 (declare (type ub8-matrix x
))
132 (declare (type (unsigned-byte 32) acc
)
133 (type fixnum rows cols
))
135 (declare (type fixnum i
))
137 (declare (type fixnum j
))
138 (setf acc
(+ acc
(the (unsigned-byte 8) (clem::mref x i j
))))))
141 (defun matrix-bench/ub8-matrix-add-aref
()
142 (declare (optimize (speed 3)
144 (let ((x (make-instance 'ub8-matrix
:rows
2048 :cols
2048 :initial-element
1)))
145 (declare (type ub8-matrix x
))
149 (v (clem::matrix-vals x
)))
150 (declare (type (simple-array (unsigned-byte 8) (* *)) v
)
151 (type (unsigned-byte 32) acc
)
152 (type fixnum rows cols
))
154 (declare (type fixnum i
))
156 (declare (type fixnum j
))
157 (setf acc
(+ acc
(the (unsigned-byte 8) (aref v i j
))))))
160 (defun matrix-bench-16 ()
161 (let ((x (make-instance 'ub32-matrix
:rows
1024 :cols
1024 :initial-element
1)))
162 (declare (type ub32-matrix x
))
168 (setf acc
(+ acc
(the (unsigned-byte 32) (clem::mref x i j
))))))
171 (defun matrix-bench-18 ()
172 (let ((x (make-instance 'bit-matrix
:rows
1024 :cols
1024 :initial-element
1)))
173 (declare (type bit-matrix x
))
179 (setf acc
(+ acc
(the (unsigned-byte 32) (clem::mref x i j
))))))
182 (defun matrix-bench-19 (x)
188 (setf acc
(+ acc
(clem::mref x i j
)))))
193 (defun matrix-bench-23/aref
()
194 (declare (optimize (speed 3)
197 (let ((x (make-instance 'double-float-matrix
:rows
2048 :cols
2048 :initial-element
2d0
)))
198 (declare (type double-float-matrix x
))
202 (v (clem::matrix-vals x
)))
203 (declare (type double-float acc
)
204 (type fixnum rows cols
)
205 (type (simple-array double-float
(* *)) v
))
207 (declare (type fixnum i
))
209 (declare (type fixnum j
))
210 (setf acc
(+ acc
(the double-float
(* (aref v i j
)
214 (defun matrix-bench-23/mref
()
215 (declare (optimize (speed 3)
218 (let ((x (make-instance 'double-float-matrix
:rows
2048 :cols
2048 :initial-element
2d0
)))
219 (declare (type double-float-matrix x
))
223 (declare (type double-float acc
)
224 (type fixnum rows cols
))
226 (declare (type fixnum i
))
228 (declare (type fixnum j
))
229 (setf acc
(+ acc
(the double-float
(* (clem::double-float-matrix-mref x i j
)
230 (clem::double-float-matrix-mref x i j
)))))))
234 (defun matrix-bench-24 (x)
235 (let ((y (clem:mat-copy-proto x
)))
236 (declare (type double-float-matrix x
))
237 (let ((rows (rows x
))
241 (setf (clem::mref y i j
) (clem::mref x i j
)))))
244 (defun matrix-bench-26 (x)
245 (let ((y (clem:mat-copy-proto x
)))
246 (declare (type double-float-matrix x
))
247 (let ((rows (rows x
))
251 (setf (clem::double-float-matrix-mref y i j
)
252 (clem::double-float-matrix-mref x i j
)))))