2016-11-12 18:01:00 +01:00
|
|
|
/*
|
|
|
|
* MPI version with the matrix subdivided by "lines".
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2016-11-13 13:11:24 +01:00
|
|
|
#include <string.h>
|
2016-11-12 18:01:00 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <mpi.h>
|
|
|
|
#include "../config/config.h"
|
2016-11-12 21:53:11 +01:00
|
|
|
#include "../utils/utils.h"
|
2016-11-12 18:01:00 +01:00
|
|
|
|
|
|
|
#define TAG_BORDER 0
|
2016-11-13 13:11:24 +01:00
|
|
|
#define TAG_MATRIX 1
|
2016-11-12 18:01:00 +01:00
|
|
|
|
2016-11-13 13:11:24 +01:00
|
|
|
double *compute_jacobi(int n, double init_value, double threshold, borders b, int *iterations);
|
2016-11-13 10:39:28 +01:00
|
|
|
|
|
|
|
int rank;
|
|
|
|
int numprocs;
|
2016-11-12 18:01:00 +01:00
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
int n;
|
|
|
|
double init_value, threshold;
|
|
|
|
double north, south, east, west;
|
|
|
|
borders b;
|
|
|
|
int config_loaded;
|
|
|
|
configuration config;
|
2016-11-13 11:06:05 +01:00
|
|
|
double *x;
|
2016-11-13 10:39:28 +01:00
|
|
|
double startwtime = 0.0, endwtime;
|
|
|
|
int iterations;
|
2016-11-12 18:01:00 +01:00
|
|
|
|
|
|
|
MPI_Init(&argc, &argv);
|
|
|
|
|
|
|
|
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|
|
|
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
|
|
|
|
|
|
|
|
if (rank == 0) {
|
|
|
|
config_loaded = load_config(&config);
|
|
|
|
if (config_loaded != 0) {
|
|
|
|
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
|
|
}
|
|
|
|
n = config.n;
|
|
|
|
threshold = config.threshold;
|
|
|
|
init_value = config.init_value;
|
|
|
|
north = config.north;
|
|
|
|
south = config.south;
|
|
|
|
east = config.east;
|
|
|
|
west = config.west;
|
|
|
|
}
|
|
|
|
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|
|
|
MPI_Bcast(&init_value, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
|
|
|
MPI_Bcast(&threshold, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
|
|
|
MPI_Bcast(&north, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
|
|
|
MPI_Bcast(&south, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
|
|
|
MPI_Bcast(&east, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
|
|
|
MPI_Bcast(&west, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
|
|
|
|
|
|
|
b.north = north;
|
|
|
|
b.south = south;
|
|
|
|
b.east = east;
|
|
|
|
b.west = west;
|
2016-11-13 10:39:28 +01:00
|
|
|
|
|
|
|
if (rank == 0) {
|
|
|
|
startwtime = MPI_Wtime();
|
|
|
|
}
|
2016-11-12 18:01:00 +01:00
|
|
|
|
2016-11-13 13:11:24 +01:00
|
|
|
x = compute_jacobi(n, init_value, threshold, b, &iterations);
|
2016-11-13 10:39:28 +01:00
|
|
|
|
2016-11-12 18:57:58 +01:00
|
|
|
if (rank == 0) {
|
2016-11-13 10:39:28 +01:00
|
|
|
endwtime = MPI_Wtime();
|
|
|
|
printf("Wall clock time: %fs\n", endwtime - startwtime);
|
|
|
|
printf("Iterations: %d\n", iterations);
|
2016-11-17 14:14:59 +01:00
|
|
|
print_sa_matrix(x, n + 2, n + 2);
|
2016-11-12 18:57:58 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 11:06:05 +01:00
|
|
|
destroy_sa_matrix(x);
|
2016-11-13 10:39:28 +01:00
|
|
|
|
|
|
|
MPI_Finalize();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-13 13:11:24 +01:00
|
|
|
double *compute_jacobi(int n, double init_value, double threshold, borders b, int *iterations) {
|
|
|
|
double *complete_x;
|
2016-11-13 11:06:05 +01:00
|
|
|
double *x;
|
2016-11-12 18:01:00 +01:00
|
|
|
double max_diff, global_max_diff, new_x;
|
2016-11-13 10:39:28 +01:00
|
|
|
int i, j;
|
2016-11-17 14:24:56 +01:00
|
|
|
int nb = n + 2; // n plus the border
|
2016-11-13 13:11:24 +01:00
|
|
|
int rows, rows_to_transmit;
|
|
|
|
int receive_pos;
|
2016-11-12 18:01:00 +01:00
|
|
|
MPI_Status status;
|
2016-11-12 21:53:11 +01:00
|
|
|
|
|
|
|
if (rank == 0) {
|
2016-11-13 13:11:24 +01:00
|
|
|
rows = n - (n / numprocs) * (numprocs - 1);
|
2016-11-13 10:39:28 +01:00
|
|
|
} else {
|
2016-11-13 13:11:24 +01:00
|
|
|
rows = n / numprocs;
|
2016-11-12 21:53:11 +01:00
|
|
|
}
|
2016-11-13 13:11:24 +01:00
|
|
|
LOG(printf("[Process %d/%d] rows: %d\n", rank, numprocs, rows));
|
2016-11-12 21:53:11 +01:00
|
|
|
/* LOG(printf("[Process %d/%d] initializing matrix\n", rank, numprocs)); */
|
2016-11-12 18:01:00 +01:00
|
|
|
/* Initialize the matrix */
|
2016-11-17 14:24:56 +01:00
|
|
|
x = create_sa_matrix(rows + 2, nb);
|
2016-11-13 13:11:24 +01:00
|
|
|
for (i = 0; i < rows + 2; i++) {
|
2016-11-12 18:01:00 +01:00
|
|
|
for (j = 1; j <= n; j++) {
|
2016-11-17 14:24:56 +01:00
|
|
|
x[IDX(nb, i, j)] = init_value;
|
2016-11-12 18:01:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Initialize boundary regions */
|
2016-11-13 13:11:24 +01:00
|
|
|
for (i = 0; i < rows + 2; i++) {
|
2016-11-17 14:24:56 +01:00
|
|
|
x[IDX(nb, i, 0)] = b.west;
|
|
|
|
x[IDX(nb, i, n + 1)] = b.east;
|
2016-11-12 18:01:00 +01:00
|
|
|
}
|
|
|
|
if (rank == 0) {
|
|
|
|
for (i = 1; i <= n + 1; i++) {
|
2016-11-17 14:24:56 +01:00
|
|
|
x[IDX(nb, 0, i)] = b.north;
|
2016-11-12 18:01:00 +01:00
|
|
|
}
|
2016-11-12 21:53:11 +01:00
|
|
|
}
|
|
|
|
if (rank == numprocs - 1){
|
2016-11-12 18:01:00 +01:00
|
|
|
for (i = 1; i < n + 1; i++) {
|
2016-11-17 14:24:56 +01:00
|
|
|
x[IDX(nb, rows + 1, i)] = b.south;
|
2016-11-12 18:01:00 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-12 21:53:11 +01:00
|
|
|
/* LOG(printf("[Process %d/%d] matrix initialized\n", rank, numprocs)); */
|
2016-11-12 18:01:00 +01:00
|
|
|
/* Iterative refinement of x until values converge */
|
2016-11-13 10:39:28 +01:00
|
|
|
*iterations = 0;
|
2016-11-12 18:01:00 +01:00
|
|
|
do {
|
|
|
|
max_diff = 0;
|
|
|
|
global_max_diff = 0;
|
2016-11-13 13:11:24 +01:00
|
|
|
for (i = 1; i <= rows; i++) {
|
2016-11-12 18:01:00 +01:00
|
|
|
for (j = 1; j <= n; j++) {
|
2016-11-17 14:24:56 +01:00
|
|
|
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;
|
2016-11-12 18:01:00 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-12 18:57:58 +01:00
|
|
|
if (rank % 2 == 0) {
|
|
|
|
if (rank != numprocs - 1) {
|
|
|
|
// Send and receive south border
|
2016-11-17 14:24:56 +01:00
|
|
|
MPI_Send(&x[IDX(nb, rows, 0)], nb, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
|
|
|
|
MPI_Recv(&x[IDX(nb, rows + 1, 0)], nb, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
2016-11-12 18:57:58 +01:00
|
|
|
}
|
|
|
|
if (rank != 0) {
|
|
|
|
// Send and receive north border
|
2016-11-17 14:24:56 +01:00
|
|
|
MPI_Send(&x[IDX(nb, 1, 0)], nb, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
|
|
|
|
MPI_Recv(&x[IDX(nb, 0, 0)], nb, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
2016-11-12 18:57:58 +01:00
|
|
|
}
|
2016-11-12 18:01:00 +01:00
|
|
|
} else {
|
2016-11-12 18:57:58 +01:00
|
|
|
// Receive and send north border
|
2016-11-17 14:24:56 +01:00
|
|
|
MPI_Recv(&x[IDX(nb, 0, 0)], nb, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
|
|
|
MPI_Send(&x[IDX(nb, 1, 0)], nb, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
|
2016-11-12 18:57:58 +01:00
|
|
|
if (rank != numprocs - 1) {
|
|
|
|
// Receive and send south border
|
2016-11-17 14:24:56 +01:00
|
|
|
MPI_Recv(&x[IDX(nb, rows + 1, 0)], nb, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
|
|
|
MPI_Send(&x[IDX(nb, rows, 0)], nb, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
|
2016-11-12 18:57:58 +01:00
|
|
|
}
|
2016-11-12 18:01:00 +01:00
|
|
|
}
|
2016-11-12 21:53:11 +01:00
|
|
|
/* LOG(printf("[Process %d/%d] max_diff: %f\n", rank, numprocs, max_diff)); */
|
2016-11-12 18:01:00 +01:00
|
|
|
MPI_Allreduce(&max_diff, &global_max_diff, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
|
2016-11-12 21:53:11 +01:00
|
|
|
/* LOG(printf("[Process %d/%d] global_max_diff: %f\n", rank, numprocs, global_max_diff)); */
|
2016-11-13 10:39:28 +01:00
|
|
|
(*iterations)++;
|
2016-11-12 18:01:00 +01:00
|
|
|
} while (global_max_diff > threshold);
|
2016-11-13 13:11:24 +01:00
|
|
|
|
|
|
|
if (rank == 0) {
|
2016-11-17 14:24:56 +01:00
|
|
|
complete_x = create_sa_matrix(nb, nb);
|
|
|
|
memcpy(complete_x, x, (rows + ((rank == numprocs - 1) ? 2 : 1)) * (nb) * sizeof(double));
|
2016-11-13 13:11:24 +01:00
|
|
|
rows_to_transmit = n / numprocs;
|
|
|
|
receive_pos = rows + 1;
|
|
|
|
for (i = 1; i < numprocs; i++) {
|
|
|
|
if (i == numprocs - 1) {
|
|
|
|
rows_to_transmit++;
|
|
|
|
}
|
2016-11-17 14:24:56 +01:00
|
|
|
MPI_Recv(&complete_x[IDX(nb, receive_pos, 0)], rows_to_transmit * (nb), MPI_DOUBLE, i, TAG_MATRIX, MPI_COMM_WORLD, &status);
|
2016-11-13 13:11:24 +01:00
|
|
|
receive_pos += n / numprocs;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
complete_x = NULL;
|
|
|
|
rows_to_transmit = rows;
|
|
|
|
if (rank == numprocs - 1) {
|
|
|
|
rows_to_transmit++;
|
|
|
|
}
|
2016-11-17 14:24:56 +01:00
|
|
|
MPI_Send(&x[IDX(nb, 1, 0)], rows_to_transmit * (nb), MPI_DOUBLE, 0, TAG_MATRIX, MPI_COMM_WORLD);
|
2016-11-13 13:11:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return complete_x;
|
2016-11-12 18:01:00 +01:00
|
|
|
}
|