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

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

hello-world
Principiante
2.655 Visualizações

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 Respostas
mecej4
Colaborador honorário III
2.636 Visualizações

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.

hello-world
Principiante
2.607 Visualizações

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

RahulV_intel
Moderador
2.625 Visualizações

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


Gennady_F_Intel
Moderador
2.620 Visualizações

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

hello-world
Principiante
2.609 Visualizações

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;

}

 

Gennady_F_Intel
Moderador
2.393 Visualizações

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


Gennady_F_Intel
Moderador
2.393 Visualizações

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


Responder