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

distributing.md 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # Redistributing OpenBLAS
  2. !!! note
  3. This document contains recommendations only - packagers and other
  4. redistributors are in charge of how OpenBLAS is built and distributed in their
  5. systems, and may have good reasons to deviate from the guidance given on this
  6. page. These recommendations are aimed at general packaging systems, with a user
  7. base that typically is large, open source (or freely available at least), and
  8. doesn't behave uniformly or that the packager is directly connected with.*
  9. OpenBLAS has a large number of build-time options which can be used to change
  10. how it behaves at runtime, how artifacts or symbols are named, etc. Variation
  11. in build configuration can be necessary to acheive a given end goal within a
  12. distribution or as an end user. However, such variation can also make it more
  13. difficult to build on top of OpenBLAS and ship code or other packages in a way
  14. that works across many different distros. Here we provide guidance about the
  15. most important build options, what effects they may have when changed, and
  16. which ones to default to.
  17. The Make and CMake build systems provide equivalent options and yield more or
  18. less the same artifacts, but not exactly (the CMake builds are still
  19. experimental). You can choose either one and the options will function in the
  20. same way, however the CMake outputs may require some renaming. To review
  21. available build options, see `Makefile.rule` or `CMakeLists.txt` in the root of
  22. the repository.
  23. Build options typically fall into two categories: (a) options that affect the
  24. user interface, such as library and symbol names or APIs that are made
  25. available, and (b) options that affect performance and runtime behavior, such
  26. as threading behavior or CPU architecture-specific code paths. The user
  27. interface options are more important to keep aligned between distributions,
  28. while for the performance-related options there are typically more reasons to
  29. make choices that deviate from the defaults.
  30. Here are recommendations for user interface related packaging choices where it
  31. is not likely to be a good idea to deviate (typically these are the default
  32. settings):
  33. 1. Include CBLAS. The CBLAS interface is widely used and it doesn't affect
  34. binary size much, so don't turn it off.
  35. 2. Include LAPACK and LAPACKE. The LAPACK interface is also widely used, and
  36. while it does make up a significant part of the binary size of the installed
  37. library, that does not outweigh the regression in usability when deviating
  38. from the default here.[^1]
  39. 3. Always distribute the pkg-config (`.pc`) and CMake `.cmake`) dependency
  40. detection files. These files are used by build systems when users want to
  41. link against OpenBLAS, and there is no benefit of leaving them out.
  42. 4. Provide the LP64 interface by default, and if in addition to that you choose
  43. to provide an ILP64 interface build as well, use a symbol suffix to avoid
  44. symbol name clashes (see the next section).
  45. [^1]: All major distributions do include LAPACK as of mid 2023 as far as we
  46. know. Older versions of Arch Linux did not, and that was known to cause
  47. problems.
  48. ## ILP64 interface builds
  49. The LP64 (32-bit integer) interface is the default build, and has
  50. well-established C and Fortran APIs as determined by the reference (Netlib)
  51. BLAS and LAPACK libraries. The ILP64 (64-bit integer) interface however does
  52. not have a standard API: symbol names and shared/static library names can be
  53. produced in multiple ways, and this tends to make it difficult to use.
  54. As of today there is an agreed-upon way of choosing names for OpenBLAS between
  55. a number of key users/redistributors, which is the closest thing to a standard
  56. that there is now. However, there is an ongoing standardization effort in the
  57. reference BLAS and LAPACK libraries, which differs from the current OpenBLAS
  58. agreed-upon convention. In this section we'll aim to explain both.
  59. Those two methods are fairly similar, and have a key thing in common: *using a
  60. symbol suffix*. This is good practice; it is recommended that if you distribute
  61. an ILP64 build, to have it use a symbol suffix containing `64` in the name.
  62. This avoids potential symbol clashes when different packages which depend on
  63. OpenBLAS load both an LP64 and an ILP64 library into memory at the same time.
  64. ### The current OpenBLAS agreed-upon ILP64 convention
  65. This convention comprises the shared library name and the symbol suffix in the
  66. shared library. The symbol suffix to use is `64_`, implying that the library
  67. name will be `libopenblas64_.so` and the symbols in that library end in `64_`.
  68. The central issue where this was discussed is
  69. [openblas#646](https://github.com/xianyi/OpenBLAS/issues/646), and adopters
  70. include Fedora, Julia, NumPy and SciPy - SuiteSparse already used it as well.
  71. To build shared and static libraries with the currently recommended ILP64
  72. conventions with Make:
  73. ```bash
  74. $ make INTERFACE64=1 SYMBOLSUFFIX=64_
  75. ```
  76. This will produce libraries named `libopenblas64_.so|a`, a pkg-config file
  77. named `openblas64.pc`, and CMake and header files.
  78. Installing locally and inspecting the output will show a few more details:
  79. ```bash
  80. $ make install PREFIX=$PWD/../openblas/make64 INTERFACE64=1 SYMBOLSUFFIX=64_
  81. $ tree . # output slightly edited down
  82. .
  83. ├── include
  84. │   ├── cblas.h
  85. │   ├── f77blas.h
  86. │   ├── lapacke_config.h
  87. │   ├── lapacke.h
  88. │   ├── lapacke_mangling.h
  89. │   ├── lapacke_utils.h
  90. │   ├── lapack.h
  91. │   └── openblas_config.h
  92. └── lib
  93. ├── cmake
  94. │   └── openblas
  95. │   ├── OpenBLASConfig.cmake
  96. │   └── OpenBLASConfigVersion.cmake
  97. ├── libopenblas64_.a
  98. ├── libopenblas64_.so
  99. └── pkgconfig
  100. └── openblas64.pc
  101. ```
  102. A key point are the symbol names. These will equal the LP64 symbol names, then
  103. (for Fortran only) the compiler mangling, and then the `64_` symbol suffix.
  104. Hence to obtain the final symbol names, we need to take into account which
  105. Fortran compiler we are using. For the most common cases (e.g., gfortran, Intel
  106. Fortran, or Flang), that means appending a single underscore. In that case, the
  107. result is:
  108. | base API name | binary symbol name | call from Fortran code | call from C code |
  109. |---------------|--------------------|------------------------|-----------------------|
  110. | `dgemm` | `dgemm_64_` | `dgemm_64(...)` | `dgemm_64_(...)` |
  111. | `cblas_dgemm` | `cblas_dgemm64_` | n/a | `cblas_dgemm64_(...)` |
  112. It is quite useful to have these symbol names be as uniform as possible across
  113. different packaging systems.
  114. The equivalent build options with CMake are:
  115. ```bash
  116. $ mkdir build && cd build
  117. $ cmake .. -DINTERFACE64=1 -DSYMBOLSUFFIX=64_ -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=ON
  118. $ cmake --build . -j
  119. ```
  120. Note that the result is not 100% identical to the Make result. For example, the
  121. library name ends in `_64` rather than `64_` - it is recommended to rename them
  122. to match the Make library names (also update the `libsuffix` entry in
  123. `openblas64.pc` to match that rename).
  124. ```bash
  125. $ cmake --install . --prefix $PWD/../../openblas/cmake64
  126. $ tree .
  127. .
  128. ├── include
  129. │   └── openblas64
  130. │   ├── cblas.h
  131. │   ├── f77blas.h
  132. │   ├── lapacke_config.h
  133. │   ├── lapacke_example_aux.h
  134. │   ├── lapacke.h
  135. │   ├── lapacke_mangling.h
  136. │   ├── lapacke_utils.h
  137. │   ├── lapack.h
  138. │   ├── openblas64
  139. │   │   └── lapacke_mangling.h
  140. │   └── openblas_config.h
  141. └── lib
  142. ├── cmake
  143. │   └── OpenBLAS64
  144. │   ├── OpenBLAS64Config.cmake
  145. │   ├── OpenBLAS64ConfigVersion.cmake
  146. │   ├── OpenBLAS64Targets.cmake
  147. │   └── OpenBLAS64Targets-noconfig.cmake
  148. ├── libopenblas_64.a
  149. ├── libopenblas_64.so -> libopenblas_64.so.0
  150. └── pkgconfig
  151. └── openblas64.pc
  152. ```
  153. ### The upcoming standardized ILP64 convention
  154. While the `64_` convention above got some adoption, it's slightly hacky and is
  155. implemented through the use of `objcopy`. An effort is ongoing for a more
  156. broadly adopted convention in the reference BLAS and LAPACK libraries, using
  157. (a) the `_64` suffix, and (b) applying that suffix _before_ rather than after
  158. Fortran compiler mangling. The central issue for this is
  159. [lapack#666](https://github.com/Reference-LAPACK/lapack/issues/666).
  160. For the most common cases of compiler mangling (a single `_` appended), the end
  161. result will be:
  162. | base API name | binary symbol name | call from Fortran code | call from C code |
  163. |---------------|--------------------|------------------------|-----------------------|
  164. | `dgemm` | `dgemm_64_` | `dgemm_64(...)` | `dgemm_64_(...)` |
  165. | `cblas_dgemm` | `cblas_dgemm_64` | n/a | `cblas_dgemm_64(...)` |
  166. For other compiler mangling schemes, replace the trailing `_` by the scheme in use.
  167. The shared library name for this `_64` convention should be `libopenblas_64.so`.
  168. Note: it is not yet possible to produce an OpenBLAS build which employs this
  169. convention! Once reference BLAS and LAPACK with support for `_64` have been
  170. released, a future OpenBLAS release will support it. For now, please use the
  171. older `64_` scheme and avoid using the name `libopenblas_64.so`; it should be
  172. considered reserved for future use of the `_64` standard as prescribed by
  173. reference BLAS/LAPACK.
  174. ## Performance and runtime behavior related build options
  175. For these options there are multiple reasonable or common choices.
  176. ### Threading related options
  177. OpenBLAS can be built as a multi-threaded or single-threaded library, with the
  178. default being multi-threaded. It's expected that the default `libopenblas`
  179. library is multi-threaded; if you'd like to also distribute single-threaded
  180. builds, consider naming them `libopenblas_sequential`.
  181. OpenBLAS can be built with pthreads or OpenMP as the threading model, with the
  182. default being pthreads. Both options are commonly used, and the choice here
  183. should not influence the shared library name. The choice will be captured by
  184. the `.pc` file. E.g.,:
  185. ```bash
  186. $ pkg-config --libs openblas
  187. -fopenmp -lopenblas
  188. $ cat openblas.pc
  189. ...
  190. openblas_config= ... USE_OPENMP=0 MAX_THREADS=24
  191. ```
  192. The maximum number of threads users will be able to use is determined at build
  193. time by the `NUM_THREADS` build option. It defaults to 24, and there's a wide
  194. range of values that are reasonable to use (up to 256). 64 is a typical choice
  195. here; there is a memory footprint penalty that is linear in `NUM_THREADS`.
  196. Please see `Makefile.rule` for more details.
  197. ### CPU architecture related options
  198. OpenBLAS contains a lot of CPU architecture-specific optimizations, hence when
  199. distributing to a user base with a variety of hardware, it is recommended to
  200. enable CPU architecture runtime detection. This will dynamically select
  201. optimized kernels for individual APIs. To do this, use the `DYNAMIC_ARCH=1`
  202. build option. This is usually done on all common CPU families, except when
  203. there are known issues.
  204. In case the CPU architecture is known (e.g. you're building binaries for macOS
  205. M1 users), it is possible to specify the target architecture directly with the
  206. `TARGET=` build option.
  207. `DYNAMIC_ARCH` and `TARGET` are covered in more detail in the main `README.md`
  208. in this repository.
  209. ## Real-world examples
  210. OpenBLAS is likely to be distributed in one of these distribution models:
  211. 1. As a standalone package, or multiple packages, in a packaging ecosystem like
  212. a Linux distro, Homebrew, conda-forge or MSYS2.
  213. 2. Vendored as part of a larger package, e.g. in Julia, NumPy, SciPy, or R.
  214. 3. Locally, e.g. making available as a build on a single HPC cluster.
  215. The guidance on this page is most important for models (1) and (2). These links
  216. to build recipes for a representative selection of packaging systems may be
  217. helpful as a reference:
  218. - [Fedora](https://src.fedoraproject.org/rpms/openblas/blob/rawhide/f/openblas.spec)
  219. - [Debian](https://salsa.debian.org/science-team/openblas/-/blob/master/debian/rules)
  220. - [Homebrew](https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openblas.rb)
  221. - [MSYS2](https://github.com/msys2/MINGW-packages/blob/master/mingw-w64-openblas/PKGBUILD)
  222. - [conda-forge](https://github.com/conda-forge/openblas-feedstock/blob/main/recipe/build.sh)
  223. - [NumPy/SciPy](https://github.com/MacPython/openblas-libs/blob/main/tools/build_openblas.sh)
  224. - [Nixpkgs](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/libraries/science/math/openblas/default.nix)