commit 54daeb8: [Rework] Change and improve openblas detection and usage

Vsevolod Stakhov vsevolod at highsecure.ru
Wed Sep 2 12:07:06 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-09-02 13:05:58 +0100
URL: https://github.com/rspamd/rspamd/commit/54daeb811d83f5d4e29e208e7285c669d123f606 (HEAD -> master)

[Rework] Change and improve openblas detection and usage

---
 blas-config.h.in          | 10 ++++++++++
 cmake/Openblas.cmake      | 28 ++++++++++++++++++++++------
 contrib/kann/kautodiff.c  |  1 +
 src/libserver/cfg_utils.c | 10 ++++------
 src/libutil/util.c        | 12 ++----------
 src/lua/lua_tensor.c      |  1 +
 6 files changed, 40 insertions(+), 22 deletions(-)

diff --git a/blas-config.h.in b/blas-config.h.in
new file mode 100644
index 000000000..8d10ebf2e
--- /dev/null
+++ b/blas-config.h.in
@@ -0,0 +1,10 @@
+#ifndef RSPAMD_BLAS_CONFIG_H_IN
+#define RSPAMD_BLAS_CONFIG_H_IN
+
+#cmakedefine HAVE_CBLAS_SGEMM 1
+#cmakedefine HAVE_CBLAS_SAXPY 1
+#cmakedefine HAVE_OPENBLAS_SET_NUM_THREADS 1
+#cmakedefine HAVE_CBLAS_H 1
+#cmakedefine HAVE_CBLAS 1
+
+#endif
\ No newline at end of file
diff --git a/cmake/Openblas.cmake b/cmake/Openblas.cmake
index 7615408b2..042d00f8f 100644
--- a/cmake/Openblas.cmake
+++ b/cmake/Openblas.cmake
@@ -22,10 +22,10 @@ IF(WITH_BLAS)
         IF(NOT HAVE_CBLAS_H)
             MESSAGE(STATUS "Blas header cblas.h has not been found, use internal workaround")
         ELSE()
-            ADD_COMPILE_OPTIONS(-DHAVE_CBLAS_H)
+            SET(HAVE_CBLAS_H 1)
         ENDIF()
     ELSE()
-        ADD_COMPILE_OPTIONS(-DHAVE_CBLAS_H)
+        SET(HAVE_CBLAS_H 1)
     ENDIF()
     file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/sgemm.c" "
 #include <stddef.h>
@@ -68,19 +68,35 @@ int main(int argc, char **argv)
             LINK_LIBRARIES ${BLAS_REQUIRED_LIBRARIES}
             OUTPUT_VARIABLE SAXPY_ERR)
 
+    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/openblas_set_num_threads.c" "
+#include <stddef.h>
+extern void openblas_set_num_threads(int num_threads);
+int main(int argc, char **argv)
+{
+    openblas_set_num_threads(1);
+    return 0;
+}
+")
+    try_compile(HAVE_OPENBLAS_SET_NUM_THREADS
+            ${CMAKE_CURRENT_BINARY_DIR}
+            "${CMAKE_CURRENT_BINARY_DIR}/openblas_set_num_threads.c"
+            COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
+            LINK_LIBRARIES ${BLAS_REQUIRED_LIBRARIES}
+            OUTPUT_VARIABLE OPENBLAS_SET_NUM_THREADS_ERR)
+
     # Cmake is just brain damaged
     #CHECK_LIBRARY_EXISTS(${BLAS_REQUIRED_LIBRARIES} cblas_sgemm "" HAVE_CBLAS_SGEMM)
     if(HAVE_CBLAS_SGEMM)
         MESSAGE(STATUS "Blas has CBLAS sgemm")
-        ADD_COMPILE_OPTIONS(-DHAVE_CBLAS_SGEMM)
     else()
         MESSAGE(STATUS "Blas has -NOT- CBLAS sgemm, use internal workaround: ${SGEMM_ERR}")
     endif()
     if(HAVE_CBLAS_SAXPY)
         MESSAGE(STATUS "Blas has CBLAS saxpy")
