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

Impossible to link statically MKL from within a DLL

Trifon
Novice
1,494 Views

Can anyone help with the following issue?

Newer versions 9.* and 10.* do not allow MKL to be linked statically from within a DLL except ifthis DLL is built with the custom DLL builder.

To demonstrate the problem, usingVS2005 create a Class Library project (DLL project) that contains a single class with a single method. Link statically the Class Library project to mkl_c.lib and libguide.lib. Then create a console project and try to callthe DoDGEMM method.

The following code runs fine with version 8.1.1 but fails to run with versions 9.* and 10.*

This is the code for the DLL project

// stdafx.h

#pragma

once

#include

"mkl.h"

#define

ALIGNBYTES 16

extern

"C"

{

void* _aligned_malloc(size_t size, size_t alignment);

void _aligned_free(void *memblock);

}

// TEST2005_DLL.h

#pragma

once

#include

"stdafx.h"

using

namespace System;

namespace

TEST2005_DLL {

public ref class Class1

{

public:

void DoDGEMM()

{

// The following code works fine with version 8.1.1

// but it does not work with versions 9.* and 10.*

// Testing matrix multiplicatio c = a * b

// where a is a m x k matrix

// where b is a k x n matrix

// where c is a m x n matrix

// define m, k, n

int m = 50;

int k = 60;

int n = 70;

System::Random ^rand =

gcnew System::Random(1);

// define matrix "a" with random values

double *a = (double*) _aligned_malloc( m * k * sizeof(double), ALIGNBYTES);

for(int i=0; i< (m*k); i++) a = rand->NextDouble();

// define matrix "b" with random values

double *b = (double*) _aligned_malloc( k * n * sizeof(double), ALIGNBYTES);

for(int i=0; i < (k*n); i++) b = rand->NextDouble();

// define space for matrix "c"

double *c = (double*) _aligned_malloc( m * n * sizeof(double), ALIGNBYTES);

char transa = 'N';

char transb = 'N';

double alpha = 1.0;

double beta = 0.0;

int lda = m;

int ldb = k;

int ldc = m;

DGEMM(&transa, &transb, &m, &n, &k, α, a, &lda, b, &ldb, β, c, &ldc);

_aligned_free(a);

_aligned_free(b);

_aligned_free(c);

}

};

}

This is the code for the console application project using theDLL project

// TEST2005_EXE.cpp : main project file.

#include

"stdafx.h"

using

namespace System;

using

namespace TEST2005_DLL;

int

main(array<:STRING> ^args)

{

Class1^ cl =

gcnew Class1();

cl->DoDGEMM();

Console::WriteLine(

"Succeded");

Console::Read();

return 0;

}

0 Kudos
7 Replies
Intel_C_Intel
Employee
1,494 Views
I have the same problem. Any solution ?
0 Kudos
Andrey_G_Intel2
Employee
1,494 Views

Yes, you are right - it`s impossible to use statical MKL in your own DLL. It happen because in MKL we have thread safe mechanism and this mechanism is different for statical and dynamical libraries at windows. If you are trying to use statical libraries in your dynamical libraries you`ll receive a crash because MKL TLS section will not be initialized.

If you plan to create your own DLL you should use dynamical MKL libraries.

0 Kudos
Trifon
Novice
1,494 Views

Yes, but it did work with older versions of MKL. Newer versions should not reduce functionality.

At least there should be an option or a switch which allow this to be done. For example MKL TLS section would require a "manual" initialization before MKL is used.

0 Kudos
Andrey_G_Intel2
Employee
1,494 Views
As I can see you are using BLAS functionality. In newest MKL version we have new feature - memory manager what used in BLAS. This feature is thread safety and used TLS. In older MKL we have not memory manager and you are able to use statical MKL in your dynamical libraries, but not in case of VML&VSL.
0 Kudos
Trifon
Novice
1,493 Views

If that is because of the new memory manager can it be resolved by redefining memory functions as described in the user guide?

E.g. redefining i_malloc, i_free etc.

0 Kudos
Andrey_G_Intel2
Employee
1,493 Views
No, redefining default memory managers functions will not resolve your problem.
0 Kudos
lanmat
Beginner
1,494 Views

I am trying to use the Pardiso solver in Intel MKL in a matlab mex dll using IVF 10.1 on Windows. The program crashes when Pardiso is called. I have statically linked libs: libguide40.lib mkl_c.lib mkl_solver.lib. Can I do this with dynamic linking? Which libs do I then need and howdo I link to them?

/ Mats Landervik

0 Kudos
Reply