2016-11-12 15:38:02 +01:00
|
|
|
/*
|
|
|
|
* Sequential version.
|
|
|
|
* No paralleliization used.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
2016-11-12 21:53:11 +01:00
|
|
|
#include <mpi.h>
|
2016-11-12 15:38:02 +01:00
|
|
|
#include "../config/config.h"
|
2016-11-12 21:53:11 +01:00
|
|
|
#include "../utils/utils.h"
|
2016-11-12 15:38:02 +01:00
|
|
|
|
2016-11-13 10:39:28 +01:00
|
|
|
double **compute_jacobi(int n, double init_value, double threshold, borders b);
|
2016-11-12 15:38:02 +01:00
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2016-11-12 21:53:11 +01:00
|
|
|
int numprocs;
|
2016-11-12 15:38:02 +01:00
|
|
|
int config_loaded;
|
|
|
|
configuration config;
|
|
|
|
borders b;
|
2016-11-12 21:53:11 +01:00
|
|
|
double startwtime = 0.0, endwtime;
|
2016-11-13 10:39:28 +01:00
|
|
|
double **x;
|
2016-11-12 21:53:11 +01:00
|
|
|
|
|
|
|
MPI_Init(&argc, &argv);
|
|
|
|
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
|
|
|
|
|
|
|
|
if (numprocs != 1) {
|
|
|
|
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
|
|
}
|
2016-11-12 15:38:02 +01:00
|
|
|
|
|
|
|
config_loaded = load_config(&config);
|
|
|
|
if (config_loaded != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
b.north = config.north;
|
|
|
|
b.east = config.east;
|
|
|
|
b.south = config.south;
|
|
|
|
b.west = config.west;
|
|
|
|
|
2016-11-12 21:53:11 +01:00
|
|
|
startwtime = MPI_Wtime();
|
2016-11-13 10:39:28 +01:00
|
|
|
x = compute_jacobi(config.n, config.init_value, config.threshold, b);
|
2016-11-12 15:38:02 +01:00
|
|
|
|
2016-11-12 21:53:11 +01:00
|
|
|
endwtime = MPI_Wtime();
|
|
|
|
printf("Wall clock time: %fs\n", endwtime - startwtime);
|
|
|
|
|
2016-11-13 10:39:28 +01:00
|
|
|
destroy_matrix(x, config.n + 2);
|
2016-11-12 21:53:11 +01:00
|
|
|
MPI_Finalize();
|
|
|
|
|
2016-11-12 15:38:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-13 10:39:28 +01:00
|
|
|
double **compute_jacobi(int n, double init_value, double threshold, borders b) {
|
2016-11-12 21:53:11 +01:00
|
|
|
double **x;
|
2016-11-12 15:38:02 +01:00
|
|
|
double max_diff, new_x;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
/* Initialize boundary regions */
|
2016-11-12 21:53:11 +01:00
|
|
|
x = create_matrix(n + 2, n + 2);
|
2016-11-12 15:38:02 +01:00
|
|
|
for (i = 1; i <= n; i++) {
|
|
|
|
x[0][i] = b.north;
|
|
|
|
x[n + 1][i] = b.south;
|
|
|
|
x[i][0] = b.west;
|
|
|
|
x[i][n + 1] = b.east;
|
|
|
|
}
|
|
|
|
/* Initialize the rest of the matrix */
|
|
|
|
for (i = 1; i <= n; i++) {
|
|
|
|
for (j = 1; j <= n; j++) {
|
|
|
|
x[i][j] = init_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Iterative refinement of x until values converge */
|
|
|
|
do {
|
|
|
|
max_diff = 0;
|
|
|
|
for (i = 1; i <= n; i++) {
|
|
|
|
for (j = 1; j <= n; j++) {
|
|
|
|
new_x = 0.25 * (x[i - 1][j] + x[i][j + 1] + x[i + 1][j] + x[i][j - 1]);
|
|
|
|
max_diff = (double) fmax(max_diff, fabs(new_x - x[i][j]));
|
|
|
|
x[i][j] = new_x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (max_diff > threshold);
|
2016-11-13 10:39:28 +01:00
|
|
|
return x;
|
2016-11-12 15:38:02 +01:00
|
|
|
}
|