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

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