Recent

Author Topic: intel Mkl Oneapi (library usage)  (Read 1858 times)

sedsil

  • New Member
  • *
  • Posts: 11
intel Mkl Oneapi (library usage)
« on: July 03, 2025, 12:52:14 pm »
Dear All,
I used to use Turbo Pascal. I am a new user of Lazarus 4.0. I can not resolve to add the Intel MKL library to Lazarus. On the windows 10, I have defined PATH  "C:\Program Files (x86)\Intel\oneAPI\mkl\latest\bin". This folder contains mkl_rt.2.dll and mkl_rt.dll. my code is like that: ********************************************
program MKLEigenSolver;
{$mode objfpc}{$H+}
{$APPTYPE CONSOLE}

uses
  Windows, SysUtils;

type
  TLAPACKE_dsyev = function(layout: Integer; jobz, uplo: Char;
                           n: Integer; a: PDouble; lda: Integer;
                           w: PDouble): Integer; cdecl;

const
  MKL_PATH = 'C:\Program Files (x86)\Intel\oneAPI\mkl\latest\bin\';
  LAPACK_ROW_MAJOR = 101;

var
  hMKL: HMODULE;
  dsyevFunc: TLAPACKE_dsyev;
  A: array[0..8] of Double = (
    4.0, 1.0, 0.5,
    1.0, 3.0, 1.0,
    0.5, 1.0, 2.0
  );
  W: array[0..2] of Double;
  i: Integer;

