views:

498

answers:

7

Has anyone used the Intel Math Kernel library http://software.intel.com/en-us/intel-mkl/

I am thinking of using this for Random Number generation from a C# application as we need extreme performance (1.6 trillion random numbers per day).

Also any advice on minimising the overhead of consuming functions from this c++ code in my c# Monte Carlo simulation.

  • Am about to download the Eval from the site and above and try and benchmark this from my c# app, any help much appreciated.

Thanks

A: 

I don't know much / anything about this library. But if it is truly C++ code then you won't be able to call it directly from C#. C# is only capable of interacting with C++ in one of three ways

  1. PInvoke into a C wrapper on top of the C++ lib
  2. COM Interop
  3. Reverse PInvoke (still need 1 or 2 above to insert the wrapper func)

If it is a large C++ code base, it may be best to create a thin C++/CLI wrapper to interact with the library. Then call that from C#.

JaredPar
What is Reverse P/Invoke?
Erik Forbes
@Erik Reverse PInvoke is a call to managed code which originates from native code. It's done by passing a function pointer down into native code and having it be invoked back to managed code.
JaredPar
A: 

Declare the function call as "static" (although I'm not sure this would make any difference), and make sure your benchmarking compares the DLL call with the built-in C# Random class. I'm not sure the Intel code would be much faster, but who knows?

MusiGenesis
The built in .net Random Number is not very good. I am switching to this as a combination of speed and improvement in pseudo random number quality.
m3ntat
+1  A: 

I've developed a monte carlo/stochastic software using that utilizes the MKL and the Intel Compiler. In general, you will have to wrap the random number generation in a c++ dll. This is the easiest, as you can control name mangling and calling convention. As for minimizing overhead, the best way of going about this is to keep the simulation code completely in c++, where it probably belongs anyway, and only having the C# layer call in to get an update. The only way to minimize the interop penalty is to make fewer calls, I've found the other advice (/unsafe, etc) to be useless from a performance perspective. You can see an example of the interaction and structure of this type of program at my project's repository - Stochfit.

Steve
A: 

1) The link says "it includes improved integration with Microsoft Visual Studio"

2) There's an evaluation version

Therefore, why not try it? It may very well be Intel already provided the necessary bindings. Or not. At least you won't have wasted money on useless software.

JCCyC
A: 

Here's a high-quality efficient random number generator in C#. This code would be more efficient calling C++. Even if your C++ code ran in zero time, you'd still have the overhead of moving from managed code to unmanaged coded and back.

John D. Cook
A: 

Something that may or may not be obvious to you: Calls across managed and unmanaged code boundaries are slow, so if you go this route you will probably want to retrieve blocks of random numbers in large batches to amortize the calling time.

PeterAllenWebb
A: 

Intel has a set of examples which demonstrate calling MKL from C# http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program

kenbaldwin