OMP version

This commit is contained in:
Fabio Salvini 2016-11-19 18:57:05 +01:00
parent 74ed44be68
commit 906a47d4b3
5 changed files with 72 additions and 11 deletions

View File

@ -1,10 +1,12 @@
CC=mpicc CC=gcc
CC_OMP=gcc -fopenmp
CC_MPI=mpicc
CFLAGS=-Wall -lm -std=c99 CFLAGS=-Wall -lm -std=c99
SRC=src SRC=src
BUILD=build BUILD=build
BIN=bin BIN=bin
all: sequential mpi_line mpi_line_async all: sequential mpi_line mpi_line_async omp
sequential: config utils main sequential: config utils main
${CC} ${CFLAGS} \ ${CC} ${CFLAGS} \
@ -14,8 +16,16 @@ sequential: config utils main
${SRC}/impl/sequential.c \ ${SRC}/impl/sequential.c \
-o ${BIN}/jacobi_sequential -o ${BIN}/jacobi_sequential
omp: config utils main
${CC_OMP} ${CFLAGS} \
${BUILD}/config.o \
${BUILD}/utils.o \
${BUILD}/main.o \
${SRC}/impl/omp.c \
-o ${BIN}/jacobi_omp
mpi_line: config utils main_mpi mpi_line: config utils main_mpi
${CC} ${CFLAGS} \ ${CC_MPI} ${CFLAGS} \
${BUILD}/config.o \ ${BUILD}/config.o \
${BUILD}/utils.o \ ${BUILD}/utils.o \
${BUILD}/main_mpi.o \ ${BUILD}/main_mpi.o \
@ -23,7 +33,7 @@ mpi_line: config utils main_mpi
-o ${BIN}/jacobi_mpi_line -o ${BIN}/jacobi_mpi_line
mpi_line_async: config utils main_mpi mpi_line_async: config utils main_mpi
${CC} ${CFLAGS} \ ${CC_MPI} ${CFLAGS} \
${BUILD}/config.o \ ${BUILD}/config.o \
${BUILD}/utils.o \ ${BUILD}/utils.o \
${BUILD}/main_mpi.o \ ${BUILD}/main_mpi.o \
@ -31,22 +41,22 @@ mpi_line_async: config utils main_mpi
-o ${BIN}/jacobi_mpi_line_async -o ${BIN}/jacobi_mpi_line_async
main: ${SRC}/main.c main: ${SRC}/main.c
${CC} -c ${CFLAGS} \ ${CC_MPI} -c ${CFLAGS} \
${SRC}/main.c \ ${SRC}/main.c \
-o ${BUILD}/main.o -o ${BUILD}/main.o
main_mpi: ${SRC}/main_mpi.c main_mpi: ${SRC}/main_mpi.c
${CC} -c ${CFLAGS} \ ${CC_MPI} -c ${CFLAGS} \
${SRC}/main_mpi.c \ ${SRC}/main_mpi.c \
-o ${BUILD}/main_mpi.o -o ${BUILD}/main_mpi.o
config: ${SRC}/config.c config: ${SRC}/config.c
${CC} -c ${CFLAGS} \ ${CC_MPI} -c ${CFLAGS} \
${SRC}/config.c \ ${SRC}/config.c \
-o ${BUILD}/config.o -o ${BUILD}/config.o
utils: ${SRC}/utils.c utils: ${SRC}/utils.c
${CC} -c ${CFLAGS} \ ${CC_MPI} -c ${CFLAGS} \
${SRC}/utils.c \ ${SRC}/utils.c \
-o ${BUILD}/utils.o -o ${BUILD}/utils.o

View File

@ -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 10000 N 5
# The value at each border. # The value at each border.
NORTH 0.0 NORTH 0.0

47
src/impl/omp.c Normal file
View File

@ -0,0 +1,47 @@
/*
* OpenMP version.
*/
#include <stdio.h>
#include <math.h>
#include <omp.h>
#include "../config.h"
#include "../utils.h"
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
/* Initialize boundary regions */
x = create_sa_matrix(n + 2, n + 2);
for (i = 1; i <= n; i++) {
x[IDX(nb, 0, i)] = b.north;
x[IDX(nb, n + 1, i)] = b.south;
x[IDX(nb, i, 0)] = b.west;
x[IDX(nb, i, n + 1)] = b.east;
}
/* Initialize the rest of the matrix */
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
x[IDX(nb, i, j)] = init_value;
}
}
/* Iterative refinement of x until values converge */
omp_set_num_threads(4);
*iterations = 0;
do {
max_diff = 0;
#pragma omp parallel for schedule(static, 20) reduction (max:max_diff) private(new_x, j) firstprivate(n, nb) shared(x)
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
new_x = 0.25 * (x[IDX(nb, i - 1, j)] + x[IDX(nb, i, j + 1)] + x[IDX(nb, i + 1, j)] + x[IDX(nb, i, j - 1)]);
max_diff = (double) fmax(max_diff, fabs(new_x - x[IDX(nb, i, j)]));
x[IDX(nb, i, j)] = new_x;
}
}
(*iterations)++;
} while (max_diff > threshold);
return x;
}

View File

@ -35,7 +35,9 @@ int main(int argc, char* argv[]) {
printf("clock time: %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC); printf("clock time: %fs\n", (double)(endtime - starttime) / CLOCKS_PER_SEC);
printf("Iterations: %d\n", iterations); printf("Iterations: %d\n", iterations);
print_sa_matrix(x, n + 2, n + 2); if (n < 10) {
print_sa_matrix(x, n + 2, n + 2);
}
destroy_sa_matrix(x); destroy_sa_matrix(x);
return 0; return 0;

View File

@ -61,7 +61,9 @@ int main(int argc, char* argv[]) {
endwtime = MPI_Wtime(); endwtime = MPI_Wtime();
printf("Wall clock time: %fs\n", endwtime - startwtime); printf("Wall clock time: %fs\n", endwtime - startwtime);
printf("Iterations: %d\n", iterations); printf("Iterations: %d\n", iterations);
/* print_sa_matrix(x, n + 2, n + 2); */ if (n < 10) {
print_sa_matrix(x, n + 2, n + 2);
}
} }
destroy_sa_matrix(x); destroy_sa_matrix(x);