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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/*
* Copyright © 2012 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include "shared/matrix.h"
struct inverse_matrix {
double LU[16]; /* column-major */
unsigned perm[4]; /* permutation */
};
static struct timespec begin_time;
static void
reset_timer(void)
{
clock_gettime(CLOCK_MONOTONIC, &begin_time);
}
static double
read_timer(void)
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (double)(t.tv_sec - begin_time.tv_sec) +
1e-9 * (t.tv_nsec - begin_time.tv_nsec);
}
static double
det3x3(const float *c0, const float *c1, const float *c2)
{
return (double)
c0[0] * c1[1] * c2[2] +
c1[0] * c2[1] * c0[2] +
c2[0] * c0[1] * c1[2] -
c0[2] * c1[1] * c2[0] -
c1[2] * c2[1] * c0[0] -
c2[2] * c0[1] * c1[0];
}
static double
determinant(const struct weston_matrix *m)
{
double det = 0;
#if 1
/* develop on last row */
det -= m->d[3 + 0 * 4] * det3x3(&m->d[4], &m->d[8], &m->d[12]);
det += m->d[3 + 1 * 4] * det3x3(&m->d[0], &m->d[8], &m->d[12]);
det -= m->d[3 + 2 * 4] * det3x3(&m->d[0], &m->d[4], &m->d[12]);
det += m->d[3 + 3 * 4] * det3x3(&m->d[0], &m->d[4], &m->d[8]);
#else
/* develop on first row */
det += m->d[0 + 0 * 4] * det3x3(&m->d[5], &m->d[9], &m->d[13]);
det -= m->d[0 + 1 * 4] * det3x3(&m->d[1], &m->d[9], &m->d[13]);
det += m->d[0 + 2 * 4] * det3x3(&m->d[1], &m->d[5], &m->d[13]);
det -= m->d[0 + 3 * 4] * det3x3(&m->d[1], &m->d[5], &m->d[9]);
#endif
return det;
}
static void
print_permutation_matrix(const struct inverse_matrix *m)
{
const unsigned *p = m->perm;
const char *row[4] = {
"1 0 0 0\n",
"0 1 0 0\n",
"0 0 1 0\n",
"0 0 0 1\n"
};
printf(" P =\n%s%s%s%s", row[p[0]], row[p[1]], row[p[2]], row[p[3]]);
}
static void
print_LU_decomposition(const struct inverse_matrix *m)
{
unsigned r, c;
printf(" L "
" U\n");
for (r = 0; r < 4; ++r) {
double v;
for (c = 0; c < 4; ++c) {
if (c < r)
v = m->LU[r + c * 4];
else if (c == r)
v = 1.0;
else
v = 0.0;
printf(" %12.6f", v);
}
printf(" | ");
for (c = 0; c < 4; ++c) {
if (c >= r)
v = m->LU[r + c * 4];
else
v = 0.0;
printf(" %12.6f", v);
}
printf("\n");
}
}
static void
print_inverse_data_matrix(const struct inverse_matrix *m)
{
unsigned r, c;
for (r = 0; r < 4; ++r) {
for (c = 0; c < 4; ++c)
printf(" %12.6f", m->LU[r + c * 4]);
printf("\n");
}
printf("permutation: ");
for (r = 0; r < 4; ++r)
printf(" %u", m->perm[r]);
printf("\n");
}
static void
print_matrix(const struct weston_matrix *m)
{
unsigned r, c;
for (r = 0; r < 4; ++r) {
for (c = 0; c < 4; ++c)
printf(" %14.6e", m->d[r + c * 4]);
printf("\n");
}
}
static double
frand(void)
{
double r = random();
return r / (double)(RAND_MAX / 2) - 1.0f;
}
static void
randomize_matrix(struct weston_matrix *m)
{
unsigned i;
for (i = 0; i < 16; ++i)
#if 1
m->d[i] = frand() * exp(10.0 * frand());
#else
m->d[i] = frand();
#endif
}
/* Take a matrix, compute inverse, multiply together
* and subtract the identity matrix to get the error matrix.
* Return the largest absolute value from the error matrix.
*/
static double
test_inverse(struct weston_matrix *m)
{
unsigned i;
struct inverse_matrix q;
double errsup = 0.0;
if (matrix_invert(q.LU, q.perm, m) != 0)
return INFINITY;
for (i = 0; i < 4; ++i)
inverse_transform(q.LU, q.perm, &m->d[i * 4]);
m->d[0] -= 1.0f;
m->d[5] -= 1.0f;
m->d[10] -= 1.0f;
m->d[15] -= 1.0f;
for (i = 0; i < 16; ++i) {
double err = fabs(m->d[i]);
if (err > errsup)
errsup = err;
}
return errsup;
}
enum {
TEST_OK,
TEST_NOT_INVERTIBLE_OK,
TEST_FAIL,
TEST_COUNT
};
static int
test(void)
{
struct weston_matrix m;
double det, errsup;
randomize_matrix(&m);
det = determinant(&m);
errsup = test_inverse(&m);
if (errsup < 1e-6)
return TEST_OK;
if (fabs(det) < 1e-5 && isinf(errsup))
return TEST_NOT_INVERTIBLE_OK;
printf("test fail, det: %g, error sup: %g\n", det, errsup);
return TEST_FAIL;
}
static int running;
static void
stopme(int n)
{
running = 0;
}
static void
test_loop_precision(void)
{
int counts[TEST_COUNT] = { 0 };
printf("\nRunning a test loop for 10 seconds...\n");
running = 1;
alarm(10);
while (running) {
counts[test()]++;
}
printf("tests: %d ok, %d not invertible but ok, %d failed.\n"
"Total: %d iterations.\n",
counts[TEST_OK], counts[TEST_NOT_INVERTIBLE_OK],
counts[TEST_FAIL],
counts[TEST_OK] + counts[TEST_NOT_INVERTIBLE_OK] +
counts[TEST_FAIL]);
}
static void __attribute__((noinline))
test_loop_speed_matrixvector(void)
{
struct weston_matrix m;
struct weston_vector v = { { 0.5, 0.5, 0.5, 1.0 } };
unsigned long count = 0;
double t;
printf("\nRunning 3 s test on weston_matrix_transform()...\n");
weston_matrix_init(&m);
running = 1;
alarm(3);
reset_timer();
while (running) {
weston_matrix_transform(&m, &v);
count++;
}
t = read_timer();
printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
count, t, 1e9 * t / count);
}
static void __attribute__((noinline))
test_loop_speed_inversetransform(void)
{
struct weston_matrix m;
struct inverse_matrix inv;
struct weston_vector v = { { 0.5, 0.5, 0.5, 1.0 } };
unsigned long count = 0;
double t;
printf("\nRunning 3 s test on inverse_transform()...\n");
weston_matrix_init(&m);
matrix_invert(inv.LU, inv.perm, &m);
running = 1;
alarm(3);
reset_timer();
while (running) {
inverse_transform(inv.LU, inv.perm, v.f);
count++;
}
t = read_timer();
printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
count, t, 1e9 * t / count);
}
static void __attribute__((noinline))
test_loop_speed_invert(void)
{
struct weston_matrix m;
struct inverse_matrix inv;
unsigned long count = 0;
double t;
printf("\nRunning 3 s test on matrix_invert()...\n");
weston_matrix_init(&m);
running = 1;
alarm(3);
reset_timer();
while (running) {
matrix_invert(inv.LU, inv.perm, &m);
count++;
}
t = read_timer();
printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
count, t, 1e9 * t / count);
}
static void __attribute__((noinline))
test_loop_speed_invert_explicit(void)
{
struct weston_matrix m;
unsigned long count = 0;
double t;
printf("\nRunning 3 s test on weston_matrix_invert()...\n");
weston_matrix_init(&m);
running = 1;
alarm(3);
reset_timer();
while (running) {
weston_matrix_invert(&m, &m);
count++;
}
t = read_timer();
printf("%lu iterations in %f seconds, avg. %.1f ns/iter.\n",
count, t, 1e9 * t / count);
}
int main(void)
{
struct sigaction ding;
struct weston_matrix M;
struct inverse_matrix Q;
int ret;
double errsup;
double det;
ding.sa_handler = stopme;
sigemptyset(&ding.sa_mask);
ding.sa_flags = 0;
sigaction(SIGALRM, &ding, NULL);
srandom(13);
M.d[0] = 3.0; M.d[4] = 17.0; M.d[8] = 10.0; M.d[12] = 0.0;
M.d[1] = 2.0; M.d[5] = 4.0; M.d[9] = -2.0; M.d[13] = 0.0;
M.d[2] = 6.0; M.d[6] = 18.0; M.d[10] = -12; M.d[14] = 0.0;
M.d[3] = 0.0; M.d[7] = 0.0; M.d[11] = 0.0; M.d[15] = 1.0;
ret = matrix_invert(Q.LU, Q.perm, &M);
printf("ret = %d\n", ret);
printf("det = %g\n\n", determinant(&M));
if (ret != 0)
return 1;
print_inverse_data_matrix(&Q);
printf("P * A = L * U\n");
print_permutation_matrix(&Q);
print_LU_decomposition(&Q);
printf("a random matrix:\n");
randomize_matrix(&M);
det = determinant(&M);
print_matrix(&M);
errsup = test_inverse(&M);
printf("\nThe matrix multiplied by its inverse, error:\n");
print_matrix(&M);
printf("max abs error: %g, original determinant %g\n", errsup, det);
test_loop_precision();
test_loop_speed_matrixvector();
test_loop_speed_inversetransform();
test_loop_speed_invert();
test_loop_speed_invert_explicit();
return 0;
}
|