forked from mrychlik/GaussianElimination
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
272 lines (202 loc) · 5.3 KB
/
main.c
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
/*----------------------------------------------------------------
* File: main.c
*----------------------------------------------------------------
*
* Author: Marek Rychlik ([email protected])
* Date: Sun Sep 22 10:54:06 2024
* Copying: (C) Marek Rychlik, 2020. All rights reserved.
*
*----------------------------------------------------------------*/
/* Driver for gauss_solve.c */
#define _GNU_SOURCE
#include <math.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fenv.h>
#include <setjmp.h>
#include "gauss_solve.h"
#include "helpers.h"
/* Size of the matrix */
#define N 3
void test_gauss_solve()
{
printf("Entering function: %s\n", __func__);
const double A0[N][N] = {
{2, 3, -1},
{4, 1, 2},
{-2, 7, 2}
};
const double b0[N] = {5, 6, 3};
double A[N][N], b[N], x[N], y[N];
/* Create copies of the matrices.
NOTE: the copies will get destroyed. */
memcpy(A, A0, sizeof(A0));
memcpy(b, b0, sizeof(b0));
gauss_solve_in_place(N, A, b);
memcpy(x, b, sizeof(b0));
matrix_times_vector(N, A0, x, y);
double eps = 1e-6, dist = norm_dist(N, b0, y);
assert( dist < eps);
/* Print x */
puts("x:\n");
print_vector(N, x);
/* Print U */
puts("U:\n");
print_matrix(N, A, FLAG_UPPER_PART);
/* Print L */
puts("L:\n");
print_matrix(N, A, FLAG_LOWER_PART);
}
/*
void test_plu()
{
printf("Entering function: %s\n", __func__);
const double A0[N][N] = {
{2, 3, -1},
{4, 1, 2},
{-2, 7, 2}
};
const double P[N] = {1, 2, 3};
double A[N][N], b[N], x[N], y[N];
// Create copies of the matrices.
// NOTE: the copies will get destroyed.
memcpy(A, A0, sizeof(A0));
memcpy(b, b0, sizeof(b0));
plu(N, A, P);
memcpy(x, b, sizeof(b0));
matrix_times_vector(N, A0, x, y);
double eps = 1e-6, dist = norm_dist(N, b0, y);
assert( dist < eps);
// Print x
puts("x:\n");
print_vector(N, x);
// Print U
puts("U:\n");
print_matrix(N, A, FLAG_UPPER_PART);
// Print L
puts("L:\n");
print_matrix(N, A, FLAG_LOWER_PART);
}
*/
jmp_buf env; // Buffer to store the state for setjmp/longjmp
void test_gauss_solve_with_zero_pivot()
{
printf("Entering function: %s\n", __func__);
double A[N][N] = {
{0, 3, -1},
{4, 1, 2},
{-2, 7, 2}
};
double b[N] = {5, 6, 3};
// Save the program state with setjmp
if (setjmp(env) == 0) {
gauss_solve_in_place(N, A, b);
print_matrix(N, A, FLAG_LOWER_PART);
} else {
// This block is executed when longjmp is called
printf("Returned to main program flow after exception\n");
}
}
void test_lu_in_place()
{
printf("Entering function: %s\n", __func__);
const double A0[N][N] = {
{2, 3, -1},
{4, 1, 2},
{-2, 7, 2}
};
const double b0[N] = {5, 6, 3};
double A[N][N];
memcpy(A, A0, sizeof(A0));
lu_in_place(N, A);
/* Print U */
puts("U:\n");
print_matrix(N, A, FLAG_UPPER_PART);
/* Print L */
puts("L:\n");
print_matrix(N, A, FLAG_LOWER_PART);
lu_in_place_reconstruct(N, A);
/* Print U */
puts("Reconstructed A:\n");
print_matrix(N, A, FLAG_WHOLE);
memcpy(A, A0, sizeof(A0));
puts("Original A:\n");
print_matrix(N, A, FLAG_WHOLE);
double eps = 1e-6;
assert(frobenius_norm_dist(N, A, A0) < eps);
}
void benchmark_test(int n)
{
/* Allocate matrix on stack */
double A0[n][n], A[n][n];
generate_random_matrix(n, A0);
copy_matrix(n, A0, A);
lu_in_place(N, A);
lu_in_place_reconstruct(N, A);
double eps = 1e-6;
assert(frobenius_norm_dist(N, A0, A) < eps);
}
void benchmark_test_dynamic(int n)
{
/* Allocate memory */
double *store = (double *)malloc(n * n * sizeof(double));
double (*A)[n] = (double (*)[n])store;
assert(A);
double *store_copy = (double *)malloc(n * n * sizeof(double));
double (*A_copy)[n] = (double (*)[n])store_copy;
assert(A_copy);
generate_random_matrix(n, A);
copy_matrix(n, A, A_copy);
puts("Random matrix A:\n");
print_matrix(n, A, FLAG_WHOLE);
lu_in_place(n, A);
lu_in_place_reconstruct(n, A);
double eps = 1e-6;
assert(frobenius_norm_dist(n, A_copy, A) < eps);
/* Ensure memory is deallocated */
free(store);
free(store_copy);
}
void benchmark_test_dynamic_alt(int n)
{
/* Allocate memory */
double (*A)[n] = NULL, (*A_copy)[n] = NULL;
create_matrix(n, &A);
assert(A);
create_matrix(n, &A_copy);
assert(A_copy);
generate_random_matrix(n, A);
copy_matrix(n, A, A_copy);
lu_in_place(n, A);
lu_in_place_reconstruct(n, A);
double eps = 1e-4, dist = frobenius_norm_dist(n, A_copy, A);
assert(dist < eps);
/* Ensure memory is deallocated */
destroy_matrix(n, A);
destroy_matrix(n, A_copy);
}
void fpe_handler(int sig) {
printf("Entering %s...\n", __func__);
if(sig == SIGFPE) {
printf("Floating point exception occurred, ignoring...\n");
longjmp(env, 1); // Jump back to where setjmp was called
}
}
int main()
{
// Enable trapping for specific floating-point exceptions
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
sighandler_t old_handler = signal(SIGFPE, fpe_handler);
printf("Old handler: %p\n", old_handler);
test_gauss_solve();
//test_plu();
test_lu_in_place();
benchmark_test(5);
benchmark_test_dynamic(5);
benchmark_test_dynamic_alt(2000);
test_gauss_solve_with_zero_pivot();
exit(EXIT_SUCCESS);
}