-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbd_2.c
347 lines (272 loc) · 9.01 KB
/
bbd_2.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
#include <stdio.h>
#include <stddef.h>
// gcc t2.c -llapack -std=c99
// lapack and blas functions used
extern void dgetrf_ (int * m, int * n, double * A, int * LDA, int * IPIV, int * INFO);
extern void dgetri_ (int * n, double * A, int * LDA, int * IPIV, double * WORK, int * LWORK, int * INFO);
extern void dgemm_(const char *TRANSA, const char *TRANSB, const int *M, const int *N, const int *K, double *ALPHA, double *A, const int *LDA, double *B, const int *LDB, double *BETA, double *C, const int *LDC);
extern void dscal_ (int * N, double * DA, double * DX, int * INCX);
int print_matrix(int size, double * M)
{
int N = size;
int elements = size*size;
for (size_t i=0; i<elements; i++){
printf(" %g, ", M[i]);
if( i%N == (N-1)) {
printf("\n");
}
}
return 0;
}
int print_vector(int size, double * M)
{
for (size_t i=0; i<size; i++){
printf(" %g, ", M[i]);
printf("\n");
}
return 0;
}
void matrix_add(int size, double * A, double * B, double scalar)
{
// A = (A)+(s*B)
int N = size;
int elements = size*size;
for (size_t i=0; i<elements; i++){
A[i] = A[i] + scalar * B[i];
}
}
void vector_add(int size, double * A, double * B, double scalar)
{
// A = (A)+(s*B)
int N = size;
for (size_t i=0; i<N; i++){
A[i] = A[i] + scalar * B[i];
}
}
void matrix_invm (int size, double * M, double * N, double * R)
{
// Agruments:
// size: Size of matrix
// M: Pointer to start of Matrix, stored in an array
// R = inv(M)*N
int elements = size*size;
int pivotArray[size];
int errorHandler;
double lapackWorkspace[elements]; // change to malloc
char TRANS = 'N';
double ALPHA = 1.0;
double BETA = 0.0;
// matrix inversion M = inv(M)
dgetrf_(&size, &size, M, &size, pivotArray, &errorHandler);
dgetri_(&size, M, &size, pivotArray, lapackWorkspace, &elements, &errorHandler);
// matrix multiplication R = M*N
dgemm_(&TRANS, &TRANS, &size, &size, &size, &ALPHA, M, &size, N, &size, &BETA, R, &size);
}
void matrix_inv_m (int size, double * A, double * C, double * B, double * G, double * tempB, double * tempG) {
// Agruments:
// size: int - Size of matrix
// A: pointer to double - Pointer to start of Matrix M, stored in contagious array
// C: pointer to double - Pointer to start of Matrix B, stored in contagious array
// B: pointer to double - Pointer to start of Matrix C, stored in contagious array
// G: pointer to double - Pointer to start of Matrix D, stored in contagious array
// tempB = C*inv(A)*B
// tempG = C*inv(A)*G
int elements = size*size;
int pivotArray[size];
int errorHandler;
// TODO: Change this to malloc, not a good idea when elements is a large integer
double lapackWorkspace0[elements];
char TRANS = 'N';
double ALPHA = 1.0;
double BETA = 0.0;
// matrix inversion
// Inverted matrix in again stored in A
dgetrf_(&size, &size, A, &size, pivotArray, &errorHandler);
dgetri_(&size, A, &size, pivotArray, lapackWorkspace0, &elements, &errorHandler);
// matrix multiplication lapackworkspace0 = C*inv(A)
dgemm_(&TRANS, &TRANS, &size, &size, &size, &ALPHA, C, &size, A, &size, &BETA, lapackWorkspace0, &size);
// matrix multiplication tempB = lapackworkspace0*B
dgemm_(&TRANS, &TRANS, &size, &size, &size, &ALPHA, lapackWorkspace0, &size, B, &size, &BETA, tempB, &size);
// TODO: Replace this with matrix vector multiplication
// matrix multiplication tempG = lapackworkspace0*G
dgemm_(&TRANS, &TRANS, &size, &size, &size, &ALPHA, lapackWorkspace0, &size, G, &size, &BETA, tempG, &size);
}
void matrix_xi(int size, double * A, double * B, double * G, double * Xn, double * Xi)
{
// Xi = A*G - A*B*Xn
int elements = size*size;
char TRANS = 'N';
double ALPHA = 1.0;
double BETA = 0.0;
// TODO: Change this to malloc
double lapackWorkspace1[elements];
double lapackWorkspace2[elements];
// matrix multiplication Xi = A*G
dgemm_(&TRANS, &TRANS, &size, &size, &size, &ALPHA, A, &size, G, &size, &BETA, Xi, &size);
// matrix multiplication lapackworkspace1 = A*B
dgemm_(&TRANS, &TRANS, &size, &size, &size, &ALPHA, A, &size, B, &size, &BETA, lapackWorkspace1, &size);
// matrix multiplication lapackworkspace2 = lapackworkspace1*Xn
dgemm_(&TRANS, &TRANS, &size, &size, &size, &ALPHA, lapackWorkspace1, &size, Xn, &size, &BETA, lapackWorkspace2, &size);
// Xi = Xi - lapackworkspace2
matrix_add(size, Xi, lapackWorkspace2, -1.0);
}
struct Matrix {
// row first form
// Each matrix can have max size of 10x10
// TODO Add support for having non square matrices
int size;
double matrix[100];
};
void solve_bbd( int nMat, struct Matrix * matA, struct Matrix * matB, struct Matrix * matC, struct Matrix * matG, struct Matrix * matX) {
/*
A B X G
A B X G
A B * X = G
A B X G
C C C C A X G
Arguments:
nMat: Number of diagonal blocks minus one (nMat = 4 in above example)
matA: Pointer to array of Struct Matrix A, diagonal matrix blocks
matB: Pointer to array of Struct Matrix B, bottom border matrix blocks
matC: Pointer to array of Struct Matrix C, right border matrix blocks
matG: Pointer to array of Struct Matrix G, vector
matX: Pointer to array of Struct Matrix X, vector
*/
int size = matA[0].size;
int elements = size*size;
double AddG[elements];
for(int i =0; i<elements; i++) AddG[i] = 0;
double tempG[elements];
for(int i =0; i<elements; i++) tempG[i] = 0;
double AddB[elements];
for(int i =0; i<elements; i++) AddB[i] = 0;
double tempB[elements];
for(int i =0; i<elements; i++) tempB[i] = 0;
#pragma omp prallel for
for (int i=0; i<nMat; i++){
printf("\n #%d ", i);
printf("%s\n", "Matrix Before Inversion");
print_matrix(matA[i].size, matA[i].matrix);
// tempB = C*inv(A)*B
// tempG = C*inv(A)*G
matrix_inv_m(matA[i].size, matA[i].matrix, matC[i].matrix, matB[i].matrix, matG[i].matrix, tempB, tempG);
// AddG = AddG + tempG :This is vector operation
matrix_add(matA[i].size, AddG, tempG, 1.0);
// AddB = AddB + tempB :This is matrix operation
matrix_add(matA[i].size, AddB, tempB, 1.0);
printf("#%d ", i);
printf("%s\n", "Matrix After Inversion");
print_matrix(matG[i].size, tempG);
}
// printf("%s\n", "AddG Matrix WorkSpace After Addition");
// print_matrix(size, AddG);
printf("%s\n", "AddB Matrix WorkSpace After Addition");
print_matrix(size, AddB);
printf("%s\n", "matA[nMat]");
print_matrix(size, matA[nMat].matrix);
printf("%s\n", "matG[nMat]");
print_matrix(size, matG[nMat].matrix);
matrix_add(size, matG[nMat].matrix, AddG, -1.0); // this should be vector addition
matrix_add(size, matA[nMat].matrix, AddB, -1.0);
printf("%s\n", "After sub matA[nMat]");
print_matrix(size, matA[nMat].matrix);
printf("%s\n", "After sub matG[nMat]");
print_matrix(size, matG[nMat].matrix);
matrix_invm(size, matA[nMat].matrix, matG[nMat].matrix, matX[nMat].matrix);
printf("%s\n", "After final Inversion matX[nMat]");
print_matrix(size, matX[nMat].matrix);
for (int i=0; i<nMat; i++){
matrix_xi(size, matA[i].matrix, matB[i].matrix, matG[i].matrix, matX[nMat].matrix, matX[i].matrix);
printf("%s\n", "-------------------------------------------------" );
printf("%d th ",i);
printf("%s\n", "Solution Xi Matrices");
print_matrix(size, matX[i].matrix);
}
}
int main() {
int nMat = 2;
struct Matrix matA[nMat+1];
struct Matrix matB[nMat];
struct Matrix matC[nMat];
struct Matrix matG[nMat+1];
struct Matrix matX[nMat+1];
int n; // temp
for (int i=0; i<nMat+1; i++){
matA[i].size = 3;
matB[i].size = 3;
matC[i].size = 3;
matG[i].size = 3;
n = matA[i].size;
// Initialize each Matrix
if (i<nMat)
{
/* code */
for (int j=0; j<n*n; j++) {
if( j/n == j%n ) {
matA[i].matrix[j] = (i+1)*2;
matB[i].matrix[j] = (i+1)*2;
matC[i].matrix[j] = (i+1)*2;
matG[i].matrix[j] = (i+1)*2;
}
else {
matA[i].matrix[j] = 0;
matB[i].matrix[j] = 0;
matC[i].matrix[j] = 0;
matG[i].matrix[j] = 0;
}
}
}
else
{
for (int j=0; j<n*n; j++) {
if( j/n == j%n ) {
matA[i].matrix[j] = (i+1)*4;
matG[i].matrix[j] = (i+1)*4;
}
else {
matA[i].matrix[j] = 0;
matG[i].matrix[j] = 0;
}
}
}
}
solve_bbd(nMat, matA, matB, matC, matG, matX);
/*
#pragma omp prallel for
for (int i=0; i<nMat; i++){
mat[i].size = i+3;
n = mat[i].size;
// Initialize each Matrix
for (int j=0; j<n*n; j++) {
if( j/n == j%n )
mat[i].matrix[j] = (i+1)*2;
else
mat[i].matrix[j] = 0;
}
printf("\n #%d ", i);
printf("%s\n", "Matrix Before Inversion");
print_matrix(mat[i].size, mat[i].matrix);
matrix_inv(mat[i].size, mat[i].matrix);
printf("#%d ", i);
printf("%s\n", "Matrix After Inversion");
print_matrix(mat[i].size, mat[i].matrix);
}
*/
/*
int N1 = 4;
double M1[16] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
int N2 = 4;
double M2[16] = { 1, 0, 0,
0, 1, 0,
0, 0, 1
};
matrix_inv(N2, M2);
print_matrix(N2,M2);
*/
printf("\n %s\n", "-------------------------------------------------" );
return 0;
}