31

I am trying to embed Python 2.6 into MATLAB (7.12). I wanted to embed with a mex file written in C. This worked fine for small simple examples using scalars. However, if Numpy (1.6.1) is imported in anyway MATLAB crashes. I say anyway because I have tried a number of ways to load the numpy libraries including

  1. In the python module (.py):

    from numpy import *
    
  2. With PyRun_SimpleString in the mex file:

    PyRun_SimpleString(“from numpy import *”);
    
  3. Calling numpy functions with Py_oBject_CallObject:

    pOut  = PyObject_CallObject(pFunc, pArgs); 
    

Originally, I thought this may be a problem with embedding Numpy in C. However, Numpy works fine when embedded in simple C files that are compiled from the command line with /MD (multithread) switch with the Visual Studios 2005 C compiler. Next, I thought I will just change the make file in MATLAB to include the /MD switch. No such luck, mexopts.bat compiles with the /MD switch. I also manually commented out lines in the Numpy init module to find what was crashing MATLAB. It seems that loading any file with the extension pyd crashes MATLAB. The first of such files loaded in NumPy is multiarray.pyd. The MATLAB documentation describes how to debug mex files with visual studios which I did and placed the error message below. At this point I know the problem is a memory problem with the pyd’s and some conflict with MATLAB. Interestingly, I can use a system command in MATLAB to kick off a process in python that uses numpy and no error is generated. I will paste below the error message from MATLAB followed by the DEBUG output in visual studios of the processes that crash MATLAB. However, I am not pasting the whole thing because the list of first-chance exceptions is very long. Are there any suggestions for solving this integration problem?

MATLAB error
Matlab has encountered an internal problem and needs to close 
MATLAB crash file:C:\Users\pml355\AppData\Local\Temp\matlab_crash_dump.3484-1:


------------------------------------------------------------------------
       Segmentation violation detected at Tue Oct 18 12:19:03 2011
------------------------------------------------------------------------

Configuration:
  Crash Decoding  : Disabled
  Default Encoding: windows-1252
  MATLAB License  : 163857
  MATLAB Root     : C:\Program Files\MATLAB\R2011a
  MATLAB Version  : 7.12.0.635 (R2011a)
  Operating System: Microsoft Windows 7
  Processor ID    : x86 Family 6 Model 7 Stepping 10, GenuineIntel
  Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
  Window System   : Version 6.1 (Build 7600)

Fault Count: 1

Abnormal termination:
Segmentation violation

Register State (from fault):
  EAX = 00000001  EBX = 69c38c20
  ECX = 00000001  EDX = 24ae1da8
  ESP = 0088af0c  EBP = 0088af44
  ESI = 69c38c20  EDI = 24ae1da0

  EIP = 69b93d31  EFL = 00010202

   CS = 0000001b   DS = 00000023   SS = 00000023
   ES = 00000023   FS = 0000003b   GS = 00000000


Stack Trace (from fault):
[  0] 0x69b93d31 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00081201 ( ???+000000 )
[  1] 0x69bfead4 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00518868 ( ???+000000 )
[  2] 0x69c08039 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00557113 ( ???+000000 )
[  3] 0x08692b09                           C:/Python26/python26.dll+00076553 ( PyEval_EvalFrameEx+007833 )
[  4] 0x08690adf                           C:/Python26/python26.dll+00068319 ( PyEval_EvalCodeEx+002255 )



This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.

If this problem is reproducible, please submit a Service Request via:
    http://www.mathworks.com/support/contact_us/

A technical support engineer might contact you with further information.

Thank you for your help. 

Output from Visual Studios DEBUGGER

