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

SVD inside c#

darkcminor
Beginner
767 Views
I am doing a class for using svd inside c#, I have searched and think the svd function is dgesvd
I am doing something like this, but I do not know if the names are correct, Could you please tell me How to put the correct call and the parameters, thanks in advance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Runtime.InteropServices;
using mkl;
namespace csmkl
{
class Program
{
static void Main(string[] args)
{
Int32 n = 3, m = 3;
Int32 lda = n, ldu = m, ldvt = n;
Double[] superb = new Double[m - 1];
Double[] s = new Double;
Double[] u = new Double[n * n];
Double[] vt = new Double[n * n];
Double[] A = new Double[9]
{
8.79, 9.93, 9.83,
6.11, 6.91, 5.04,
-9.15, -7.93, 4.86
};
Char a1 = 'A';
Char a2 = 'N';
int mat_order = 0;
MKLImports.LAPACKE_dgesvd(
ref mat_order,
ref a1,
ref a2,
ref m,
ref n,
A,
ref lda,
s,
u,
ref ldu,
vt,
ref ldvt,
ref superb);
Console.WriteLine("s[0]: " + s[0]);
}
}
}


namespace mkl
{
[SuppressUnmanagedCodeSecurity]
internal sealed class MKLImports
{
private MKLImports()
{
}
[DllImport("mkl_rt.dll", ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
internal static extern voidLAPACKE_dgesvd(
ref int matrix_order,
ref char a1,
ref char a2,
ref int m,
ref int n,
[In, Out] double[] input_matrix,
ref int lda,
[In, Out] double[] s,
[In, Out] double[] u,
ref int ldu,
[In, Out] double[] vt,
ref int ldvt,
ref double[] superb
);
}
}
0 Kudos
1 Solution
Ying_H_Intel
Employee
767 Views

Hello darkcminor,

As MKL blas (Cblas)and lapack (lapacke)support both fortran and C interface.The LAPACKE is C interface, so the ref pass is not needed. you may change the code either to use c interface as below or use dgesvd fortran interface as Using Intel Mkl In Your C# Program

Best Regards,
Ying

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)
{
Int32 n = 3, m = 3;
Int32 lda = n, ldu = m, ldvt = n;
Double[] superb = new Double[m - 1];
Double[] s = new Double;
Double[] u = new Double[n * n];
Double[] vt = new Double[n * n];

Double[] A = new Double[9]
{
8.79, 9.93, 9.83,
6.11, 6.91, 5.04,
-9.15, -7.93, 4.86
};
Char a1 = 'A';
Char a2 = 'N';
int mat_order = 101;
;
Int32 info = 0;
double[] work1 = new double[1];
double[] work;
int lwork = -1;


MKLImports.LAPACKE_dgesvd(
mat_order,
a1,
a2,
m,
n,
A,
lda,
s,
u,
ldu,
vt,
ldvt,
superb);


/* MKLImports.dgesvd(
ref a1,
ref a2,
ref m,
ref n,
A,
ref lda,
s,
u,
ref ldu,
vt,
ref ldvt,
work1,
ref lwork,
ref info);

Console.WriteLine("info on exit: " + info);
Console.WriteLine("work1[0]: " + work1[0]);

lwork = (int)work1[0];
work = new double[lwork];
MKLImports.dgesvd(
ref a1,
ref a2,
ref m,
ref n,
A,
ref lda,
s,
u,
ref ldu,
vt,
ref ldvt,
work,
ref lwork,
ref info);
Console.WriteLine("info on exit: " + info);
*/
Console.WriteLine("s[0]: " + s[0]);

}
}
}


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_dgesvd(
int matrix_order,
char a1,
char a2,
int m,
int n,
[In, Out] double[] input_matrix,
int lda,
[In, Out] double[] s,
[In, Out] double[] u,
int ldu,
[In, Out] double[] vt,
int ldvt,
double[] superb
);

