From 33d096f131006873f6261c0fe99de80aa5dfa701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ko=CC=88hler?= Date: Fri, 30 Aug 2024 17:25:02 +0200 Subject: [PATCH 1/5] Vect: add const version of array accessor --- Vect.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Vect.h b/Vect.h index 37b9e4d..378f6c0 100644 --- a/Vect.h +++ b/Vect.h @@ -181,6 +181,7 @@ class CLin_Vector inline CLin_subscript dim() const { return m_iN; } inline CLin_subscript size() const { return m_iN; } inline double* array() { return m_dV; } + inline const double* array() const { return m_dV; } // // methods From ea5659994a3f32c97a5a2a9634ea64f546ce2986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ko=CC=88hler?= Date: Fri, 30 Aug 2024 17:32:27 +0200 Subject: [PATCH 2/5] Support BLAS / OpenBLAS / Accelerate in build process --- CMakeLists.txt | 31 ++++++++++++++++++++++--------- Linalg.h | 5 +++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9061aec..5e154ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,5 @@ +cmake_minimum_required(VERSION 3.20) + set(LinAlgebra_Sources Linalg.h LUDecomp.cpp @@ -6,18 +8,29 @@ set(LinAlgebra_Sources Mtx.h Range.h Vect.cpp - Vect.h) + Vect.h +) -#set(LinAlgebra_Required_Libs -# debug MSVCRTD.LIB -# debug MSVCPRTD.LIB -# optimized MSVCRT.LIB -# optimized MSVCPRT.LIB) +set(LinAlgebra_Required_Libs +) +if (APPLE) + find_library(ACCELERATE_FRAMEWORK Accelerate) + if (ACCELERATE_FRAMEWORK) + message(STATUS "Accelerate framework found") + add_compile_definitions(LINALG_USE_ACCELERATE ACCELERATE_LAPACK_ILP64=1) + add_compile_options(-DACCELERATE_NEW_LAPACK) + else() + message(WARNING "Accelerate framework not found") + endif() +endif() -add_library(LinAlgebra ${LinAlgebra_Sources}) -# libraries that needs to be linked to the present library -#target_link_libraries(LinAlgebra ${LinAlgebra_Required_Libs}) +find_package(BLAS REQUIRED) +include_directories(${BLAS_INCLUDE_DIRS}) +set(LinAlgebra_Required_Libs ${LinAlgebra_Required_Libs} ${BLAS_LIBRARIES}) +add_library(LinAlgebra ${LinAlgebra_Sources}) +# libraries that needs to be linked to the present library +target_link_libraries(LinAlgebra ${LinAlgebra_Required_Libs}) diff --git a/Linalg.h b/Linalg.h index b3bfdeb..aa0d2aa 100644 --- a/Linalg.h +++ b/Linalg.h @@ -74,6 +74,11 @@ inline void CLin_assert(bool a) { _ASSERT(a); } inline void CLin_assert(bool ) { } #endif +#if defined(LINALG_USE_ACCELERATE) +# include +#else +# include +#endif #endif // LINALG_H From d121ed623334b78bde476e6b9b65833a1564d6e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ko=CC=88hler?= Date: Fri, 30 Aug 2024 17:33:03 +0200 Subject: [PATCH 3/5] Vect: rewrote dot_prod with faster BLAS-based impl --- Vect.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Vect.h b/Vect.h index 378f6c0..69ae8a5 100644 --- a/Vect.h +++ b/Vect.h @@ -418,13 +418,15 @@ inline double dot_prod(const CLin_Vector &A, const CLin_Vector &B) CLin_subscript N = A.dim(); CLin_assert(N == B.dim()); - CLin_subscript i; - double sum = 0; - - for (i=0; i Date: Fri, 30 Aug 2024 22:09:54 +0200 Subject: [PATCH 4/5] Mtx: added array() accessor to underlying double* buffer --- Mtx.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Mtx.h b/Mtx.h index b228c87..0cf65e3 100644 --- a/Mtx.h +++ b/Mtx.h @@ -88,6 +88,9 @@ class CLin_Matrix inline CLin_subscript size() const { return m_iMN; } + inline double *array() { return m_pV; } + + inline const double *array() const { return m_pV; } // returns # of rows or cols according to parameter d (1=rows, 2=cols, other=0) inline CLin_subscript dim(CLin_subscript d) { From 857f1ab8338e63db48cd1bffe37bf210fe93c8bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ko=CC=88hler?= Date: Fri, 30 Aug 2024 22:19:46 +0200 Subject: [PATCH 5/5] Mtx: rewrote matrix multiplication with faster BLAS-based impl --- Mtx.h | 136 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 70 insertions(+), 66 deletions(-) diff --git a/Mtx.h b/Mtx.h index 0cf65e3..43caf12 100644 --- a/Mtx.h +++ b/Mtx.h @@ -368,90 +368,94 @@ inline CLin_Vector matmult(const CLin_Matrix &A, const CLin_Vector &x) #endif CLin_subscript M, N; - CLin_subscript i, j; M = A.num_rows(); N = A.num_cols(); CLin_Vector tmp(M); - double sum; - - for (i=0; i