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

Can one use MKL with C++/WinRT?

Eckhardt__Stephen
889 Views

I'm trying to call MKL routines from a program written for WinUI 3 using C++/WinRT.  Unfortunately, when I call a cblas routine, the program crashes.  STL exception handling catches nothing.  Is MKL compatible with C++/WinRT?

 

I created a repro and saved it in https://github.com/dr-eck/MiniEditor  as the TestMKL project.  Here's what I did:

First, create a standard "Blank App, Packaged (WinUI 3 in Desktop)" app and remove all traces of myProperty. Here's the new MainWindow.cpp:

#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif
#include "mkl.h"
#include <exception>

using namespace winrt;
using namespace Microsoft::UI::Xaml;

namespace winrt::TestMKL::implementation
{
    MainWindow::MainWindow()
    {
        InitializeComponent();
    }
    void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
    {
        myButton().Content(box_value(L"Clicked"));
        // Test MKL
        // Example: Compute the dot product of two vectors
        double x[3] = { 1.0, 2.0, 3.0 };
        double y[3] = { 4.0, 5.0, 6.0 };
        double rv;
        try {
            rv = cblas_ddot(3, x, 1, y, 1);
        }
        catch (const std::exception& e) {
            OutputDebugString((L"MKL exception: " + to_hstring(e.what()) + L"\n").c_str());
        }
        OutputDebugString((L"Dot product: " + std::to_wstring(rv) + L"\n").c_str());
        result().Text(winrt::hstring{ std::to_wstring(rv) });
    }
}

Then, as a review for OneAPI users:

  1. Install the OneAPI base toolkit
  2. Make sure ONEAPI_ROOT points to C:\Program Files (x86)\Intel\oneAPI\
  3. Add $(ONEAPI_ROOT)\mkl\latest\include to the C/C++ | General | Additional Include Directories
  4. Add $(ONEAPI_ROOT)\mkl\latest\lib to the Linker | General | Additional Library Directories
  5. Add mkl_intel_lp64_dll.lib;mkl_tbb_thread_dll.lib;mkl_core_dll.lib; to the Linker | Input | Additional Dependencies
  6. copy mkl_blacs_lp64.2.dll, mkl_core.2.dll and mkl_tbb_thread.2.dll to the x64\Debug\TestMKL\AppX directory

You must copy the DLLs even if you work from the Github repo.

The program should compile and run. When the button is clicked, the program disappears. Commenting out the MKL code verifies that the program does not disappear if cblas_ddot is not called.

How do I make this work?

0 Kudos
1 Solution
Viraj_M_Intel
Employee
708 Views

Hi @Eckhardt__Stephen ,

Looks like oneMKL stopped shipping these 2 files some time ago. I will check with the team and see if we can re-enable them. Sorry for the inconvenience.

As a workaround until then, you may use the Custom DLL builder to build a custom DLL with only the APIs that you need, and also enable UWP support for that DLL: https://www.intel.com/content/www/us/en/docs/onemkl/developer-guide-windows/2025-0/using-the-custom-dll-builder-in-command-line-mode.html. Check the "uwd_compat" and "crt" options. Note that custom DLL builder only works in the command-line. Once you have your custom DLL, you can use that in steps 5 and 6 instead of the default oneMKL libraries.

Please share if this works for you.

View solution in original post

0 Kudos
10 Replies
Shiquan_Su
Moderator
874 Views

Hi, Stephen:

Would you be more specific about which button you click? Can you run the debug mode to output the debug result? Can you see the actual compile/link commands to be run? Which step does your code crash? The compile step? The link step? Or the run time?

 

 

0 Kudos
Eckhardt__Stephen
843 Views

Thanks for the quick response.  In my instruction "First, create a standard "Blank App, Packaged (WinUI 3 in Desktop)" app", I should have mentioned that you need to use Visual Studio 2022.  If you follow this instruction, you will end up with a WinUI 3 app that displays one button.  When you click that button, the code in 

myButton_Click()

 runs.  The line of code where the program crashes is: 

rv = cblas_ddot(3, x, 1, y, 1);

Here's the debug output:

The thread 'DWM Compositor Thread' (25732) has exited with code 2 (0x2).
The thread 'DWM Master Input Thread' (37808) has exited with code 2 (0x2).
The thread 35044 has exited with code 2 (0x2).
The thread 28696 has exited with code 2 (0x2).
The thread 22284 has exited with code 2 (0x2).
The thread 38236 has exited with code 2 (0x2).
The thread 19496 has exited with code 2 (0x2).
The thread 'DManip Delegate Thread' (38376) has exited with code 2 (0x2).
The thread 23180 has exited with code 2 (0x2).
The thread 35644 has exited with code 2 (0x2).
The thread 35052 has exited with code 2 (0x2).
The thread 29832 has exited with code 2 (0x2).
The thread 19784 has exited with code 2 (0x2).
The thread 'Lifted DMIT' (25556) has exited with code 2 (0x2).
The thread 14616 has exited with code 2 (0x2).
The thread 'DWM Manipulation Thread' (37100) has exited with code 2 (0x2).
The thread 16132 has exited with code 2 (0x2).
The thread 'Composition Token Thread' (28208) has exited with code 2 (0x2).
The thread 34600 has exited with code 2 (0x2).
The program '[17628] TestMKL.exe' has exited with code 2 (0x2).

