[kepler-dev] Matlab interface on the Mac

Daniel Crawl crawl at sdsc.edu
Wed Jun 17 19:25:11 PDT 2009


Hi Christopher,

Thanks for letting me know about the updates.

I updated my sources, recompiled, and also got an error
when using Matlab 2009a. It appears to be caused by line
209 of ptmatlab.cc:

    int retval = mexPutVariable("global", str, ma);

If I replace it with:

    int retval = engPutVariable(ep, str, ma);

it works! According to:

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/rn/f34-998197.html

engPutArray() should be replaced with engPutVariable().

However, I haven't applied the latest Apple Java patch:

# java -version
java version "1.5.0_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)


Do you want ptmatlab.cc to compile with older versions of
Matlab? I tried with R2008a:

# make
CLASSPATH="../.." "/usr/bin/javah" -jni -o ptmatlab.h ptolemy.matlab.Engine
"/usr/bin/gcc" -DPT_NO_ENGGETARRAY -DPT_NO_ENGPUTARRAY \
        "-I/Applications/MATLAB_R2008a/extern/include" \
        
"-I/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/../Headers" 
\
        
"-I/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/../include/darwin" 
\
        ptmatlab.cc -fno-exceptions -dynamiclib -o libptmatlab.dylib    
-L"/Applications/MATLAB_R2008a/bin/maci"  -leng -lmx -lmex
/Applications/MATLAB_R2008a/extern/include/matrix.h:1231: error: 
expected unqualified-id before string constant
/Applications/MATLAB_R2008a/extern/include/matrix.h:1231: error: 
expected initializer before string constant

This error is caused by mxGetName() defined at the top of
ptmatlab.cc and then in matrix.h. It shouldn't be defined
if the version < R2009a.

Also, I think the deprecated functions were removed for
all platforms, not just Mac. I haven't installed 2009a yet
on my Linux and Windows boxes, but will try to verify in
the next few days.

  --dan


