Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Mex file error with large array

Subject: Mex file error with large array

From: m

Date: 20 Mar, 2013 11:57:06

Message: 1 of 2

Hello,

I have two cubes of data and I want to make a linear regression between two little cubes (one in each big cube of data), so I make this code (example here) : X_pad and Y_pad are our data but with a padding around the cube to compute the linear regression everywhere.
So I have to do three loops and I make a mex file in Fortran to do it.

But when I run it, with, for example data with this size : 1500x300x800, I have this error :

'test.mexa64': test.mexa64: cannot map zero-fill pages: Cannot allocate memory

Do you know what that mean and how to fix it ?

Here is the interface function :

     subroutine mexFunction(nlhs, plhs, nrhs, prhs)
 
      implicit none

      mwPointer plhs(*), prhs(*)
      integer nlhs, nrhs
 
      mwPointer mxCreateDoubleMatrix, mxGetPr
      integer mxIsNumeric
      mwSize mxGetM, mxGetN
 
      mwPointer x_pr, y_pr, z_pr, sized_pr, win_pr
 
      mwsize m, n, size, nx, nz, ny, wl, wx, wy, long
 

      integer*4 numel
      parameter(numel = 9000000000)
      real*8 x(numel), y(numel), z(numel), sized(numel), win(numel)
      call mexWarnMsgTxt("test1")

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

      long = 3
      x_pr = mxGetPr(prhs(1))
      z_pr = mxGetPr(prhs(2))
      sized_pr = mxGetPr(prhs(3))
      win_pr = mxGetPr(prhs(4))
      call mxCopyPtrToReal8(x_pr,x,size)
      call mxCopyPtrToReal8(z_pr,z,size)
      plhs(1) = mxCreateDoubleMatrix(m,n,0)
      y_pr = mxGetPr(plhs(1))
      call mxCopyPtrToReal8(sized_pr,sized,long)
      call mxCopyPtrToReal8(win_pr,win,long)
      wl=win(1)
      wx=win(2)
      wy=win(3)
      nz=sized(1)
      nx=sized(2)
      ny=sized(3)

      call grad(y, x, z, m, n, wl, wx, wy, nx, ny, nz)
      call mxCopyReal8ToPtr(y,y_pr,size)
      return
      end



Thank you very much.

Subject: Mex file error with large array

From: James Tursa

Date: 20 Mar, 2013 21:46:05

Message: 2 of 2

"m " <mathdu69180@hotmail.fr> wrote in message <kic86i$ppm$1@newscl01ah.mathworks.com>...
> Hello,
>
> I have two cubes of data and I want to make a linear regression between two little cubes (one in each big cube of data), so I make this code (example here) : X_pad and Y_pad are our data but with a padding around the cube to compute the linear regression everywhere.
> So I have to do three loops and I make a mex file in Fortran to do it.
>
> But when I run it, with, for example data with this size : 1500x300x800, I have this error :
>
> 'test.mexa64': test.mexa64: cannot map zero-fill pages: Cannot allocate memory
>
> Do you know what that mean and how to fix it ?
>
> Here is the interface function :

(snip)

> integer*4 numel
> parameter(numel = 9000000000)
> real*8 x(numel), y(numel), z(numel), sized(numel), win(numel)

You do NOT want to do what you are doing above. The variables x, y, z, sized, and win are LOCAL variables, meaning they will in all likelihood get their memory from the stack which has a limited size. So you are probably overflowing your stack. Particularly for large variables you need to be getting this memory from the heap, not the stack. So make these variables allocatable instead. E.g.,

       integer*4 numel, istat
       parameter(numel = 9000000000)
       real*8, allocatable :: x(:), y(:), z(:), sized(:), win(:)
       :
       allocate(x(numel),stat=istat)
       if( istat /= 0 ) then
           ! take appropriate action
       endif
       :
       etc for y, z, sized, win

Then at the end of your code deallocate them.

Another option to consider is this F95/MATLAB interface package which allows you to point directly at MATLAB variable memory, often avoiding the need to copy memory:

http://www.mathworks.com/matlabcentral/fileexchange/25934-fortran-95-interface-to-matlab-api-with-extras

James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us