Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
13 Views

P/Invoke signature for mkl_domatcopy

[DllImport("mkl.dll", ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]

internal static extern void mkl_domatcopy(int order, int transpose, int rows, int cols, double alpha, [In] double[] source, int src_stride, [In, Out] double[] destination, int dst_stride);

but when I try to use it i get an an AccessViolationException (Attempted to read or write protected memory. This is often an indication that other memory is corrupt.)

The documentation says taht the unmanaged signature is //void mkl_domatcopy(char ordering, char trans, size_t rows, size_t cols, float *alpha, double *SRC, size_t src_stride, double *DST, size_t dst_stride);

which would mean that the signature should be sbyte and uint (as well as arrays for the dereferenced pointer) but using these types, using ref keyword instead didn't work - I got the same exception.

Does anyone have any ideas please?

0 Kudos
9 Replies
Highlighted
Beginner
13 Views

I've tried these signatures as well - with the same result:

[DllImport("mkl.dll", EntryPoint = "mkl_domatcopy", CallingConvention = CallingConvention.Cdecl)]

public static extern void MKL_Domatcopy(byte ordering, byte trans, uint rows, uint cols, double alpha, ref double[] A, uint lda, ref double[] B, uint ldb);

//C := alpha*op(A) + beta*op(B)

[DllImport("mkl.dll", EntryPoint = "mkl_domatadd", CallingConvention = CallingConvention.Cdecl)]

public static extern void MKL_DomatAdd(int ordering, int transA, int transB, int m, int n, double alpha, [In] double[] A, int lda, double beta, [In, Out] double[] B, int ldb, [In, Out] double[] C, int ldc);

I've wrapped these in a NativeMethods class and call:

NativeMethods.MKL_Domatcopy(byte.Parse("102"), byte.Parse("112"), rows, cols, 1.0, ref A, cols, ref b, rows);

NativeMethods.MKL_DomatAdd(Order, TransA, TransB, M, N, 1.0, A, lda, beta, B, ldb, C, ldc);

int Order = (int)CBLAS_ORDER.CblasRowMajor;

int TransA = (int)CBLAS_TRANSPOSE.CblasTrans;

int TransB = (int)CBLAS_TRANSPOSE.CblasTrans;

int M = 1, N = 1, K = 1;

int lda = K, ldb = N, ldc = N;

double[] A = new double[] { 1, 2, 3, 4, 5, 6 };

double[] B = new double[] { 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0 };

double[] C = new double[] { 5, 1, 3, 3, 11, 4, 6, 9 };

double alpha = 1, beta = 0;

I've tried row-major and column major order and M=2,N=3. I've also tried 'C' and 'T' so use lda = M, ldb = M. What am I doing wrong??

0 Kudos
Highlighted
Moderator
13 Views

What version of MKL you are using?


0 Kudos
Highlighted
Beginner
13 Views

build10.2.3.029

I'm using an AMD64 processor and building for any CPU in visual studio.

set %PATH%=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\x86_amd64;C:\Windows\Microsoft.NET\Framework64\v3.5

set %LIBRARY_PATH%=%MKLROOT%\em64t\lib;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64;C:\Program Files (x86)\Intel\Compiler\11.1\054\lib\intel64;%lib%

cd "C:\Users\Administrator\Desktop\Intel Math Kernel Library for Windows\Intel_MKL_C#_Examples"

nmake em64t MKLROOT="C:\Program Files\Intel\MKL\10.2.3.029"

0 Kudos
Highlighted
New Contributor III
13 Views

Hi,

Could you please provide the whole test code?
My test passes without a problem. I've create it on the base of the dgeev.cs MKLexample from the MKL KB (Knowledge Base):

using System;
using System.Security;
using System.Runtime.InteropServices;
using mkl;

public class mkl_domatcopy
{
private mkl_domatcopy() {}
public static void Main(String[] args)
{
int n = 5;
int info;
double[] A = new double[] { -2, -5, 1, -1, -3,
5, 0, -4, -1, 9,
8, -1, 1, 6, 0,
2, 0, 3, 1, -2,
-4, -3, 4, -4, -2, };
double[] B = new double[] { 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, };

printMatrix("Matrix B on eentry",B,n,n);
LAPACK.mkl_domatcopy( 'R', 'N', n, n, 1.0, A, n, B, n );
printMatrix("Matrix B on exit:",B,n,n);
Console.WriteLine("TEST PASSED");
}

/** Print the matrix X assuming row-major order of elements. */
private static void printMatrix(String prompt, double[] X, int I, int J)
{
Console.WriteLine(prompt);
for (int i=0; i {
for (int j=0; j Console.Write("\t" + X[i*J+j]);
Console.WriteLine();
}
}
}

namespace mkl
{
public sealed class LAPACK
{
private LAPACK() {}
/* LAPACK mkl_domatcopy wrapper */
public static void mkl_domatcopy(
char ordering, char trans,
int rows, int cols, double alpha,
double[] A, int lda, double[] B, int ldb)
{
LAPACKNative.mkl_domatcopy(
ordering, trans, rows, cols, alpha,
A, lda, B, ldb);
}
}

/** LAPACK native declarations */
[SuppressUnmanagedCodeSecurity]
internal sealed class LAPACKNative
{
private LAPACKNative() {}

[DllImport("mkl.dll", CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, SetLastError=false)]
internal static extern void mkl_domatcopy(
char ordering, char trans,
int rows, int cols, double alpha,
[In] double[] A, int lda,
[In, Out] double[] B, int ldb);
}
}

The output is:

Matrix B on eentry
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Matrix B on exit:
-2 -5 1 -1 -3
5 0 -4 -1 9
8 -1 1 6 0
2 0 3 1 -2
-4 -3 4 -4 -2
TEST PASSED

Thanks,
Vladimir

0 Kudos
Highlighted
Moderator
13 Views

For the more info, please look at the KB article which Vladimir had mentioned above by follow to the link.

--Gennady

0 Kudos
Highlighted
Beginner
13 Views

Thank you Vladimir - I used your code above and still got the same error. This rules out a problem with the P/Invoke signature. I'm thinking it might be a something to do with my Visual studio installation (everthing is referencing the x86 versions of the core dlls - shouldn'tmatter since the CLR chooses the correct versions at runtime - but might still be a possibility).Is this your experience?

Regards,


Emmett

0 Kudos
Highlighted
New Contributor III
13 Views

Hi Emmett,

Its my mistake. Of course, managed UInt64 type should be used instead of int type to pass sizes_t arguments onthe64-bit architecture.

Also MKL_Domatcopy name should be used instead of mkl_domatcopy.

So try the following code:

using System;
using System.Security;
using System.Runtime.InteropServices;
using mkl;

public class mkl_domatcopy
{
private mkl_domatcopy() {}
public static void Main(String[] args)
{
UInt64 n = 5;
double[] A = new double[] { -2, -5, 1, -1, -3,
5, 0, -4, -1, 9,
8, -1, 1, 6, 0,
2, 0, 3, 1, -2,
-4, -3, 4, -4, -2, };
double[] B = new double[] { 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, };

printMatrix("Matrix B on eentry",B,n,n);
LAPACK.mkl_domatcopy( 'R', 'N', n, n, 1.0, A, n, B, n );
printMatrix("Matrix B on exit:",B,n,n);
Console.WriteLine("TEST PASSED");
}

/** Print the matrix X assuming row-major order of elements. */
private static void printMatrix(String prompt, double[] X, UInt64 I, UInt64 J)
{
Console.WriteLine(prompt);
for (UInt64 i=0; i {
for (UInt64 j=0; j Console.Write("\t" + X[i*J+j]);
Console.WriteLine();
}
}
}

namespace mkl
{
public sealed class LAPACK
{
private LAPACK() {}
/* LAPACK mkl_domatcopy wrapper */
public static void mkl_domatcopy(
char ordering, char trans,
UInt64 rows, UInt64 cols, double alpha,
double[] A, UInt64 lda, double[] B, UInt64 ldb)
{
LAPACKNative.mkl_domatcopy(
ordering, trans, rows, cols, alpha,
A, lda, B, ldb);
}
}

/** LAPACK native declarations */
[SuppressUnmanagedCodeSecurity]
internal sealed class LAPACKNative
{
private LAPACKNative() {}

[DllImport("mkl.dll", CallingConvention=CallingConvention.Cdecl,
EntryPoint="MKL_Domatcopy",
CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=false)]
internal static extern void mkl_domatcopy(
char ordering, char trans,
UInt64 rows, UInt64 cols, double alpha,
[In] double[] A, UInt64 lda,
[In, Out] double[] B, UInt64 ldb);
}
}

Thanks,
Vladimir

0 Kudos
Highlighted
Beginner
13 Views

Hi Vladimir,

I have tried your code but the runtime raised an EntryPointNotFoundException for the MKL_Domatcopy entry point. Reverting to the mkl_domatcopy symbol, the entry point is found but this time an AccessViolationException is raised, as discussed in previous posts.

Im using version 10.2.4.032 of the the MKL, targeting the em64t platform in Visual Studio 2010 Ultimate RC (OS: Windows 7 Ultimate 64, CPU: Intel Core 2 Duo T7700).

Best regards,

Giovanni

0 Kudos
Highlighted
New Contributor III
13 Views

Giovanni,

Please make sure the symbol MKL_Domatcopy is on the list of functions to be included in the custom DLL and regenerate this library.

Best regards,

-Vladimir (another one)

0 Kudos