-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSchwarz_DD.cpp
274 lines (199 loc) · 11 KB
/
Schwarz_DD.cpp
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
class Schwarz_DD {
Jacobi j;
Gauss_Seidel gs;
public:
Schwarz_DD() {}
Schwarz_DD(Jacobi jacobi, Gauss_Seidel gauss_seidel) {
j = jacobi;
gs = gauss_seidel;
}
// Algebraic Block Jacobi Additive Schwarz Method
int ABJASM(double *X,
double TOL,
int max_iterations,
int N,
const int THREAD_COUNT,
int block_size,
double* A_Blocks,
double* inv_main_block,
double* F
) {
int iteration = 0;
for(iteration=0; iteration<max_iterations; iteration++) {
double *old_X = copy_vector(X, N);
double local_errors[N];
#pragma omp parallel num_threads(THREAD_COUNT) shared(local_errors, X, inv_main_block, A_Blocks, F)
{
#pragma omp for
for(int i=0; i<THREAD_COUNT; i++) {
double *subdomain_solution;
if(i == THREAD_COUNT-1) {
subdomain_solution = AAS_Residual_Calc(inv_main_block, F, block_size, i*block_size, 2*block_size, 1, old_X[block_size * i - 1 ] * -1.0);
/*
For general matrices, comment the above 'subdomain_solution' and use the below 'subdomain_solution'.
*/
//subdomain_solution = matvecmul(inv_main_block, subtract(sliced_vector_F[i], matvecmul(&A_Blocks[((i*THREAD_COUNT)+(i-1)) * block_size * block_size], old_X, block_size, (i-1)*block_size, block_size), block_size), block_size, 0, 2*block_size);
} else {
subdomain_solution = AAS_Residual_Calc(inv_main_block, F, block_size, i*block_size, 2*block_size, 0, old_X[block_size*(i+1)] * -1.0);
/*
For general matrices, comment the above 'subdomain_solution' and use the below 'subdomain_solution'.
*/
//subdomain_solution = matvecmul(inv_main_block, subtract(sliced_vector_F[i], matvecmul(&A_Blocks[((i*THREAD_COUNT)+(i+1)) * block_size * block_size], old_X, block_size, (i+1)*block_size, block_size), block_size), block_size, 0, 2*block_size);
}
int sub_solution_index = 0;
for(int j=i*block_size; j<block_size*(i+1); j++) {
X[j] = subdomain_solution[sub_solution_index++];
local_errors[j] = fabs(X[j] - old_X[j]);
}
free(subdomain_solution);
}
#pragma omp barrier
}
free(old_X);
double *max_error = std::max_element(local_errors, local_errors+N);
if(*max_error < TOL) {
break;
}
}
return iteration;
}
// Matrix Block Jacobi Additive Schwarz Method
int MBJASM(
double *X,
double TOL,
int max_iterations,
int N,
const int THREAD_COUNT,
int block_size,
double* A,
double* F
) {
int iteration = 0;
for(iteration=0; iteration<max_iterations; iteration++) {
double *old_X = copy_vector(X, N);
double local_errors[N];
#pragma omp parallel for shared(local_errors, X, A, F, old_X)
for(int i=0; i<THREAD_COUNT; i++) {
if(i > 0 && i < (THREAD_COUNT-1)) {
/*
For tridiagonal matrices.
*/
gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N, i*block_size, block_size*(i+1), THREAD_COUNT-1, i, {old_X[block_size * i - 1 ] * -1.0, old_X[block_size*(i+1)] * -1.0});
}
else if(i == THREAD_COUNT-1) {
/*
For tridiagonal matrices.
*/
//gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N, block_size, block_size*2, 1, old_X[block_size * i - 1 ] * -1.0);
printf("\n%d %d", iteration, gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N, i*block_size, block_size*(i+1), THREAD_COUNT-1, i, {old_X[block_size * i - 1 ] * -1.0, 0.0}));
//gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N, i*block_size, block_size*(i+1), THREAD_COUNT-1, i, {old_X[block_size * i - 1 ] * -1.0, 0.0});
} else {
/*
For tridiagonal matrices.
*/
printf("\n%d %d", iteration, gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N, 0, block_size, THREAD_COUNT-1, i, {0.0, old_X[block_size*(i+1)] * -1.0}));
//gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N, 0, block_size, THREAD_COUNT-1, i, {0.0, old_X[block_size*(i+1)] * -1.0});
}
for(int j=i*block_size; j<block_size*(i+1); j++) {
local_errors[j] = fabs(X[j] - old_X[j]);
}
}
#pragma omp barrier
free(old_X);
double *max_error = std::max_element(local_errors, local_errors+N);
if(*max_error < TOL) {
break;
}
}
return iteration;
}
// Matrix Block Jacobi Restricted Additive Schwarz Method
int MBJRASM(
double *X,
double TOL,
int max_iterations,
int N,
const int THREAD_COUNT,
int DDBounds[][2],
double* A,
double* F
) {
int iteration = 0;
for(iteration=0; iteration<max_iterations; iteration++) {
double *old_X = copy_vector(X, N);
double local_errors[N];
#pragma omp parallel for shared(local_errors, X, A, F, old_X)
for(int i=0; i<THREAD_COUNT; i++) {
if(i > 0 && i < (THREAD_COUNT-1)) {
/*
For tridiagonal matrices.
*/
gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N,
DDBounds[i][0],
DDBounds[i][1] + 1,
THREAD_COUNT-1,
i,
{X[DDBounds[i][0] - 1] * -1.0, X[DDBounds[i][1] + 1] * -1.0});
}
else if(i == THREAD_COUNT-1) {
/*
For tridiagonal matrices.
*/
//gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N, block_size, block_size*2, 1, old_X[block_size * i - 1 ] * -1.0);
//printf("\n%d %d", iteration, gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N, i*block_size, block_size*(i+1), THREAD_COUNT-1, i, {old_X[block_size * i - 1 ] * -1.0, 0.0}));
gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N,
DDBounds[i][0],
DDBounds[i][1] + 1,
THREAD_COUNT-1, i,
{X[DDBounds[i][0] - 1] * -1.0, 0.0});
} else {
/*
For tridiagonal matrices.
*/
//printf("\n%d %d", iteration, gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N, 0, block_size, THREAD_COUNT-1, i, {0.0, old_X[block_size*(i+1)] * -1.0}));
gs.gauss_seidel_serial_ASM(X, F, A, TOL, max_iterations, N,
DDBounds[0][0],
DDBounds[0][1] + 1,
THREAD_COUNT-1, i,
{0.0, X[DDBounds[i][1] + 1] * -1.0});
}
for(int j=0; j<N; j++) {
local_errors[j] = fabs(X[j] - old_X[j]);
}
}
#pragma omp barrier
free(old_X);
double *max_error = std::max_element(local_errors, local_errors+N);
if(*max_error < TOL) {
break;
}
}
return iteration;
}
// Algebraic Additive Schwarz residual calculation.
double* AAS_Residual_Calc(double* INV, double* F, int N, int vector_begin_index, int W, int subdomain_index, double residual_component) {
double *V = create_vector(N);
initialize_vector(V, 0.0, N);
if(subdomain_index == 0) {
int i,j,v_index;
for(i=0;i<N;i++){
v_index = vector_begin_index;
for(j=0;j<N-1;j++){
V[i] = V[i] + (F[v_index++] * INV[i * W + j]);
}
V[i] = V[i] + ((F[v_index] - residual_component) * INV[i * W + j]);
}
return V;
} else {
int i,j,v_index;
for(i=0;i<N;i++){
v_index = vector_begin_index;
V[i] = V[i] + ((F[v_index++] - residual_component) * INV[i * W + 0]);
for(j=1;j<N;j++){
V[i] = V[i] + (F[v_index++] * INV[i * W + j]);
}
}
return V;
}
}
};