From 8ff3e6cf650524253d52136cfe4cdcddf38a4b36 Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Wed, 23 Nov 2016 10:14:40 +0100 Subject: [PATCH] Used new matrix --- src/impl/mpi_line.c | 19 +++++++++++++++---- src/impl/mpi_line_async.c | 19 +++++++++++++++---- src/impl/omp.c | 26 ++++++++++++++++++-------- src/impl/sequential.c | 22 ++++++++++++++++------ 4 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/impl/mpi_line.c b/src/impl/mpi_line.c index c9e9a79..e7e8a4b 100644 --- a/src/impl/mpi_line.c +++ b/src/impl/mpi_line.c @@ -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 diff --git a/src/impl/mpi_line_async.c b/src/impl/mpi_line_async.c index 787f60e..b304251 100644 --- a/src/impl/mpi_line_async.c +++ b/src/impl/mpi_line_async.c @@ -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); diff --git a/src/impl/omp.c b/src/impl/omp.c index f57be1e..3a5a51f 100644 --- a/src/impl/omp.c +++ b/src/impl/omp.c @@ -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; diff --git a/src/impl/sequential.c b/src/impl/sequential.c index 5ebc09d..2767447 100644 --- a/src/impl/sequential.c +++ b/src/impl/sequential.c @@ -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;