The code crashes in run time.

Please let me know if you need any other information.
 

0 Kudos
Viraj_M_Intel
Employee
816 Views

Hi @Eckhardt__Stephen 

Can you add `mkl_uwp_compat_dll.lib` and `mkl_uwp_compat.dll` in steps 5 and 6 of VS project setup respectively and check if the app works?

If you still have failures, please share the build commands from the command-line or build output in Visual studio.

0 Kudos
Eckhardt__Stephen
758 Views

I'll be happy to try that.  Where do I get copies of the files?  They aren't in my OneAPI distro, and I can't find them on the Intel.com site.

0 Kudos
Viraj_M_Intel
Employee
709 Views

Hi @Eckhardt__Stephen ,

Looks like oneMKL stopped shipping these 2 files some time ago. I will check with the team and see if we can re-enable them. Sorry for the inconvenience.

As a workaround until then, you may use the Custom DLL builder to build a custom DLL with only the APIs that you need, and also enable UWP support for that DLL: https://www.intel.com/content/www/us/en/docs/onemkl/developer-guide-windows/2025-0/using-the-custom-dll-builder-in-command-line-mode.html. Check the "uwd_compat" and "crt" options. Note that custom DLL builder only works in the command-line. Once you have your custom DLL, you can use that in steps 5 and 6 instead of the default oneMKL libraries.

Please share if this works for you.

0 Kudos
Eckhardt__Stephen
696 Views

Hi @Viraj_M_Intel ,

Thanks for checking with the team.  One important note:  WinUI 3 is part of the Windows App SDK, which is significantly different from UWP.  Because UWP is deprecated and Win App SDK is recommended for all future development, the ideal case would be to have `mkl_was_compat_dll.*` files rather than the UWP versions.  Feel free to fork my https://github.com/dr-eck/MiniEditor , specifically  the TestMKL project, to test the new files.

 

Thanks also for the workaround.  I'll try it, but "uwd_compat" forces "threading = sequential", which does not give me the performance enhancement I want.  I'll let you know later today if it works.

0 Kudos
Eckhardt__Stephen
661 Views

Hi @Viraj_M_Intel ,

I'm still working on creating a custom DLL.  The page you reference tells me to use nmake to make the DLL.  It took me a while to find the makefile, and it doesn't quite work.  I didn't have time to fix it today.  I also tried the Visual Studio project in the same part of the distro, but haven't got that to work yet either.  I'll try again tomorrow.

0 Kudos
Viraj_M_Intel
Employee
655 Views

NMake can be installed from the Visual studio installer, but needs to be used from the command-line for the custom DLL builder.

0 Kudos
Eckhardt__Stephen
609 Views

@Viraj_M_Intel Thanks.  I'm familiar with nmake, but a bit rusty.  I had to add the locations of lib.exe and mt.exe to my PATH and the location of libcmt.lib to my LIB.  Once I did this, nmake worked flawlessly.  I added the output files to the appropriate folders in my TestMKL app, and it works!

Now the bad news.  My real app uses a few LAPACKE functions, such as LAPACKE_dposv.  I added these to the "export" input file for nmake and rebuilt the DLLs.  They grew, so things were looking good.  Unfortunately, although the linker finds the cblas_* functions, it does not find the LAPACKE_* functions in the same .lib file.  I used Notepad++ to look at the .exp and .lib files, and the LAPACKE_*  files are in there. 

Here's one error message:

unresolved external symbol LAPACKE_dposv referenced in function "public: class std::vector<double,class std::allocator<double> > __cdecl DLS::Solve(class std::vector<double,class std::allocator<double> >,class std::vector<double,class std::allocator<double> >)" (?Solve@DLS@@QEAA?AV?$vector@NV?$allocator@N@std@@@std@@V23@V23@@Z)

Any clues as to why the linker is able to find the cblas_* functions but not the LAPACKE_* ones?

0 Kudos
Eckhardt__Stephen
596 Views

Oops!  Things work much better when you copy the new files to the correct folder.  (Blush).  Problem solved, at least for as long as I can tolerate single threaded performance.  Please let me know when I can either make a new custom DLL with a WindowsAppSDK_compat flag or beta test the new "mkl_was_compat_dll.*" files.

Reply