Microsoft Visual C# and Intel MKL

Microsoft Visual C# and Intel MKL

Hi All!

Is there any example how to call Intel MKL from C#?

Thanks in advance

9 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Ditto, I have been successful with IPP and C# (where I was led by some kind person to samples), but not yet with MKL.

There seems to be no downloadable samples for MKL?

Hvard

Currently, there is only the Fortran and C sample code that is included with the MKL product in the examples directory. There is no C# sample source code. There are no separately downloadable samples at this point either.

Has anyone used MKL in a simple C# program? Please attach your source code?

Todd

We are using P/Invoke to write a wrapper around some of the BLAS and LAPACK routines in MKL. Since P/Invoke requires a DLL, we couldn't call the MKL lib files directly. We wrote a simple wrapper DLL that we make P/Invoke calls to. You can get the source from
http://www.dnanalytics.net/, it might give you some
ideas.

Feel free to email at marcus@cuda.org if you have any questions.

Quoting - Todd Rosenquist (Intel)

Currently, there is only the Fortran and C sample code that is included with the MKL product in the examples directory. There is no C# sample source code. There are no separately downloadable samples at this point either.

Has anyone used MKL in a simple C# program? Please attach your source code?

Todd

Here's how we do at CenterSpace Software. The data blocks are C# wrappers of double arrays. The offset is into the array. Basically, you statically link the mkl libraries, pin a pointer to your double array and call MKL.

- Trevor

#using

#using

#include // for memset.

#include "mkl.h"

# define BLAS_PREFIX(x) x
# define BLAS_ARGUMENT(x) &##x
# define INTEGER int

using namespace CenterSpace::NMath::Core;

namespace CenterSpace
{
namespace NMath
{
namespace Kernel
{
public ref class DotNetBlas
{
public:

// Deep copy of vector
static void copy( INTEGER n, FloatDataBlock x, INTEGER xOffset, INTEGER incx, FloatDataBlock y, INTEGER yOffset, INTEGER incy )
{
pin_ptr xptr;
pin_ptr yptr;

if ( n > 0 ) {
xOffset += AdjustVectorOffset( n, incx, x.Offset );
yOffset += AdjustVectorOffset( n, incy, y.Offset );
xptr = &( x.Data[xOffset] );
yptr = &( y.Data[yOffset] );
BLAS_PREFIX(scopy)( BLAS_ARGUMENT(n), xptr, BLAS_ARGUMENT(incx), yptr, BLAS_ARGUMENT(incy) );
}
}
}
}
}

--
CEO, CenterSpace Software
.NET math libraries

We've posted some example C# programs that call Intel MKL.

Let us know if there are improvements we can make to further show how to access the features of Intel MKL.

Todd

OK, I've gotten it to work in a C# and Visual Studio IDE environment. (I was hung up for a while on getting the PATH environment variable set properly).

I'd like to know the reason why you create a separate class and "wrapper" functions. That is, in your example, you have access structured like this:

public sealed class DFTI
{

/** DFTI DftiCreateDescriptor wrapper */

public static int DftiCreateDescriptor(ref IntPtr desc,
int precision, int domain, int dimention, int length)
{
return DFTINative.DftiCreateDescriptor(ref desc,
precision, domain, dimention, length);
}

...

[SuppressUnmanagedCodeSecurity]
internal sealed class DFTINative
{
/** DFTI native DftiCreateDescriptor declaration */
[DllImport("mkl.dll", CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, SetLastError=false)]
internal static extern int DftiCreateDescriptor(ref IntPtr desc,
int precision, int domain, int dimention, int length);

Since the wrapper functions simply pass their arguments through unchanged, what is the benefit of the wrapper functions? Why is it better for the parts of the code that use this function to DFTI.DftiCreateDescriptor, rather than calling DFTINative.DftiCreateDescriptor directly themselves?

I assume it has something or other to do with containment of unsafe code, but I'd like to understand it better... and understand whether it is truly making the code safer.

Here is one piece of sample code you can use. "mkl_custom.dll" is a custom build mkl lib.

#region PInvoke calls
// CBLAS
[DllImport(@"mkl_custom.dll", ExactSpelling = true, SetLastError = false,
CallingConvention = CallingConvention.Cdecl)]
internal static extern void cblas_dcopy(int n, [In] double[] x, int incX,
[In, Out] double[] y, int incY);

#endregion PInvoke calls

Leave a Comment

Please sign in to add a comment. Not a member? Join today