mex on 64 bits mac os 10.6.3

  • Follow


Hello all,

I am having trouble compiling Fortran mex files in Matlab on Mac OS X Snow Leopard. I tested quite some things, but I am still not able to get things running. I took as test file timestwo.F and I have issues mex -v timestwo.F. The good news is that the file timestwo.mexmaci64 gets compiled without any warnings. The bad new is that when I try to run it, for example with 
a=2; b=timestwo(a);

Matlab crashes and immediately shuts down.

Here's some info on my configuration:

- Matlab 2009b 64-bits, 'computer' gives MACI64
- gcc:
 issuing gcc --version in the terminal gives
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646) (dot 1), 
 which gcc gives
 /usr/bin/gcc

- gfortran:
 issuing gfortran --version in the terminal gives
GNU Fortran (GCC) 4.5.0 20100107 (experimental)
 which gfortran gives
  /usr/local/bin/gfortran

- contents of mexopts.sh ( /Users/sanderse/.matlab/R2009b/mexopts.sh ):

        maci64)
#----------------------------------------------------------------------------
            # StorageVersion: 1.0
            # CkeyName: GNU C
            # CkeyManufacturer: GNU
            # CkeyLanguage: C
            # CkeyVersion:
            CC='gcc'
            SDKROOT='/Developer/SDKs/MacOSX10.6.sdk'
            MACOSX_DEPLOYMENT_TARGET='10.6'
            ARCHS='x86_64'
            CFLAGS="-fno-common -no-cpp-precomp -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
            CFLAGS="$CFLAGS  -fexceptions"
            CLIBS="$MLIBS"
            COPTIMFLAGS='-O2 -DNDEBUG'
            CDEBUGFLAGS='-g'
#
            CLIBS="$CLIBS -lstdc++"
            # C++keyName: GNU C++
            # C++keyManufacturer: GNU
            # C++keyLanguage: C++
            # C++keyVersion: 
            CXX='g++'
            CXXFLAGS="-fno-common -no-cpp-precomp -fexceptions -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
            CXXLIBS="$MLIBS -lstdc++"
            CXXOPTIMFLAGS='-O2 -DNDEBUG'
            CXXDEBUGFLAGS='-g'
#
            # FortrankeyName: GNU Fortran
            # FortrankeyManufacturer: GNU
            # FortrankeyLanguage: Fortran
            # FortrankeyVersion: 
            FC='/usr/local/bin/gfortran'
            FFLAGS='-fexceptions -fbackslash'
            FC_LIBDIR=`$FC -print-file-name=libgfortran.dylib 2>&1 | sed -n '1s/\/*libgfortran\.dylib//p'`
            FC_LIBDIR2=`$FC -print-file-name=libgfortranbegin.a 2>&1 | sed -n '1s/\/*libgfortranbegin\.a//p'`
            FLIBS="$MLIBS -L$FC_LIBDIR -lgfortran -L$FC_LIBDIR2 -lgfortranbegin"
            FOPTIMFLAGS='-O'
            FDEBUGFLAGS='-g'
#
            LD="$CC"
            LDEXTENSION='.mexmaci64'
            LDFLAGS="-Wl,-twolevel_namespace -undefined error -arch $ARCHS -Wl,-syslibroot,$SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
            LDFLAGS="$LDFLAGS -bundle -Wl,-exported_symbols_list,$TMW_ROOT/extern/lib/$Arch/$MAPFILE"
            LDOPTIMFLAGS='-O'
            LDDEBUGFLAGS='-g'
#
            POSTLINK_CMDS=':'
#----------------------------------------------------------------------------

As you can see I changed the 10.5 lines to 10.6, but without success. 
Any help on this issue is greatly appreciated. 

Benjamin
0
Reply Benjamin 4/19/2010 7:10:20 PM

"Benjamin " <bsanderse.remove.this@hotmail.com> wrote in message <hqi9qs$e2j$1@fred.mathworks.com>...
> Hello all,
> 
> I am having trouble compiling Fortran mex files in Matlab on Mac OS X Snow Leopard. I tested quite some things, but I am still not able to get things running. I took as test file timestwo.F and I have issues mex -v timestwo.F. 

Not sure whereas it's the cure, but IIRC sometime the option "-largeArrayDims" is needed when compiling on 64-bit OS.

