-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlab2_omp.c
360 lines (337 loc) · 10.2 KB
/
lab2_omp.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
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
#include <malloc.h>
#include <omp.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#define TOLERANCE 1e-3
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
float matrix_similarity(float *M_1, int m, int n, float *M_2)
{
float l2_diff=0.0;
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
{
l2_diff+=(M_1[i*n+j]-M_2[i*n+j])*(M_1[i*n+j]-M_2[i*n+j]);
}
}
l2_diff = sqrtf(l2_diff);
printf("L2-diff b/w D_T's: %f\n", l2_diff);
return l2_diff;
}
void transpose(float *M, int m, int n, float *M_T)
{
int i, j;
#pragma omp parallel for num_threads(1) private(i, j) collapse(2) schedule(static)
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
M_T[j * m + i] = M[i * n + j];
}
}
}
void multiply(float *M_1, int m1, int n1, float *M_2, int m2, int n2, float *result)
{
assert(n1 == m2);
float sum = 0.0;
//compute M_2_T:
float *M_2_T = (float *) malloc(sizeof(float)*n2*m2);
transpose(M_2, m2, n2, M_2_T);
int i, j, k, temp1, temp2;
#pragma omp parallel for private(i, j, k, sum, temp1, temp2) schedule(static)
for (i = 0; i < m1; i++)
{
temp1 = i*n1;
for (j = 0; j < n2; j++)
{
sum = 0.0;
temp2 = j*m2;
for (k = 0; k < n1; k++)
{
sum += M_1[temp1 + k] * M_2_T[temp2 + k];
}
result[i * n2 + j] = sum;
}
}
free(M_2_T);
}
float* initialize_identity(int size)
{
float *I = (float *)calloc(size * size, sizeof(float));
for (int i = 0; i < size; i++)
{
I[i * size + i] = 1.0;
}
return I;
}
float l2_norm(float *v_col, int length)
{
float norm, sum_sq = 0.0;
for (int i = 0; i < length; i++)
{
sum_sq += v_col[i] * v_col[i];
}
return norm = sqrtf(sum_sq);
}
float l2_norm_diagonal_diff(float *A_next, float *A_current, int P, float *E_next, float *E_current)
{
int i,j;
float norm, sum_sq = 0.0;
for (i = 0; i < P; i++)
{
//sum_sq += fabs(A_next[i * P + i] - A_current[i * P + i]);
sum_sq += (A_next[i * P + i] - A_current[i * P + i]) * (A_next[i * P + i] - A_current[i * P + i]);
}
if (sum_sq>TOLERANCE)
return norm = sqrtf(sum_sq);
#pragma omp parallel for private(i, j) reduction(+: sum_sq)
for (i=0; i<P; i++)
{
for (j=0; j<P; j++)
{
//sum_sq+=fabs(E_next[i*P+j]-E_current[i*P+j]);
sum_sq+=(E_next[i*P+j]-E_current[i*P+j])*(E_next[i*P+j]-E_current[i*P+j]);
}
}
return norm = sqrtf(sum_sq);
}
void print_matrix(float *A, int M, int N, bool console)
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (!console)
fprintf(stderr, "%f ", A[i * N + j]);
else
printf("%f ", A[i * N + j]);
}
if (!console)
fprintf(stderr, "\n");
else
printf("\n");
}
}
void classicalGS(float *A_current, float *A_T, int P, float *Q_current, float *R_current)
{
/*QR-factorisation of A_current (|=PXP)*/
float *v_col = (float *)malloc(sizeof(float) * P);
int col, row, row_;
float result;
for (col = 0; col < P; col++)
{
memcpy(v_col, A_T + col * P, sizeof(float) * P);
//v_col = a_col
for (row = 0; row < col; row++)
{
//r[row][col] computation:
result = 0.0;
for (row_ = 0; row_ < P; row_++)
{
result += (Q_current[row_ * P + row] * (A_T[col * P + row_]));
//result += (Q_current[row_ * P + row] * (v_col[row_]));
}
R_current[row * P + col] = result;
//v[col] computation:
for (row_ = 0; row_ < P; row_++)
{
v_col[row_] -= R_current[row * P + col] * Q_current[row_ * P + row];
}
}
R_current[col * P + col] = l2_norm(v_col, P);
for (row = 0; row < P; row++)
{
Q_current[row * P + col] = v_col[row] / R_current[col * P + col];
}
}
free(v_col);
}
void modifiedGS(float *A_current, int P, float *Q_current, float *R_current)
{
/*QR-factorisation of A_current (|=PXP)*/
float *V = (float *)malloc(sizeof(float) * P*P);
memcpy(V, A_current, sizeof(float)*P*P);
int i, j, k;
float l2_norm=0.0, inner_product=0.0;
for (i=0; i<P; i++)
{
l2_norm=0.0;
for (j=0; j<P; j++)
{
l2_norm+=V[j*P+i]*V[j*P+i];
}
l2_norm = sqrtf(l2_norm);
R_current[i*P+i] = l2_norm;
for (j=0; j<P; j++)
{
Q_current[j*P+i] = V[j*P+i]/l2_norm;
}
for (j=i+1; j<P; j++)
{
inner_product=0.0;
for (k=0; k<P; k++)
{
inner_product+=Q_current[k*P+i]*V[k*P+j];
}
R_current[i*P+j] = inner_product;
for (k=0; k<P; k++)
{
V[k*P+j]-=R_current[i*P+j]*Q_current[k*P+i];
}
}
}
free(V);
}
void compute_V(float **SIGMA, float *D_T, float **U, float **V_T, int N, int P)
{
//V_T = INV-SIGMA * U_T * M
float *INV_SIGMA = (float *)calloc(N * P, sizeof(float)); //|=NXP
for (int i = 0; i < P; i++)
{
INV_SIGMA[i * P + i] = 1.0 / ((*SIGMA)[i]);
}
float *U_T = (float *)malloc(sizeof(float) * P * P);
transpose(*U, P, P, U_T);
//first, multiply INV-SIGMA X U_T |=(NXP)
float *product = (float *)malloc(sizeof(float) * N * P);
multiply(INV_SIGMA, N, P, U_T, P, P, product);
//now, multiply product X D_T |=(NXN)
multiply(product, N, P, D_T, P, N, *V_T);
free(INV_SIGMA);
free(U_T);
free(product);
}
void SVD(int N, int P, float *D, float **U, float **SIGMA, float **V_T)
{
/* 1.Perform SVD for D_T */
// Get eigen-values & eigen-vectors for D_T*D
float *D_T = (float *)malloc(sizeof(float) * P * N);
transpose(D, N, P, D_T);
int i, j;
#pragma omp parallel for private(i, j) collapse(2) schedule(static)
for (i = 0; i < N; i++)
{
for (j = 0; j < P; j++)
{
D_T[j * N + i] = D[i * P + j];
}
}
float *A = (float *)calloc(P * P, sizeof(float)); //A=D_T*D|(PxP)
float *A_T = (float *)calloc(P * P, sizeof(float)); //A_T|(PXP)
multiply(D_T, P, N, D, N, P, A);
//begin QR-algorithm for A:
float *A_current = (float *)malloc(sizeof(float) * P * P);
memcpy(A_current, A, sizeof(float) * P * P); //PxP; initialised with A_0
float *E_current = initialize_identity(P); //PXP; initialised to E_0
float *Q_ = (float *)calloc(P * P, sizeof(float));
float *R_ = (float *)calloc(P * P, sizeof(float));
float diff_norm;
int iter = 0;
do //QR-algorithm loop
{
/*Both Classical Gram Schmidt & Modified Gram Schmidt procedures have been implemented*/
//transpose(A_current, P, P, A_T);
//classicalGS(A_current, A_T, P, Q_, R_);
modifiedGS(A_current, P, Q_, R_);
float *A_next = (float *)calloc(P * P, sizeof(float));
multiply(R_, P, P, Q_, P, P, A_next);
float *E_next = (float *)calloc(P * P, sizeof(float));
multiply(E_current, P, P, Q_, P, P, E_next);
diff_norm = l2_norm_diagonal_diff(A_next, A_current, P, E_next, E_current);
//printf("diff_norm: %f\n", diff_norm);
free(A_current);
free(E_current);
A_current = A_next;
E_current = E_next;
iter++;
}
while(diff_norm > TOLERANCE && iter<6000);
//printf("num of iters:%d\n", iter);
//eigenvalues are diagonals of A_current
float temp = FLT_MAX;
for (int i = 0; i < P; i++)
{
(*SIGMA)[i] = sqrtf(A_current[i * P + i]);
if ((*SIGMA)[i] > temp)
{
printf("EXCEPTION!\n");
exit(0);
}
temp = (*SIGMA)[i];
}
//eigenvectors matrix (U for D_T*D) is E_current
for (int i = 0; i < P; i++)
{
for (int j = 0; j < P; j++)
{
(*U)[i * P + j] = E_current[i * P + j];
}
}
// float *temp_sigma = (float *)calloc(P * N, sizeof(float));
// for (int i = 0; i < P; i++)
// {
// temp_sigma[i * N + i] = (*SIGMA)[i];
// }
//compute V_T
compute_V(SIGMA, D_T, U, V_T, N, P);
/*SVD verification*/ //COMMENTED CODE ONLY FOR SVD VERIFICATION
//D_T == U*SIGMA*V_T
// float *product_one = (float *)malloc(sizeof(float) * P * N);
// multiply(*U, P, P, temp_sigma, P, N, product_one); //U*SIGMA
// float *product_two = (float *)malloc(sizeof(float) * P * N);
// multiply(product_one, P, N, *V_T, N, N, product_two); //(U*SIGMA)*V_T [==A_0]
// //printf("\nORIGINAL D_T:\n");
// //print_matrix(D_T, P, N, 0);
// //printf("\nORIGINAL D:\n");
// //print_matrix(D, N, P, 0);
// //printf("\nVERIFIED D_T:\n");
// //print_matrix(product_two, P, N, 0);
// //printf("\n A0 = D_TXD: \n");
// //print_matrix(A, P, P, 0);
// matrix_similarity(D_T, P, N, product_two);
// free(product_one);
// free(temp_sigma);
// free(product_two);
}
void PCA(int retention, int N, int P, float *D, float *U, float *SIGMA, float **D_HAT, int *K)
{
float sum_eigenvalues = 0.0;
int i,j,k, temp1, temp2;
float sum;
for (i = 0; i < P; i++)
{
sum_eigenvalues += SIGMA[i]*SIGMA[i];
}
*K = 0;
float retention_ = 0.0;
i = 0;
while ((retention_ < retention) && (i < P))
{
retention_ += (SIGMA[i]*SIGMA[i] / sum_eigenvalues) * 100;
(*K)++;
i++;
}
//fprintf(stdout, "K: %d, retention_: %f\n", *K, retention_);
*D_HAT = (float *)malloc(sizeof(float) * N * (*K));
#pragma omp parallel for private(i, j, k, sum, temp1, temp2) schedule(static)
for (i=0; i<N; i++)
{
temp1 = i*P;
for (j=0; j<(*K); j++)
{
sum = 0.0;
for (k=0; k<P; k++)
{
sum += D[temp1+k]*U[k*P+j];
}
(*D_HAT)[i*(*K)+j] = sum;
}
}
//printf("PRINTING D_HAT:\n");
//print_matrix(*D_HAT, N, *K, 1);
}