begin
  try
    // Load only the main MKL DLL
    WriteLn('Loading MKL runtime...');

    // Try loading in preferred order
    hMKL := LoadLibrary(PChar(MKL_PATH + 'mkl_rt.2.dll'));
    if hMKL = 0 then
      hMKL := LoadLibrary(PChar(MKL_PATH + 'mkl_rt.dll'));

    if hMKL = 0 then
    begin
      WriteLn('ERROR: Failed to load MKL runtime library');
      WriteLn('Please ensure either mkl_rt.2.dll or mkl_rt.dll exists in:');
      WriteLn(MKL_PATH);
      ReadLn;
      Exit;
    end;

    // Get function pointer
    WriteLn('Locating dsyev function...');
    dsyevFunc := TLAPACKE_dsyev(GetProcAddress(hMKL, 'LAPACKE_dsyev'));
    if not Assigned(dsyevFunc) then
    begin
      WriteLn('ERROR: Failed to find required function');
      FreeLibrary(hMKL);
      ReadLn;
      Exit;
    end;

    // Compute eigenvalues
    WriteLn('Calculating eigenvalues...');
    if dsyevFunc(LAPACK_ROW_MAJOR, 'N', 'U', 3, @A[0], 3, @W[0]) = 0 then
    begin
      WriteLn(#13#10'RESULTS:');
      for i := 0 to 2 do
        WriteLn(Format('λ%d = %.6f', [i+1, W]));
    end
    else
      WriteLn('ERROR: Eigenvalue computation failed');

  except
    on E: Exception do
      WriteLn('FATAL ERROR: ', E.ClassName, ' -> ', E.Message);
  end;

  if hMKL <> 0 then
    FreeLibrary(hMKL);

  WriteLn(#13#10'Press Enter to exit...');
  ReadLn;
end.
***************************************
the output for the error message is attached. Could you direct me to solve this matter?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12562
  • FPC developer.
Re: intel Mkl Oneapi (library usage)
« Reply #1 on: July 03, 2025, 01:21:47 pm »
Maybe the library needs some initializer? Some exception occurs in the DLL.

Zvoni

  • Hero Member
  • *****
  • Posts: 3161
Re: intel Mkl Oneapi (library usage)
« Reply #2 on: July 03, 2025, 01:53:36 pm »
Quote
C:\Program Files (x86)\Intel\oneAPI\mkl\latest\bin
32-Bit vs. 64-Bit?
What's the Compile-Target?

EDIT: Ignore the above. Just saw his LoadLibrary-Call, which apparently works.
So my money is on wrong native FreePascal-Types used.
Maybe use ctypes-unit and its types?
« Last Edit: July 03, 2025, 01:59:54 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12562
  • FPC developer.
Re: intel Mkl Oneapi (library usage)
« Reply #3 on: July 03, 2025, 01:55:49 pm »
Quote
C:\Program Files (x86)\Intel\oneAPI\mkl\latest\bin
32-Bit vs. 64-Bit?
What's the Compile-Target?

No, then the library wouldn't load and loadlibrary and getprocaddress calls would fail

Zvoni

  • Hero Member
  • *****
  • Posts: 3161
Re: intel Mkl Oneapi (library usage)
« Reply #4 on: July 03, 2025, 02:00:11 pm »
Quote
C:\Program Files (x86)\Intel\oneAPI\mkl\latest\bin
32-Bit vs. 64-Bit?
What's the Compile-Target?

No, then the library wouldn't load and loadlibrary and getprocaddress calls would fail
Yeah, see my edit above. Noticed that, too

I've found this in the lapacke_config.h-file
Code: C  [Select][+][-]
  1. #ifndef lapack_int
  2.    #if defined(LAPACK_ILP64)
  3.    #define lapack_int              long
  4.    #else
  5.    #define lapack_int              int
  6. #endif
« Last Edit: July 03, 2025, 02:03:45 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12562
  • FPC developer.
Re: intel Mkl Oneapi (library usage)
« Reply #5 on: July 03, 2025, 02:13:31 pm »
ILP64 targets are rare. Windows is LLP64 and open source Unix is usually LP64.   In both of them integer is 32-bit on 64-bit targets.

Zvoni

  • Hero Member
  • *****
  • Posts: 3161
Re: intel Mkl Oneapi (library usage)
« Reply #6 on: July 03, 2025, 02:57:17 pm »
Found this example: https://www.intel.com/content/www/us/en/docs/onemkl/code-samples-lapack/2024-1/lapacke-dsyev-example-c-column.html

The only thing i see is
Code: C  [Select][+][-]
  1. /* Solve eigenproblem */
  2.         info = LAPACKE_dsyev( LAPACK_COL_MAJOR, 'V', 'U', n, a, lda, w );
is in the example 'V' as second Argument (as opposed to 'N' in TS code)
Maybe the dll chokes on that?
The C-code basically transfers work to
https://netlib.org/lapack/explore-html-3.6.1/de/ddd/lapacke_8h_a6fe2a100c43ce81b43ae242f9763e3ec.html#a6fe2a100c43ce81b43ae242f9763e3ec
Code: C  [Select][+][-]
  1. void LAPACK_dsyev       (       char *          jobz,
  2.                 char *          uplo,
  3.                 lapack_int *    n,
  4.                 double *        a,
  5.                 lapack_int *    lda,
  6.                 double *        w,
  7.                 double *        work,
  8.                 lapack_int *    lwork,
  9.                 lapack_int *    info
  10.         )              
But i couldn't find the implementation file

EDIT: Looking further into this:
In ctypes-unit:
a c-"char" is a typedef for a cint8, which is a typedef for shortint
a c-"int" is a typedef for cint32, which is a TypeDef for longint

so my money is still on wrong types in his Prototype
« Last Edit: July 03, 2025, 03:15:45 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 18475
  • Here stood a man who saw the Elbe and jumped it.
Re: intel Mkl Oneapi (library usage)
« Reply #7 on: July 03, 2025, 03:41:23 pm »
I suspect it is the olden exception mask that needs to be set:
Code: Pascal  [Select][+][-]
  1. SetExceptionMask(GetExceptionMask + [exOverflow,exZeroDivide,exInvalidOp]);
(By coincedense I ran into the same issue using python4lazarus today: on Windows, that also needs the exception mask set properly.)

Or the deprecated:
Code: Pascal  [Select][+][-]
  1.  Set8087CW($1331);
« Last Edit: July 03, 2025, 03:45:19 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

LV

  • Sr. Member
  • ****
  • Posts: 377
Re: intel Mkl Oneapi (library usage)
« Reply #8 on: July 03, 2025, 07:41:41 pm »
Place libiomp5md.dll in the project folder.  :-[

Code: Pascal  [Select][+][-]
  1. program MKLEigenSolver;
  2. {$mode objfpc}{$H+}
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   Windows, SysUtils;
  7.  
  8. type
  9.   TLAPACKE_dsyev = function(layout: Integer; jobz, uplo: Char;
  10.                            n: Integer; a: PDouble; lda: Integer;
  11.                            w: PDouble): Integer; cdecl;
  12.  
  13. const
  14.   MKL_PATH = 'C:\Program Files (x86)\Intel\oneAPI\mkl\latest\bin\';
  15.   LAPACK_ROW_MAJOR = 101;
  16.  
  17. var
  18.   hMKL: HMODULE;
  19.   dsyevFunc: TLAPACKE_dsyev;
  20.   A: array[0..8] of Double = (
  21.     4.0, 1.0, 0.5,
  22.     1.0, 3.0, 1.0,
  23.     0.5, 1.0, 2.0
  24.   );
  25.   W: array[0..2] of Double;
  26.   i: Integer;
  27.  
  28. begin
  29.   try
  30.     // Load only the main MKL DLL
  31.     WriteLn('Loading MKL runtime...');
  32.  
  33.     // Try loading in preferred order
  34.     hMKL := LoadLibrary(PChar(MKL_PATH + 'mkl_rt.2.dll'));
  35.     if hMKL = 0 then
  36.       hMKL := LoadLibrary(PChar(MKL_PATH + 'mkl_rt.dll'));
  37.  
  38.     if hMKL = 0 then
  39.     begin
  40.       WriteLn('ERROR: Failed to load MKL runtime library');
  41.       WriteLn('Please ensure either mkl_rt.2.dll or mkl_rt.dll exists in:');
  42.       WriteLn(MKL_PATH);
  43.       ReadLn;
  44.       Exit;
  45.     end;
  46.  
  47.     // Get function pointer
  48.     WriteLn('Locating dsyev function...');
  49.     dsyevFunc := TLAPACKE_dsyev(GetProcAddress(hMKL, 'LAPACKE_dsyev'));
  50.     if not Assigned(dsyevFunc) then
  51.     begin
  52.       WriteLn('ERROR: Failed to find required function');
  53.       FreeLibrary(hMKL);
  54.       ReadLn;
  55.       Exit;
  56.     end;
  57.  
  58.     // Compute eigenvalues
  59.     WriteLn('Calculating eigenvalues...');
  60.     if dsyevFunc(LAPACK_ROW_MAJOR, 'N', 'U', 3, @A[0], 3, @W[0]) = 0 then
  61.     begin
  62.       WriteLn(#13#10'RESULTS:');
  63.       for i := 0 to 2 do
  64.         WriteLn(Format('λ%d = %.6f', [i+1, W[i]]));
  65.     end
  66.     else
  67.       WriteLn('ERROR: Eigenvalue computation failed');
  68.  
  69.   except
  70.     on E: Exception do
  71.       WriteLn('FATAL ERROR: ', E.ClassName, ' -> ', E.Message);
  72.   end;
  73.  
  74.   if hMKL <> 0 then
  75.     FreeLibrary(hMKL);
  76.  
  77.   WriteLn(#13#10'Press Enter to exit...');
  78.   ReadLn;
  79. end.
  80.  

Get the result you want  ;)

Code: Text  [Select][+][-]
  1. Loading MKL runtime...
  2. Locating dsyev function...
  3. Calculating eigenvalues...
  4.  
  5. RESULTS:
  6. О>1 = 1,377095
  7. О>2 = 2,682456
  8. О>3 = 4,940450
  9.  
  10. Press Enter to exit...
  11.  

Enjoy  :D

PascalDragon

  • Hero Member
  • *****
  • Posts: 6229
  • Compiler Developer
Re: intel Mkl Oneapi (library usage)
« Reply #9 on: July 03, 2025, 08:52:35 pm »
my code is like that:

Please use [code][/code]-tags to avoid the forum software interpreting your code.

Zvoni

  • Hero Member
  • *****
  • Posts: 3161
Re: intel Mkl Oneapi (library usage)
« Reply #10 on: July 04, 2025, 08:15:57 am »
Place libiomp5md.dll in the project folder.  :-[
*groan*
Did we actually forgot it might be a dependant dll missing?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

sedsil

  • New Member
  • *
  • Posts: 11
Re: intel Mkl Oneapi (library usage)
« Reply #11 on: July 04, 2025, 10:10:20 am »
Place libiomp5md.dll in the project folder.  :-[

Dear friend, I have tested the proposal you advised me on. YES, it works very well. Thank you for your help. Thank you all for the help.

 

TinyPortal © 2005-2018