First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x751d9673 in MATLAB.exe: Microsoft C++ exception: jitCgFailedException at memory location 0x00c3e210..
First-chance exception at 0x751d9673 in MATLAB.exe: Microsoft C++ exception: jitCgFailedException at memory location 0x00c3e400..
First-chance exception at 0x69b93d31 in MATLAB.exe: 0xC0000005: Access violation writing location 0x00000001.
> throw_segv_longjmp_seh_filter()
throw_segv_longjmp_seh_filter(): invoking THROW_SEGV_LONGJMP SEH filter
> mnUnhandledWindowsExceptionFilter()
MATLAB.exe has triggered a breakpoint
eykanal
  • 26,437
  • 19
  • 82
  • 113
Paul Linden
  • 421
  • 4
  • 8
  • 1
    This will probably not be an easy one to track down. A possible reason is some conflict with loading multiple C runtime libraries (one for Python, one for Matlab, and possibly a different one for Numpy). First suggestion: you say that any .pyd file crashes. Did you try ones not from Numpy? The first step would be to get this basic part working, and only after that move to trying to make Numpy (which is more complicated) to work. (If solving this is too much work, the plan B would be to run Python in a separate process, and use some IPC method to communicate with Matlab.) – pv. Oct 18 '11 at 20:56
  • Good point. The first option as you mention is following the .pyd thread to ground. However, that may involve examining and possibly rewriting the C compiled pyd files. It takes one line of code to save a .mat file in MATLAB (save ‘data’, ‘variable’, ‘-v7’) (must be saved in earlier format for compatibility with Python) and one line of code to read it in Python (variable = loadmat(‘data.mat’))with the Scipy package (from scipy.io import loadmat). Also, there seems to be more written about wrapping MATLAB functions in Python which is what I will try. – Paul Linden Oct 20 '11 at 14:25
  • 1
    I'd say that avoiding embedding is the better course. Some years back I wrote a MEX file that embeds Python inside Matlab, and got everything working. (This was on Unix, though, google "pythoncall".) There were issues with loading binary Python modules (.pyd/.so) also there, but with some work those could be resolved. However, it didn't work with the next Matlab version --- overall, felt like a pretty fragile setup. – pv. Oct 21 '11 at 15:18
  • 2
    Ok, it seems that the stars are right now, and the http://github.com/pv/pythoncall module works again. In any case, you can see the hack there, and I'd imagine something similar could be required on Windows. – pv. Oct 21 '11 at 15:52
  • 3
    See also [Calling a Python function from MATLAB](http://stackoverflow.com/questions/1707780/call-python-function-from-matlab). – Cees Timmerman Nov 03 '11 at 09:39
  • a little late here, but I think @pv is right about conflicting CRT versions. Python 2.6 Windows binaries are compiled with VS2008. Anyway, I've tested [this code](http://algoholic.eu/matpy/) (had to comment a few Linux-specific lines), and it worked perfectly for me (WinXP 32-bit, MATLAB R2012a, VS2010, Python2.6)... – Amro Aug 03 '12 at 20:42
  • Similar question: http://stackoverflow.com/questions/1707780/call-python-function-from-matlab – Memming Jul 04 '13 at 19:24

2 Answers2

3

Try to approach the problem from the Python side: Python is a great glue language, I would suggest you to have Python run your Matlab and C programs. Python has:

  1. Numpy
  2. PyLab
  3. Matplotlib
  4. IPython

Thus, the combination is a good alternative for almost any existing Matlab module.

Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
kecske
  • 649
  • 1
  • 8
  • 19
  • 3
    Though civilly-put, this is the most misguided, useless, and insulting kind of response. Insulting because it assigns non-zero probability to the possibility that op didn't think of this. Useless because it ignores the "cross platform issues, proprietary tool chains, certification gates, licensed technologies, and stringent performance requirements on top of the issues with legacy codebases and workforce availability" (John Carmack) that op is probably facing. And misguided because you mean to help (but just don't). – Ahmed Fasih Dec 31 '13 at 18:03
0

With matlab 2014b a possibility to call python functions directly in m code was added.

Daniel
  • 36,610
  • 3
  • 36
  • 69