Use Intel® MKL from Microsoft* Office Excel

Overview

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).

There are some ways of interfacing with third party software libraries from Excel which are well documented in Excel online help, like

How to create a user-defined function in Excel 2007 or Excel 2010

Developing User-Defined Functions for Excel 2007 and Excel 2010...

Create Custom Functions in Excel 2007

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.

Prerequisites:
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.

For example:

VDSQR

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

>nmake libia32 threading=sequential interface=stdcall export=user_example_list name=mkl_custom MKLROOT="C:\Program Files (x86)\Intel\Composer XE 2013\mkl"

This will create a dll named mkl_custom.dll if success to build as below

Notes:

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.

Change:

!if "\$(interface)"=="std" || "\$(interface)"=="stdcall" || "\$(interface)"=="STD" || "\$(interface)"=="STDCALL"

IFACE_LIB_32=mkl_intel_s.lib

IFACE_LIB_32_DLL=mkl_intel_s_dll.lib

!endif

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

ReDim a(n)

ReDim b(n)

For I = 0 To n

a(I) = I

Next I

Call VDSQR(n + 1, a(0), b(0))

Sum = 0

For I = 0 To n

Sum = Sum + b(I)

Next I

TextBox1.Text = "Answer 0^2+ 1^2 + 2^2 + 3^2 is " & Str(Sum)

End Sub

This will create a VBA to call mkl VDSQR function from Excel 2007. Please refer the below screenshots for more details

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.

Troubleshooting

•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

For more complete information about compiler optimizations, see our Optimization Notice.
Categories:
Tags: