Used macro instead of function to get the array index
This commit is contained in:
parent
c38550c8aa
commit
3c4dfda3ee
|
@ -1,7 +1,7 @@
|
||||||
# Configuration file for the Jacobi project.
|
# Configuration file for the Jacobi project.
|
||||||
|
|
||||||
# The size of the matrix (borders excluded).
|
# The size of the matrix (borders excluded).
|
||||||
N 5000
|
N 5
|
||||||
|
|
||||||
# The value at each border.
|
# The value at each border.
|
||||||
NORTH 0.0
|
NORTH 0.0
|
||||||
|
|
|
@ -69,7 +69,7 @@ int main(int argc, char* argv[]) {
|
||||||
endwtime = MPI_Wtime();
|
endwtime = MPI_Wtime();
|
||||||
printf("Wall clock time: %fs\n", endwtime - startwtime);
|
printf("Wall clock time: %fs\n", endwtime - startwtime);
|
||||||
printf("Iterations: %d\n", iterations);
|
printf("Iterations: %d\n", iterations);
|
||||||
/* print_sa_matrix(x, n + 2, n + 2); */
|
print_sa_matrix(x, n + 2, n + 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy_sa_matrix(x);
|
destroy_sa_matrix(x);
|
||||||
|
@ -99,22 +99,22 @@ double *compute_jacobi(int n, double init_value, double threshold, borders b, in
|
||||||
x = create_sa_matrix(rows + 2, n + 2);
|
x = create_sa_matrix(rows + 2, n + 2);
|
||||||
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[sa_index(n + 2, i, j)] = init_value;
|
x[IDX(n + 2, 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[sa_index(n + 2, i, 0)] = b.west;
|
x[IDX(n + 2, i, 0)] = b.west;
|
||||||
x[sa_index(n + 2, i, n + 1)] = b.east;
|
x[IDX(n + 2, 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[sa_index(n + 2, 0, i)] = b.north;
|
x[IDX(n + 2, 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[sa_index(n + 2, rows + 1, i)] = b.south;
|
x[IDX(n + 2, 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 +125,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[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)]);
|
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)]);
|
||||||
max_diff = (double) fmax(max_diff, fabs(new_x - x[sa_index(n + 2, i, j)]));
|
max_diff = (double) fmax(max_diff, fabs(new_x - x[IDX(n + 2, i, j)]));
|
||||||
x[sa_index(n + 2, i, j)] = new_x;
|
x[IDX(n + 2, 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[sa_index(n + 2, rows, 0)], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD);
|
MPI_Send(&x[IDX(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);
|
MPI_Recv(&x[IDX(n + 2, rows + 1, 0)], n + 2, 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[sa_index(n + 2, 1, 0)], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD);
|
MPI_Send(&x[IDX(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);
|
MPI_Recv(&x[IDX(n + 2, 0, 0)], n + 2, 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[sa_index(n + 2, 0, 0)], n + 2, MPI_DOUBLE, rank - 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
MPI_Recv(&x[IDX(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);
|
MPI_Send(&x[IDX(n + 2, 1, 0)], n + 2, 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[sa_index(n + 2, rows + 1, 0)], n + 2, MPI_DOUBLE, rank + 1, TAG_BORDER, MPI_COMM_WORLD, &status);
|
MPI_Recv(&x[IDX(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);
|
MPI_Send(&x[IDX(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)); */
|
/* LOG(printf("[Process %d/%d] max_diff: %f\n", rank, numprocs, max_diff)); */
|
||||||
|
@ -166,7 +166,7 @@ double *compute_jacobi(int n, double init_value, double threshold, borders b, in
|
||||||
if (i == numprocs - 1) {
|
if (i == numprocs - 1) {
|
||||||
rows_to_transmit++;
|
rows_to_transmit++;
|
||||||
}
|
}
|
||||||
MPI_Recv(&complete_x[sa_index(n + 2, receive_pos, 0)], rows_to_transmit * (n + 2), MPI_DOUBLE, i, TAG_MATRIX, MPI_COMM_WORLD, &status);
|
MPI_Recv(&complete_x[IDX(n + 2, receive_pos, 0)], rows_to_transmit * (n + 2), MPI_DOUBLE, i, TAG_MATRIX, MPI_COMM_WORLD, &status);
|
||||||
receive_pos += n / numprocs;
|
receive_pos += n / numprocs;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -175,7 +175,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[sa_index(n + 2, 1, 0)], rows_to_transmit * (n + 2), MPI_DOUBLE, 0, TAG_MATRIX, MPI_COMM_WORLD);
|
MPI_Send(&x[IDX(n + 2, 1, 0)], rows_to_transmit * (n + 2), MPI_DOUBLE, 0, TAG_MATRIX, MPI_COMM_WORLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
return complete_x;
|
return complete_x;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
|
||||||
double *create_sa_matrix(int rows, int cols) {
|
double *create_sa_matrix(int rows, int cols) {
|
||||||
double *x;
|
double *x;
|
||||||
|
@ -12,16 +13,11 @@ void destroy_sa_matrix(double *x) {
|
||||||
free(x);
|
free(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
int sa_index(int cols, int r, int c) {
|
|
||||||
return r * cols + c;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void print_sa_matrix(double *x, int rows, int cols) {
|
void print_sa_matrix(double *x, int rows, int cols) {
|
||||||
int i, j;
|
int i, j;
|
||||||
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[sa_index(cols, i, j)]);
|
printf("%f\t", x[IDX(cols, i, j)]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,13 @@
|
||||||
# define LOG(x) (void) 0
|
# define LOG(x) (void) 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Macro used with single array matrices to
|
||||||
|
* get the array index given the number of columns,
|
||||||
|
* the row index and the column index.
|
||||||
|
*/
|
||||||
|
#define IDX(cols, r, c) ((r) * (cols) + (c))
|
||||||
|
|
||||||
typedef struct borders {
|
typedef struct borders {
|
||||||
double north;
|
double north;
|
||||||
double east;
|
double east;
|
||||||
|
@ -27,10 +34,6 @@ double *create_sa_matrix(int rows, int cols);
|
||||||
*/
|
*/
|
||||||
void destroy_sa_matrix(double *x);
|
void destroy_sa_matrix(double *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);
|
int sa_index(int cols, int r, int c);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue
Block a user