diff options
author | Juan Gomez Luna <juan.gomez@safari.ethz.ch> | 2021-06-16 19:46:05 +0200 |
---|---|---|
committer | Juan Gomez Luna <juan.gomez@safari.ethz.ch> | 2021-06-16 19:46:05 +0200 |
commit | 3de4b495fb176eba9a0eb517a4ce05903cb67acb (patch) | |
tree | fc6776a94549d2d4039898f183dbbeb2ce013ba9 /GEMV/baselines/cpu | |
parent | ef5c3688c486b80a56d3c1cded25f2b2387f2668 (diff) |
PrIM -- first commit
Diffstat (limited to 'GEMV/baselines/cpu')
-rw-r--r-- | GEMV/baselines/cpu/Makefile | 7 | ||||
-rw-r--r-- | GEMV/baselines/cpu/README | 9 | ||||
-rw-r--r-- | GEMV/baselines/cpu/gemv_openmp.c | 78 | ||||
-rw-r--r-- | GEMV/baselines/cpu/gemv_utils.h | 29 |
4 files changed, 123 insertions, 0 deletions
diff --git a/GEMV/baselines/cpu/Makefile b/GEMV/baselines/cpu/Makefile new file mode 100644 index 0000000..c779651 --- /dev/null +++ b/GEMV/baselines/cpu/Makefile @@ -0,0 +1,7 @@ +all: + gcc -o gemv -fopenmp gemv_openmp.c + +clean: + rm gemv + + diff --git a/GEMV/baselines/cpu/README b/GEMV/baselines/cpu/README new file mode 100644 index 0000000..92906c3 --- /dev/null +++ b/GEMV/baselines/cpu/README @@ -0,0 +1,9 @@ +Matrix-Vector Multiplication (GEMV) + +Compilation instructions: + + make + +Execution instructions + + ./gemv diff --git a/GEMV/baselines/cpu/gemv_openmp.c b/GEMV/baselines/cpu/gemv_openmp.c new file mode 100644 index 0000000..307e03b --- /dev/null +++ b/GEMV/baselines/cpu/gemv_openmp.c @@ -0,0 +1,78 @@ +#include <stdlib.h> +#include <stdio.h> +#include "../../support/timer.h" +#include "gemv_utils.h" + +int main(int argc, char *argv[]) +{ + const size_t rows = 20480; + const size_t cols = 8192; + + double **A, *b, *x; + + b = (double*) malloc(sizeof(double)*rows); + x = (double*) malloc(sizeof(double)*cols); + + allocate_dense(rows, cols, &A); + + make_hilbert_mat(rows,cols, &A); + +#pragma omp parallel + { +#pragma omp for + for (size_t i = 0; i < cols; i++) { + x[i] = (double) i+1 ; + } + +#pragma omp for + for (size_t i = 0; i < rows; i++) { + b[i] = (double) 0.0; + } + } + + Timer timer; + start(&timer, 0, 0); + + + gemv(A, x, rows, cols, &b); + + stop(&timer, 0); + + + printf("Kernel "); + print(&timer, 0, 1); + printf("\n"); + +#if 0 + print_vec(x, rows); + print_mat(A, rows, cols); + print_vec(b, rows); +#endif + + printf("sum(x) = %f, sum(Ax) = %f\n", sum_vec(x,cols), sum_vec(b,rows)); + return 0; +} + +void gemv(double** A, double* x, size_t rows, size_t cols, double** b) { +#pragma omp parallel for + for (size_t i = 0; i < rows; i ++ ) + for (size_t j = 0; j < cols; j ++ ) { + (*b)[i] = (*b)[i] + A[i][j]*x[j]; + } +} + +void make_hilbert_mat(size_t rows, size_t cols, double*** A) { +#pragma omp parallel for + for (size_t i = 0; i < rows; i++) { + for (size_t j = 0; j < cols; j++) { + (*A)[i][j] = 1.0/( (double) i + (double) j + 1.0); + } + } +} + +double sum_vec(double* vec, size_t rows) { + double sum = 0.0; +#pragma omp parallel for reduction(+:sum) + for (int i = 0; i < rows; i++) sum = sum + vec[i]; + return sum; +} diff --git a/GEMV/baselines/cpu/gemv_utils.h b/GEMV/baselines/cpu/gemv_utils.h new file mode 100644 index 0000000..605f148 --- /dev/null +++ b/GEMV/baselines/cpu/gemv_utils.h @@ -0,0 +1,29 @@ +void allocate_dense(size_t rows,size_t cols, double*** dense) { + + *dense = malloc(sizeof(double)*rows); + **dense = malloc(sizeof(double)*rows*cols); + + for (size_t i=0; i < rows; i++ ) { + (*dense)[i] = (*dense)[0] + i*cols; + } + +} + +void print_mat(double** A, size_t rows, size_t cols) { + for (size_t i = 0; i < rows; i++) { + for (size_t j = 0; j < cols; j++) { + printf("%f ", A[i][j]); + } + printf("\n"); + } +} + +void print_vec(double* b, size_t rows) { + for (size_t i = 0; i < rows; i++) { + printf("%f\n", b[i]); + } +} + +void gemv(double** A, double* x, size_t rows, size_t cols, double** b); +void make_hilbert_mat(size_t rows, size_t cols, double*** A); +double sum_vec(double* vec, size_t rows); |