-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLU.c
More file actions
240 lines (187 loc) · 5.47 KB
/
Copy pathLU.c
File metadata and controls
240 lines (187 loc) · 5.47 KB
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
#include <math.h>
//#include "LU.h"
double LU_num_flops(int N)
{
/* rougly 2/3*N^3 */
double Nd = (double) N;
return (2.0 * Nd *Nd *Nd/ 3.0);
}
double LU_diff_matrix(int M, int N, double **lu, double **A)
{
int i;
int j;
double sum=0.0;
for (i=0; i<M; i++){
for (j=0; j<N; j++){
double err = lu[i][j]-A[i][j];
sum+=err*err;
}
}
return sqrt(sum/(M*N));
}
void LU_copy_matrix(int M, int N, double **lu, double **A)
{
int i;
int j;
for (i=0; i<M; i++)
for (j=0; j<N; j++)
lu[i][j] = A[i][j];
}
int LU_factor(int M, int N, double **A, int *pivot)
{
int minMN = M < N ? M : N;
int j=0;
for (j=0; j<minMN; j++)
{
/* find pivot in column j and test for singularity. */
int jp=j;
int i;
double t = fabs(A[j][j]);
for (i=j+1; i<M; i++)
{
double ab = fabs(A[i][j]);
if ( ab > t)
{
jp = i;
t = ab;
}
}
pivot[j] = jp;
/* jp now has the index of maximum element */
/* of column j, below the diagonal */
if ( A[jp][j] == 0 )
return 1; /* factorization failed because of zero pivot */
if (jp != j)
{
/* swap rows j and jp */
double *tA = A[j];
A[j] = A[jp];
A[jp] = tA;
}
if (j<M-1) /* compute elements j+1:M of jth column */
{
/* note A(j,j), was A(jp,p) previously which was */
/* guarranteed not to be zero (Label #1) */
double recp = 1.0 / A[j][j];
int k;
for (k=j+1; k<M; k++)
A[k][j] *= recp;
}
if (j < minMN-1)
{
/* rank-1 update to trailing submatrix: E = E - x*y; */
/* E is the region A(j+1:M, j+1:N) */
/* x is the column vector A(j+1:M,j) */
/* y is row vector A(j,j+1:N) */
int ii;
for (ii=j+1; ii<M; ii++)
{
double *Aii = A[ii];
double *Aj = A[j];
double AiiJ = Aii[j];
int jj;
for (jj=j+1; jj<N; jj++)
Aii[jj] -= AiiJ * Aj[jj];
}
}
}
return 0;
}
int LU_factor_bitflip(int M, int N, double **A, int *pivot)
{
int minMN = M < N ? M : N;
int j=0;
for (j=0; j<minMN; j++)
{
/* find pivot in column j and test for singularity. */
int jp=j;
int i;
double t = fabs(A[j][j]);
for (i=j+1; i<M; i++)
{
double ab = fabs(A[i][j]);
if ( ab > t)
{
jp = i;
t = ab;
}
}
pivot[j] = bitflip_int(jp);
/* jp now has the index of maximum element */
/* of column j, below the diagonal */
if ( A[jp][j] == 0 )
return 1; /* factorization failed because of zero pivot */
if (jp != j)
{
/* swap rows j and jp */
double *tA = A[j];
A[j] = A[jp];
A[jp] = tA;
}
if (j<M-1) /* compute elements j+1:M of jth column */
{
/* note A(j,j), was A(jp,p) previously which was */
/* guarranteed not to be zero (Label #1) */
double recp = 1.0 / A[j][j];
int k;
for (k=j+1; k<M; k++)
A[k][j] *= recp;
}
if (j < minMN-1)
{
/* rank-1 update to trailing submatrix: E = E - x*y; */
/* E is the region A(j+1:M, j+1:N) */
/* x is the column vector A(j+1:M,j) */
/* y is row vector A(j,j+1:N) */
int ii;
for (ii=j+1; ii<M; ii++)
{
double *Aii = A[ii];
double *Aj = A[j];
double AiiJ = Aii[j];
int jj;
for (jj=j+1; jj<N; jj++)
Aii[jj] -= AiiJ * Aj[jj];
}
}
}
return 0;
}
int main(){
FILE* args;
char line[256];
double **A = NULL;
double **lu = NULL;
double **lu1 = NULL;
int *pivot = NULL;
int N = LU_SIZE;
double min_time = RESOLUTION_DEFAULT;
Random R = new_Random_seed(RANDOM_SEED);
Stopwatch Q = new_Stopwatch();
int i=0;
int cycles=1;
if ((A = RandomMatrix(N, N, R)) == NULL) exit(1);
if ((lu = new_Array2D_double(N, N)) == NULL) exit(1);
if ((lu_bitflip = new_Array2D_double(N, N)) == NULL) exit(1);
if ((pivot = (int *) malloc(N * sizeof(int))) == NULL) exit(1);
while(1)
{
Stopwatch_start(Q);
for (i=0; i<cycles; i++)
{
Array2D_double_copy(N, N, lu, A);
Array2D_double_copy(N, N, lu_bitflip, A);
LU_factor(N, N, lu, pivot);
LU_factor_bitflip(N, N, lu1, pivot);
}
Stopwatch_stop(Q);
if (Stopwatch_read(Q) >= min_time) break;
cycles *= 2;
}
printf("bitflip %lf\n",LU_diff_matrix(N,N,lu,lu_bitflip));
Stopwatch_delete(Q);
free(pivot);
Array2D_double_delete(N, N, lu);
Array2D_double_delete(N, N, A);
return 0;
}