Christopher Brooks wrote:
> Hi Daniel,
> I'm working on getting the Matlab interface to run on my Mac.
>
> I had to make a few changes, see below.
> The biggest change is that in UtilityFunctions.loadProperty(),
> I'm now optionally prepending "lib" to the path on the Mac.
>
> Do you see this as a problem?  I just checked in my change to the
> ptII tree, when you have a chance, could you build from the current
> head (not the stable version) and check it?
>
> One other issue is that the interface does not work right now for
> me with Matlab2009a.  When I try to run one of the demos, I get
>
> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
>     at 
> apple.awt.CGraphicsEnvironment.resetDisplays(CGraphicsEnvironment.java:99) 
>
>     at 
> apple.awt.CGraphicsEnvironment.displayChanged(CGraphicsEnvironment.java:49) 
>
>     at apple.awt.CToolkit$4.run(CToolkit.java:1310)
>     at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
>     at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
>     at 
> java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269) 
>
>     at 
> java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) 
>
>     at 
> java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
>     at 
> java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
>     at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
>
>
> Interestingly, I get a similar stack trace when I start
> Matlab outside Ptolemy.  I suspect the recent patch to Java from
> Apple.  I've reported this as a bug to The Mathworks, we'll see
> what happens.
>
> It might be interesting to try the Ptolemy/Matlab interface before 
> patching
> Java and then after.  After the Apple Java patch, I'm running:
>
> bash-3.2$ java -version
> java version "1.5.0_19"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_19-b02-304)
> Java HotSpot(TM) Client VM (build 1.5.0_19-137, mixed mode, sharing)
>
> The complete changes are below.
>
> _Christopher
>
>> Author: cxh
>> Date: 2009-06-17 14:19:59 -0700 (Wed, 17 Jun 2009)
>> New Revision: 54467
>>
>> Modified:
>>    trunk/configure.in
>>    trunk/ptolemy/data/expr/UtilityFunctions.java
>>    trunk/ptolemy/matlab/ptmatlab.cc
>> Log:
>> Changes to get Matlab 2009a to run under Mac OS X: engGetArray() and 
>> engPutArray() are no longer defined.  We need to include -lmex.  
>> UtilityFunctions.loadLibrary() now prepends 'lib' if necessary.  Some 
>> of these changes might be generic to Matlab 2009a and not specific to 
>> Mac OS X.
>>
>> Modified: trunk/configure.in
>> ===================================================================
>> --- trunk/configure.in    2009-06-17 19:38:33 UTC (rev 54466)
>> +++ trunk/configure.in    2009-06-17 21:19:59 UTC (rev 54467)
>> @@ -2898,8 +2898,8 @@
>>              AC_MSG_CHECKING([which major type of OS we are running 
>> under])
>>          # Older versions of cygwin might not have env, but 1.1 does
>>          if env CLASSPATH="$PTII/config" "$JAVA" MajorOSName > 
>> /dev/null; then
>> -            # Run it again and get the results
>> -            MAJOR_OS_NAME=`CLASSPATH="$PTII/config" "$JAVA" 
>> MajorOSName`
>> +            # Run it again and get the results.  We get just the 
>> first word: Windows, Linux, Mac etc.
>> +            MAJOR_OS_NAME=`CLASSPATH="$PTII/config" "$JAVA" 
>> MajorOSName | awk '{print $1}'`
>>              PT_MSG_RESULT($MAJOR_OS_NAME)
>>          else                AC_MSG_WARN(['CLASSPATH="$PTII/config" 
>> $JAVA MajorOSName' failed.
>> @@ -2945,6 +2945,7 @@
>>              ;;
>>          Darwin)
>>              MATLAB_LIBDIR=${MATLAB_DIR}/bin/maci
>> +            PTMATLAB_CC_FLAGS="-DPT_NO_ENGGETARRAY 
>> -DPT_NO_ENGPUTARRAY"              ;;
>>          *)
>>              MATLAB_LIBDIR=${MATLAB_DIR}/extern/lib
>> @@ -2974,7 +2975,7 @@
>>              fi
>>                  else              if test $PTJNI_ARCHITECTURE = 
>> "darwin"; then
>> -                PTMATLAB_LD_ARGS="ptmatlab.cc -fno-exceptions 
>> -dynamiclib -o 
>> ${PTJNI_SHAREDLIBRARY_PREFIX}ptmatlab.${PTJNI_SHAREDLIBRARY_SUFFIX}    
>> -L\"${MATLAB_LIBDIR}\" ${PTJNI_SHAREDLIBRARY_LDFLAG} -leng -lmx"
>> +                PTMATLAB_LD_ARGS="ptmatlab.cc -fno-exceptions 
>> -dynamiclib -o 
>> ${PTJNI_SHAREDLIBRARY_PREFIX}ptmatlab.${PTJNI_SHAREDLIBRARY_SUFFIX}    
>> -L\"${MATLAB_LIBDIR}\" ${PTJNI_SHAREDLIBRARY_LDFLAG} -leng -lmx -lmex"
>>              else
>>                  PTMATLAB_LD_ARGS="-shared ptmatlab.cc 
>> -fno-exceptions -o 
>> ${PTJNI_SHAREDLIBRARY_PREFIX}ptmatlab.${PTJNI_SHAREDLIBRARY_SUFFIX}    
>> -L\"${MATLAB_LIBDIR}\" ${PTJNI_SHAREDLIBRARY_LDFLAG} -lc -leng -lmx"
>>                  if test "`uname -m`" = "x86_64"; then
>>
>> Modified: trunk/ptolemy/data/expr/UtilityFunctions.java
>> ===================================================================
>> --- trunk/ptolemy/data/expr/UtilityFunctions.java    2009-06-17 
>> 19:38:33 UTC (rev 54466)
>> +++ trunk/ptolemy/data/expr/UtilityFunctions.java    2009-06-17 
>> 21:19:59 UTC (rev 54467)
>> @@ -859,11 +859,13 @@
>>                      shortLibraryName = library;
>>                  } else {
>>                      if (!library.substring(index, index + 
>> 4).equals("/lib")) {
>> -                        if (osName.startsWith("Mac OS X")
>> -                                || osName.startsWith("Linux")) {
>> +                        if (osName.startsWith("Linux")) {
>>                              library = library.substring(index + 1);
>>                              shortLibraryName = library;
>>                          } else {
>> +                            // Under SunOS and Mac OS X, we add lib to
>> +                            // the path.  If we don't do this on the 
>> Mac,
>> +                            // then libptymatlab.dynlib will not be 
>> found.
>>                              shortLibraryName = "/lib"
>>                                      + library.substring(index + 1);
>>                              library = library.substring(0, index)
>>
>> Modified: trunk/ptolemy/matlab/ptmatlab.cc
>> ===================================================================
>> --- trunk/ptolemy/matlab/ptmatlab.cc    2009-06-17 19:38:33 UTC (rev 
>> 54466)
>> +++ trunk/ptolemy/matlab/ptmatlab.cc    2009-06-17 21:19:59 UTC (rev 
>> 54467)
>> @@ -52,10 +52,36 @@
>>  // version of the ptmatlab shared library to load...
>>  #define V5_COMPAT
>>  
>> +#define PT_NO_MXGETNAME
>> +#ifdef PT_NO_MXGETNAME
>> +#define mxGetName(a) "a"
>> +#endif
>> +
>>  #include <jni.h>
>>  #include "ptmatlab.h"
>>  #include "engine.h"
>>  
>> +// These defines were needed for Matlab r2009a under Mac OS X
>> +//#define PT_NO_ENGGETARRAY +//#define PT_NO_ENGPUTARRAY
>> +
>> +
>> +#ifdef PT_NO_ENGGETARRAY
>> +#ifndef PT_INCLUDE_MEX_H
>> +#define PT_INCLUDE_MEX_H
>> +#endif
>> +#endif
>> +
>> +#ifdef PT_NO_ENGPUTARRAY
>> +#ifndef PT_INCLUDE_MEX_H
>> +#define PT_INCLUDE_MEX_H
>> +#endif
>> +#endif
>> +
>> +#ifdef PT_INCLUDE_MEX_H
>> +#include "mex.h"
>> +#endif
>> +
>>  // The following test is a kludge for a missing #define in the matlab
>>  // include files that would allow engine interface C code to test which
>>  // matlab version it is dealing with... This is a test for an earlier
>> @@ -155,7 +181,11 @@
>>        Engine *ep = (Engine*)(ptrlong) e;
>>      char debug = ptmatlabGetDebug(jni,obj);
>>      const char *str = jni->GetStringUTFChars(name, 0);
>> +#ifdef PT_NO_ENGGETARRAY +    mxArray *ma = engGetVariable(ep, str);
>> +#else
>>      mxArray *ma = engGetArray(ep, str);
>> +#endif
>>      if (debug > 1 && ma != NULL) {
>>          const int *dimArray = mxGetDimensions(ma);
>>          printf("ptmatlabEngGetArray(%s) %d x %d\n", str, 
>> dimArray[0], dimArray[1]);
>> @@ -175,10 +205,14 @@
>>      Engine *ep = (Engine*)(ptrlong) e;
>>      const char *str = jni->GetStringUTFChars(name, 0);
>>      mxArray *ma = (mxArray*)(ptrlong)pma;
>> +#ifdef PT_NO_ENGPUTARRAY
>> +    int retval = mexPutVariable("global", str, ma);
>> +#else
>>      mxSetName(ma, str);
>>      const int *dimArray = mxGetDimensions(ma);
>>      if (debug > 1) printf("ptmatlabEngPutArray(%s) %d x %d\n", str, 
>> dimArray[0], dimArray[1]);
>>      int retval = engPutArray(ep, ma);
>> +#endif
>>      jni->ReleaseStringUTFChars(name, str);
>>      return retval;
>>    }
>>
>> _______________________________________________
>> Ptexternal-cvs mailing list
>> Ptexternal-cvs at chess.eecs.berkeley.edu
>> http://chess.eecs.berkeley.edu/ptexternal/listinfo/ptexternal-cvs
>




More information about the Kepler-dev mailing list