removed dummy file.
[CommonLispStat.git] / external / clem / test / bench-matrix.cl
blob1cced3c7a773bb979b16ec68c1aaae9044f8ea8d
2 (in-package :clem-test)
5 (defmacro with-benchmark (&body body)
6 (let ((start-var (gensym))
7 (end-var (gensym)))
8 `(let ((,start-var (get-internal-run-time)))
9 (values
10 (multiple-value-list
11 (progn ,@body))
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)
26 (print (dim xb1))
27 (print (dim ub2))
28 (let ((q (time (mat-subtr xb1 ub2 :matrix-class 'sb16-matrix))))
29 (print q)
30 (print (sum q))))
31 )))
33 (defun matrix-bench-2 ()
34 (let* ((r 64) (c 64)
35 (x (copy-to-ub8-matrix (normalize (random-matrix r c)))))
36 (time
37 (dotimes (i r)
38 (dotimes (j c)
39 (val x i j))))))
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)))
44 (time
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)))
50 (time
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)))
57 (time
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)
108 (space 0)
109 (safety 0)))
110 (let ((x (make-instance 'single-float-matrix :rows 1024 :cols 1024 :initial-element 1f0)))
111 (declare (type single-float-matrix x))
112 (let ((acc 0f0)
113 (rows (rows x))
114 (cols (cols 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))
119 (dotimes (i rows)
120 (declare (type fixnum i))
121 (dotimes (j cols)
122 (declare (type fixnum j))
123 (setf acc (+ acc (the single-float (aref v i j))))))
124 (print acc))))
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))
129 (let ((acc 0)
130 (rows (rows x))
131 (cols (cols x)))
132 (declare (type (unsigned-byte 32) acc)
133 (type fixnum rows cols))
134 (dotimes (i rows)
135 (declare (type fixnum i))
136 (dotimes (j cols)
137 (declare (type fixnum j))
138 (setf acc (+ acc (the (unsigned-byte 8) (clem::mref x i j))))))
139 (print acc))))
141 (defun matrix-bench/ub8-matrix-add-aref ()
142 (declare (optimize (speed 3)
143 (safety 0)))
144 (let ((x (make-instance 'ub8-matrix :rows 2048 :cols 2048 :initial-element 1)))
145 (declare (type ub8-matrix x))
146 (let ((acc 0)
147 (rows (rows x))
148 (cols (cols 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))
153 (dotimes (i rows)
154 (declare (type fixnum i))
155 (dotimes (j cols)
156 (declare (type fixnum j))
157 (setf acc (+ acc (the (unsigned-byte 8) (aref v i j))))))
158 (print acc))))
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))
163 (let ((acc 0)
164 (rows (rows x))
165 (cols (cols x)))
166 (dotimes (i rows)
167 (dotimes (j cols)
168 (setf acc (+ acc (the (unsigned-byte 32) (clem::mref x i j))))))
169 (print acc))))
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))
174 (let ((acc 0)
175 (rows (rows x))
176 (cols (cols x)))
177 (dotimes (i rows)
178 (dotimes (j cols)
179 (setf acc (+ acc (the (unsigned-byte 32) (clem::mref x i j))))))
180 (print acc))))
182 (defun matrix-bench-19 (x)
183 (let ((acc 0)
184 (rows (rows x))
185 (cols (cols x)))
186 (dotimes (i rows)
187 (dotimes (j cols)
188 (setf acc (+ acc (clem::mref x i j)))))
189 (print acc)))
193 (defun matrix-bench-23/aref ()
194 (declare (optimize (speed 3)
195 (space 0)
196 (safety 0)))
197 (let ((x (make-instance 'double-float-matrix :rows 2048 :cols 2048 :initial-element 2d0)))
198 (declare (type double-float-matrix x))
199 (let ((acc 0d0)
200 (rows (rows x))
201 (cols (cols 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))
206 (dotimes (i rows)
207 (declare (type fixnum i))
208 (dotimes (j cols)
209 (declare (type fixnum j))
210 (setf acc (+ acc (the double-float (* (aref v i j)
211 (aref v i j)))))))
212 (print acc))))
214 (defun matrix-bench-23/mref ()
215 (declare (optimize (speed 3)
216 (space 0)
217 (safety 0)))
218 (let ((x (make-instance 'double-float-matrix :rows 2048 :cols 2048 :initial-element 2d0)))
219 (declare (type double-float-matrix x))
220 (let ((acc 0d0)
221 (rows (rows x))
222 (cols (cols x)))
223 (declare (type double-float acc)
224 (type fixnum rows cols))
225 (dotimes (i rows)
226 (declare (type fixnum i))
227 (dotimes (j cols)
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)))))))
231 (print acc))))
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))
238 (cols (cols x)))
239 (dotimes (i rows)
240 (dotimes (j cols)
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))
248 (cols (cols x)))
249 (dotimes (i rows)
250 (dotimes (j cols)
251 (setf (clem::double-float-matrix-mref y i j)
252 (clem::double-float-matrix-mref x i j)))))
256 (defun run-bench ()
257 ;; (matrix-bench-3)
258 (matrix-bench-4)
259 ;; (matrix-bench-5)