Bruno
0
Reply Bruno 4/19/2010 7:44:04 PM


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hqibq4$jn6$1@fred.mathworks.com>...
> "Benjamin " <bsanderse.remove.this@hotmail.com> wrote in message <hqi9qs$e2j$1@fred.mathworks.com>...
> > Hello all,
> > 
> > I am having trouble compiling Fortran mex files in Matlab on Mac OS X Snow Leopard. I tested quite some things, but I am still not able to get things running. I took as test file timestwo.F and I have issues mex -v timestwo.F. 
> 
> Not sure whereas it's the cure, but IIRC sometime the option "-largeArrayDims" is needed when compiling on 64-bit OS.
> 
> Bruno

If you still have problems after adding "-largeArrayDims", try adding "-m64" to your compiler flags.

Ben
0
Reply Ben 4/20/2010 12:22:06 AM

Thanks for the suggestions. I added both -largeArrayDims and -m64 to the gfortran compiler flags, but unfortunately the problem persists. Any other suggestions?
0
Reply Benjamin 4/20/2010 5:51:05 PM

"Benjamin " <bsanderse.remove.this@hotmail.com> wrote in message <hqkpi9$t5i$1@fred.mathworks.com>...
> Thanks for the suggestions. I added both -largeArrayDims and -m64 to the gfortran compiler flags, but unfortunately the problem persists. Any other suggestions?

Hi Ben,

Did you find a solution to your problem?

I have a similar problem running on a 64bit Linux system:
 Linux 2.6.26-2-amd64 #1 SMP Tue Mar 9 22:29:32 UTC 2010 x86_64 GNU/Linux

However, my problem is that I cannot get the code to run when using -largeArrayDims.

Like you, I also used the timestwo.F as a test function.

 mex timestwo.F  WORKS
 mex -largeArrayDims timestwo.F  FAILS
 mex timestwo.c  WORKS
 mex -largeArrayDims timestwo.c  WORKS

By "WORKS" I mean that:
>> X=5; timestwo(X)        
ans =
    10

By "FAIL", I mean:
>> X=5; timestwo(X)        
??? Error using ==> timestwo
Too many output arguments.

