Added single array matrix

This commit is contained in:
Fabio Salvini 2016-11-13 11:06:05 +01:00
parent 51442687a0
commit df121930ad
3 changed files with 52 additions and 23 deletions

View File

@ -10,7 +10,7 @@
#define TAG_BORDER 0
double **compute_jacobi(int n, double init_value, double threshold, borders b, int *rows, int *iterations);
double *compute_jacobi(int n, double init_value, double threshold, borders b, int *rows, int *iterations);
int rank;
int numprocs;
@ -22,7 +22,7 @@ int main(int argc, char* argv[]) {
borders b;
int config_loaded;
configuration config;
double **x;
double *x;
double startwtime = 0.0, endwtime;
int iterations;
int rows;
@ -70,15 +70,15 @@ int main(int argc, char* argv[]) {
printf("Iterations: %d\n", iterations);
}
destroy_matrix(x, rows);
destroy_sa_matrix(x);
MPI_Finalize();
return 0;
}
double **compute_jacobi(int n, double init_value, double threshold, borders b, int *rows, int *iterations) {
double **x;
double *compute_jacobi(int n, double init_value, double threshold, borders b, int *rows, int *iterations) {
double *x;
double max_diff, global_max_diff, new_x;
int i, j;
MPI_Status status;
@ -91,25 +91,25 @@ double **compute_jacobi(int n, double init_value, double threshold, borders b, i
LOG(printf("[Process %d/%d] rows: %d\n", rank, numprocs, *rows));
/* LOG(printf("[Process %d/%d] initializing matrix\n", rank, numprocs)); */
/* Initialize the matrix */
x = create_matrix(*rows + 2, n + 2);
x = create_sa_matrix(*rows + 2, n + 2);
for (i = 0; i < *rows + 2; i++) {
for (j = 1; j <= n; j++) {
x[i][j] = init_value;
x[sa_index(n + 2, i, j)] = init_value;
}
}
/* Initialize boundary regions */
for (i = 0; i < *rows + 2; i++) {
x[i][0] = b.west;
x[i][n + 1] = b.east;
x[sa_index(n + 2, i, 0)] = b.west;
x[sa_index(n + 2, i, n + 1)] = b.east;
}
if (rank == 0) {
for (i = 1; i <= n + 1; i++) {
x[0][i] = b.north;
x[sa_index(n + 2, 0, i)] = b.north;
}
}
if (rank == numprocs - 1){
for (i = 1; i < n + 1; i++) {
x[*rows + 1][i] = b.south;
x[sa_index(n + 2, *rows + 1, i)] = b.south;
}
}
/* LOG(printf("[Process %d/%d] matrix initialized\n", rank, numprocs)); */
@ -120,30 +120,30 @@ double **compute_jacobi(int n, double init_value, double threshold, borders b, i
global_max_diff = 0;
for (i = 1; i <= *rows; i++) {
for (j = 1; j <= n; j++) {
new_x = 0.25 * (x[i - 1][j] + x[i][j + 1] + x[i + 1][j] + x[i][j - 1]);
max_diff = (double) fmax(max_diff, fabs(new_x - x[i][j]));
x[i][j] = new_x;
new_x = 0.25 * (x[sa_index(n + 2, i - 1, j)] + x[sa_index(n + 2, i, j + 1)] + x[sa_index(n + 2, i + 1, j)] + x[sa_index(n + 2, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_x - x[sa_index(n + 2, i, j)]));
x[sa_index(n + 2, i, j)] = new_x;
}
}
if (rank % 2 == 0) {
if (rank != numprocs - 1) {
// 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);
MPI_Send(&x[sa_index(n + 2, *rows, 0)], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
MPI_Recv(&x[sa_index(n + 2, *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);
MPI_Send(&x[sa_index(n + 2, 1, 0)], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
MPI_Recv(&x[sa_index(n + 2, 0, 0)], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
}
} else {
// Receive and send north border
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);
MPI_Recv(&x[sa_index(n + 2, 0, 0)], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
MPI_Send(&x[sa_index(n + 2, 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_Recv(&x[sa_index(n + 2, *rows + 1, 0)], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
MPI_Send(&x[sa_index(n + 2, *rows, 0)], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
}
}
/* LOG(printf("[Process %d/%d] max_diff: %f\n", rank, numprocs, max_diff)); */

View File

@ -1,6 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
/*
* Create a matrix stored in a single array.
*/
double *create_sa_matrix(int rows, int cols) {
double *x;
x = (double *) malloc(rows * cols * sizeof(double));
return x;
}
/*
* Destroy a single array matrix.
*/
void destroy_sa_matrix(double *x) {
free(x);
}
/*
* Get the index for the single array matrix
* that correspond to the given row and column.
*/
int sa_index(int cols, int r, int c) {
return r * cols + c;
}
double **create_matrix(int rows, int cols) {
int i;

View File

@ -16,7 +16,12 @@ typedef struct borders {
double west;
} borders;
double *create_sa_matrix(int rows, int cols);
void destroy_sa_matrix(double *x);
int sa_index(int cols, int r, int c);
double **create_matrix(int rows, int cols);
void destroy_matrix(double **x, int rows);
void print_matrix(double **x, int rows, int cols);
void destroy_matrix(double **x, int rows);