From 6b83411a49f943f882fb2421026b1e78203699d7 Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Sat, 19 Nov 2016 14:49:50 +0100 Subject: [PATCH] Separated main --- Makefile | 7 +++-- src/impl/sequential.c | 7 +---- src/jacobi.h | 1 - src/main.c | 72 ++++++++++++------------------------------- src/main_mpi.c | 72 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 62 deletions(-) delete mode 100644 src/jacobi.h create mode 100644 src/main_mpi.c diff --git a/Makefile b/Makefile index f87372f..a120085 100644 --- a/Makefile +++ b/Makefile @@ -9,12 +9,15 @@ all: sequential mpi_line sequential: config utils main ${CC} ${CFLAGS} ${BUILD}/config.o ${BUILD}/utils.o ${BUILD}/main.o ${SRC}/impl/sequential.c -o ${BIN}/jacobi_sequential -mpi_line: config utils main - ${CC} ${CFLAGS} ${BUILD}/config.o ${BUILD}/utils.o ${BUILD}/main.o ${SRC}/impl/mpi_line.c -o ${BIN}/jacobi_mpi_line +mpi_line: config utils main_mpi + ${CC} ${CFLAGS} ${BUILD}/config.o ${BUILD}/utils.o ${BUILD}/main_mpi.o ${SRC}/impl/mpi_line.c -o ${BIN}/jacobi_mpi_line main: ${SRC}/main.c ${CC} -c ${CFLAGS} ${SRC}/main.c -o ${BUILD}/main.o +main_mpi: ${SRC}/main_mpi.c + ${CC} -c ${CFLAGS} ${SRC}/main_mpi.c -o ${BUILD}/main_mpi.o + config: ${SRC}/config.c ${CC} -c ${CFLAGS} ${SRC}/config.c -o ${BUILD}/config.o diff --git a/src/impl/sequential.c b/src/impl/sequential.c index 441fc96..5ebc09d 100644 --- a/src/impl/sequential.c +++ b/src/impl/sequential.c @@ -5,20 +5,15 @@ #include #include -#include #include "../config.h" #include "../utils.h" -double *compute_jacobi(int rank, int numprocs, int n, double init_value, double threshold, borders b, int *iterations) { +double *compute_jacobi(int n, double init_value, double threshold, borders b, int *iterations) { double *x; double max_diff, new_x; int i, j; int nb = n + 2; // n plus the border - if (numprocs != 1) { - MPI_Abort(MPI_COMM_WORLD, 1); - } - /* Initialize boundary regions */ x = create_sa_matrix(n + 2, n + 2); for (i = 1; i <= n; i++) { diff --git a/src/jacobi.h b/src/jacobi.h deleted file mode 100644 index 74f34da..0000000 --- a/src/jacobi.h +++ /dev/null @@ -1 +0,0 @@ -double *compute_jacobi(int rank, int numprocs, int n, double init_value, double threshold, borders b, int *iterations); diff --git a/src/main.c b/src/main.c index 8d410e8..051358b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,76 +1,42 @@ -/* - * MPI version with the matrix subdivided by "lines". - */ - #include #include #include -#include +#include #include "config.h" #include "utils.h" -#include "jacobi.h" +double *compute_jacobi(int n, double init_value, double threshold, borders b, int *iterations); int main(int argc, char* argv[]) { - int rank; - int numprocs; int n; double init_value, threshold; - double north, south, east, west; borders b; int config_loaded; configuration config; double *x; - double startwtime = 0.0, endwtime; + clock_t starttime, endtime; int iterations; - 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; + config_loaded = load_config(&config); + if (config_loaded != 0) { + return 1; } - 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); + n = config.n; + threshold = config.threshold; + init_value = config.init_value; + b.north = config.north; + b.south = config.south; + b.east = config.east; + b.west = config.west; - b.north = north; - b.south = south; - b.east = east; - b.west = west; - - if (rank == 0) { - startwtime = MPI_Wtime(); - } - - x = compute_jacobi(rank, numprocs, n, init_value, threshold, b, &iterations); - - if (rank == 0) { - endwtime = MPI_Wtime(); - printf("Wall clock time: %fs\n", endwtime - startwtime); - printf("Iterations: %d\n", iterations); - print_sa_matrix(x, n + 2, n + 2); - } + starttime = clock(); + x = compute_jacobi(n, init_value, threshold, b, &iterations); + endtime = clock(); + printf("clock time: %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); + printf("Iterations: %d\n", iterations); + print_sa_matrix(x, n + 2, n + 2); destroy_sa_matrix(x); - MPI_Finalize(); - return 0; } diff --git a/src/main_mpi.c b/src/main_mpi.c new file mode 100644 index 0000000..f7994ed --- /dev/null +++ b/src/main_mpi.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include "config.h" +#include "utils.h" + +double *compute_jacobi(int rank, int numprocs, int n, double init_value, double threshold, borders b, int *iterations); + +int main(int argc, char* argv[]) { + int rank; + int numprocs; + int n; + double init_value, threshold; + double north, south, east, west; + borders b; + int config_loaded; + configuration config; + double *x; + double startwtime = 0.0, endwtime; + int iterations; + + 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; + + if (rank == 0) { + startwtime = MPI_Wtime(); + } + + x = compute_jacobi(rank, numprocs, n, init_value, threshold, b, &iterations); + + if (rank == 0) { + endwtime = MPI_Wtime(); + printf("Wall clock time: %fs\n", endwtime - startwtime); + printf("Iterations: %d\n", iterations); + print_sa_matrix(x, n + 2, n + 2); + } + + destroy_sa_matrix(x); + + MPI_Finalize(); + + return 0; +}