JacobiHPC/src/main.c

77 lines
1.7 KiB
C

/*
* MPI version with the matrix subdivided by "lines".
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include "config.h"
#include "utils.h"
#include "jacobi.h"
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;
}