Using mkl_set_xerbla

Using mkl_set_xerbla

euan的头像

I'm trying to set a custom xerbla function in MKL 11 update 2. I'm calling the library from c# using the code below. However, it does not seem to replace the error function and still calls the inbuilt one. The code below is from a console app in visual studio 2010.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace MKLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // correct call to initialise the library
            IntelMathLibrary.CBLASDoubleMatrixVectorMultiply(CBLAS_ORDER.CblasRowMajor, CBLAS_TRANSPOSE.CblasNoTrans, 2, 4, 1, new double[8], 4, new double[4], 1, 1, new double[2], 1);

            // Do this to make sure that we have kept a reference to the callback correctly
            // otherwise it will be garbage collected and the callback will fail with a call to collected delegate exception
            GC.Collect();

            // the leading dimension of the matrix is wrong here
            IntelMathLibrary.CBLASDoubleMatrixVectorMultiply(CBLAS_ORDER.CblasRowMajor, CBLAS_TRANSPOSE.CblasNoTrans, 2, 4, 1, new double[8], 2, new double[4], 1, 1, new double[2], 1);
        }
    }

    public enum CBLAS_TRANSPOSE
    {
        CblasNoTrans = 111, /* trans='N' */
        CblasTrans = 112, /* trans='T' */
        CblasConjTrans = 113
    };

    public enum CBLAS_ORDER
    {
        CblasRowMajor = 101, /* row-major arrays */
        CblasColMajor = 102
    };

    public class IntelMathLibrary
    {
        private const string INTEL_DLL_NAME = "mkl_rt.dll";

        private static XerblaErrorHandlerCallback xerblaCallbackReferenceToStopItBeingGarbageCollected;
        static IntelMathLibrary()
        {
            xerblaCallbackReferenceToStopItBeingGarbageCollected = XerblaErrorHandler;
            setXerblaHandler(xerblaCallbackReferenceToStopItBeingGarbageCollected);
        }

        [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
        private delegate void XerblaErrorHandlerCallback(IntPtr srname, ref int info, int len);

        [DllImport(INTEL_DLL_NAME, EntryPoint = "mkl_set_xerbla", CallingConvention = CallingConvention.Cdecl)]
        private static extern XerblaErrorHandlerCallback setXerblaHandler(XerblaErrorHandlerCallback errorHandler);

        private static void XerblaErrorHandler(IntPtr srname, ref int info, int len)
        {
            throw new ArgumentException("Error in parameter " + info);
        }

        [DllImport(INTEL_DLL_NAME, EntryPoint = "cblas_dgemv", CallingConvention = CallingConvention.Cdecl,
           ExactSpelling = true, SetLastError = false)]
        public static extern void CBLASDoubleMatrixVectorMultiply([In] CBLAS_ORDER order,
            [In] CBLAS_TRANSPOSE transposeMatrixFlag, [In] int numberOfMatrixRows, [In] int numberOfMatrixColumns,
            [In] double matrixFactor, [In] double[] matrixData, [In] int leadingDimensionMatrix,
            [In] double[] vectorData, [In] int vectorIncrement,
            [In] double outputFactor, [In, Out] double[] outputVector, [In] int outputVectorIncrement);
    }
}

9 帖子 / 0 new
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项.
Zhang Z (Intel)的头像

These two online articles describe in great details how to use Intel MKL in C# programs. Please take a look and see if you followed these procedures:

euan的头像

Yes I have followed the procedures. We have been using MKL 10.1 for a number of years and previously created our own dll and replaced xerbla at the link stage. We did this in a similar way by setting a callback function to get back to managed code and throw the exception.

However, we are just in the process of updating to version 11.0 and have switched to using the single dynamic link library. In my code example above the mkl_set_xerbla function appears to function correctly. If I call it twice then on the second call I get the pointer to my function back and can call it and it behaves as expected. However, when I then call cblas_dgemv with incorrect parameters I just get the error message printed to the console and the callback function is not called.

Zhang Z (Intel)的头像

I'll take a deeper look at this issue. Is the code snippet you provided the only thing I need to reproduce the issue? Do yo happen to have a Visual Studio project that I can easily build and run?

euan的头像

I've attached a visual studio example. I've modified the code above slightly to make it clearer. In the static constructor for the IntelMathLibrary it sets the error handler and then checks that the returned function does what you expect. After the first call the MKL one is called and after the second our one is called. The program then calls cblas_dgemv with the incorrect parameters, but the error message is still the MKL one.

附件: 

附件尺寸
下载 mkltest.zip13.98 KB
Zhang Z (Intel)的头像

Thanks a lot for providing a reproducer. This issue is now escalated to the MKL team. As a workaround at this time, can you still build your custom DLL and replace the error handling function there? We will investigate why the single dynamic library (mkl_rt.lib) does not work the way you expected. I'll let you know as soon as possible.

scottw@eeg-persyst.com的头像

Hi, I'm having a similar problem with MKL update 2 and just downloaded update 3 with the same result.

I'm trying to use my own error handler from a C++ program (modified dgemm_with_timing.c). Here is the code I've added:

void xerbla(constchar*srname,constint*info,constintlsrname)

{

printf("\nXERBLA is called :%s: %d\n", srname, *info);

}

But the function never gets called when I run from the debugger and set an invalid parameter to dgemm. I do see the printf output from the function in the lib. I've tried running with program w/wo the call to mkl_set_xerbla.

I'm statically linking to the MKL libs. Running on Win7 x64.I've attached my VC++ project.

 

附件: 

附件尺寸
下载 testmlk.zip1.82 MB
scottw@eeg-persyst.com的头像

Hi, I'm having a similar problem with MKL update 2 and just downloaded update 3 with the same result.

I'm trying to use my own error handler from a C++ program (modified dgemm_with_timing.c). Here is the code I've added:

void xerbla(constchar*srname,constint*info,constintlsrname)

{

printf("\nXERBLA is called :%s: %d\n", srname, *info);

}

But the function never gets called when I run from the debugger and set an invalid parameter to dgemm. I do see the printf output from the function in the lib. I've tried running with program w/wo the call to mkl_set_xerbla.

I'm statically linking to the MKL libs. Running on Win7 x64.I've attached my VC++ project.

 

附件: 

附件尺寸
下载 testmlk.zip1.82 MB
Zhang Z (Intel)的头像

It is already a known issue that xerbla doesn't work with CBLAS functions. We are going to fix it in the upcoming release (MKL 11.0 Update 4) in the June-July timeframe.

Thanks!

登陆并发表评论。