Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28435 Discussions

Cannot inverse matrix by using by using dgetrf/dgetri from liblapack.dll in excel VBA

Ngo__Nhan
Novice
1,848 Views

Hello guys,

I have a problem that I cannot inverse matrix by using liblapack.dll by using dgetrf/dgetri  in VBA Excel 32bit.

The Excel always crash when I test the code below:

The Declare:

    ''CALL dgetrf_ from liblapack DLL
Private Declare Sub dgetrf_ Lib "liblapack.dll" _
   (ByVal N As Integer, _
     ByVal M As Integer, _
     ByRef A As Double, _
     ByVal LDA As Integer, _
     ByRef IPIV As Integer, _
    ByRef INFO As Integer)
    
''CALL dgetri_ from liblapack DLL
    Private Declare Sub dgetri_ Lib "liblapack.dll" _
    (ByVal N As Integer, _
    ByRef A As Double, _
    ByRef LDA As Integer, _
    ByRef IPIV As Integer, _
    ByVal WORK As Double, _
    ByVal LWORK As Integer, _
    ByRef INFO1 As Integer)

 

The Sub:

Public Sub MatrixSolver(ByVal Matrix1 As Matrix)
 Dim UPLO, TRANS As String
 Dim M, N, LDA, INCX, INCY As Integer
 Dim INFO As Integer
 Dim A() As Double
 Dim X() As Double
 Dim Y() As Double
 Dim IPIV() As Integer

Dim MRows As Integer
Dim MCols As Integer
MRows = Matrix1.NumRows
MCols = Matrix1.NumCols


ReDim A(1 To MRows, 1 To MCols) As Double         
ReDim IPIV(1 To MRows) As Integer

''''Add value to Matrix A


M = MRows
N = MCols
LDA = MRows
INFO = 0


    Call dgetrf_(M, N, A(1, 1), LDA, IPIV(1), INFO)
    
       If INFO <> 0 Then
          MsgBox ("Error: Unable to invert matrix.")
           Stop
      End If   

    LWORK = MRows
     Call dgetri_(N, A(1, 1), LDA, IPIV(1), WORK, LWORK, INFO1)     
        If INFO1 <> 0 Then
          MsgBox ("Error: Unable to invert matrix.")
            Stop
      End If
     

'''' Use inversed A array


End Sub
 

The excel always crack while running Call dgetrf_, so I cannot check the call dgetri_

I do not have much experience in Fortran and Lapack also, I did a test with function strsv_ in libblas.dll and it works.

I created the dll by using CMAKE and lapack-3.9.0

Thank you for reading this topic, please let me know if you need more information.

Nhan Ngo

 

0 Kudos
8 Replies
Steve_Lionel
Honored Contributor III
1,847 Views

I am not seeing any Fortran in this post. Why did you ask it here? dgetri is part of the Intel Math Kernel Library, which has its own forum at https://software.intel.com/en-us/forums/intel-math-kernel-library  When you ask there, you'll also need to specify which set of LAPACK libraries you linked against, as when calling from VBA you need to use the STDCALL version. You should also verify that your use of "ByVal" is appropriate.

Ngo__Nhan
Novice
1,821 Views

Hi Steve,

Thank you for your answer, I post the question in the intel-math-kernel-library: https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Cannot-Invert-Matrix-using-Lapack-DGETRF-DGETRI-in-VBA/m-p/1188023#M29646

I have already used the Intel MKL library, but the code still does not work, the Excel still always quietly quits when the call dgetrf is made.  Could you visit the post for more information? If anything is unclear, please let me know.  

How can I define that dgetrf , dgetri or dsymv from mkl_rt.dll are belong the STDCALL version?

Thank you again for reading.

 

0 Kudos
mecej4
Honored Contributor III
1,848 Views

Steve, he is not using MKL, he is using a Lapack DLL that he built from source (source code probably from Netlib).

Nhan Ngo, you have to make sure that the calling convention that you used when building your Lapack DLL is consistent with the calling sequence that Excel/VBA use to call the DLL routines.

Many variations are possible in building Lapack and Blas DLLs from Netlib sources using Cmake scripts. If you followed someone/some-Web-site's prescription in building the DLL, they/that site would also be the appropriate target for your build questions.

High performance versions of Lapack routines are available in the Intel MKL library. They can be called from Fortran, C, C# and other languages with compatible calling sequences.

 

Ngo__Nhan
Novice
1,847 Views

Hi Steve, mecej4,

Thank you a lot for the information.

As mecej4 said, I used functions from Lapack DLL  from Netlib: DGETRF, DGETRI

I will try to use the Lapack routines from the Intel MKL library to rebuild the dlls as you recommended. I hope they can work easily with the VBA Excel as well because I have lots of giant matrices to solve.

Thank you again and have a nice weekend

0 Kudos
avinashs
New Contributor I
1,848 Views

1. Build the .dll containing LAPACK routines using the /iface:cvf option in IVF. Using the default calling convention causes crashes.

2. Some LAPACK routines have character arguments. I would recommend writing a wrapper so that the calling interface to VBA has only integer arguments that are then mapped to character arguments within the wrapper for a call to LAPACK.

 

Ngo__Nhan
Novice
1,820 Views

Hi avinashs,

1. After installing the mkl _2020, I have used the mkl_rt.dll from the existing library, I do not build my own dll so I cannot change the default calling convention to  /iface:cvf option, could you give the instruction how to change the existing dll calling convention?

2. I will try writing a wrapper for arguments as you comments soon.

I have some update information in the new post: https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Cannot-Invert-Matrix-using-Lapack-DGETRF-DGETRI-in-VBA/m-p/1188023#M29646

Thank you for giving instructions. 

0 Kudos
JohnNichols
Valued Contributor III
1,848 Views

If you have excel files it would be a lot easier to save the matrices as csv files and then use Fortran to invert. 

 

Ngo__Nhan
Novice
1,819 Views

Hi John,

I suggested your idea, but the team still chooses VBA and Excel for developing this tool.

You can follow the new post here: https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Cannot-Invert-Matrix-using-Lapack-DGETRF-DGETRI-in-VBA/m-p/1188023#M29646

Thank you for your idea

0 Kudos
Reply