Removed all n + 2
This commit is contained in:
parent
3c4dfda3ee
commit
c59f5a4b9b
|
@ -84,6 +84,7 @@ double *compute_jacobi(int n, double init_value, double threshold, borders b, in
|
||||||
double *x;
|
double *x;
|
||||||
double max_diff, global_max_diff, new_x;
|
double max_diff, global_max_diff, new_x;
|
||||||
int i, j;
|
int i, j;
|
||||||
|
int nb = n + 2; // n plus the border
|
||||||
int rows, rows_to_transmit;
|
int rows, rows_to_transmit;
|
||||||
int receive_pos;
|
int receive_pos;
|
||||||
MPI_Status status;
|
MPI_Status status;
|
||||||
|
@ -96,25 +97,25 @@ double *compute_jacobi(int n, double init_value, double threshold, borders b, in
|
||||||
LOG(printf("[Process %d/%d] rows: %d\n", rank, numprocs, rows));
|
LOG(printf("[Process %d/%d] rows: %d\n", rank, numprocs, rows));
|
||||||
/* LOG(printf("[Process %d/%d] initializing matrix\n", rank, numprocs)); */
|
/* LOG(printf("[Process %d/%d] initializing matrix\n", rank, numprocs)); */
|
||||||
/* Initialize the matrix */
|
/* Initialize the matrix */
|
||||||
x = create_sa_matrix(rows + 2, n + 2);
|
x = create_sa_matrix(rows + 2, nb);
|
||||||
for (i = 0; i < rows + 2; i++) {
|
for (i = 0; i < rows + 2; i++) {
|
||||||
for (j = 1; j <= n; j++) {
|
for (j = 1; j <= n; j++) {
|
||||||
x[IDX(n + 2, i, j)] = init_value;
|
x[IDX(nb, i, j)] = init_value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Initialize boundary regions */
|
/* Initialize boundary regions */
|
||||||
for (i = 0; i < rows + 2; i++) {
|
for (i = 0; i < rows + 2; i++) {
|
||||||
x[IDX(n + 2, i, 0)] = b.west;
|
x[IDX(nb, i, 0)] = b.west;
|
||||||
x[IDX(n + 2, i, n + 1)] = b.east;
|
x[IDX(nb, i, n + 1)] = b.east;
|
||||||
}
|
}
|
||||||
if (rank == 0) {
|
if (rank == 0) {
|
||||||
for (i = 1; i <= n + 1; i++) {
|
for (i = 1; i <= n + 1; i++) {
|
||||||
x[IDX(n + 2, 0, i)] = b.north;
|
x[IDX(nb, 0, i)] = b.north;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (rank == numprocs - 1){
|
if (rank == numprocs - 1){
|
||||||
for (i = 1; i < n + 1; i++) {
|
for (i = 1; i < n + 1; i++) {
|
||||||
x[IDX(n + 2, rows + 1, i)] = b.south;
|
x[IDX(nb, rows + 1, i)] = b.south;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* LOG(printf("[Process %d/%d] matrix initialized\n", rank, numprocs)); */
|
/* LOG(printf("[Process %d/%d] matrix initialized\n", rank, numprocs)); */
|
||||||
|
@ -125,30 +126,30 @@ double *compute_jacobi(int n, double init_value, double threshold, borders b, in
|
||||||
global_max_diff = 0;
|
global_max_diff = 0;
|
||||||
for (i = 1; i <= rows; i++) {
|
for (i = 1; i <= rows; i++) {
|
||||||
for (j = 1; j <= n; j++) {
|
for (j = 1; j <= n; j++) {
|
||||||
new_x = 0.25 * (x[IDX(n + 2, i - 1, j)] + x[IDX(n + 2, i, j + 1)] + x[IDX(n + 2, i + 1, j)] + x[IDX(n + 2, i, j - 1)]);
|
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(n + 2, i, j)]));
|
max_diff = (double) fmax(max_diff, fabs(new_x - x[IDX(nb, i, j)]));
|
||||||
x[IDX(n + 2, i, j)] = new_x;
|
x[IDX(nb, i, j)] = new_x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (rank % 2 == 0) {
|
if (rank % 2 == 0) {
|
||||||
if (rank != numprocs - 1) {
|
if (rank != numprocs - 1) {
|
||||||
// Send and receive south border
|
// Send and receive south border
|
||||||
MPI_Send(&x[IDX(n + 2, rows, 0)], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
|
MPI_Send(&x[IDX(nb, rows, 0)], nb, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
|
||||||
MPI_Recv(&x[IDX(n + 2, rows + 1, 0)], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
MPI_Recv(&x[IDX(nb, rows + 1, 0)], nb, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
||||||
}
|
}
|
||||||
if (rank != 0) {
|
if (rank != 0) {
|
||||||
// Send and receive north border
|
// Send and receive north border
|
||||||
MPI_Send(&x[IDX(n + 2, 1, 0)], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
|
MPI_Send(&x[IDX(nb, 1, 0)], nb, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
|
||||||
MPI_Recv(&x[IDX(n + 2, 0, 0)], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
MPI_Recv(&x[IDX(nb, 0, 0)], nb, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Receive and send north border
|
// Receive and send north border
|
||||||
MPI_Recv(&x[IDX(n + 2, 0, 0)], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
MPI_Recv(&x[IDX(nb, 0, 0)], nb, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
||||||
MPI_Send(&x[IDX(n + 2, 1, 0)], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
|
MPI_Send(&x[IDX(nb, 1, 0)], nb, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
|
||||||
if (rank != numprocs - 1) {
|
if (rank != numprocs - 1) {
|
||||||
// Receive and send south border
|
// Receive and send south border
|
||||||
MPI_Recv(&x[IDX(n + 2, rows + 1, 0)], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
MPI_Recv(&x[IDX(nb, rows + 1, 0)], nb, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
||||||
MPI_Send(&x[IDX(n + 2, rows, 0)], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
|
MPI_Send(&x[IDX(nb, rows, 0)], nb, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* LOG(printf("[Process %d/%d] max_diff: %f\n", rank, numprocs, max_diff)); */
|
/* LOG(printf("[Process %d/%d] max_diff: %f\n", rank, numprocs, max_diff)); */
|
||||||
|
@ -158,15 +159,15 @@ double *compute_jacobi(int n, double init_value, double threshold, borders b, in
|
||||||
} while (global_max_diff > threshold);
|
} while (global_max_diff > threshold);
|
||||||
|
|
||||||
if (rank == 0) {
|
if (rank == 0) {
|
||||||
complete_x = create_sa_matrix(n + 2, n + 2);
|
complete_x = create_sa_matrix(nb, nb);
|
||||||
memcpy(complete_x, x, (rows + ((rank == numprocs - 1) ? 2 : 1)) * (n + 2) * sizeof(double));
|
memcpy(complete_x, x, (rows + ((rank == numprocs - 1) ? 2 : 1)) * (nb) * sizeof(double));
|
||||||
rows_to_transmit = n / numprocs;
|
rows_to_transmit = n / numprocs;
|
||||||
receive_pos = rows + 1;
|
receive_pos = rows + 1;
|
||||||
for (i = 1; i < numprocs; i++) {
|
for (i = 1; i < numprocs; i++) {
|
||||||
if (i == numprocs - 1) {
|
if (i == numprocs - 1) {
|
||||||
rows_to_transmit++;
|
rows_to_transmit++;
|
||||||
}
|
}
|
||||||
MPI_Recv(&complete_x[IDX(n + 2, receive_pos, 0)], rows_to_transmit * (n + 2), MPI_DOUBLE, i, TAG_MATRIX, MPI_COMM_WORLD, &status);
|
MPI_Recv(&complete_x[IDX(nb, receive_pos, 0)], rows_to_transmit * (nb), MPI_DOUBLE, i, TAG_MATRIX, MPI_COMM_WORLD, &status);
|
||||||
receive_pos += n / numprocs;
|
receive_pos += n / numprocs;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -175,7 +176,7 @@ double *compute_jacobi(int n, double init_value, double threshold, borders b, in
|
||||||
if (rank == numprocs - 1) {
|
if (rank == numprocs - 1) {
|
||||||
rows_to_transmit++;
|
rows_to_transmit++;
|
||||||
}
|
}
|
||||||
MPI_Send(&x[IDX(n + 2, 1, 0)], rows_to_transmit * (n + 2), MPI_DOUBLE, 0, TAG_MATRIX, MPI_COMM_WORLD);
|
MPI_Send(&x[IDX(nb, 1, 0)], rows_to_transmit * (nb), MPI_DOUBLE, 0, TAG_MATRIX, MPI_COMM_WORLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
return complete_x;
|
return complete_x;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user