-        ADD_COMPILE_OPTIONS(-DHAVE_CBLAS_SAXPY)
     else()
         MESSAGE(STATUS "Blas has -NOT- CBLAS saxpy, use internal workaround: ${SAXPY_ERR}")
     endif()
-    ADD_COMPILE_OPTIONS(-DHAVE_CBLAS)
-ENDIF(WITH_BLAS)
\ No newline at end of file
+    SET(HAVE_CBLAS 1)
+ENDIF(WITH_BLAS)
+
+CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/blas-config.h.in" "${CMAKE_BINARY_DIR}/src/blas-config.h")
\ No newline at end of file
diff --git a/contrib/kann/kautodiff.c b/contrib/kann/kautodiff.c
index f336c958b..d05cc00a4 100644
--- a/contrib/kann/kautodiff.c
+++ b/contrib/kann/kautodiff.c
@@ -7,6 +7,7 @@
 #include <float.h>
 #include <math.h>
 #include "kautodiff.h"
+#include "blas-config.h"
 
 typedef struct {
 	uint64_t s[2];
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index 6b3f3e15b..0b87bc985 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -55,6 +55,8 @@
 #endif
 #include <math.h>
 
+#include "blas-config.h"
+
 #define DEFAULT_SCORE 10.0
 
 #define DEFAULT_RLIMIT_NOFILE 2048
@@ -2782,13 +2784,9 @@ rspamd_free_zstd_dictionary (struct zstd_dictionary *dict)
 	}
 }
 
-#ifdef HAVE_CBLAS
-#ifdef HAVE_CBLAS_H
-#include "cblas.h"
-#else
+#ifdef HAVE_OPENBLAS_SET_NUM_THREADS
 extern void openblas_set_num_threads(int num_threads);
 #endif
-#endif
 
 gboolean
 rspamd_config_libs (struct rspamd_external_libs_ctx *ctx,
@@ -2892,7 +2890,7 @@ rspamd_config_libs (struct rspamd_external_libs_ctx *ctx,
 			ZSTD_freeCStream (ctx->out_zstream);
 			ctx->out_zstream = NULL;
 		}
-#ifdef HAVE_CBLAS
+#ifdef HAVE_OPENBLAS_SET_NUM_THREADS
 		openblas_set_num_threads (cfg->max_blas_threads);
 #endif
 	}
diff --git a/src/libutil/util.c b/src/libutil/util.c
index 9e5147ab5..1aed0f8b4 100644
--- a/src/libutil/util.c
+++ b/src/libutil/util.c
@@ -64,6 +64,7 @@
 
 #include "zlib.h"
 #include "contrib/uthash/utlist.h"
+#include "blas-config.h"
 
 /* Check log messages intensity once per minute */
 #define CHECK_TIME 60
@@ -1705,17 +1706,8 @@ void rspamd_gerror_free_maybe (gpointer p)
 
 
 
-#ifdef HAVE_CBLAS
-#ifdef HAVE_CBLAS_H
-#include "cblas.h"
-#else
-#ifdef __APPLE__
-/* OSX is pure evil: number of threads must be set via env: VECLIB_MAXIMUM_THREADS */
-void openblas_set_num_threads(int num_threads) {}
-#else
+#ifdef HAVE_OPENBLAS_SET_NUM_THREADS
 extern void openblas_set_num_threads(int num_threads);
-#endif
-#endif
 /*
  * Openblas creates threads that are not supported by
  * jemalloc allocator (aside of being bloody stupid). So this hack
diff --git a/src/lua/lua_tensor.c b/src/lua/lua_tensor.c
index 06b7cdffe..960fc31e0 100644
--- a/src/lua/lua_tensor.c
+++ b/src/lua/lua_tensor.c
@@ -17,6 +17,7 @@
 #include "lua_common.h"
 #include "lua_tensor.h"
 #include "contrib/kann/kautodiff.h"
+#include "blas-config.h"
 
 /***
  * @module rspamd_tensor


More information about the Commits mailing list