Generalized mpi line

This commit is contained in:
Fabio Salvini 2016-11-12 18:57:58 +01:00
parent 2111bfbadf
commit 85c3977901

View File

@ -33,10 +33,6 @@ int main(int argc, char* argv[]) {
MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
if (numprocs != 2) {
MPI_Abort(MPI_COMM_WORLD, 1);
}
if (rank == 0) { if (rank == 0) {
config_loaded = load_config(&config); config_loaded = load_config(&config);
if (config_loaded != 0) { if (config_loaded != 0) {
@ -62,12 +58,14 @@ int main(int argc, char* argv[]) {
b.south = south; b.south = south;
b.east = east; b.east = east;
b.west = west; b.west = west;
if (n != 4) {
MPI_Abort(MPI_COMM_WORLD, 1);
}
int rows = 2; int rows;
if (rank == 0) {
rows = n - (n / numprocs) * (numprocs - 1);
} else {
rows = n / numprocs;
}
double x[rows + 2][n + 2]; double x[rows + 2][n + 2];
double max_diff, global_max_diff, new_x; double max_diff, global_max_diff, new_x;
int i, j; int i, j;
@ -88,7 +86,7 @@ int main(int argc, char* argv[]) {
for (i = 1; i <= n + 1; i++) { for (i = 1; i <= n + 1; i++) {
x[0][i] = b.north; x[0][i] = b.north;
} }
} else if (rank == 1){ } else if (rank == numprocs - 1){
for (i = 1; i < n + 1; i++) { for (i = 1; i < n + 1; i++) {
x[rows + 1][i] = b.south; x[rows + 1][i] = b.south;
} }
@ -104,12 +102,26 @@ int main(int argc, char* argv[]) {
x[i][j] = new_x; x[i][j] = new_x;
} }
} }
if (rank == 0) { if (rank % 2 == 0) {
MPI_Send(&x[rows][0], n + 2, MPI_DOUBLE, 1, TAG_BORDER, MPI_COMM_WORLD); if (rank != numprocs - 1) {
MPI_Recv(&x[rows + 1][0], n + 2, MPI_DOUBLE, 1, TAG_BORDER, MPI_COMM_WORLD, &status); // Send and receive south border
MPI_Send(&x[rows][0], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
MPI_Recv(&x[rows + 1][0], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
}
if (rank != 0) {
// Send and receive north border
MPI_Send(&x[1][0], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
MPI_Recv(&x[0][0], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
}
} else { } else {
MPI_Recv(&x[0][0], n + 2, MPI_DOUBLE, 0, TAG_BORDER, MPI_COMM_WORLD, &status); // Receive and send north border
MPI_Send(&x[1][0], n + 2, MPI_DOUBLE, 0, TAG_BORDER, MPI_COMM_WORLD); MPI_Recv(&x[0][0], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
MPI_Send(&x[1][0], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
if (rank != numprocs - 1) {
// Receive and send south border
MPI_Recv(&x[rows + 1][0], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
MPI_Send(&x[rows][0], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
}
} }
MPI_Allreduce(&max_diff, &global_max_diff, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); MPI_Allreduce(&max_diff, &global_max_diff, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
} while (global_max_diff > threshold); } while (global_max_diff > threshold);
@ -121,13 +133,11 @@ int main(int argc, char* argv[]) {
void print_matrix(int rows, int cols, double x[rows][cols]) { void print_matrix(int rows, int cols, double x[rows][cols]) {
int i, j; int i, j;
printf("---------------------------------\n");
for (i = 0; i < rows; i++) { for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) { for (j = 0; j < cols; j++) {
printf("%f\t", x[i][j]); printf("%f\t", x[i][j]);
} }
printf("\n"); printf("\n");
} }
printf("---------------------------------\n");
fflush(stdout); fflush(stdout);
} }