(which is really just an indication that the test (nlhs .gt. 1) is failing, which it does so since it (probably) thinks nlhs is a long integer, when matlab is actually passing it a 4-byte integer.
nlhs is declared as:
 mwsize nlhs, nrhs
and with -largeArrayDims, mwsize is integer*8

Matlab is 64bit: Version 7.8.0.347 (R2009a) 64-bit (glnxa64)

Perhaps they are different problems.  Could it be the compiler version?  I'm using:
GNU Fortran (Debian 4.3.2-1.1) 4.3.2
0
Reply Eric 6/1/2010 2:39:04 PM

Hi Eric,

Nice that your mex-files are compiling. My problem is that I cannot compile on my Macbook, on Linux 64bits everythings works perfectly.
Regarding your problem: I think you somehow have to use the int64 command in Matlab and do something like 
a = timestwo(x),

because Matlab wants you to put this a=... notation (if you have specified nlhs(1)).

Hope this helps...

Ben

in case you want  
"Eric Tittley" <3ert@delete.this.and.the.3.roe.ac.uk> wrote in message <hu3628$6n9$1@fred.mathworks.com>...
> "Benjamin " <bsanderse.remove.this@hotmail.com> wrote in message <hqkpi9$t5i$1@fred.mathworks.com>...
> > Thanks for the suggestions. I added both -largeArrayDims and -m64 to the gfortran compiler flags, but unfortunately the problem persists. Any other suggestions?
> 
> Hi Ben,
> 
> Did you find a solution to your problem?
> 
> I have a similar problem running on a 64bit Linux system:
>  Linux 2.6.26-2-amd64 #1 SMP Tue Mar 9 22:29:32 UTC 2010 x86_64 GNU/Linux
> 
> However, my problem is that I cannot get the code to run when using -largeArrayDims.
> 
> Like you, I also used the timestwo.F as a test function.
> 
>  mex timestwo.F  WORKS
>  mex -largeArrayDims timestwo.F  FAILS
>  mex timestwo.c  WORKS
>  mex -largeArrayDims timestwo.c  WORKS
> 
> By "WORKS" I mean that:
> >> X=5; timestwo(X)        
> ans =
>     10
> 
> By "FAIL", I mean:
> >> X=5; timestwo(X)        
> ??? Error using ==> timestwo
> Too many output arguments.
> 
> (which is really just an indication that the test (nlhs .gt. 1) is failing, which it does so since it (probably) thinks nlhs is a long integer, when matlab is actually passing it a 4-byte integer.
> nlhs is declared as:
>  mwsize nlhs, nrhs
> and with -largeArrayDims, mwsize is integer*8
> 
> Matlab is 64bit: Version 7.8.0.347 (R2009a) 64-bit (glnxa64)
> 
> Perhaps they are different problems.  Could it be the compiler version?  I'm using:
> GNU Fortran (Debian 4.3.2-1.1) 4.3.2
0
Reply Benjamin 6/1/2010 3:40:05 PM

"Benjamin Sanderse" <bsanderse@hotmail.com> wrote in message <hu39kl$bql$1@fred.mathworks.com>...
>
> Regarding your problem: I think you somehow have to use the int64 command in Matlab and do something like 
> a = timestwo(x),
> 
> because Matlab wants you to put this a=... notation (if you have specified nlhs(1)).

No. You can always set prhs(1) even if nlhs = 0. MATLAB always reserves at least one spot so the answer can appear as ans if necessary.

James Tursa
0
Reply James 6/1/2010 5:00:22 PM

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hu3eb6$3eg$1@fred.mathworks.com>...
> "Benjamin Sanderse" <bsanderse@hotmail.com> wrote in message <hu39kl$bql$1@fred.mathworks.com>...
> >
> > Regarding your problem: I think you somehow have to use the int64 command in Matlab and do something like 
> > a = timestwo(x),
> > 
> > because Matlab wants you to put this a=... notation (if you have specified nlhs(1)).
> 
> No. You can always set prhs(1) ...

Typo ... should have written plhs(1)

James Tursa
0
Reply James 6/1/2010 6:17:04 PM

"Benjamin " <bsanderse.remove.this@hotmail.com> wrote in message <hqkpi9$t5i$1@fred.mathworks.com>...
> Thanks for the suggestions. I added both -largeArrayDims and -m64 to the gfortran compiler flags, but unfortunately the problem persists. Any other suggestions?

Was this issue resolved?  I'm having exactly the same problem with Mac OS X 10.6.4.  Mex compiles find but crashes Matlab when run.
0
Reply Jason 11/11/2010 7:19:04 PM

"Jason McEwen" <jason.mcewen@gmail.com> wrote in message <ibhfj8$ali$1@fred.mathworks.com>...
> "Benjamin " <bsanderse.remove.this@hotmail.com> wrote in message <hqkpi9$t5i$1@fred.mathworks.com>...
> > Thanks for the suggestions. I added both -largeArrayDims and -m64 to the gfortran compiler flags, but unfortunately the problem persists. Any other suggestions?
> 
> Was this issue resolved?  I'm having exactly the same problem with Mac OS X 10.6.4.  Mex compiles find but crashes Matlab when run.

I still don't have a solution. I have been running Matlab on a Linux system as a workaround.  
0
Reply Benjamin 11/12/2010 7:31:07 AM

"Benjamin" wrote in message <ibiqfr$qb9$1@fred.mathworks.com>...
> "Jason McEwen" <jason.mcewen@gmail.com> wrote in message <ibhfj8$ali$1@fred.mathworks.com>...
> > "Benjamin " <bsanderse.remove.this@hotmail.com> wrote in message <hqkpi9$t5i$1@fred.mathworks.com>...
> > > Thanks for the suggestions. I added both -largeArrayDims and -m64 to the gfortran compiler flags, but unfortunately the problem persists. Any other suggestions?
> > 
> > Was this issue resolved?  I'm having exactly the same problem with Mac OS X 10.6.4.  Mex compiles find but crashes Matlab when run.
> 
> I still don't have a solution. I have been running Matlab on a Linux system as a workaround.  

I have the same problem (the OSX one) and really unhappy right now about getting MATLAB on my new OSX Mac Pro. I use  Fortran a lot and would really like to be able to use MATLAB for high-end stuff (optimization, graphs, matrix computations). Fortran's gfortran works perfectly otherwise on my box and MATLAB as well. It is just about the mex issue...
0
Reply pcmichaud (4) 6/20/2011 3:30:20 PM

"Pierre" wrote in message <itnp2c$p8n$1@newscl01ah.mathworks.com>...
> "Benjamin" wrote in message <ibiqfr$qb9$1@fred.mathworks.com>...
> > "Jason McEwen" <jason.mcewen@gmail.com> wrote in message <ibhfj8$ali$1@fred.mathworks.com>...
> > > "Benjamin " <bsanderse.remove.this@hotmail.com> wrote in message <hqkpi9$t5i$1@fred.mathworks.com>...
> > > > Thanks for the suggestions. I added both -largeArrayDims and -m64 to the gfortran compiler flags, but unfortunately the problem persists. Any other suggestions?
> > > 
> > > Was this issue resolved?  I'm having exactly the same problem with Mac OS X 10.6.4.  Mex compiles find but crashes Matlab when run.
> > 
> > I still don't have a solution. I have been running Matlab on a Linux system as a workaround.  
> 
> I have the same problem (the OSX one) and really unhappy right now about getting MATLAB on my new OSX Mac Pro. I use  Fortran a lot and would really like to be able to use MATLAB for high-end stuff (optimization, graphs, matrix computations). Fortran's gfortran works perfectly otherwise on my box and MATLAB as well. It is just about the mex issue...

Same thing here.   For me it is really frustating since my mex functions work on Win32, Win64, Lnx32, Lnx64 (gcc, gfortran, mingw32 and ming64).  I have no problem to compile them but when I try to run some of them, matlab crashes saying that a symbol is missing on libgfortran.   What is weird is that some of them run without a problem and they have been compiled with the same options, compilers and etc.  

Cheers

Ed
0
Reply emammendes (145) 6/20/2011 8:47:04 PM

"Pierre" wrote in message <itnp2c$p8n$1@newscl01ah.mathworks.com>...
> "Benjamin" wrote in message <ibiqfr$qb9$1@fred.mathworks.com>...
> > "Jason McEwen" <jason.mcewen@gmail.com> wrote in message <ibhfj8$ali$1@fred.mathworks.com>...
> > > "Benjamin " <bsanderse.remove.this@hotmail.com> wrote in message <hqkpi9$t5i$1@fred.mathworks.com>...
> > > > Thanks for the suggestions. I added both -largeArrayDims and -m64 to the gfortran compiler flags, but unfortunately the problem persists. Any other suggestions?
> > > 
> > > Was this issue resolved?  I'm having exactly the same problem with Mac OS X 10.6.4.  Mex compiles find but crashes Matlab when run.
> > 
> > I still don't have a solution. I have been running Matlab on a Linux system as a workaround.  
> 
> I have the same problem (the OSX one) and really unhappy right now about getting MATLAB on my new OSX Mac Pro. I use  Fortran a lot and would really like to be able to use MATLAB for high-end stuff (optimization, graphs, matrix computations). Fortran's gfortran works perfectly otherwise on my box and MATLAB as well. It is just about the mex issue...


*** I solved my problem by mexing using cpp but calling a fortran program or function. For a fortran programmer, simply passing arguments trough c is easy enough and there is no need to program the underlying program in c. So the cost is minimal (i usually pass scalars and arrays or numbers, so easy with pointers,etc). 

Here is how I did it for the timestwo example.

You first create your f95 program, containing a ftimestwo function in fortran that simply doubles the input and compile using (i use gfortran)
gfortran -c ftimestwo.f95 -o ftimestwo.o

Then in the cpp (or c) timestwo.cpp file you add

extern "C" {
      void ftimestwo_(double *y, double *x);
}

in the preamble. In ftimestwo.o your function is referenced as ftimestwo_.So refer to your function with _ at end. Call the function in the cpp program (mexFunction). Fortran uses pointers by default to pass the arguments. Work in fortran like you usually do... The c gateway needs to pass pointers, this is why you have *y and not y. 

then mex using 

mex timestwo.cpp ftimestwo.o

this works like a charm for what I do. I hope Matlab works out in the future a more stable fortran mexing gateway.
0
Reply pcmichaud (4) 10/30/2011 3:52:10 PM

"Benjamin" wrote in message <hqi9qs$e2j$1@fred.mathworks.com>...
> Hello all,
> 
> I am having trouble compiling Fortran mex files in Matlab on Mac OS X Snow Leopard. I tested quite some things, but I am still not able to get things running. I took as test file timestwo.F and I have issues mex -v timestwo.F. The good news is that the file timestwo.mexmaci64 gets compiled without any warnings. The bad new is that when I try to run it, for example with 
> a=2; b=timestwo(a);
> 
> Matlab crashes and immediately shuts down.
> 
> Here's some info on my configuration:
> 
> - Matlab 2009b 64-bits, 'computer' gives MACI64
> - gcc:
>  issuing gcc --version in the terminal gives
> i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646) (dot 1), 
>  which gcc gives
>  /usr/bin/gcc
> 
> - gfortran:
>  issuing gfortran --version in the terminal gives
> GNU Fortran (GCC) 4.5.0 20100107 (experimental)
>  which gfortran gives
>   /usr/local/bin/gfortran
> 
> - contents of mexopts.sh ( /Users/sanderse/.matlab/R2009b/mexopts.sh ):
> 
>         maci64)
> #----------------------------------------------------------------------------
>             # StorageVersion: 1.0
>             # CkeyName: GNU C
>             # CkeyManufacturer: GNU
>             # CkeyLanguage: C
>             # CkeyVersion:
>             CC='gcc'
>             SDKROOT='/Developer/SDKs/MacOSX10.6.sdk'
>             MACOSX_DEPLOYMENT_TARGET='10.6'
>             ARCHS='x86_64'
>             CFLAGS="-fno-common -no-cpp-precomp -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
>             CFLAGS="$CFLAGS  -fexceptions"
>             CLIBS="$MLIBS"
>             COPTIMFLAGS='-O2 -DNDEBUG'
>             CDEBUGFLAGS='-g'
> #
>             CLIBS="$CLIBS -lstdc++"
>             # C++keyName: GNU C++
>             # C++keyManufacturer: GNU
>             # C++keyLanguage: C++
>             # C++keyVersion: 
>             CXX='g++'
>             CXXFLAGS="-fno-common -no-cpp-precomp -fexceptions -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
>             CXXLIBS="$MLIBS -lstdc++"
>             CXXOPTIMFLAGS='-O2 -DNDEBUG'
>             CXXDEBUGFLAGS='-g'
> #
>             # FortrankeyName: GNU Fortran
>             # FortrankeyManufacturer: GNU
>             # FortrankeyLanguage: Fortran
>             # FortrankeyVersion: 
>             FC='/usr/local/bin/gfortran'
>             FFLAGS='-fexceptions -fbackslash'
>             FC_LIBDIR=`$FC -print-file-name=libgfortran.dylib 2>&1 | sed -n '1s/\/*libgfortran\.dylib//p'`
>             FC_LIBDIR2=`$FC -print-file-name=libgfortranbegin.a 2>&1 | sed -n '1s/\/*libgfortranbegin\.a//p'`
>             FLIBS="$MLIBS -L$FC_LIBDIR -lgfortran -L$FC_LIBDIR2 -lgfortranbegin"
>             FOPTIMFLAGS='-O'
>             FDEBUGFLAGS='-g'
> #
>             LD="$CC"
>             LDEXTENSION='.mexmaci64'
>             LDFLAGS="-Wl,-twolevel_namespace -undefined error -arch $ARCHS -Wl,-syslibroot,$SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
>             LDFLAGS="$LDFLAGS -bundle -Wl,-exported_symbols_list,$TMW_ROOT/extern/lib/$Arch/$MAPFILE"
>             LDOPTIMFLAGS='-O'
>             LDDEBUGFLAGS='-g'
> #
>             POSTLINK_CMDS=':'
> #----------------------------------------------------------------------------
> 

I have now found that this was all due to the installation of gcc and gfortran. gcc 4.2 is installed as default on snow leopard. But not gfortran. So I had downloaded a gfortran package and installed it. Turns out, you need to re-install from scratch both GCC and gfortran together at 4.3. Check out this web page. Now everything works like a charm. 

http://jeffreyearly.com/2011/01/21/fortran-compiled-mex-files-with-matlab-2010b-on-mac-os-x/

> As you can see I changed the 10.5 lines to 10.6, but without success. 
> Any help on this issue is greatly appreciated. 
> 
> Benjamin
0
Reply pcmichaud (4) 3/18/2012 4:02:13 AM

13 Replies
421 Views

(page loaded in 0.189 seconds)

Similiar Articles:


















7/26/2012 12:18:58 PM


Reply: