I can call Intel MKL cblas_dgem from C#, see the following code:
[DllImport("custom_mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
internal static extern void cblas_dgemm(
int Order, int TransA, int TransB, MKL_INT M, MKL_INT N, MKL_INT K,
double alpha, [In] double[,] A, MKL_INT lda, [In] double[,] B, MKL_INT ldb,
double beta, [In, Out] double[,] C, MKL_INT ldc);
and
void cblas_dgemm (const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE transa, const CBLAS_TRANSPOSE transb, const MKL_INT m, const MKL_INT n, const MKL_INT k, const double alpha, const double *a, const MKL_INT lda, const double *b, const MKL_INT ldb, const double beta, double *c, const MKL_INT ldc);
But I'm not able to call cblas_dgemm_batch from C#, see the following code:
[DllImport("custom_mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)] // not working
internal static extern void cblas_dgemm_batch(
int Layout, [In] int[] transa_array, [In] int[] transb_array, [In] MKL_INT[] m_array, [In] MKL_INT[] n_array, [In] MKL_INT[] k_array,
[In] double[] alpha_array, [In] double[][,] a_array, [In] MKL_INT[] lda_array, [In] double[][,] b_array, [In] MKL_INT[] ldb_array,
[In] double[] beta_array, [In, Out] double[][,] c_array, [In] MKL_INT[] ldc_array, MKL_INT group_count, [In] MKL_INT[] group_size);
and
void cblas_dgemm_batch (const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE* transa_array, const CBLAS_TRANSPOSE* transb_array, const MKL_INT* m_array, const MKL_INT* n_array, const MKL_INT* k_array, const double* alpha_array, const double **a_array, const MKL_INT* lda_array, const double **b_array, const MKL_INT* ldb_array, const double* beta_array, double **c_array, const MKL_INT* ldc_array, const MKL_INT group_count, const MKL_INT* group_size);
I'm getting the following error message:
- System.Runtime.InteropServices.MarshalDirectiveException
- Cannot marshal 'parameter #8': There is no marshaling support for nested arrays.
I can understand that the problem are the nested array parameters. This parameter should be array of pointers to arrays. But how can I call cblas_dgemm_batch from C#?
Using the following custom marshaler for the jagged arrays is the solution:
and using the above marshaler:
I'm using the following code to test it:
and
When I run the code it works. I can see in the debugger that the method
MarshalManagedToNative
is called 3 times (as expected) before the cblas_dgemm_batch is called.