/* internal static extern void dgesvd(
ref char a1,
ref char a2,
ref int m,
ref int n,
[In, Out] double[] input_matrix,
ref int lda,
[In, Out] double[] s,
[In, Out] double[] u,
ref int ldu,
[In, Out] double[] vt,
ref int ldvt,
[In, Out] double[] work,
ref int lwork,
ref int info
);

*/
}
}

View solution in original post

0 Kudos
3 Replies
Ying_H_Intel
Employee
768 Views

Hello darkcminor,

As MKL blas (Cblas)and lapack (lapacke)support both fortran and C interface.The LAPACKE is C interface, so the ref pass is not needed. you may change the code either to use c interface as below or use dgesvd fortran interface as Using Intel Mkl In Your C# Program

Best Regards,
Ying

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)
{
Int32 n = 3, m = 3;
Int32 lda = n, ldu = m, ldvt = n;
Double[] superb = new Double[m - 1];
Double[] s = new Double;
Double[] u = new Double[n * n];
Double[] vt = new Double[n * n];

Double[] A = new Double[9]
{
8.79, 9.93, 9.83,
6.11, 6.91, 5.04,
-9.15, -7.93, 4.86
};
Char a1 = 'A';
Char a2 = 'N';
int mat_order = 101;
;
Int32 info = 0;
double[] work1 = new double[1];
double[] work;
int lwork = -1;


MKLImports.LAPACKE_dgesvd(
mat_order,
a1,
a2,
m,
n,
A,
lda,
s,
u,
ldu,
vt,
ldvt,
superb);


/* MKLImports.dgesvd(
ref a1,
ref a2,
ref m,
ref n,
A,
ref lda,
s,
u,
ref ldu,
vt,
ref ldvt,
work1,
ref lwork,
ref info);

Console.WriteLine("info on exit: " + info);
Console.WriteLine("work1[0]: " + work1[0]);

lwork = (int)work1[0];
work = new double[lwork];
MKLImports.dgesvd(
ref a1,
ref a2,
ref m,
ref n,
A,
ref lda,
s,
u,
ref ldu,
vt,
ref ldvt,
work,
ref lwork,
ref info);
Console.WriteLine("info on exit: " + info);
*/
Console.WriteLine("s[0]: " + s[0]);

}
}
}


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_dgesvd(
int matrix_order,
char a1,
char a2,
int m,
int n,
[In, Out] double[] input_matrix,
int lda,
[In, Out] double[] s,
[In, Out] double[] u,
int ldu,
[In, Out] double[] vt,
int ldvt,
double[] superb
);

/* internal static extern void dgesvd(
ref char a1,
ref char a2,
ref int m,
ref int n,
[In, Out] double[] input_matrix,
ref int lda,
[In, Out] double[] s,
[In, Out] double[] u,
ref int ldu,
[In, Out] double[] vt,
ref int ldvt,
[In, Out] double[] work,
ref int lwork,
ref int info
);

*/
}
}

0 Kudos
darkcminor
Beginner
767 Views
Thanks this is working, I would like to ask something more
I have found this page where you can see what libraries are recommended for a particular use case, specify the parameters in the drop down lists
If I Got
mkl_intel_c_dll.lib mkl_intel_thread_dll.lib mkl_core_dll.lib /Qopenmp
Where do I include this in Parrallel Studio XE 2011 with Visual Studio 2008?...
Does mkl_intel_thread_dll.lib assures multithreading?
0 Kudos
Ying_H_Intel
Employee
767 Views
Right, mkl_intel_thread_dll.lib is multithreading layer library. and *_dll, means dynamic link.

Here is more informationabout the topic.
http://software.intel.com/en-us/articles/parallelism-in-the-intel-math-kernel-library/
and http://software.intel.com/en-us/articles/intel-math-kernel-library-intel-mkl-for-linux-linking-applications-with-intel-mkl-version-100/

Best Regards,
Ying
0 Kudos
Reply