-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartdiff.c
More file actions
508 lines (422 loc) · 14.6 KB
/
partdiff.c
File metadata and controls
508 lines (422 loc) · 14.6 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* partdiff - PDE solver for Gauß-Seidel and Jacobi methods
* Copyright (C) 1997 Thomas Ludwig
* Copyright (C) 1997 Thomas A. Zochler
* Copyright (C) 1997 Andreas C. Schmidt
* Copyright (C) 2007-2010 Julian M. Kunkel
* Copyright (C) 2010-2021 Michael Kuhn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* ************************************************************************ */
/* Include standard header file. */
/* ************************************************************************ */
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <math.h>
#include <malloc.h>
#include <string.h>
#include <sys/time.h>
/* ************* */
/* Some defines. */
/* ************* */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define MAX_INTERLINES 100000
#define MAX_ITERATION 200000
#define MAX_THREADS 1024
#define METH_GAUSS_SEIDEL 1
#define METH_JACOBI 2
#define FUNC_F0 1
#define FUNC_FPISIN 2
#define TERM_ACC 1
#define TERM_ITER 2
struct calculation_arguments
{
uint64_t N; /* number of spaces between lines (lines=N+1) */
uint64_t num_matrices; /* number of matrices */
double h; /* length of a space between two lines */
double* M; /* two matrices with real values */
};
struct calculation_results
{
uint64_t m;
uint64_t stat_iteration; /* number of current iteration */
double stat_accuracy; /* actual accuracy of all slaves in iteration */
};
struct options
{
uint64_t number; /* Number of threads */
uint64_t method; /* Gauss Seidel or Jacobi method of iteration */
uint64_t interlines; /* matrix size = interlines*8+9 */
uint64_t pert_func; /* perturbation function */
uint64_t termination; /* termination condition */
uint64_t term_iteration; /* terminate if iteration number reached */
double term_accuracy; /* terminate if accuracy reached */
};
/* ************************************************************************ */
/* Global variables */
/* ************************************************************************ */
/* time measurement variables */
struct timeval start_time; /* time when program started */
struct timeval comp_time; /* time when calculation completed */
static void
usage(char* name)
{
printf("Usage: %s [num] [method] [lines] [func] [term] [acc/iter]\n", name);
printf("\n");
printf(" - num: number of threads (1 .. %d)\n", MAX_THREADS);
printf(" - method: calculation method (1 .. 2)\n");
printf(" %1d: Gauß-Seidel\n", METH_GAUSS_SEIDEL);
printf(" %1d: Jacobi\n", METH_JACOBI);
printf(" - lines: number of interlines (0 .. %d)\n", MAX_INTERLINES);
printf(" matrixsize = (interlines * 8) + 9\n");
printf(" - func: perturbation function (1 .. 2)\n");
printf(" %1d: f(x,y) = 0\n", FUNC_F0);
printf(" %1d: f(x,y) = 2 * pi^2 * sin(pi * x) * sin(pi * y)\n", FUNC_FPISIN);
printf(" - term: termination condition (1 .. 2)\n");
printf(" %1d: sufficient accuracy\n", TERM_ACC);
printf(" %1d: number of iterations\n", TERM_ITER);
printf(" - acc/iter: depending on term:\n");
printf(" accuracy: 1e-4 .. 1e-20\n");
printf(" iterations: 1 .. %d\n", MAX_ITERATION);
printf("\n");
printf("Example: %s 1 2 100 1 2 100 \n", name);
}
static void
askParams(struct options* options, int argc, char** argv)
{
int ret;
if (argc < 7 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-?") == 0)
{
usage(argv[0]);
exit(0);
}
ret = sscanf(argv[1], "%" SCNu64, &(options->number));
if (ret != 1 || !(options->number >= 1 && options->number <= MAX_THREADS))
{
usage(argv[0]);
exit(1);
}
ret = sscanf(argv[2], "%" SCNu64, &(options->method));
if (ret != 1 || !(options->method == METH_GAUSS_SEIDEL || options->method == METH_JACOBI))
{
usage(argv[0]);
exit(1);
}
ret = sscanf(argv[3], "%" SCNu64, &(options->interlines));
if (ret != 1 || !(options->interlines <= MAX_INTERLINES))
{
usage(argv[0]);
exit(1);
}
ret = sscanf(argv[4], "%" SCNu64, &(options->pert_func));
if (ret != 1 || !(options->pert_func == FUNC_F0 || options->pert_func == FUNC_FPISIN))
{
usage(argv[0]);
exit(1);
}
ret = sscanf(argv[5], "%" SCNu64, &(options->termination));
if (ret != 1 || !(options->termination == TERM_ACC || options->termination == TERM_ITER))
{
usage(argv[0]);
exit(1);
}
if (options->termination == TERM_ACC)
{
ret = sscanf(argv[6], "%lf", &(options->term_accuracy));
options->term_iteration = MAX_ITERATION;
if (ret != 1 || !(options->term_accuracy >= 1e-20 && options->term_accuracy <= 1e-4))
{
usage(argv[0]);
exit(1);
}
}
else
{
ret = sscanf(argv[6], "%" SCNu64, &(options->term_iteration));
options->term_accuracy = 0;
if (ret != 1 || !(options->term_iteration >= 1 && options->term_iteration <= MAX_ITERATION))
{
usage(argv[0]);
exit(1);
}
}
}
/* ************************************************************************ */
/* initVariables: Initializes some global variables */
/* ************************************************************************ */
static void
initVariables(struct calculation_arguments* arguments, struct calculation_results* results, struct options const* options)
{
arguments->N = (options->interlines * 8) + 9 - 1;
arguments->num_matrices = (options->method == METH_JACOBI) ? 2 : 1;
arguments->h = 1.0 / arguments->N;
results->m = 0;
results->stat_iteration = 0;
results->stat_accuracy = 0;
}
/* ************************************************************************ */
/* freeMatrices: frees memory for matrices */
/* ************************************************************************ */
static void
freeMatrices(struct calculation_arguments* arguments)
{
free(arguments->M);
}
/* ************************************************************************ */
/* allocateMemory () */
/* allocates memory and quits if there was a memory allocation problem */
/* ************************************************************************ */
static void*
allocateMemory(size_t size)
{
void* p;
if ((p = malloc(size)) == NULL)
{
printf("Memory error! (%" PRIu64 " Bytes requested)\n", size);
exit(1);
}
return p;
}
/* ************************************************************************ */
/* allocateMatrices: allocates memory for matrices */
/* ************************************************************************ */
static void
allocateMatrices(struct calculation_arguments* arguments)
{
uint64_t const N = arguments->N;
arguments->M = allocateMemory(arguments->num_matrices * (N + 1) * (N + 1) * sizeof(double));
}
/* ************************************************************************ */
/* initMatrices: Initialize matrix/matrices and some global variables */
/* ************************************************************************ */
static void
initMatrices(struct calculation_arguments* arguments, struct options const* options)
{
uint64_t g, i, j; /* local variables for loops */
uint64_t const N = arguments->N;
double const h = arguments->h;
typedef double (*matrix)[N + 1][N + 1];
matrix Matrix = (matrix)arguments->M;
/* initialize matrix/matrices with zeros */
for (g = 0; g < arguments->num_matrices; g++)
{
for (i = 0; i <= N; i++)
{
for (j = 0; j <= N; j++)
{
Matrix[g][i][j] = 0.0;
}
}
}
/* initialize borders, depending on function (function 2: nothing to do) */
if (options->pert_func == FUNC_F0)
{
for (g = 0; g < arguments->num_matrices; g++)
{
for (i = 0; i <= N; i++)
{
Matrix[g][i][0] = 1.0 - (h * i);
Matrix[g][i][N] = h * i;
Matrix[g][0][i] = 1.0 - (h * i);
Matrix[g][N][i] = h * i;
}
Matrix[g][N][0] = 0.0;
Matrix[g][0][N] = 0.0;
}
}
}
/* ************************************************************************ */
/* calculate: solves the equation */
/* ************************************************************************ */
static void
calculate(struct calculation_arguments const* arguments, struct calculation_results* results, struct options const* options)
{
int i, j; /* local variables for loops */
int m1, m2; /* used as indices for old and new matrices */
double star; /* four times center value minus 4 neigh.b values */
double residuum; /* residuum of current iteration */
double maxresiduum; /* maximum residuum value of a slave in iteration */
int const N = arguments->N;
double const h = arguments->h;
double pih = 0.0;
double fpisin = 0.0;
int term_iteration = options->term_iteration;
typedef double (*matrix)[N + 1][N + 1];
matrix Matrix = (matrix)arguments->M;
/* initialize m1 and m2 depending on algorithm */
if (options->method == METH_JACOBI)
{
m1 = 0;
m2 = 1;
}
else
{
m1 = 0;
m2 = 0;
}
if (options->pert_func == FUNC_FPISIN)
{
pih = M_PI * h;
fpisin = 0.25 * (2 * M_PI * M_PI) * h * h;
}
while (term_iteration > 0)
{
maxresiduum = 0;
/* over all rows */
for (i = 1; i < N; i++)
{
double fpisin_i = 0.0;
if (options->pert_func == FUNC_FPISIN)
{
fpisin_i = fpisin * sin(pih * (double)i);
}
/* over all columns */
for (j = 1; j < N; j++)
{
star = 0.25 * (Matrix[m2][i - 1][j] + Matrix[m2][i][j - 1] + Matrix[m2][i][j + 1] + Matrix[m2][i + 1][j]);
if (options->pert_func == FUNC_FPISIN)
{
star += fpisin_i * sin(pih * (double)j);
}
if (options->termination == TERM_ACC || term_iteration == 1)
{
residuum = Matrix[m2][i][j] - star;
residuum = fabs(residuum);
maxresiduum = (residuum < maxresiduum) ? maxresiduum : residuum;
}
Matrix[m1][i][j] = star;
}
}
results->stat_iteration++;
results->stat_accuracy = maxresiduum;
/* exchange m1 and m2 */
i = m1;
m1 = m2;
m2 = i;
/* check for stopping calculation depending on termination method */
if (options->termination == TERM_ACC)
{
if (maxresiduum < options->term_accuracy)
{
term_iteration = 0;
}
}
else if (options->termination == TERM_ITER)
{
term_iteration--;
}
}
results->m = m2;
}
/* ************************************************************************ */
/* displayStatistics: displays some statistics about the calculation */
/* ************************************************************************ */
static void
displayStatistics(struct calculation_arguments const* arguments, struct calculation_results const* results, struct options const* options)
{
int N = arguments->N;
double time = (comp_time.tv_sec - start_time.tv_sec) + (comp_time.tv_usec - start_time.tv_usec) * 1e-6;
printf("Calculation time: %f s\n", time);
printf("Memory usage: %f MiB\n", (N + 1) * (N + 1) * sizeof(double) * arguments->num_matrices / 1024.0 / 1024.0);
printf("Calculation method: ");
if (options->method == METH_GAUSS_SEIDEL)
{
printf("Gauß-Seidel");
}
else if (options->method == METH_JACOBI)
{
printf("Jacobi");
}
printf("\n");
printf("Interlines: %" PRIu64 "\n", options->interlines);
printf("Perturbation function: ");
if (options->pert_func == FUNC_F0)
{
printf("f(x,y) = 0");
}
else if (options->pert_func == FUNC_FPISIN)
{
printf("f(x,y) = 2 * pi^2 * sin(pi * x) * sin(pi * y)");
}
printf("\n");
printf("Termination: ");
if (options->termination == TERM_ACC)
{
printf("Required accuracy");
}
else if (options->termination == TERM_ITER)
{
printf("Number of iterations");
}
printf("\n");
printf("Number of iterations: %" PRIu64 "\n", results->stat_iteration);
printf("Residuum: %e\n", results->stat_accuracy);
printf("\n");
}
/****************************************************************************/
/** Explanation of the displayMatrix function: **/
/** **/
/** The function displayMatrix outputs a Matrix **/
/** in a humanly readable way. **/
/** **/
/** This is achieved by only printing parts of the matrix. **/
/** From the matrix the first and last lines/columns and seven in between **/
/** rows/cols are printed out. **/
/****************************************************************************/
static void
displayMatrix(struct calculation_arguments* arguments, struct calculation_results* results, struct options* options)
{
int x, y;
int const interlines = options->interlines;
int const N = arguments->N;
typedef double (*matrix)[N + 1][N + 1];
matrix Matrix = (matrix)arguments->M;
printf("Matrix:\n");
for (y = 0; y < 9; y++)
{
for (x = 0; x < 9; x++)
{
printf("%7.4f", Matrix[results->m][y * (interlines + 1)][x * (interlines + 1)]);
}
printf("\n");
}
fflush(stdout);
}
/* ************************************************************************ */
/* main */
/* ************************************************************************ */
int
main(int argc, char** argv)
{
struct options options;
struct calculation_arguments arguments;
struct calculation_results results;
askParams(&options, argc, argv);
initVariables(&arguments, &results, &options);
allocateMatrices(&arguments);
initMatrices(&arguments, &options);
gettimeofday(&start_time, NULL);
calculate(&arguments, &results, &options);
gettimeofday(&comp_time, NULL);
displayStatistics(&arguments, &results, &options);
displayMatrix(&arguments, &results, &options);
freeMatrices(&arguments);
return 0;
}