out of memory using a fortran-compiled mex

Hi All,
I compiled a fortran file download from http://oces.us/RNT/ using
>>mex rnt_oa3d_mex.f
and got rnt_oa3d_mex.mexa64 without errors.
Then, I run a matlab function "rnt_oa3d" in which rnt_oa3d_mex.mexa64 will be called, but I got a "out of memory" error:
>>[dataout,error,pmap]=rnt_oa3d(lonin,latin,zrin,tracer,lonout,latout,zrout,a,b,''); Error using rnt_oa3d_mex Out of memory. Type HELP MEMORY for your options.
Error in rnt_oa3d (line 50) [t2,error]=rnt_oa3d_mex(lonr,latr,zr,t1, lon,lat,z,pmap,a,b);
The OS of my computer is CentOS 6 with a 64-bit AMD CPU. The mexfunction in the nt_oa3d_mex.f is shown below.
Can anyone help me out? I have no idea where I can debug where the error is from. Thanks.
Li
subroutine mexfunction(nlhs, plhs, nrhs, prhs)
implicit none
C-----------------------------------------------------------------------
C (integer) Replace integer by integer*8 on the DEC Alpha and the
C SGI 64-bit platforms
C
integer*8 plhs(*), prhs(*)
integer*8 x,y,z,datain,xhat,yhat,zhat
integer*8 pmap,dataout,errout,a,b
integer*8 mxGetPr, mxCreateDoubleMatrix
C-----------------------------------------------------------------------
C
! matlab call:
! [dataout,errout]=oaTest(x,y,data,xhat,yhat);
integer nlhs, nrhs, mxGetM, mxGetN
integer m_in, n_in, size,PtIn,PtOut,PtIns,
c PtZin,PtZout
! get address of input arrays
x = mxGetPr(prhs(1))
y = mxGetPr(prhs(2))
z = mxGetPr(prhs(3))
datain = mxGetPr(prhs(4))
xhat = mxGetPr(prhs(5))
yhat = mxGetPr(prhs(6))
zhat = mxGetPr(prhs(7))
pmap = mxGetPr(prhs(8))
a = mxGetPr(prhs(9))
b = mxGetPr(prhs(10))
! set output matrix according to sizes of Sn (3)
m_in = mxGetM(prhs(4))
n_in = mxGetN(prhs(4))
PtIn = m_in
PtZin= n_in
m_in = mxGetM(prhs(7))
n_in = mxGetN(prhs(7))
PtOut = m_in
PtZout= n_in
plhs(1) = mxCreateDoubleMatrix(m_in, n_in, 0)
dataout = mxGetPr(plhs(1))
n_in = 1
plhs(2) = mxCreateDoubleMatrix(m_in, n_in, 0)
errout = mxGetPr(plhs(2))
m_in = mxGetM(prhs(8))
n_in = mxGetN(prhs(8))
PtIns = n_in
call oa_ex(%val(x), %val(y),%val(z),%val(datain),
c %val(xhat),%val(yhat),%val(zhat),
c PtIn,PtOut,PtZin,PtZout,%val(dataout),
c %val(errout),%val(a), %val(b),
c PtIns, %val(pmap))
return
end

 Accepted Answer

Qiang
Qiang on 19 Jun 2013
Edited: Qiang on 19 Jun 2013
Ok. I think I found the problem. Line 15-17 should be changed to fit a 64-bit system.
Original:
integer nlhs, nrhs, mxGetM, mxGetN
integer m_in, n_in, size,PtIn,PtOut,PtIns,
c PtZin,PtZout
change to
integer*8 nlhs, nrhs, mxGetM, mxGetN
integer*8 m_in, n_in, size,PtIn,PtOut,PtIns,
c PtZin,PtZout
In this case, matlab gives "out of memory" error. Maybe some mathworks people can give a answer why.
Li

More Answers (2)

Jan
Jan on 18 Jun 2013
You can either use a debugger to examine the fortran code oder place some commands to display messages until you find the command, which tries to create a variable, which does not fit into the available memory.
But then I guess, that your somputer simply does not habe enough memory installed to solve your problem. Then different strategies can be applied:
  1. Install more RAM.
  2. Clear variables, which are not used anymore.
  3. Install even more RAM.
  4. A proper pre-allocation reduces the memory fragmentation.
  5. Use smaller data types when possible (single instead of double, etc)
  6. You are using a 64 bit system already, so I omit this as a tip.
  7. But installing as much RAM as you can afford is the best solution.
Nothing can compete with real and free RAM. I've learned this is the day of my 1kB ZX81, and even the expensive upgrade to 16 kB did not allow the programmers to waste any byte.
Qiang
Qiang on 18 Jun 2013
My matrix is 163*107 for 2D case and 163*107*33 for 3D and my RAM is 8Gb. Even I reduced the matrix by (1:10:end,1:10:end), I still got the "out of memory" error.
I don't know how to debug in this case.
The only thing I did from the original code is to change mxCreateFull to mxCreateDoubleMatrix, as Matlab suggested.

Asked:

on 18 Jun 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!