Software Archive
Read-only legacy content
17061 Discussions

Linear equations in c#

darkcminor
Beginner
545 Views
I am trying to follow the c program that makes use of dgesvroutine,
After executing the code if I print the variable info, the value is 1, I initialize it to 1, so after the call I would expect its value to be changed, but seems this is not happening, also the variable ipiv holds only zeros....
Could you please tell if I am doing things correctly? maybe, Could you suggest other routine to solve linear systems...
I have the following code:
namespace mkl
{
[SuppressUnmanagedCodeSecurity]
internal sealed class MKLImports
{
private MKLImports()
{
}
[DllImport("D:\\\\LINEAR_EQUATIONS\\\\LIB\\\\mkl_rt.dll",
ExactSpelling = true, SetLastError = false,
CallingConvention = CallingConvention.Cdecl)]
internal static extern void LAPACKE_dgesv(
int n,
int nrhs,
[In, Out] double[] input_matrix,
int lda,
[In, Out] int[] ipiv,
[In, Out] double[] b,
int incx,
int info
);
}
}
int N = 3;
int NRHS =5;
int LDA =N;
int LDB =N;
int n = N, nrhs = NRHS,lda = LDA,ldb = LDB, info=1;
int [] ipiv = new int;
Double[] a = new Double[5*5]
{
6.80, -2.11, 5.66, 5.97, 8.23,
-6.05, -3.30, 5.36, -4.44, 1.08,
-0.45, 2.58, -2.70, 0.27, 9.04,
8.32, 2.71, 4.35, -7.17, 2.14,
-9.67, -5.14, -7.26, 6.08, -6.87
};
Double[] b = new Double[5*3]
{
4.02, 6.19, -8.22, -7.57, -3.03,
-1.56, 4.00, -8.67, 1.75, 2.86,
9.81, -4.09, -4.57, -8.61, 8.99
};
MKLImports.LAPACKE_dgesv(n, nrhs, a, lda, ipiv, b, ldb, info);
0 Kudos
1 Reply
Ying_H_Intel
Moderator
545 Views

Deardarkcminor,

Most of your codes are correct. Just misstwo detials,
one is the function definition,
It is lapack_int LAPACKE_gesv( int matrix_order, lapack_int n, lapack_int nrhs, * a, lapack_int lda, lapack_int* ipiv, * b, lapack_int ldb ); not info as parameter.
You maysee mkl reference manual inMKL install directory.

Another one is the function interface, LAPACKE, which is C interface of LAPACK, special for C/C++ developer.The test code in dgesv_ex.c.htm is Fortran interface,so wedon't need transfer the input matrix to column-major as the sample code.

You may do themodificationas belowand get correct answer.

Additioanlly, there is a bunch of discussion about usemkl in C#in MKL forum,
for example, http://software.intel.com/en-us/forums/showthread.php?t=103272
C# mkl_alloc mkl_dcscmv sparse matrix/vector multiply.

If any more question,you maysubmitit in MKL forum
http://software.intel.com/en-us/forums/intel-math-kernel-library
and refer MKL KB Link:
http://software.intel.com/en-us/articles/intel-mkl-kb/all/1/

Best Regards,
Ying H.
//dgesv C# sample
using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Runtime.InteropServices;
using mkl;
namespace csmkl
{
class Program
{
static void Main(string[] args)
{
int mat_order = 101;
int N = 5;
int NRHS =3;
int LDA =N;
int LDB =N;
int n = N, nrhs = NRHS,lda = 5,ldb = 3, info=1;
int [] ipiv = new int;
/* Double[] a = new Double[5*5]
{
6.80, -2.11, 5.66, 5.97, 8.23,
-6.05, -3.30, 5.36, -4.44, 1.08,
-0.45, 2.58, -2.70, 0.27, 9.04,
8.32, 2.71, 4.35, -7.17, 2.14,
-9.67, -5.14, -7.26, 6.08, -6.87
};

*/
Double[] a = new Double[5 * 5] {
6.80 , -6.05 , -0.45, 8.32, -9.67,
-2.11, -3.30 , 2.58 , 2.71 , -5.14,
5.66 , 5.36 , -2.70, 4.35 , -7.26,
5.97 , -4.44 , 0.27, -7.17 , 6.08,
8.23, 1.08 , 9.04 , 2.14 , -6.87
};


/* Double[] b = new Double[5*3]
{
4.02, 6.19, -8.22, -7.57, -3.03,
-1.56, 4.00, -8.67, 1.75, 2.86,
9.81, -4.09, -4.57, -8.61, 8.99
};
*/
Double[] b = new Double[5 * 3]
{
4.02, -1.56 , 9.81,
6.19, 4.00, -4.09,
-8.22 , -8.67 , -4.57,
-7.57, 1.75 , -8.61,
-3.03 , 2.86, 8.99
};

MKLImports.LAPACKE_dgesv(mat_order,n, nrhs, a, lda, ipiv, b, ldb);

Console.WriteLine("info on exit: " + info);

Console.WriteLine("ipiv: " + ipiv[0] + ipiv[1] +ipiv[2]+ipiv[3]+ipiv[4]);

for (int i = 0; i < 5; i++)
for (int j = 0; j < 3; j++)
Console.WriteLine(b[i*3+j]);

}
}
}

namespace mkl
{
[SuppressUnmanagedCodeSecurity]
internal sealed class MKLImports
{
private MKLImports()
{
}

[DllImport("mkl_rt.dll",
ExactSpelling = true, SetLastError = false,
CallingConvention = CallingConvention.Cdecl)]
internal static extern void LAPACKE_dgesv(
int matrix_order,
int n,
int nrhs,
[In, Out] double[] input_matrix,
int lda,
[In, Out] int[] ipiv,
[In, Out] double[] b,
int ldb

);
}
}

//lapack_int LAPACKE_gesv( int matrix_order, lapack_int n, lapack_int nrhs, * a, lapack_int lda, lapack_int* ipiv, * b, lapack_int ldb );



0 Kudos
Reply