Separated main

This commit is contained in:
Fabio Salvini 2016-11-19 14:49:50 +01:00
parent deb20e1382
commit 6b83411a49
5 changed files with 97 additions and 62 deletions

View File

@ -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

View File

@ -5,20 +5,15 @@
#include <stdio.h>
#include <math.h>
#include <mpi.h>
#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++) {

View File

@ -1 +0,0 @@
double *compute_jacobi(int rank, int numprocs, int n, double init_value, double threshold, borders b, int *iterations);

View File

@ -1,76 +1,42 @@
/*
* MPI version with the matrix subdivided by "lines".
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include <time.h>
#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;
}

72
src/main_mpi.c Normal file
View File

@ -0,0 +1,72 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#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;
}