Memory mapping and Fortran

In my Fortran program I’d like to perform a direct memory mapping to an array on disk using the Linux “mmap” function. Anyone have any examples I could use as a starter?


From Engineering

Here is a sample that makes use of mmap from FORTRAN.

Here is an example of an interface block that could be used for the
mmap function.

use iso_c_binding


type(c_ptr) function mmap(addr,len,prot,flags,fildes,off) bind(c,name=‘mmap’)
use iso_c_binding
integer(c_int), value :: addr
integer(c_size_t), value :: len
integer(c_int), value :: prot
integer(c_int), value :: flags
integer(c_int), value :: fildes
integer(c_size_t), value :: off
end function mmap

end interface

Here are the variables used to make the call to mmap.

type(c_ptr) :: cptr
integer(c_size_t) :: len, off
integer,parameter :: PROT_READ=1
integer,parameter :: MAP_PRIVATE=2
integer :: fd
real*4, pointer :: x(:)

Here is the actual call to mmap. This call will return an address
in memory that points to the first location(off=0) in the file associated
with the file descriptor, fd. The size of this memory buffer is 4096
bytes. The function c_f_pointer must be called to convert the “cptr”
returned by mmap, to a fortran pointer.

len = 4096
off = 0
cptr = mmap(0,len,PROT_READ,MAP_PRIVATE,fd,off)

call c_f_pointer(cptr,x,[len])

do i = 1, 1024
print *, i, x(i)