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

Access violation error when calling dlls built on MKL from Python using ctypes

hello-world
Beginner
1,164 Views

We have some c++ code built on MKL.  Recently, we would like to expose the functionality as a dll to Python using ctypes.

The code on Python side is the following

####################################################

conv = ctypes.cdll.LoadLibrary('myconvolutiondll.dll')

fun = conv.convolution

fun.argtypes = [ctypes.size_t, ndpointer(ctypes.c_float, flags="C_CONTINGUOUS"), ndpointer(ctypes.c_float, flags="C_CONTINGUOUS"), ]

input = numpy.arrange(6, dtype = "float32")

kernel = numpy.arrange(6, dtype = "float32")

fun(6, input, kernel)

#########################################################

When I ran this, I got "access violation reading 0x00000000000000003".  The error occurs when I call DftiCreateDescriptor in the c++ code.

Any thoughts on what might go wrong? Thanks

0 Kudos
7 Replies
mecej4
Honored Contributor III
1,145 Views

Try the correct spelling of the flag, C_CONTIGUOUS.

If that does not work, I'm afraid that you will have to wait for someone who knows Python to help you. In general, before using a user-built DLL with an interpreted language, it is useful to test the DLL by calling it from a C or Fortran main program.

0 Kudos
hello-world
Beginner
1,116 Views

Thanks mecej4. That was a typo. I have to type in the code... I posted the code at the bottom of the thread. 

0 Kudos
RahulV_intel
Moderator
1,134 Views

Hi,


Could you share your minimal reproducer code so that I can give it a try at my end?


Also, kindly specify your OS env, Visual Studio/Parallel Studio version(if applicable).


Thanks,

Rahul


0 Kudos
Gennady_F_Intel
Moderator
1,129 Views

you may also show us how did you build this dll.

0 Kudos
hello-world
Beginner
1,118 Views

Thanks Rahul and Gennady. 

The dlls works by calling from a c++ code. I am using visual studio 2019. 

In the python code, I did use "C_CONTIGUOUS" in the code. Had a typo while retype the code. 

Below are four files. myconvolutiondll.h and myconvolutiondll.cpp are the files to create dll. Convolution.h and Convolution.cpp are supporting class for computation. The access violation occurs in the function call DftiCreateDescriptor inside Convolution.cpp. The four files are listed below...

File 1: myconvolutiondll.h

/////////////////////////////////////////////////////////////////////

#ifdef MYCONVOLUTIONDLL_EXPORTS

#define MYCONVOLUTIONDLL_API __declspec(dllexport)

#else

#define MYCONVOLUTIONDLL_API __declspec(dllimport)

#endif

extern "C" MYCONVOLUTIONDLL_API size_t convolution(size_t n, float* input, const float* kernel);

//////////////////////////////////////////////////////////////////////////////////

File 2: myconvolutiondll.cpp

//////////////////////////////////////////////////////////////////////////////////

#include "Convolve.h"

#include "myconvolutiondll.h"

MYCONVOLUTIONDLL_API size_t convolution(size_t n, float* input, const float* kernel)

{

   return Convolve::convole(n, input, kernel);

}

///////////////////////////////////////////////////////////////////

File 3: Convolve.h

//////////////////////////////////////////////////////////////////

class Convolve

{

public:

static size_t convolve(size_t n, float* input, const float* kernel);

};

////////////////////////////////////////////////////////////////////

File 4: Convolve.cpp

//////////////////////////////////////////////////////////////////

#include "Convolve.h"

#Include "mkl_dfti.h"

#include "mkl.h"

#if defined(_WIN32)

#ifdef _WIN64

#pragma comment(lib, "mkl_intel_lp64_dll.lib")

#else

#pragma comment(lib, "mkl_intel_c_dll.lib")

#endif

#pragma comment(lib, "mkl_intel_thread_dll.lib")

#pragma comment(lib, "mkl_core_dll.lib")

#endif

size_t Convolve::convolve(size_t n, float* input, const float* kernel)

{

MKL_LONG status;

DFTI_DESCRIPTOR_HANDLE my_desc1_handle;

status = DftiCreateDescriptor(&my_desc1_handle, DFTI_SINGLE, DFTI_COMPLEX, 1, n); /// This is where access violation is occurred!

return 0;

}

 

0 Kudos
Gennady_F_Intel
Moderator
902 Views

I guess nobody at the mkl forum has no experience to help with this case. You may try to ask this question to the python forum.

https://community.intel.com/t5/Intel-Distribution-for-Python/bd-p/distribution-python


0 Kudos
Gennady_F_Intel
Moderator
902 Views

The issue is closing and we will no longer respond to this thread


0 Kudos
Reply