-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirichlet.c
288 lines (240 loc) · 7.18 KB
/
dirichlet.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
/* 3D Poisson Equation with Pure Dirichlet Boundary Conditions*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <mpi.h>
#define N (32+2) // Grid Size (Must be power of 2)
int i,j,k;
double eps;
double T[N][N][N];
double Tk[N][N][N];
double Q[N][N][N];
//Dirichlet Boundary Conditions
double T1 = 6.0; //Top face
double T2 = -6.0; //Bottom face
double T3 = 0.0; //Front face
double T4 = 0.0; //Back face
double T5 = 0.0; //Left face
double T6 = 0.0; //Right face
double L = 10.0; // Length of side of cube
void print_output();
void compile_array();
void relaxation(int rank, int size, double d);
void L2();
void init(int rank, int size);
void update(int rank, int size);
void reassign();
int block, start_index, last_index;
int main(int argc, char **argv)
{
int iter_count;
double time_start, time_finish, d;
iter_count = 1;
static const double error_tolerance = 0.1e-7; // Error Tolerance
d = L/(N-3);
// Initialize MPI
int process_rank, cluster_size;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
MPI_Comm_size(MPI_COMM_WORLD, &cluster_size);
MPI_Barrier(MPI_COMM_WORLD);
// Split between processors
block = N/cluster_size;
start_index = block*process_rank;
last_index = block*(process_rank+1) + 1;
if(process_rank == 0) {printf("Jacobian 3D started %d", cluster_size);}
// Initialize matrices
init(process_rank, cluster_size);
MPI_Barrier(MPI_COMM_WORLD);
if (!process_rank) {time_start = MPI_Wtime(); }
// Main Loop
do
{
relaxation(process_rank, cluster_size, d);
L2();
reassign();
update(process_rank,cluster_size);
iter_count++;
}while(eps > error_tolerance);
MPI_Barrier(MPI_COMM_WORLD);
compile_array();
if(process_rank == 0){print_output();}
if(!process_rank){
time_finish = MPI_Wtime();
printf("Time: %f\n", time_finish - time_start);
printf("error is eps = %f\n", eps);
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
void init(int rank, int size)
{
// Initializing the 3D Array:
for(i = start_index; i <= last_index; i++){
for(j = 0; j <= N-1; j++){
for(k = 0; k <= N-1; k++){
T[i][j][k] = 0.0;
Q[i][j][k] = 0.0;
}
}
}
// Set Sources/Sinks:
//Q[16][16][16] = 100;
// Setting BCs:
//Top face
for(int i = start_index+1; i <= last_index-1; i++){
for(int j = 1; j <= N-2; j++){
T[i][j][N-2] = T1;
}
}
//Bottom face
for(int i = start_index+1; i <= last_index-1; i++){
for(int j = 0; j <= N-2; j++){
T[i][j][1] = T2;
}
}
//Front face
for(int i = start_index+1; i <= last_index-1; i++){
for(int k = 1; k <= N-2; k++){
T[i][1][k] = T3;
}
}
//Back face
for(int i = start_index+1; i <= last_index-1; i++){
for(int k = 1; k <= N-2; k++){
T[i][N-2][k] = T4;
}
}
if(rank == 0){
//Left face
for(int j = 1; j <= N-2; j++){
for(int k = 1; k <= N-2; k++){
T[1][j][k] = T5;
}
}
}
//Right face
if(rank == size - 1){
for(int j = 1; j <= N-2; j++){
for(int k = 1; k <= N-2; k++){
T[N-2][j][k] = T6;
}
}
}
// Initializing Tk Array:
for(i = start_index; i <= last_index; i++){
for(j = 0; j <= N-1; j++){
for(k = 0; k <= N-1; k++){
Tk[i][j][k] = T[i][j][k];
}
}
}
}
void relaxation(int rank, int size, double d)
{
if(rank == 0){
for(i = start_index+2; i <= last_index-1; i++){
for(j = 2; j <= N-3; j++){
for(k = 2; k <= N-3; k++){
Tk[i][j][k] = (T[i-1][j][k] + T[i+1][j][k] + T[i][j-1][k] + T[i][j+1][k] + T[i][j][k-1] + T[i][j][k+1] - (d*d)*Q[i][j][k])/6.0;
}
}
}
}
else if(rank == size-1){
for(i = start_index+1; i <= last_index-2; i++){
for(j = 2; j <= N-3; j++){
for(k = 2; k <= N-3; k++){
Tk[i][j][k] = (T[i-1][j][k] + T[i+1][j][k] + T[i][j-1][k] + T[i][j+1][k] + T[i][j][k-1] + T[i][j][k+1] - (d*d)*Q[i][j][k])/6.0;
}
}
}
}
else{
for(i = start_index+1; i <= last_index-1; i++){
for(j = 2; j <= N-3; j++){
for(k = 2; k <= N-3; k++){
Tk[i][j][k] = (T[i-1][j][k] + T[i+1][j][k] + T[i][j-1][k] + T[i][j+1][k] + T[i][j][k-1] + T[i][j][k+1] - (d*d)*Q[i][j][k])/6.0;
}
}
}
}
}
void L2()
{
double subdomain_error;
double e;
e = 0.0;
for(i = start_index+1; i <= last_index-1; i++){
for(j = 2; j <= N-3; j++){
for(k = 2; k <= N-3; k++){
if (i == 1 || i == N-2) continue;
e += pow((Tk[i][j][k] - T[i][j][k]),2);
}
}
}
subdomain_error = sqrt(e);
MPI_Allreduce(&subdomain_error, &eps, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
}
void print_output()
{
FILE* fp;
char* filename = "T.txt";
fp = fopen(filename, "wb");
for(int i = 1; i <= N-2; i++){
for(int j = 1; j <= N-2; j++){
for(int k = 1; k <= N-2; k++){
fprintf(fp, "%f ", Tk[i][j][k]);
}
fprintf(fp, "\n");
}
}
}
void update(int rank, int size)
{
MPI_Request request[4];
MPI_Status status[4];
// Update neighbours
if (rank){
MPI_Isend(&T[start_index+1][0][0], N*N, MPI_DOUBLE, rank-1, 1216, MPI_COMM_WORLD, &request[1]);
MPI_Irecv(&T[start_index][0][0], N*N, MPI_DOUBLE, rank-1, 1215, MPI_COMM_WORLD, &request[0]);
}
if(rank != size-1) {
MPI_Isend(&T[last_index-1][0][0], N*N, MPI_DOUBLE, rank+1, 1215, MPI_COMM_WORLD, &request[2]);
MPI_Irecv(&T[last_index][0][0], N*N, MPI_DOUBLE, rank+1, 1216, MPI_COMM_WORLD, &request[3]);
}
// Wait for processes
int no_of_operations = 4, shift = 0; // all processes
if(!rank) { // first process
no_of_operations = 2;
shift = 2;
}
if(rank == size-1) { // last process
no_of_operations = 2;
}
MPI_Waitall(no_of_operations, &request[shift], &status[0]);
}
void reassign(){
for(i = start_index+1; i <= last_index-1; i++){
for(j = 1; j <= N-2; j++){
for(k = 1; k <= N-2; k++){
T[i][j][k] = Tk[i][j][k]; ;
}
}
}
}
void compile_array(){
for(j = 1; j <= N-2; j++){
for(k = 1; k <= N-2; k++){
T[start_index][j][k] = 0;
}
}
for(j = 1; j <= N-2; j++){
for(k = 1; k <= N-2; k++){
T[last_index][j][k] = 0;
}
}
MPI_Allreduce(&T[0][0][0], &Tk[0][0][0], N*N*N, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
}