Heap memory for matrix and elapsed time
This commit is contained in:
parent
85c3977901
commit
784416b7ca
|
@ -1,12 +1,15 @@
|
||||||
CC=mpicc
|
CC=mpicc
|
||||||
CFLAGS=-Wall -lm -std=c99
|
CFLAGS=-Wall -lm -std=c99
|
||||||
|
|
||||||
all: config jacobi_mpi_line.c
|
all: config utils jacobi_mpi_line.c
|
||||||
${CC} ${CFLAGS} config.o jacobi_mpi_line.c -o jacobi.out
|
${CC} ${CFLAGS} config.o utils.o jacobi_mpi_line.c -o jacobi.out
|
||||||
|
|
||||||
config: ../config/config.c
|
config: ../config/config.c
|
||||||
${CC} -c ${CFLAGS} ../config/config.c
|
${CC} -c ${CFLAGS} ../config/config.c
|
||||||
|
|
||||||
|
utils: ../utils/utils.c
|
||||||
|
${CC} -c ${CFLAGS} ../utils/utils.c
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm -f *.out *.o
|
rm -f *.out *.o
|
||||||
|
|
|
@ -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 4
|
N 5000
|
||||||
|
|
||||||
# The value at each border.
|
# The value at each border.
|
||||||
NORTH 0.0
|
NORTH 0.0
|
||||||
|
|
|
@ -6,18 +6,11 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <mpi.h>
|
#include <mpi.h>
|
||||||
#include "../config/config.h"
|
#include "../config/config.h"
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
|
||||||
#define TAG_BORDER 0
|
#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 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 main(int argc, char* argv[]) {
|
||||||
int rank, numprocs;
|
int rank, numprocs;
|
||||||
|
@ -65,13 +58,22 @@ int main(int argc, char* argv[]) {
|
||||||
} else {
|
} else {
|
||||||
rows = n / numprocs;
|
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;
|
double max_diff, global_max_diff, new_x;
|
||||||
int i, j;
|
int i, j, iterations;
|
||||||
MPI_Status status;
|
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 */
|
/* Initialize the matrix */
|
||||||
|
x = create_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[i][j] = init_value;
|
x[i][j] = init_value;
|
||||||
|
@ -86,12 +88,15 @@ int main(int argc, char* argv[]) {
|
||||||
for (i = 1; i <= n + 1; i++) {
|
for (i = 1; i <= n + 1; i++) {
|
||||||
x[0][i] = b.north;
|
x[0][i] = b.north;
|
||||||
}
|
}
|
||||||
} else if (rank == numprocs - 1){
|
}
|
||||||
|
if (rank == numprocs - 1){
|
||||||
for (i = 1; i < n + 1; i++) {
|
for (i = 1; i < n + 1; i++) {
|
||||||
x[rows + 1][i] = b.south;
|
x[rows + 1][i] = b.south;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* LOG(printf("[Process %d/%d] matrix initialized\n", rank, numprocs)); */
|
||||||
/* Iterative refinement of x until values converge */
|
/* Iterative refinement of x until values converge */
|
||||||
|
iterations = 0;
|
||||||
do {
|
do {
|
||||||
max_diff = 0;
|
max_diff = 0;
|
||||||
global_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);
|
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);
|
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);
|
} 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();
|
MPI_Finalize();
|
||||||
|
|
||||||
return 0;
|
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);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
CC=gcc
|
CC=mpicc
|
||||||
CFLAGS=-Wall -lm -std=c99
|
CFLAGS=-Wall -lm -std=c99
|
||||||
|
|
||||||
all: config jacobi_sequential.c
|
all: config utils jacobi_sequential.c
|
||||||
${CC} ${CFLAGS} config.o jacobi_sequential.c -o jacobi.out
|
${CC} ${CFLAGS} config.o utils.o jacobi_sequential.c -o jacobi.out
|
||||||
|
|
||||||
config: ../config/config.c
|
config: ../config/config.c
|
||||||
${CC} -c ${CFLAGS} ../config/config.c
|
${CC} -c ${CFLAGS} ../config/config.c
|
||||||
|
|
||||||
|
utils: ../utils/utils.c
|
||||||
|
${CC} -c ${CFLAGS} ../utils/utils.c
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm -f *.out *.o
|
rm -f *.out *.o
|
||||||
|
|
|
@ -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 10
|
N 5000
|
||||||
|
|
||||||
# The value at each border.
|
# The value at each border.
|
||||||
NORTH 0.0
|
NORTH 0.0
|
||||||
|
|
|
@ -5,21 +5,25 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <mpi.h>
|
||||||
#include "../config/config.h"
|
#include "../config/config.h"
|
||||||
|
#include "../utils/utils.h"
|
||||||
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 compute_jacobi(int n, double init_value, double threshold, borders b);
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
int numprocs;
|
||||||
int config_loaded;
|
int config_loaded;
|
||||||
configuration config;
|
configuration config;
|
||||||
borders b;
|
borders b;
|
||||||
|
double startwtime = 0.0, endwtime;
|
||||||
|
|
||||||
|
MPI_Init(&argc, &argv);
|
||||||
|
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
|
||||||
|
|
||||||
|
if (numprocs != 1) {
|
||||||
|
MPI_Abort(MPI_COMM_WORLD, 1);
|
||||||
|
}
|
||||||
|
|
||||||
config_loaded = load_config(&config);
|
config_loaded = load_config(&config);
|
||||||
if (config_loaded != 0) {
|
if (config_loaded != 0) {
|
||||||
|
@ -31,17 +35,24 @@ int main(int argc, char* argv[]) {
|
||||||
b.south = config.south;
|
b.south = config.south;
|
||||||
b.west = config.west;
|
b.west = config.west;
|
||||||
|
|
||||||
|
startwtime = MPI_Wtime();
|
||||||
compute_jacobi(config.n, config.init_value, config.threshold, b);
|
compute_jacobi(config.n, config.init_value, config.threshold, b);
|
||||||
|
|
||||||
|
endwtime = MPI_Wtime();
|
||||||
|
printf("Wall clock time: %fs\n", endwtime - startwtime);
|
||||||
|
|
||||||
|
MPI_Finalize();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void compute_jacobi(int n, double init_value, double threshold, borders b) {
|
void compute_jacobi(int n, double init_value, double threshold, borders b) {
|
||||||
double x[n + 2][n + 2];
|
double **x;
|
||||||
double max_diff, new_x;
|
double max_diff, new_x;
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
/* Initialize boundary regions */
|
/* Initialize boundary regions */
|
||||||
|
x = create_matrix(n + 2, n + 2);
|
||||||
for (i = 1; i <= n; i++) {
|
for (i = 1; i <= n; i++) {
|
||||||
x[0][i] = b.north;
|
x[0][i] = b.north;
|
||||||
x[n + 1][i] = b.south;
|
x[n + 1][i] = b.south;
|
||||||
|
@ -65,4 +76,6 @@ void compute_jacobi(int n, double init_value, double threshold, borders b) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (max_diff > threshold);
|
} while (max_diff > threshold);
|
||||||
|
|
||||||
|
destroy_matrix(x, n + 2);
|
||||||
}
|
}
|
||||||
|
|
34
utils/utils.c
Normal file
34
utils/utils.c
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
double **create_matrix(int rows, int cols) {
|
||||||
|
int i;
|
||||||
|
double **x;
|
||||||
|
|
||||||
|
x = (double **) malloc(rows * sizeof(double));
|
||||||
|
for (i = 0; i < rows; i++) {
|
||||||
|
x[i] = (double *) malloc(cols * sizeof(double));
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_matrix(double **x, int rows) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < rows; i++) {
|
||||||
|
free(x[i]);
|
||||||
|
}
|
||||||
|
free(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_matrix(double **x, int rows, int 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);
|
||||||
|
}
|
22
utils/utils.h
Normal file
22
utils/utils.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* #define ENABLE_LOG */
|
||||||
|
|
||||||
|
#ifdef ENABLE_LOG
|
||||||
|
# define LOG(x) x
|
||||||
|
#else
|
||||||
|
# define LOG(x) (void) 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct borders {
|
||||||
|
double north;
|
||||||
|
double east;
|
||||||
|
double south;
|
||||||
|
double west;
|
||||||
|
} borders;
|
||||||
|
|
||||||
|
double **create_matrix(int rows, int cols);
|
||||||
|
|
||||||
|
void print_matrix(double **x, int rows, int cols);
|
||||||
|
void destroy_matrix(double **x, int rows);
|
Loading…
Reference in New Issue
Block a user