This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Question on OpenMP and Fortran memory allocation


This must be a silly question, but I have been fighting over an issue
through the weekend, and could not find a definitive answer on the
matter of GNU Fortran memory allocation thread safety. The question is
very simple: is GNU Fortran's memory allocation routines (ALLOCATE and
DEALLOCATE) guaranteed to be thread-safe with OpenMP? OpenMP 2.5 specs
indicates that these routines must be thread-safe to be compliant with
OpenMP standard.

I have GNU Fortran 4.3.1 compiled on my machine, which has an Intel
Pentium M processor (x86-32 system). I tested a simple code (shown at
the end of this email) and assembled it using command line:

$ gfortran -fopenmp -S alloc.F90 -O2

(The -O2 is not necessary, but it cleans the code.)

It reveals that GNU Fortran uses a simple C malloc() and free()
routines. For example:

.globl __alloc_mod_MOD_alloc_mem
       .type   __alloc_mod_MOD_alloc_mem, @function
__alloc_mod_MOD_alloc_mem:
       pushl   %ebp
       movl    %esp, %ebp
       pushl   %ebx
       subl    $4, %esp
       movl    8(%ebp), %ebx
       movl    (%ebx), %eax
       movl    $537, 8(%ebx)
       movl    $1, 16(%ebx)
       movl    $50, 20(%ebx)
       testl   %eax, %eax
       movl    $1, 12(%ebx)
       jne     .L2
       movl    $400, (%esp)
       call    malloc
       testl   %eax, %eax
       je      .L6
       movl    %eax, (%ebx)
       movl    $-1, 4(%ebx)
       addl    $4, %esp
       popl    %ebx
       popl    %ebp
       ret

...

The C library in my machine is GNU libc version 2.5 . Is glibc's
free() and malloc() thread-safe? I tried to find this information on
the web but could not find it. The only link that seems to indicate
this is:

http://www.linux-kongress.org/1997/gloger.html


Thanks,
Wirawan

------------


module alloc_mod

implicit none

contains

 subroutine alloc_mem(arr)
   real*8, allocatable :: arr(:)
   allocate(arr(50))
 end subroutine

 subroutine dealloc_mem(arr)
   real*8, allocatable :: arr(:)
   deallocate(arr)
 end subroutine

 subroutine print1(v)
   real*8 :: v
   write(*,*) v
 end subroutine

end module

program alloc_test
 use alloc_mod
 implicit none
 real*8, allocatable :: arr1(:)
 integer :: i

!$omp parallel default(none) &
!$omp   & private(arr1, i)
 call alloc_mem(arr1)
!$omp do
 do i = 1, 50
   arr1(i) = i * 251
   call print1(arr1(i))
 enddo
!$omp end do
 call dealloc_mem(arr1)

!$omp end parallel

end program

-- 
Wirawan Purwanto
College of William and Mary
Physics Department
Williamsburg, VA 23187


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]