-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential.c
438 lines (347 loc) · 9.52 KB
/
sequential.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
// Problem parameters.
int screen_width, screen_height;
int wire_width, wire_height;
int wire_shift_x, wire_shift_y;
int iteration_limit;
double wire_voltage;
// Algorithm parameters.
double v_top, v_right, v_bottom, v_left, v_x;
// Data rows.
// Each segment border shares with other adjacent segments.
double* data;
// Process data size;
int segment_size_x, segment_size_y;
// Board size.
int process_board_x, process_board_y;
// Position on board for current process_number.
int process_x, process_y;
// MPI specific definitions.
int available_processes, process_number;
/*
#ifdef DEBUG
#define d(format, args...) _debug
#else
#define d(format, args...)
#endif
*/
// former _debug
void d(const char*, ...);
void display_progress(int, int);
void initialize_globals(char**);
void print_board();
double initial_voltage(const int, const int);
int is_wire(const int, const int);
int is_outside(const int, const int);
double calculation(const int);
double* get_point_address(int, int);
int top_point(const int, const int);
int right_point(const int, const int);
int bottom_point(const int, const int);
int left_point(const int, const int);
int top_segment();
int right_segment();
int bottom_segment();
int left_segment();
int file_exists(const char*);
void finalize();
void communicate(int);
int main(int argc, char* argv[]) {
int x, y;
char buffer[255];
FILE* file;
int iteration = 0;
int progress = -1;
int add_headers = 0;
double timer;
clock_t start;
if (argc < 7) {
printf("Usage: %s screen_width screen_height wire_width wire_height wire_mili_voltage iteration_limit\n", argv[0]);
return 1;
}
initialize_globals(argv);
d("Simulating MPI world");
available_processes = 1;
process_number = 0;
wire_shift_x = (screen_width - wire_width) / 2;
wire_shift_y = (screen_height - wire_height) / 2;
process_board_x = sqrt(available_processes);
process_board_y = available_processes / process_board_x;
process_x = process_number % process_board_x;
process_y = process_number / process_board_x;
// printf("My rank: %d.\nMy location: %d:%d (board size: %d:%d)\n", process_number, process_x, process_y, process_board_x, process_board_y);
if (process_number >= process_board_x * process_board_y) {
printf("I'm out of board. Should never happen!\n");
finalize();
return 0;
}
d("Initiate data storage%s", ".");
segment_size_x = screen_width / process_board_x + 2;
segment_size_y = screen_height / process_board_y + 2;
data = (double*) malloc(sizeof(double) * segment_size_x * segment_size_y);
if (0 == process_number) {
print_board();
}
for (y = 0; y < segment_size_y; ++y) {
for (x = 0; x < segment_size_x; ++x) {
data[y * segment_size_x + x] = initial_voltage(x, y);
}
}
d("Register custom MPI type (not applicable)%s", ".");
start = clock();
communicate(iteration);
while (iteration < iteration_limit) {
d("Iterations start%s", ".");
calculation(iteration);
communicate(iteration);
++iteration;
if (process_number == 0 && 100 * iteration / iteration_limit > progress) {
progress = 100 * iteration / iteration_limit;
display_progress(progress, 80);
// printf("Progress: %d%% (%d of %d)\n", 5 * progress, iteration, iteration_limit);
}
}
if (0 == process_number) {
printf("\n");
}
timer = (double) (clock() - start) / CLOCKS_PER_SEC;
// Dump data from each process.
sprintf(buffer, "data-%dx%d.txt", process_x, process_y);
file = fopen(buffer, "w");
for (y = 1; y < segment_size_y - 1; ++y) {
for (x = 1; x < segment_size_x - 1; ++x) {
fprintf(file, "%f\t%f\t%f\n",
1.0 * (process_x * (segment_size_x - 2) + x) / screen_width,
1.0 * (process_y * (segment_size_y - 2) + y) / screen_height,
data[y * segment_size_x + x]
);
}
fprintf(file, "\n");
}
fclose(file);
// Log program response.
if (process_number == 0) {
printf("Board size: %dx%d, iterations limit: %d, used processes: %d, elapsed time: %f\n", screen_width, screen_height, iteration_limit, available_processes, timer);
if (file_exists("results.txt") == 0) {
add_headers = 1;
}
file = fopen("results.txt", "a+");
if (add_headers) {
fprintf(file, "W\tH\tmax\tproc\tT\n");
}
fprintf(file, "%d\t%d\t%d\t%d\t%f\n", screen_width, screen_height, iteration_limit, available_processes, timer);
fclose(file);
}
finalize();
return 0;
}
// former _debug
void d(const char* format, ...) {
va_list args;
va_start(args, format);
/**/
// char _format[500];
// sprintf(_format, "%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, format);
// sprintf(_format, "%d (%d:%d): %s\n", process_number, process_x, process_y, _format);
// vprintf(_format, args);
/**/
va_end(args);
}
void display_progress(int percent, int width) {
int i;
int middle;
printf("%3d%% [", percent);
middle = width * percent / 100.0;
for (i = 0; i < middle; ++i) {
printf("=");
}
for (i = middle; i < width; ++i) {
printf(" ");
}
// printf("]\n\033[F\033[J");
printf("]\r");
}
void initialize_globals(char* argv[]) {
// Screen size.
screen_width = atoi(argv[1]);
screen_height = atoi(argv[2]);
// Wire inside screen size.
wire_width = atoi(argv[3]);
wire_height = atoi(argv[4]);
if (screen_width <= wire_width) {
screen_width = wire_width;
wire_width = atoi(argv[1]);
}
if (screen_height <= wire_height) {
screen_height = wire_height;
wire_height = atoi(argv[2]);
}
// Wire constant voltage; input as mV thus "/ 1000" part.
wire_voltage = atoi(argv[5]) / 1000;
// Maximum number of iterations.
iteration_limit = atoi(argv[6]);
}
void print_board() {
if (screen_width > 120 || screen_height > 120) {
return;
}
printf("Board layout (%dx%d):\n", screen_width, screen_height);
for (int y = 0; y < segment_size_y; ++y) {
for (int x = 0; x < segment_size_x; ++x) {
printf("%s ", is_wire(x, y) ? "x" : (is_outside(x, y) ? "o" : "."));
}
printf("\n");
}
}
double initial_voltage(const int x, const int y) {
if (is_wire(x, y)) {
return wire_voltage;
}
return 0.0;
}
int is_wire(const int x, const int y) {
int board_x, board_y;
board_x = process_x * (segment_size_x - 2) + x - 1;
board_y = process_y * (segment_size_y - 2) + y - 1;
if (board_x < wire_shift_x
|| board_y < wire_shift_y
|| board_x >= wire_shift_x + wire_width
|| board_y >= wire_shift_y + wire_height
) {
return 0;
}
return 1;
}
int is_outside(const int x, const int y) {
int board_x, board_y;
board_x = process_x * (segment_size_x - 2) + x - 1;
board_y = process_y * (segment_size_y - 2) + y - 1;
if (board_x < 0
|| board_y < 0
|| board_x >= screen_width
|| board_y >= screen_height
) {
return 1;
}
return 0;
}
// Calculate one step and max corrections
double calculation(const int iterate) {
// Vt + Vr + Vb + Vl - 4Vx = 0
int y, x;
double maxStepSize, pde, stepSize;
d("Calculation step (iterate: %d)", iterate);
maxStepSize = 0;
for (y = 1; y < segment_size_y - 1; ++y) {
for (x = 1; x < segment_size_x - 1; ++x) {
// We're using checkerboard to calculate values.
if ((y + x + iterate) % 2 == 0
|| is_outside(x, y)
|| is_wire(x, y)
) {
continue;
}
pde = data[bottom_point(x, y)]
+ data[top_point(x, y)]
+ data[left_point(x, y)]
+ data[right_point(x, y)];
pde *= .25;
stepSize = abs(pde - data[y * segment_size_x + x]);
if (stepSize > maxStepSize) {
maxStepSize = stepSize;
}
data[y * segment_size_x + x] = pde;
}
}
return maxStepSize;
}
// Get point data; wrap coordinates to fit segment box.
double* get_point_address(int x, int y) {
if (x < 0) {
while (x < 0) {
x += segment_size_x;
}
} else {
while (x >= segment_size_x) {
x -= segment_size_x;
}
}
if (y < 0) {
while (y < 0) {
y += segment_size_y;
}
} else {
while (y >= segment_size_y) {
y -= segment_size_y;
}
}
return data + y * segment_size_x + x;
}
int left_point(const int x, const int y) {
if (x > 0) {
return y * segment_size_x + x - 1;
}
return -1;
}
int right_point(const int x, const int y) {
if (1 + x < segment_size_x) {
return y * segment_size_x + x + 1;
}
return -1;
}
int top_point(const int x, const int y) {
if (y > 0) {
return (y - 1) * segment_size_x + x;
}
return -1;
}
int bottom_point(const int x, const int y) {
if (1 + y < segment_size_y) {
return (y + 1) * segment_size_x + x;
}
return -1;
}
int left_segment() {
if (0 >= process_x) {
return -1;
}
return process_y * process_board_x + process_x - 1;
}
int right_segment() {
if (process_x + 1 >= process_board_x) {
return -1;
}
return process_y * process_board_x + process_x + 1;
}
int top_segment() {
if (0 >= process_y) {
return -1;
}
return (process_y - 1) * process_board_x + process_x;
}
int bottom_segment() {
if (process_y + 1 >= process_board_y) {
return -1;
}
return (process_y + 1) * process_board_x + process_x;
}
int file_exists(const char* filename) {
FILE* file = fopen(filename, "r");
if (file) {
fclose(file);
return 1;
}
return 0;
}
// Communicate with adjacent segments.
void communicate(int iteration) {
// We don't use communication in sequential program.
}
void finalize() {
// This is just a stub function.
}