From 829835b1ede07981027da493df0179638b29947d Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Sun, 19 May 2024 07:18:40 +0200 Subject: [PATCH] blas: add initial OpenBLAS exploration code Signed-off-by: Daniel Bevenius --- fundamentals/blas/openblas/.gitignore | 1 + fundamentals/blas/openblas/Makefile | 20 ++++++++++ fundamentals/blas/openblas/README.md | 15 +++++++ fundamentals/blas/openblas/src/gemm.c | 43 +++++++++++++++++++++ fundamentals/blas/openblas/src/vector_add.c | 20 ++++++++++ 5 files changed, 99 insertions(+) create mode 100644 fundamentals/blas/openblas/.gitignore create mode 100644 fundamentals/blas/openblas/Makefile create mode 100644 fundamentals/blas/openblas/README.md create mode 100644 fundamentals/blas/openblas/src/gemm.c create mode 100644 fundamentals/blas/openblas/src/vector_add.c diff --git a/fundamentals/blas/openblas/.gitignore b/fundamentals/blas/openblas/.gitignore new file mode 100644 index 00000000..ba077a40 --- /dev/null +++ b/fundamentals/blas/openblas/.gitignore @@ -0,0 +1 @@ +bin diff --git a/fundamentals/blas/openblas/Makefile b/fundamentals/blas/openblas/Makefile new file mode 100644 index 00000000..4645231e --- /dev/null +++ b/fundamentals/blas/openblas/Makefile @@ -0,0 +1,20 @@ +CC = gcc +SRCDIR = src +BINDIR = bin + +SOURCES := $(wildcard $(SRCDIR)/*.c) +TARGETS := $(patsubst $(SRCDIR)/%.c, %, $(SOURCES)) + +all: $(TARGETS) + +$(TARGETS): % : $(SRCDIR)/%.c | bindir + $(CC) -o ${BINDIR}/$@ $< -lopenblas + +bindir: bin + +bin: + @mkdir -p $(BINDIR) + +.PHONY: clean +clean: + @${RM} -rf $(BINDIR) diff --git a/fundamentals/blas/openblas/README.md b/fundamentals/blas/openblas/README.md new file mode 100644 index 00000000..f947f22e --- /dev/null +++ b/fundamentals/blas/openblas/README.md @@ -0,0 +1,15 @@ +## OpenBLAS +This is an implementation of BLAS for CPUs. + + +### Installation +```console +$ sudo apt update +$ sudo apt install libopenblas-dev +``` + +### Building +```console +$ make vector_add +``` + diff --git a/fundamentals/blas/openblas/src/gemm.c b/fundamentals/blas/openblas/src/gemm.c new file mode 100644 index 00000000..e232a765 --- /dev/null +++ b/fundamentals/blas/openblas/src/gemm.c @@ -0,0 +1,43 @@ +#include +#include + +int main() { + const int M = 2; // Number of rows of matrix A and C + const int N = 2; // Number of columns of matrix B and C + const int K = 3; // Number of columns of matrix A and rows of matrix B + + float alpha = 1.0f; + float beta = 0.0f; + + // Declare matrices A, B, and C + float A[M*K]; + float B[K*N]; + float C[M*N]; + + // Initialize matrices A and B with given values + A[0] = 1; A[1] = 2; A[2] = 3; + A[3] = 4; A[4] = 5; A[5] = 6; + + B[0] = 1; B[1] = 2; + B[2] = 3; B[3] = 4; + B[4] = 5; B[5] = 6; + + // Initialize matrix C with initial non-zero values + C[0] = 0; C[1] = 0; + C[2] = 0; C[3] = 0; + + // Perform matrix multiplication C = alpha * A * B + beta * C + cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, alpha, A, K, B, N, beta, C, N); + + // Print the resulting matrix C + printf("Resulting matrix C:\n"); + for (int i = 0; i < M; ++i) { + for (int j = 0; j < N; ++j) { + printf("%f ", C[i*N + j]); + } + printf("\n"); + } + + return 0; +} + diff --git a/fundamentals/blas/openblas/src/vector_add.c b/fundamentals/blas/openblas/src/vector_add.c new file mode 100644 index 00000000..21bf71fc --- /dev/null +++ b/fundamentals/blas/openblas/src/vector_add.c @@ -0,0 +1,20 @@ +#include +#include + +int main() { + int n = 5; + float x[] = {1, 2, 3, 4, 5}; + float y[] = {5, 4, 3, 2, 1}; + float alpha = 1.0; + + // y = alpha * x + y + cblas_saxpy(n, alpha, x, 1, y, 1); + + for (int i = 0; i < n; i++) { + printf("%f ", y[i]); + } + printf("\n"); + + return 0; +} +