Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

MKL, dgels and C#...

-jjh-
Beginner
1,266 Views
Hi.
I'm having a problem with dgels in my C# project. I have created a class to import dgels and dgemv:
[SuppressUnmanagedCodeSecurity]
internal sealed class MKLImports
{
privateMKLImports()
{
}
[DllImport("mkl_rt.dll", ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
internal static extern void dgels(
ref char trans,
ref int m,
ref int n,
ref int nrhs,
[In, Out] double[] a,
ref int lda,
[In, Out] double[] b,
ref int ldb,
[In, Out] double[] work,
ref int lwork,
ref int info
);
[DllImport("mkl_rt.dll", ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
internal static extern void dgemv(
ref char trans,
ref int m,
ref int n,
ref double alpha,
[In] double[] a,
ref int lda,
[In] double[] x,
ref int incX,
ref double beta,
[In, Out] double[] y,
ref int incY
);
}
Here is a code snipped to show how I use the imported dgels:
Int32 matrixARowCount = 800;
Int32 matrixAColumnCount = 3;
Double[] matrixA = new Double[matrixARowCount * matrixAColumnCount];
Double[] matrixB = new Double[799];
Char transpose = 'N';
Int32 nrhs = 1;
Int32 info = 0;
Int32 lwork = matrixAColumnCount + matrixARowCount * 100;
Double[] work = new Double[lwork];
MKLImports.dgels(
ref transpose,
ref matrixARowCount,
ref matrixAColumnCount,
ref nrhs,
matrixA,
ref matrixARowCount,
matrixB,
ref matrixARowCount,
work,
ref lwork,
ref info
);
if (info < 0)
{
// info is -i and the i-th parameter had an illegal value.
}
else if (info > 0)
{
// info is i and the i-th diagonal element of the triangular
// factor of A is zero, so that A does not have full rank;
// the least squares solution could not be computed.
}
else
{
// Ok.
}
If I comment out the place where dgels is called everything works just fine but when it is used my application freezes randomly. By "randomly" I mean that it freezes everytime when I run it but sometimes it runs for 10 seconds and sometime a couple of seconds. I once managed to get an error message that described stack overflow. I'm using Visual Studio 2008 and even try-catch doesn't do any good because my application just freezes and I have to kill it using Task Manager.
I'm new to MKL so I if you guys could see if there's something wrong with my MKLImports ([In/Out]'s, ref's, ...). If I have understood MKL's documentation right they should be like that.
Kind regards,
-J-
0 Kudos
1 Solution
Vladimir_Koldakov__I
New Contributor III
1,266 Views
Hello,
Could you please provide a testcase? My test passes without problems. It is based on your code and on the examples from the article Using Intel MKL in your C# program.
Note also, the matrixB size should be800: Double[] matrixB = new Double[800];
Example: dgels.zip

c:\wrk\forum>nmake intel64 MKLREDIST=c:\mkl1034\redist
Microsoft Program Maintenance Utility Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
Add path of the MKL redistributable to the path environment variable
set path=%MKLREDIST%\intel64\mkl;%MKLREDIST%\intel64\compiler;%path%
Build and run examples
nmake /a dgels.exe
Microsoft Program Maintenance Utility Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
Compile dgels.cs
csc .\dgels.cs
Microsoft Visual C# 2008 Compiler version 3.5.30729.4926
for Microsoft .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
Run dgels example
dgels.exe
info on exit: 0
TEST PASSED

Thanks,
Vladimir

View solution in original post

0 Kudos
5 Replies
Chao_Y_Intel
Moderator
1,266 Views

Hello,

Have you got the chance the following article? It provides the steps, and examples on using in C#:
http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program/

Thanks,
Chao

0 Kudos
-jjh-
Beginner
1,266 Views
Hi Chao and thanks for you reply.
The example only show how to import dgemm and dgeev. Not dgels or dgemv which I need. I don't think there's anything wrong with my code and how I have imported the functions. It's just weird how my application is not stable when I use those imported MKL functions. It just freezes randomly.
I just wanted to show how I have imported the functions and if somebody could tell me if I have done something wrong. Usually when something mystical happens, when managed and unmanaged code is used together, there's something wrong with marshaling. Well, in this case there's no need to do anything that involves "fixed" or "unsafe" keywords so the problem is propably with the parameters that are given to dgels and dgemv functions.
Is there a chance that dgels and dgemv could cause an unhandled exception that would cause freezing when invalid parameters are passed?
-J-
0 Kudos
-jjh-
Beginner
1,266 Views
Just tested my dgels import with data that is proven to be correct and I keep feeding it to dgels every time it is called in my application. It still hangs every now and then.
Has anyone ever had this kind of problems? This has got to be some kind of memory allocation/usage conflict. But there's no need to do any marshaling when you use MKL functions like in the example, right?
-J-
0 Kudos
Vladimir_Koldakov__I
New Contributor III
1,267 Views
Hello,
Could you please provide a testcase? My test passes without problems. It is based on your code and on the examples from the article Using Intel MKL in your C# program.
Note also, the matrixB size should be800: Double[] matrixB = new Double[800];
Example: dgels.zip

c:\wrk\forum>nmake intel64 MKLREDIST=c:\mkl1034\redist
Microsoft Program Maintenance Utility Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
Add path of the MKL redistributable to the path environment variable
set path=%MKLREDIST%\intel64\mkl;%MKLREDIST%\intel64\compiler;%path%
Build and run examples
nmake /a dgels.exe
Microsoft Program Maintenance Utility Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
Compile dgels.cs
csc .\dgels.cs
Microsoft Visual C# 2008 Compiler version 3.5.30729.4926
for Microsoft .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
Run dgels example
dgels.exe
info on exit: 0
TEST PASSED

Thanks,
Vladimir
0 Kudos
-jjh-
Beginner
1,266 Views
Hi Vladimir and thanks a lot!
The "799" in my example was a typo but thanks to you I double checked my code and there was a bug where I create the arrays. Unfortunately dgels doesn't throw any exceptions so my application just crashed. Maybe it handles the exception but it does not work when playing in unmanaged/managed environment.
Naturally you cannot calculate H x b, where H is x*y and b is y*z, if H.y != b.y.
-J-
0 Kudos
Reply