-
Notifications
You must be signed in to change notification settings - Fork 474
Description
Intro
CBLAS/LAPACKE wrappers support 32bit integer and 64bit integer with the same function names located in different libraries but this approach does not allow mixing both libraries in one environment because of the symbols conflict.
This FR is for discussing potential solution to avoid this problem and have stable work in any environment for the applications/libraries that has dependency on CBLAS/LAPACKE built with 64bit integer or any other projects with similar API.
Problem
Similar to Fortran API where integer with no size specified is used, CBLAS/LAPACKE API integer type can be selected by special build option during compilation.
Example for CBLAS:
Lines 20 to 24 in 655e588
#ifdef WeirdNEC | |
#define CBLAS_INT int64_t | |
#else | |
#define CBLAS_INT int32_t | |
#endif |
Since C/Fortran symbols do not reflect the data type in the name in general (like C++), linker cannot detect if the resolved symbols has correct Integer type. As the result symbols mismatch could cause unexpected behavior, like incorrect results, data corruption, and segmentation fault.
Example:
A plugin "X" uses CBLAS API with 64bit integer type because it works with number of elements that does not fit to 32bit. Another plugin "Y" loads "libcblas.so" CBLAS library built with 32bit integer as a dependency in the complex application environment, As first loaded "libcblas.so" library will be used for the first plugin "X" which will cause segmentation fault during application execution.
Related Work
- Julia Programming Language is working with other math libraries in order to allow mixing API for different integer types in one environment: ABI conflicts due to 64-bit libopenblas.so JuliaLang/julia#4923
- OpenBLAS introduced SYMBOLSUFFIX build option based on the Julia request: Standardize ILP64 SONAME and symbols suffix OpenMathLib/OpenBLAS#646
- BLIS has good discussion for naming but it's waiting for standardization of the approach (which this FR attempts to do) to implement it in the library: BLIS should allow simultaneously exporting both 32- and 64-bit variants of BLAS/CBLAS flame/blis#43
- FlexiBLAS also has good analysis of the naming, converted to internal issue: Suffixed 64-bit integer symbols names mpimd-csc/flexiblas#12
- Intel oneMKL added Fortran-like BLAS C API extension with
_64
suffix for 64bit integer to default LP64 interface library,_64
extension for Fortran-like LAPACK C API and CBLAS/LAPACKE API (link) - cuBLAS 12.0 extends the cuBLAS API to support 64-bit integer, extended API has a
_64
suffix in the name (link) - Some work for libraries naming was already done for Reference-LAPACK: Allow a possible installation of index-64 library alongside standard index-32 library? #461
- Apple is using a different convention in Accelerate for LAPACK ILP64. Julia Computing wrappers for those routines on macOS: Add support for ILP64 Accelerate JuliaLinearAlgebra/libblastrampoline#113
Proposal
-
Define a suffix for API with 64bit integer support so that users can control integer type and right API call in the code and avoid any runtime conflicts
- 64bit integer API was selected to be extended with the suffix because 32bit integer API is default one for the most of the libraries. So it could be easier to change only code where 64bit integer API is required, and keep default API unchanged.
-
Suffix considerations
- Julia uses OpenBLAS with
64_
suffix for all C and Fortran symbols.
Please note, from the API perspective it will be two different suffixes for C and Fortran APIs:_64
for Fortran API, because e.g.,snrm2
/snrm2_64
functions will be converted tosnrm2_
/snrm2_64_
symbols (default gfortran/ifort compilers behavior on Unix)64_
for C API in order to get same symbol suffix, because e.g.cblas_snrm2
/cblas_snrm264_
functions will be converted tocblas_snrm2
/cblas_snrm264_
symbol (default gcc/icc/clang compilers behavior)
- FlexiBLAS considered adding
_64
, it will improve readability for function names with numbers, e.g.: for APIDGGES3
it is better to haveDGGES3_64
instead ofDGGES364
. - Intel oneMKL and NVIDIA cuBLAS use
_64
suffix
- Julia uses OpenBLAS with
-
Proposed suffix for CBLAS/LAPACK API:
_64
Please note,_64
must be at the end of the function name for the symbol identification simplicity, including cases likeLAPACKE_dgeqrf_work
, ILP64 API will beLAPACKE_dgeqrf_work_64
, notLAPACKE_dgeqrf_64_work
Example:
// Current API
float cblas_snrm2(const CBLAS_INT N, const float *X, const CBLAS_INT incX);
lapack_int LAPACKE_dgeev( int matrix_layout, char jobvl, char jobvr, lapack_int n, double* a, lapack_int lda, ... );
// 64bit fixed width integer API
float cblas_snrm2_64(const int64_t N, const float *X, const int64_t incX);
int64_t LAPACKE_dgeev_64( int matrix_layout, char jobvl, char jobvr, int64_t n, double* a, int64_t lda, ... );
int main() {
int64_t N, incX;
int32_t n, lda, ...;
...
cblas_snrm2_64(N, X, incX);
LAPACKE_dgeev(matrix_layout, jobvl, jobv, n, a, lda, ...);
...
}
Other Considerations
- Update APIs that do not have dependency on the Integer type, e.g.
Line 134 in 655e588
void cblas_srotg(float *a, float *b, float *c, float *s); - Do we need to consider other data types that could benefit from this approach?
- Add function mangling to the header so that users can convert names for all functions in the application during compilation, same way as currently it's supported for integer type: 32bit by default, 64bit if special macro is defined.
- Extend Fortran API. It's partially should be done anyway in order to enable extensions for CBLAS/LAPACKE wrappers