Heap memory for matrix and elapsed time

This commit is contained in:
Fabio Salvini
2016-11-12 21:53:11 +01:00
parent 85c3977901
commit 784416b7ca
8 changed files with 115 additions and 37 deletions

View File

@@ -1,12 +1,15 @@
CC=mpicc
CFLAGS=-Wall -lm -std=c99
all: config jacobi_mpi_line.c
${CC} ${CFLAGS} config.o jacobi_mpi_line.c -o jacobi.out
all: config utils jacobi_mpi_line.c
${CC} ${CFLAGS} config.o utils.o jacobi_mpi_line.c -o jacobi.out
config: ../config/config.c
${CC} -c ${CFLAGS} ../config/config.c
utils: ../utils/utils.c
${CC} -c ${CFLAGS} ../utils/utils.c
.PHONY: clean
clean:
rm -f *.out *.o

View File

@@ -1,7 +1,7 @@
# Configuration file for the Jacobi project.
# The size of the matrix (borders excluded).
N 4
N 5000
# The value at each border.
NORTH 0.0

View File

@@ -6,18 +6,11 @@
#include <math.h>
#include <mpi.h>
#include "../config/config.h"
#include "../utils/utils.h"
#define TAG_BORDER 0
typedef struct borders {
double north;
double east;
double south;
double west;
} borders;
void compute_jacobi(int n, double init_value, double threshold, borders b);
void print_matrix(int rows, int cols, double x[rows][cols]);
int main(int argc, char* argv[]) {
int rank, numprocs;
@@ -65,13 +58,22 @@ int main(int argc, char* argv[]) {
} else {
rows = n / numprocs;
}
LOG(printf("[Process %d/%d] rows: %d\n", rank, numprocs, rows));
double x[rows + 2][n + 2];
double **x;
double max_diff, global_max_diff, new_x;
int i, j;
int i, j, iterations;
MPI_Status status;
double startwtime = 0.0, endwtime;
if (rank == 0) {
startwtime = MPI_Wtime();
}
/* LOG(printf("[Process %d/%d] initializing matrix\n", rank, numprocs)); */
/* Initialize the matrix */
x = create_matrix(rows + 2, n + 2);
for (i = 0; i < rows + 2; i++) {
for (j = 1; j <= n; j++) {
x[i][j] = init_value;
@@ -86,12 +88,15 @@ int main(int argc, char* argv[]) {
for (i = 1; i <= n + 1; i++) {
x[0][i] = b.north;
}
} else if (rank == numprocs - 1){
}
if (rank == numprocs - 1){
for (i = 1; i < n + 1; i++) {
x[rows + 1][i] = b.south;
}
}
/* LOG(printf("[Process %d/%d] matrix initialized\n", rank, numprocs)); */
/* Iterative refinement of x until values converge */
iterations = 0;
do {
max_diff = 0;
global_max_diff = 0;
@@ -123,21 +128,19 @@ int main(int argc, char* argv[]) {
MPI_Send(&x[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)); */
MPI_Allreduce(&max_diff, &global_max_diff, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
/* LOG(printf("[Process %d/%d] global_max_diff: %f\n", rank, numprocs, global_max_diff)); */
iterations++;
} while (global_max_diff > threshold);
if (rank == 0) {
endwtime = MPI_Wtime();
printf("Wall clock time: %fs\n", endwtime - startwtime);
printf("Iterations: %d\n", iterations);
}
MPI_Finalize();
return 0;
}
void print_matrix(int rows, int cols, double x[rows][cols]) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%f\t", x[i][j]);
}
printf("\n");
}
fflush(stdout);
}