blob: b8decafe29462d840ec959526baa6b1862abc697 [file] [log] [blame]
bart25de6162008-03-09 13:41:26 +00001/** Compute the matrix inverse via Gauss-Jordan elimination.
2 * This program uses OpenMP separate computation steps but no
3 * mutexes. It is an example of a race-free program on which no data races
4 * are reported by the happens-before algorithm (drd), but a lot of data races
5 * (all false positives) are reported by the Eraser-algorithm (helgrind).
6 */
7
8
9#define _GNU_SOURCE
10
11/***********************/
12/* Include directives. */
13/***********************/
14
15#include <assert.h>
16#include <math.h>
17#include <pthread.h>
18#include <stdlib.h>
19#include <stdio.h>
20
21
22/*********************/
23/* Type definitions. */
24/*********************/
25
26typedef double elem_t;
27
28
29/********************/
30/* Local variables. */
31/********************/
32
33static int s_nthread;
34
35
36/*************************/
37/* Function definitions. */
38/*************************/
39
40/** Allocate memory for a matrix with the specified number of rows and
41 * columns.
42 */
43static elem_t* new_matrix(const int rows, const int cols)
44{
45 assert(rows > 0);
46 assert(cols > 0);
47 return malloc(rows * cols * sizeof(elem_t));
48}
49
50/** Free the memory that was allocated for a matrix. */
51static void delete_matrix(elem_t* const a)
52{
53 free(a);
54}
55
56/** Fill in some numbers in a matrix.
57 * @note It is important not to call srand() in this program, such that
58 * the results of a run are reproducible.
59 */
60static void init_matrix(elem_t* const a, const int rows, const int cols)
61{
62 int i, j;
63 for (i = 0; i < rows; i++)
64 {
65 for (j = 0; j < rows; j++)
66 {
67 a[i * cols + j] = rand() * 1.0 / RAND_MAX;
68 }
69 }
70}
71
72/** Print all elements of a matrix. */
73void print_matrix(const char* const label,
74 const elem_t* const a, const int rows, const int cols)
75{
76 int i, j;
77 printf("%s:\n", label);
78 for (i = 0; i < rows; i++)
79 {
80 for (j = 0; j < cols; j++)
81 {
82 printf("%g ", a[i * cols + j]);
83 }
84 printf("\n");
85 }
86}
87
88/** Copy a subset of the elements of a matrix into another matrix. */
89static void copy_matrix(const elem_t* const from,
90 const int from_rows,
91 const int from_cols,
92 const int from_row_first,
93 const int from_row_last,
94 const int from_col_first,
95 const int from_col_last,
96 elem_t* const to,
97 const int to_rows,
98 const int to_cols,
99 const int to_row_first,
100 const int to_row_last,
101 const int to_col_first,
102 const int to_col_last)
103{
104 int i, j;
105
106 assert(from_row_last - from_row_first == to_row_last - to_row_first);
107 assert(from_col_last - from_col_first == to_col_last - to_col_first);
108
109 for (i = from_row_first; i < from_row_last; i++)
110 {
111 assert(i < from_rows);
112 assert(i - from_row_first + to_row_first < to_rows);
113 for (j = from_col_first; j < from_col_last; j++)
114 {
115 assert(j < from_cols);
116 assert(j - from_col_first + to_col_first < to_cols);
117 to[(i - from_row_first + to_col_first) * to_cols
118 + (j - from_col_first + to_col_first)]
119 = from[i * from_cols + j];
120 }
121 }
122}
123
124/** Compute the matrix product of a1 and a2. */
125static elem_t* multiply_matrices(const elem_t* const a1,
126 const int rows1,
127 const int cols1,
128 const elem_t* const a2,
129 const int rows2,
130 const int cols2)
131{
132 int i, j, k;
133 elem_t* prod;
134
135 assert(cols1 == rows2);
136
137 prod = new_matrix(rows1, cols2);
138 for (i = 0; i < rows1; i++)
139 {
140 for (j = 0; j < cols2; j++)
141 {
142 prod[i * cols2 + j] = 0;
143 for (k = 0; k < cols1; k++)
144 {
145 prod[i * cols2 + j] += a1[i * cols1 + k] * a2[k * cols2 + j];
146 }
147 }
148 }
149 return prod;
150}
151
152/** Apply the Gauss-Jordan elimination algorithm on the matrix p->a starting
153 * at row r0 and up to but not including row r1. It is assumed that as many
154 * threads execute this function concurrently as the count barrier p->b was
155 * initialized with. If the matrix p->a is nonsingular, and if matrix p->a
156 * has at least as many columns as rows, the result of this algorithm is that
157 * submatrix p->a[0..p->rows-1,0..p->rows-1] is the identity matrix.
158 * @see http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
159 */
160static void gj(elem_t* const a, const int rows, const int cols)
161{
162 int i, j, k;
163
164 for (i = 0; i < rows; i++)
165 {
166 {
167 // Pivoting.
168 j = i;
169 for (k = i + 1; k < rows; k++)
170 {
171 if (a[k * cols + i] > a[j * cols + i])
172 {
173 j = k;
174 }
175 }
176 if (j != i)
177 {
178 for (k = 0; k < cols; k++)
179 {
180 const elem_t t = a[i * cols + k];
181 a[i * cols + k] = a[j * cols + k];
182 a[j * cols + k] = t;
183 }
184 }
185 // Normalize row i.
186 if (a[i * cols + i] != 0)
187 {
188 for (k = cols - 1; k >= 0; k--)
189 {
190 a[i * cols + k] /= a[i * cols + i];
191 }
192 }
193 }
194
195 // Reduce all rows j != i.
196#pragma omp parallel for
197 for (j = 0; j < rows; j++)
198 {
199 if (i != j)
200 {
201 const elem_t factor = a[j * cols + i];
202 for (k = 0; k < cols; k++)
203 {
204 a[j * cols + k] -= a[i * cols + k] * factor;
205 }
206 }
207 }
208 }
209}
210
211/** Matrix inversion via the Gauss-Jordan algorithm. */
212static elem_t* invert_matrix(const elem_t* const a, const int n)
213{
214 int i, j;
215 elem_t* const inv = new_matrix(n, n);
216 elem_t* const tmp = new_matrix(n, 2*n);
217 copy_matrix(a, n, n, 0, n, 0, n, tmp, n, 2 * n, 0, n, 0, n);
218 for (i = 0; i < n; i++)
219 for (j = 0; j < n; j++)
220 tmp[i * 2 * n + n + j] = (i == j);
221 gj(tmp, n, 2*n);
222 copy_matrix(tmp, n, 2*n, 0, n, n, 2*n, inv, n, n, 0, n, 0, n);
223 delete_matrix(tmp);
224 return inv;
225}
226
227/** Compute the average square error between the identity matrix and the
228 * product of matrix a with its inverse matrix.
229 */
230static double identity_error(const elem_t* const a, const int n)
231{
232 int i, j;
233 elem_t e = 0;
234 for (i = 0; i < n; i++)
235 {
236 for (j = 0; j < n; j++)
237 {
238 const elem_t d = a[i * n + j] - (i == j);
239 e += d * d;
240 }
241 }
242 return sqrt(e / (n * n));
243}
244
245/** Compute epsilon for the numeric type elem_t. Epsilon is defined as the
246 * smallest number for which the sum of one and that number is different of
247 * one. It is assumed that the underlying representation of elem_t uses
248 * base two.
249 */
250static elem_t epsilon()
251{
252 elem_t eps;
253 for (eps = 1; 1 + eps != 1; eps /= 2)
254 ;
255 return 2 * eps;
256}
257
258int main(int argc, char** argv)
259{
260 int matrix_size;
261 int silent;
262 elem_t *a, *inv, *prod;
263 elem_t eps;
264 double error;
265 double ratio;
266
267 matrix_size = (argc > 1) ? atoi(argv[1]) : 3;
268 s_nthread = (argc > 2) ? atoi(argv[2]) : 3;
269 silent = (argc > 3) ? atoi(argv[3]) : 0;
270
271 eps = epsilon();
272 a = new_matrix(matrix_size, matrix_size);
273 init_matrix(a, matrix_size, matrix_size);
274 inv = invert_matrix(a, matrix_size);
275 prod = multiply_matrices(a, matrix_size, matrix_size,
276 inv, matrix_size, matrix_size);
277 error = identity_error(prod, matrix_size);
278 ratio = error / (eps * matrix_size);
279 if (! silent)
280 {
281 printf("error = %g; epsilon = %g; error / (epsilon * n) = %g\n",
282 error, eps, ratio);
283 }
284 if (ratio < 100)
285 printf("Error within bounds.\n");
286 else
287 printf("Error out of bounds.\n");
288 delete_matrix(prod);
289 delete_matrix(inv);
290 delete_matrix(a);
291
292 return 0;
293}