malloc on Opteron?

I’m running into trouble with malloc on the Opteron. I understand size limitations, but this seems a little ridiculous - I’ve been unable to malloc an array larger than 10,000 words? Any ideas on what I’m missing? I’m using the default compilation flags (i.e., none). I’m running on a dual Opteron 250 box w/ 4 GB of RAM (linux kernel 2.4.21-15)

Thanks

For example the code snippet below works fine with iasize= 10000, seg faults with iasize=100000

program mymall

pointer (iaa,b(:))

iasize = 100000
iaa = malloc(4*iasize)

do 10 i=1,iasize
b(i)=0.0
10 continue

write(,)‘success’
stop
end

Hello,

This is a known problem but unfortunately there isn’t a good solution.

The problem stems from the fact that the Fortran malloc function is not an intrinsic function and thus is implicity defined to be “INTEGER4 malloc". Therefore, you must remember to explicitly declare "INTEGER8 malloc” or compile with “-i8” to be consistant with the size of the returned 64-bit pointer.

The second and more difficult problem is that the argument for malloc is INTEGER4 and is inconsistent with C’s 64-bit malloc. Ideally, we could simply port the Fortran malloc to 64-bits by changing the argument data type to INTEGER8. However, since integers are still implicity declared as INTEGER4, doing so would require all users to make sure the actual argument’s data type is INTEGER8. Since most users would probablly be unaware of this change, doing so would end up breaking a lot of code.

A second possible solution is create the INTEGER8 version of malloc and call it different name. Of course, the user would still need to explictiy declare it and make sure only INTEGER8 arguments are used, but we could then presume that the user understood these requirements.

While we don’t like to dictate how users code, the true solution to this problem is to not use malloc. Rather you should consider using Fortran 90’s allocatable arrays. Below, I’ve modified your code to use an allocatable array.

  program mymall

!   pointer (iaa,b(:))
   pointer :: b(:)
   integer*8 iasize

   iasize = 1000000

!   iaa = malloc(4*iasize)
   allocate(b(4*iasize))

  do i=1,iasize
     b(i)=0.0
  end do
  write(*,*)'success'
  stop
  end

Thanks,
Mat

Hi Mat,
Thanks for the tip - it works on my code snippet, now I just have to apply to change to 250,000 lines of legacy Fortran code. Your help was very much appreciated.