You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Openblas.cmake 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. option (ENABLE_BLAS "Enable openblas for fast neural network processing [default: OFF]" OFF)
  2. IF(ENABLE_BLAS MATCHES "ON")
  3. ProcessPackage(BLAS OPTIONAL_INCLUDE LIBRARY openblas blas blis
  4. INCLUDE cblas.h INCLUDE_SUFFIXES include/openblas
  5. include/blas
  6. include/blis
  7. ROOT ${BLAS_ROOT_DIR}
  8. LIB_OUTPUT BLAS_REQUIRED_LIBRARIES)
  9. ProcessPackage(BLAS_LAPACK OPTIONAL_INCLUDE LIBRARY lapack
  10. INCLUDE cblas.h INCLUDE_SUFFIXES include/openblas
  11. include/blas
  12. include/blis
  13. ROOT ${BLAS_ROOT_DIR}
  14. LIB_OUTPUT BLAS_REQUIRED_LIBRARIES)
  15. ENDIF()
  16. IF(WITH_BLAS)
  17. MESSAGE(STATUS "Use openblas to accelerate kann")
  18. IF(NOT BLAS_INCLUDE)
  19. FIND_FILE(HAVE_CBLAS_H HINTS "${RSPAMD_SEARCH_PATH}"
  20. NAMES cblas.h
  21. DOC "Path to cblas.h header")
  22. IF(NOT HAVE_CBLAS_H)
  23. MESSAGE(STATUS "Blas header cblas.h has not been found, use internal workaround")
  24. ELSE()
  25. SET(HAVE_CBLAS_H 1)
  26. ENDIF()
  27. ELSE()
  28. SET(HAVE_CBLAS_H 1)
  29. ENDIF()
  30. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/sgemm.c" "
  31. #include <stddef.h>
  32. enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102 };
  33. enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112 };
  34. extern void cblas_sgemm(const enum CBLAS_ORDER Order,
  35. const enum CBLAS_TRANSPOSE TA,
  36. const enum CBLAS_TRANSPOSE TB,
  37. const int M, const int N, const int K,
  38. const float alpha, const float *A, const int lda,
  39. const float *B, const int ldb, const float beta,
  40. float *C, const int ldc);
  41. int main(int argc, char **argv)
  42. {
  43. cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 0, 0, 0, 0, NULL, 0,
  44. NULL, 0, 0, NULL, 0);
  45. return 0;
  46. }
  47. ")
  48. try_compile(HAVE_CBLAS_SGEMM
  49. ${CMAKE_CURRENT_BINARY_DIR}
  50. "${CMAKE_CURRENT_BINARY_DIR}/sgemm.c"
  51. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  52. LINK_LIBRARIES ${BLAS_REQUIRED_LIBRARIES}
  53. OUTPUT_VARIABLE SGEMM_ERR)
  54. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/saxpy.c" "
  55. #include <stddef.h>
  56. extern void cblas_saxpy(const int __N,
  57. const float __alpha, const float *__X, const int __incX, float *__Y, const int __incY);
  58. int main(int argc, char **argv)
  59. {
  60. cblas_saxpy(0, 0, NULL, 0, NULL, 0);
  61. return 0;
  62. }
  63. ")
  64. try_compile(HAVE_CBLAS_SAXPY
  65. ${CMAKE_CURRENT_BINARY_DIR}
  66. "${CMAKE_CURRENT_BINARY_DIR}/saxpy.c"
  67. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  68. LINK_LIBRARIES ${BLAS_REQUIRED_LIBRARIES}
  69. OUTPUT_VARIABLE SAXPY_ERR)
  70. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/openblas_set_num_threads.c" "
  71. #include <stddef.h>
  72. extern void openblas_set_num_threads(int num_threads);
  73. int main(int argc, char **argv)
  74. {
  75. openblas_set_num_threads(1);
  76. return 0;
  77. }
  78. ")
  79. try_compile(HAVE_OPENBLAS_SET_NUM_THREADS
  80. ${CMAKE_CURRENT_BINARY_DIR}
  81. "${CMAKE_CURRENT_BINARY_DIR}/openblas_set_num_threads.c"
  82. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  83. LINK_LIBRARIES ${BLAS_REQUIRED_LIBRARIES}
  84. OUTPUT_VARIABLE OPENBLAS_SET_NUM_THREADS_ERR)
  85. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/bli_thread_set_num_threads.c" "
  86. #include <stddef.h>
  87. extern void bli_thread_set_num_threads(int num_threads);
  88. int main(int argc, char **argv)
  89. {
  90. bli_thread_set_num_threads(1);
  91. return 0;
  92. }
  93. ")
  94. try_compile(HAVE_BLI_THREAD_SET_NUM_THREADS
  95. ${CMAKE_CURRENT_BINARY_DIR}
  96. "${CMAKE_CURRENT_BINARY_DIR}/bli_thread_set_num_threads.c"
  97. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  98. LINK_LIBRARIES ${BLAS_REQUIRED_LIBRARIES}
  99. OUTPUT_VARIABLE BLI_SET_NUM_THREADS_ERR)
  100. # Cmake is just brain damaged
  101. #CHECK_LIBRARY_EXISTS(${BLAS_REQUIRED_LIBRARIES} cblas_sgemm "" HAVE_CBLAS_SGEMM)
  102. if(HAVE_CBLAS_SGEMM)
  103. MESSAGE(STATUS "Blas has CBLAS sgemm")
  104. else()
  105. MESSAGE(STATUS "Blas has -NOT- CBLAS sgemm, use internal workaround: ${SGEMM_ERR}")
  106. endif()
  107. if(HAVE_CBLAS_SAXPY)
  108. MESSAGE(STATUS "Blas has CBLAS saxpy")
  109. else()
  110. MESSAGE(STATUS "Blas has -NOT- CBLAS saxpy, use internal workaround: ${SAXPY_ERR}")
  111. endif()
  112. SET(HAVE_CBLAS 1)
  113. ENDIF(WITH_BLAS)
  114. CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/blas-config.h.in" "${CMAKE_BINARY_DIR}/src/blas-config.h")