Used new matrix

This commit is contained in:
Fabio Salvini 2016-11-23 10:14:40 +01:00
parent 906a47d4b3
commit 8ff3e6cf65
4 changed files with 64 additions and 22 deletions

View File

@ -15,7 +15,9 @@
double *compute_jacobi(int rank, int numprocs, int n, double init_value, double threshold, borders b, int *iterations) {
double *complete_x;
double *x;
double max_diff, global_max_diff, new_x;
double *new_x;
double *tmp_x;
double max_diff, global_max_diff, new_value;
int i, j;
int nb = n + 2; // n plus the border
int rows, rows_to_transmit;
@ -31,24 +33,30 @@ double *compute_jacobi(int rank, int numprocs, int n, double init_value, double
/* LOG(printf("[Process %d/%d] initializing matrix\n", rank, numprocs)); */
/* Initialize the matrix */
x = create_sa_matrix(rows + 2, nb);
new_x = create_sa_matrix(rows + 2, nb);
for (i = 0; i < rows + 2; i++) {
for (j = 1; j <= n; j++) {
x[IDX(nb, i, j)] = init_value;
new_x[IDX(nb, i, j)] = init_value;
}
}
/* Initialize boundary regions */
for (i = 0; i < rows + 2; i++) {
x[IDX(nb, i, 0)] = b.west;
x[IDX(nb, i, n + 1)] = b.east;
new_x[IDX(nb, i, 0)] = b.west;
new_x[IDX(nb, i, n + 1)] = b.east;
}
if (rank == 0) {
for (i = 1; i <= n + 1; i++) {
x[IDX(nb, 0, i)] = b.north;
new_x[IDX(nb, 0, i)] = b.north;
}
}
if (rank == numprocs - 1){
for (i = 1; i < n + 1; i++) {
x[IDX(nb, rows + 1, i)] = b.south;
new_x[IDX(nb, rows + 1, i)] = b.south;
}
}
/* LOG(printf("[Process %d/%d] matrix initialized\n", rank, numprocs)); */
@ -59,11 +67,14 @@ double *compute_jacobi(int rank, int numprocs, int n, double init_value, double
global_max_diff = 0;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= n; j++) {
new_x = 0.25 * (x[IDX(nb, i - 1, j)] + x[IDX(nb, i, j + 1)] + x[IDX(nb, i + 1, j)] + x[IDX(nb, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_x - x[IDX(nb, i, j)]));
x[IDX(nb, i, j)] = new_x;
new_value = 0.25 * (x[IDX(nb, i - 1, j)] + x[IDX(nb, i, j + 1)] + x[IDX(nb, i + 1, j)] + x[IDX(nb, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_value - x[IDX(nb, i, j)]));
new_x[IDX(nb, i, j)] = new_value;
}
}
tmp_x = new_x;
new_x = x;
x = tmp_x;
if (rank % 2 == 0) {
if (rank != numprocs - 1) {
// Send and receive south border

View File

@ -15,7 +15,9 @@
double *compute_jacobi(int rank, int numprocs, int n, double init_value, double threshold, borders b, int *iterations) {
double *complete_x;
double *x;
double max_diff, global_max_diff, new_x;
double *new_x;
double *tmp_x;
double max_diff, global_max_diff, new_value;
int i, j;
int nb = n + 2; // n plus the border
int rows, rows_to_transmit;
@ -33,24 +35,30 @@ double *compute_jacobi(int rank, int numprocs, int n, double init_value, double
/* LOG(printf("[Process %d/%d] initializing matrix\n", rank, numprocs)); */
/* Initialize the matrix */
x = create_sa_matrix(rows + 2, nb);
new_x = create_sa_matrix(rows + 2, nb);
for (i = 0; i < rows + 2; i++) {
for (j = 1; j <= n; j++) {
x[IDX(nb, i, j)] = init_value;
new_x[IDX(nb, i, j)] = init_value;
}
}
/* Initialize boundary regions */
for (i = 0; i < rows + 2; i++) {
x[IDX(nb, i, 0)] = b.west;
x[IDX(nb, i, n + 1)] = b.east;
new_x[IDX(nb, i, 0)] = b.west;
new_x[IDX(nb, i, n + 1)] = b.east;
}
if (rank == 0) {
for (i = 1; i <= n + 1; i++) {
x[IDX(nb, 0, i)] = b.north;
new_x[IDX(nb, 0, i)] = b.north;
}
}
if (rank == numprocs - 1){
for (i = 1; i < n + 1; i++) {
x[IDX(nb, rows + 1, i)] = b.south;
new_x[IDX(nb, rows + 1, i)] = b.south;
}
}
/* LOG(printf("[Process %d/%d] matrix initialized\n", rank, numprocs)); */
@ -69,11 +77,14 @@ double *compute_jacobi(int rank, int numprocs, int n, double init_value, double
global_max_diff = 0;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= n; j++) {
new_x = 0.25 * (x[IDX(nb, i - 1, j)] + x[IDX(nb, i, j + 1)] + x[IDX(nb, i + 1, j)] + x[IDX(nb, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_x - x[IDX(nb, i, j)]));
x[IDX(nb, i, j)] = new_x;
new_value = 0.25 * (x[IDX(nb, i - 1, j)] + x[IDX(nb, i, j + 1)] + x[IDX(nb, i + 1, j)] + x[IDX(nb, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_value - x[IDX(nb, i, j)]));
new_x[IDX(nb, i, j)] = new_value;
}
}
tmp_x = new_x;
new_x = x;
x = tmp_x;
if (rank != numprocs - 1) {
// Receive south border
MPI_Recv(&x[IDX(nb, rows + 1, 0)], nb, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);

View File

@ -10,17 +10,24 @@
double *compute_jacobi(int n, double init_value, double threshold, borders b, int *iterations) {
double *x;
double max_diff, new_x;
double *new_x;
double *tmp_x;
double max_diff, new_value;
int i, j;
int nb = n + 2; // n plus the border
/* Initialize boundary regions */
x = create_sa_matrix(n + 2, n + 2);
for (i = 1; i <= n; i++) {
x = create_sa_matrix(nb, nb);
new_x = create_sa_matrix(nb, nb);
for (i = 0; i < nb; i++) {
x[IDX(nb, 0, i)] = b.north;
x[IDX(nb, n + 1, i)] = b.south;
x[IDX(nb, i, 0)] = b.west;
x[IDX(nb, i, n + 1)] = b.east;
new_x[IDX(nb, 0, i)] = b.north;
new_x[IDX(nb, n + 1, i)] = b.south;
new_x[IDX(nb, i, 0)] = b.west;
new_x[IDX(nb, i, n + 1)] = b.east;
}
/* Initialize the rest of the matrix */
for (i = 1; i <= n; i++) {
@ -29,18 +36,21 @@ double *compute_jacobi(int n, double init_value, double threshold, borders b, in
}
}
/* Iterative refinement of x until values converge */
omp_set_num_threads(4);
omp_set_num_threads(2);
*iterations = 0;
do {
max_diff = 0;
#pragma omp parallel for schedule(static, 20) reduction (max:max_diff) private(new_x, j) firstprivate(n, nb) shared(x)
#pragma omp parallel for schedule(static, 20) reduction (max:max_diff) private(new_value, j) firstprivate(n, nb) shared(x, new_x)
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
new_x = 0.25 * (x[IDX(nb, i - 1, j)] + x[IDX(nb, i, j + 1)] + x[IDX(nb, i + 1, j)] + x[IDX(nb, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_x - x[IDX(nb, i, j)]));
x[IDX(nb, i, j)] = new_x;
new_value = 0.25 * (x[IDX(nb, i - 1, j)] + x[IDX(nb, i, j + 1)] + x[IDX(nb, i + 1, j)] + x[IDX(nb, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_value - x[IDX(nb, i, j)]));
new_x[IDX(nb, i, j)] = new_value;
}
}
tmp_x = new_x;
new_x = x;
x = tmp_x;
(*iterations)++;
} while (max_diff > threshold);
return x;

View File

@ -10,17 +10,24 @@
double *compute_jacobi(int n, double init_value, double threshold, borders b, int *iterations) {
double *x;
double max_diff, new_x;
double *new_x;
double *tmp_x;
double max_diff, new_value;
int i, j;
int nb = n + 2; // n plus the border
/* Initialize boundary regions */
x = create_sa_matrix(n + 2, n + 2);
for (i = 1; i <= n; i++) {
x = create_sa_matrix(nb, nb);
new_x = create_sa_matrix(nb, nb);
for (i = 0; i < nb; i++) {
x[IDX(nb, 0, i)] = b.north;
x[IDX(nb, n + 1, i)] = b.south;
x[IDX(nb, i, 0)] = b.west;
x[IDX(nb, i, n + 1)] = b.east;
new_x[IDX(nb, 0, i)] = b.north;
new_x[IDX(nb, n + 1, i)] = b.south;
new_x[IDX(nb, i, 0)] = b.west;
new_x[IDX(nb, i, n + 1)] = b.east;
}
/* Initialize the rest of the matrix */
for (i = 1; i <= n; i++) {
@ -34,11 +41,14 @@ double *compute_jacobi(int n, double init_value, double threshold, borders b, in
max_diff = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
new_x = 0.25 * (x[IDX(nb, i - 1, j)] + x[IDX(nb, i, j + 1)] + x[IDX(nb, i + 1, j)] + x[IDX(nb, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_x - x[IDX(nb, i, j)]));
x[IDX(nb, i, j)] = new_x;
new_value = 0.25 * (x[IDX(nb, i - 1, j)] + x[IDX(nb, i, j + 1)] + x[IDX(nb, i + 1, j)] + x[IDX(nb, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_value - x[IDX(nb, i, j)]));
new_x[IDX(nb, i, j)] = new_value;
}
}
tmp_x = new_x;
new_x = x;
x = tmp_x;
(*iterations)++;
} while (max_diff > threshold);
return x;