Rhino crashes on native call


#1

I am trying to implement some Intel Math Kernel Library functionality in a grasshopper plugin. See link below for the example I am trying to implement:
https://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program
I can get this working in a c# windows forms program just fine, but as soon as I run it through grasshopper and I make a call to the mkl-dll’s Rhino and Grasshopper crashes without giving me an exception.
See code code snippet below:

/** Pardiso wrappers */
public class Pardiso
{
private Pardiso() { }
public static int pardiso(IntPtr[] handle,
ref int maxfct, ref int mnum,
ref int mtype, ref int phase, ref int n,
double[] a, int[] ia, int[] ja, int[] perm,
ref int nrhs, int[] iparm, ref int msglvl,
double[] b, double[] x, ref int error)
{
return PardisoNative.pardiso(handle, <--!!!RHINO CRASHES HERE!!!
ref maxfct, ref mnum, ref mtype, ref phase, ref n,
a, ia, ja, perm, ref nrhs, iparm, ref msglvl,
b, x, ref error);
}
}
/** Pardiso native declarations */
[SuppressUnmanagedCodeSecurity]
public class PardisoNative
{
private PardisoNative() { }
[DllImport("mkl_rt.dll", CallingConvention = CallingConvention.Cdecl,
ExactSpelling = true, SetLastError = false)]
internal static extern int pardiso([In, Out] IntPtr[] handle,
ref int maxfct, ref int mnum,
ref int mtype, ref int phase, ref int n,
[In] double[] a, [In] int[] ia, [In] int[] ja, [In] int[] perm,
ref int nrhs, [In, Out] int[] iparm, ref int msglvl,
[In, Out] double[] b, [Out] double[] x, ref int error);
}

Am I doing something wrong?

This is all I get from Visual Studio:

The program '[6772] Rhino.exe: Managed (v4.0.30319)' has exited with code 2 (0x2).

See grasshopper forum post for complete code:


#3

I am running this on 64bit Rhino 5 and the librarys are compiled for intel64.


(Steve Baer) #4

You might want to try force loading the library into the Rhino process first. That is the only guess I can even come close to right now for thinking of what could be going wrong.

See http://www.pinvoke.net/default.aspx/kernel32/LoadLibrary.html

Try loading “mkl_rt.dll” into the Rhino process


(Menno Deij - van Rijswijk) #5

A few things come to mind: how do you initialise the data that goes into the native call? Looking at the example given here there is quite some initialisation required prior to the first call to PARDISO.

Also, be aware that in debug mode, usually all variables are initialised to their default values, whereas in release mode, they are not.

Lastly, you may need to change your calling convention, the use of [In] and [Out] parameters or play with MarshalAs(...) to get this right. Typically, what I do in these cases is make my own native DLL that makes the call to the native library (in your case MKL). Then I P/Invoke to my own DLL. The advantage is that when you enable “support native debugging” for the C# project, you can step into your own DLL and inspect the state of all variables prior to calling into MKL. This may help you to pinpoint the problem.


#6

RESOLVED!

It worked when i force loaded “mkl_core.dll” for some reason. I’m not sure why that particular dll needed to be loaded separately.
Anyhow, thanks a lot guys, you are all very helpful.

Sincerely,
Gustav