Intel® Math Kernel Library (Intel® MKL) is a highly optimized, extensively threaded and thread-safe library of mathematical functions for engineering, scientific, and financial applications. Microsoft Office Excel* helps developers make more informed decisions, by using spreadsheets to calculate, analyze and visualize data. This article will demonstrate a small example to call Intel® MKL from Microsoft Office Excel* 2007. It includes several steps: create a Dynamic Link Library (DLL) from Intel® MKL, call Intel® MKL functions exported by the DLL using a Microsoft Visual Basic for Applications (VBA).
Downloading the Examples: mklinExcel.zip
There are some ways of interfacing with third party software libraries from Excel which are well documented in Excel online help, like
Calling from EXCEL through VBA do require that the called routine support STDCALL calling convention. Intel® MKL, which contains over hundreds math functions, support both CDECL and STDCALL interfaces (see, for example, MSDN, for description of calling conventions). Thus, the best way to call Intel® MKL functions from Excel to build a custom DLL based on Intel® MKL library which support STDCALL interface.
In this article we will use Microsoft Office Excel 2007 and Microsoft Visual Studio 2005 as example.
Install Intel MKL : Intel Math Kernel Library product web page.
Install Microsoft office Excel 2007 and Microsoft Visual studio 2005
Step1: Build custom DLL by builder tools provided by Intel MKL
a. Unzip the contents of the attached zip file to your work directory, for example C:\temp\mklinExcel\builder
Enter the directory and edit the user_example_list text file with the functions you hope to use.
The function performs element by element squaring of the vector. For example, calculate the square of one vector a= (a1, a2, ...) and vdsqr(a)=( a1^2,a2^2,...).
Search more functions in Intel® MKL Reference Manual
b. Run the build script (makefile) using nmake
Open a Microsoft Visual Studio command prompt or add the Microsoft.NET Framework to the PATH environment variable in the command prompt
And enter the directory and type the command
This will create a dll named mkl_custom.dll if success to build as below
The makefile provides further explanation of the parameters in comments.
The builder tool is actually available in MKL install directory. C:\Program Files (x86)\Intel\Composer XE 2013\mkl\tools\builder
Copy the builder directory (including all files) to your work directory, for example, C:\temp\builder\ and edit the makefile.
!if "$(interface)"=="std" || "$(interface)"=="stdcall" || "$(interface)"=="STD" || "$(interface)"=="STDCALL"
Step 2. Build an example in Excel which call VDSQR() function
a. Please open the mklDllTest.xls (in C:\temp\mklinExcel) in Excel 2007.
Or you can create your own workshop in Excel and add command button as the steps. Add a button and assign a macro to it in a worksheet.
b. Click Alt+F11 start Visual Basic Editor and edit Module1 to correct path of the custom DLL
'in this example, it is
Declare Sub VDSQR Lib "C:\temp\mklinExcel\builder\mkl_custom.dll" _
(ByRef n As Integer, ByRef a As Double, ByRef r As Double)
c. Type your own VBA code in the sub procedure called CommandButton1_Click, as shown in the following code:
Private Sub CommandButton1_Click()
Dim n As Integer
Dim a() As Double
Dim b() As Double
Dim I As Integer
Dim Sum As Double
n = 3
For I = 0 To n
a(I) = I
Call VDSQR(n + 1, a(0), b(0))
Sum = 0
For I = 0 To n
Sum = Sum + b(I)
TextBox1.Text = "Answer 0^2+ 1^2 + 2^2 + 3^2 is " & Str(Sum)
d. Click Alt+F11 back to Excel sheet. Click the "Call mklDLL vdSqr(n,a,r)" button, you will see the run result. 0^2+1^2 + 2^2 + 3^2 = 14.
•1. Bad DLL calling convention (Error 49), please refer to the Excel on line help
The problem mainly is that your program is calling a routine in a DLL, but isn't using the StdCall calling convention.
Appendix C - References