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

Using mkl_set_xerbla

euan
New Contributor I
714 Views

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);
    }
}

0 Kudos
11 Replies
Zhang_Z_Intel
Employee
714 Views

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:

0 Kudos
euan
New Contributor I
714 Views

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.

0 Kudos
Zhang_Z_Intel
Employee
714 Views

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?

0 Kudos
euan
New Contributor I
714 Views

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.

0 Kudos
Zhang_Z_Intel
Employee
714 Views

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.

0 Kudos
Anonymous34
Beginner
714 Views

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.

 

0 Kudos
Anonymous34
Beginner
714 Views

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.

 

0 Kudos
Zhang_Z_Intel
Employee
714 Views

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!

0 Kudos
Anonymous34
Beginner
714 Views

Hi Zhang, thanks for the info. For what it's worth, this makes it virtually impossible to identify and debug problems with the calls -- I much prefer the ipp interface, which has return codes for the functions....

0 Kudos
mecej4
Honored Contributor III
714 Views

Scottw: I have verified that the example that you provided above (dgemm_with_timing.c in testmkl.zip) runs correctly with the current release of MKL (11.0.5, both the 32- and 64-bit versions) and calls the user-provided xerbla() , when a call to xerbla is instigated by intentionallly providing a negative value for the fourth argument to cblas_dgemm (I added a  '-' before "m,...").

Please note that BLAS/Lapack have a Fortran pedigree, whereas IPP has no such encumbrance. Therefore, it is to be expected that there will be dissimilarities between the characteristics of the two libraries.

0 Kudos
Gennady_F_Intel
Moderator
714 Views

the fix of the DPD200241955 issue is officially available in 11.0.Update5. Please check the problem and let us know the results.

0 Kudos
Reply