From: Guilherme Rocha on
I seem to be having trouble when compiling the timestwo.F example provided in MATLABROOT/extern/examples/refbook.
I am using R2010a, 7.10.0.499 on a Mac with OS X 10.5.8 on, so this may have to do with the 64-bit thing.

First, a question: timestwo.F is supposed to work for 2x2 arrays, correct?
Assuming that is the case, something really strange happens.

The following works just fine:
>> timestwo(3.2)
ans =
6.4000

However, if I make a call to
>> timestwo([1.1 2.1;3.1 4.1])
causes MATLAB to freeze.
Activity monitor lists MATLAB as not responding.
In addition, it seems that MATLAB starts using progressively more processing on this call.
In an 8-core Mac, it starts consuming 100% CPU, then 200%.
By the time I used the Activity Monitor to put MATLAB out of its misery, it was consuming 400% CPU power (4 cores).

I am not sure if this means anything, but on line 16, timestwo.F has
C $Revision: 1.12.2.8 $
Have updates to this example been issued?

Also, right after the declarations I have added the line
call mexPrintf("\n\tCheck -3\n");
I had added some other checkpoints (Check -2, Check -1, ...) but, interestingly enough, not even "Check -3" got printed.

Any ideas what is going on?










mex -compatibleArrayDims -v timestwo.F
-> mexopts.sh sourced from directory (DIR = $HOME/.matlab/$REL_VERSION)
FILE = /Users/gvrocha/.matlab/R2010a/mexopts.sh
----------------------------------------------------------------
-> MATLAB = /Applications/MATLAB_R2010a.app
-> CC = gcc-4.0
-> CC flags:
CFLAGS = -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -fexceptions
CDEBUGFLAGS = -g
COPTIMFLAGS = -O2 -DNDEBUG
CLIBS = -L/Applications/MATLAB_R2010a.app/bin/maci64 -lmx -lmex -lmat -lstdc++
arguments = -DMX_COMPAT_32
-> CXX = g++-4.0
-> CXX flags:
CXXFLAGS = -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5
CXXDEBUGFLAGS = -g
CXXOPTIMFLAGS = -O2 -DNDEBUG
CXXLIBS = -L/Applications/MATLAB_R2010a.app/bin/maci64 -lmx -lmex -lmat -lstdc++
arguments = -DMX_COMPAT_32
-> FC = gfortran
-> FC flags:
FFLAGS = -fexceptions -m64 -fbackslash
FDEBUGFLAGS = -g
FOPTIMFLAGS = -O
FLIBS = -L/Applications/MATLAB_R2010a.app/bin/maci64 -lmx -lmex -lmat -L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/../../.. -lgfortran -L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3 -lgfortranbegin
arguments = -DMX_COMPAT_32
-> LD = gcc-4.0
-> Link flags:
LDFLAGS = -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010a.app/extern/lib/maci64/fexport.map
LDDEBUGFLAGS = -g
LDOPTIMFLAGS = -O
LDEXTENSION = .mexmaci64
arguments =
-> LDCXX =
-> Link flags:
LDCXXFLAGS =
LDCXXDEBUGFLAGS =
LDCXXOPTIMFLAGS =
LDCXXEXTENSION =
arguments =
----------------------------------------------------------------

-> gfortran -c -I/Applications/MATLAB_R2010a.app/extern/include -fexceptions -m64 -fbackslash -DMX_COMPAT_32 -O "timestwo.F"

-> gcc-4.0 -O -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010a.app/extern/lib/maci64/fexport.map -o "timestwo.mexmaci64" timestwo.o -L/Applications/MATLAB_R2010a.app/bin/maci64 -lmx -lmex -lmat -L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/../../.. -lgfortran -L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3 -lgfortranbegin

ld warning: in /Developer/SDKs/MacOSX10.5.sdk/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/libgfortranbegin.a, file is not of required architecture
ld warning: in /Developer/SDKs/MacOSX10.5.sdk/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/libgcc.a, file is not of required architecture
>>
From: James Tursa on
"Guilherme Rocha" <gvrocha(a)gmail.com> wrote in message <i41g2r$mf9$1(a)fred.mathworks.com>...
> I seem to be having trouble when compiling the timestwo.F example provided in MATLABROOT/extern/examples/refbook.
> I am using R2010a, 7.10.0.499 on a Mac with OS X 10.5.8 on, so this may have to do with the 64-bit thing.
>
> First, a question: timestwo.F is supposed to work for 2x2 arrays, correct?

No.

> Assuming that is the case, something really strange happens.
>
> The following works just fine:
> >> timestwo(3.2)
> ans =
> 6.4000
>
> However, if I make a call to
> >> timestwo([1.1 2.1;3.1 4.1])
> causes MATLAB to freeze.
> Activity monitor lists MATLAB as not responding.
>
> Any ideas what is going on?

A bug (of sorts) in the code. On first glance it *looks* like the code should work for matrices since they have the following lines:

m = mxGetM(prhs(1))
n = mxGetN(prhs(1))
size = m*n

and then they use size (a bad choice for a name btw since it conflicts with a Fortran intrinsic function of the same name) downstream for copying:

call mxCopyPtrToReal8(x_pr,x,size)
:
call mxCopyReal8ToPtr(y,y_pr,size)

But if you look closer you see that it will only work for scalars, not arrays. e.g., this code:

real*8 x, y
:
call timestwo(y, x)
:
subroutine timestwo(y, x)
real*8 x, y

So there is only space allocated for a scalar. Passing anything else will cause memory corruption and (as you have found out) crash MATLAB. The program should probably check the inputs more carefully, or allocate space accordingly for larger arrays.


James Tursa
From: James Tursa on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i41lbh$s00$1(a)fred.mathworks.com>...
>
> The program should probably check the inputs more carefully, or allocate space accordingly for larger arrays.

e.g., you could try this if your compiler supports %VAL (caveat: untested since I do not have a Fortran compiler handy at the moment):

#include "fintrf.h"
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
implicit none
!-ARG
mwpointer plhs(*), prhs(*)
integer*4 nlhs, nrhs
!-FUN
mwpointer, external :: mxDuplicateArray
mwpointer, external :: mxGetPr
integer*4, external :: mxIsDouble
mwsize, external :: mxGetNumberOfElements
!-LOC
mwpointer y_pr
mwsize n
!-----
!\
! Check for proper number of arguments.
!/
if(nrhs .ne. 1) then
call mexErrMsgTxt('One input required.')
elseif(nlhs .gt. 1) then
call mexErrMsgTxt('Too many output arguments.')
elseif( mxIsDouble(prhs(1)) .ne. 1 ) then
call mexErrMsgTxt('Input must be double.')
endif
!\
! Create output array
!/
plhs(1) = mxDuplicateArray(prhs(1))
!\
! Get the number of elements
!/
n = mxGetNumberOfElements(plhs(1))
!\
! Get pointer to the output data
!/
y_pr = mxGetPr(plhs(1))
!\
! Call the computational subroutine.
!/
call timestwo(n, %VAL(y_pr))
!\
! Done
!/
return
end
!---------------------------------------------------
subroutine timestwo(n, y)
implicit none
!-ARG
mwsize n
real*8 y(n)
!-----
y = 2.d0 * y
return
end