Skip to content

CBLAS/LAPACKE extension for 64bit integer #666

@mkrainiuk

Description

@mkrainiuk

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:

#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

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 to snrm2_/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 to cblas_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 API DGGES3 it is better to have DGGES3_64 instead of DGGES364.
    • Intel oneMKL and NVIDIA cuBLAS use _64 suffix
  • 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 like LAPACKE_dgeqrf_work, ILP64 API will be LAPACKE_dgeqrf_work_64, not LAPACKE_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.
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions