M2 icon indicating copy to clipboard operation
M2 copied to clipboard

Using Intel's Math Kernel Library with M2

Open mahrud opened this issue 1 year ago • 0 comments

Here are the problems I ran into:

  1. First, you need to set the compiler variables, e.g for MKL 2024.1:
. /opt/local/oneapi/2024.1/setvars.sh --force
 
:: initializing oneAPI environment ...
   -bash: BASH_VERSION = 5.2.15(1)-release
   args: Using "$@" for setvars.sh arguments: --force
:: advisor -- latest
:: compiler -- latest
:: debugger -- latest
:: dev-utilities -- latest
:: dpcpp-ct -- latest
:: dpl -- latest
:: ipp -- latest
:: ippcp -- latest
:: mkl -- latest
:: mpi -- latest
:: tbb -- latest
:: oneAPI environment initialized ::

Though finding this file was a bit tricky. Then ninja build-givaro build-fflas-ffpack detected MKL fine as well, and this was reported by cmake:

## Library information
     Linear Algebra    = /opt/local/oneapi/2024.1/mkl/2024.1/lib/libmkl_intel_lp64.so;/opt/local/oneapi/2024.1/mkl/2024.1/lib/libmkl_intel_thread.so;/opt/local/oneapi/2024.1/mkl/2024.1/lib/libmkl_core.so;/opt/local/oneapi/2024.1/compiler/2024.1/lib/libiomp5.so;-lm;-ldl;-lm;-ldl
  1. Conflicting declaration of C functions from lapack.hpp, e.g.:
/usr/people/sayrafi/M2/M2/Macaulay2/e/lapack.hpp:18:5: error: conflicting declaration of C function ‘int dgesv_(int*, int*, double*, int*, int*, double*, int*, int*)’
   18 | int dgesv_(int *n,      // number of rows in A
      |     ^~~~~~
compilation terminated due to -Wfatal-errors.

I got around this by adding #ifndef _MKL_LAPACK_H_ around the extern "C" section of our lapack.hpp. It seems to me like we should be including this section from somewhere, why is it here?

  1. Incompatible complex types:
/usr/people/sayrafi/M2/M2/Macaulay2/e/dmat-lu-inplace.hpp: In member function ‘void DMatLUinPlace<RT>::computeLU() [with RT = M2::ARingCC]’:
/usr/people/sayrafi/M2/M2/Macaulay2/e/dmat-lu-inplace.hpp:372:25: error: cannot convert ‘double*’ to ‘MKL_Complex16*’
  372 |   zgetrf_(&rows, &cols, copyA, &rows, perm, &info);
      |                         ^~~~~
      |                         |
      |                         double*
compilation terminated due to -Wfatal-errors.

For reference, this is the declaration of zgetrf_ in lapack.hpp vs mkl_lapack.hpp:

int zgetrf_(int *rows,   // rows
            int *cols,   // columns
            double *M,   // input matrix, on exit L & U from A=PLU.
            int *ld,     // rows
            int *ipiv,   // becomes permutation indices of P
            int *info);  // error info

void zgetrf_( const MKL_INT* m, const MKL_INT* n, MKL_Complex16* a,
              const MKL_INT* lda, MKL_INT* ipiv, MKL_INT* info ) NOTHROW;

and this is the type MKL_Complex16 in mkl_types.hpp:

#ifndef MKL_Complex16 
typedef 
struct _MKL_Complex16 { 
    double real; 
    double imag; 
} MKL_Complex16; 
#endif

This feels very sketchy, but I got around this by pre-declaring the type in engine-includes.hpp:

#define MKL_Complex16 double

Putting this in lapack.hpp didn't work, and I'm not sure where makes more sense.

mahrud avatar Jul 21 '24 12:07 mahrud