diff --git a/src/Utils/fortran_mmap.c b/src/Utils/fortran_mmap.c index eee8337e..41ad93ec 100644 --- a/src/Utils/fortran_mmap.c +++ b/src/Utils/fortran_mmap.c @@ -70,3 +70,12 @@ void munmap_fortran(size_t bytes, int fd, void* map) } close(fd); } + + +void msync_fortran(size_t bytes, int fd, void* map) +{ + if (msync(map, bytes, MS_SYNC) == -1) { + perror("Error syncing the mmap file"); + } +} + diff --git a/src/Utils/map_functions.irp.f b/src/Utils/map_functions.irp.f index 54797679..de7f66d7 100644 --- a/src/Utils/map_functions.irp.f +++ b/src/Utils/map_functions.irp.f @@ -52,18 +52,14 @@ subroutine map_save_to_disk(filename,map) map % consolidated_idx (map % map_size + 2_8) = k map % consolidated = .True. + integer*8 :: n_elements + n_elements = int(map % n_elements,8) - call munmap( (/ map % map_size + 2_8 /), 8, fd(1), c_pointer(1)) - call mmap(trim(filename)//'_consolidated_idx', (/ map % map_size + 2_8 /), 8, fd(1), .True., c_pointer(1)) - call c_f_pointer(c_pointer(1),map % consolidated_idx, (/ map % map_size +2_8/)) - - call munmap( (/ map % n_elements /), cache_key_kind, fd(2), c_pointer(2)) - call mmap(trim(filename)//'_consolidated_key', (/ map % n_elements /), cache_key_kind, fd(2), .True., c_pointer(2)) - call c_f_pointer(c_pointer(2),map % consolidated_key, (/ map % n_elements /)) - - call munmap( (/ map % n_elements /), integral_kind, fd(3), c_pointer(3)) - call mmap(trim(filename)//'_consolidated_value', (/ map % n_elements /), integral_kind, fd(3), .True., c_pointer(3)) - call c_f_pointer(c_pointer(3),map % consolidated_value, (/ map % n_elements /)) + print *, 'Writing data to disk...' + call msync ( (/ map % map_size + 2_8 /), 8, fd(1), c_pointer(1)) + call msync ( (/ n_elements /), cache_key_kind, fd(2), c_pointer(2)) + call msync ( (/ n_elements /), integral_kind , fd(3), c_pointer(3)) + print *, 'Done' end @@ -79,8 +75,6 @@ subroutine map_load_from_disk(filename,map) integer*8 :: i,k,l integer*4 :: j,n_elements - - if (map % consolidated) then stop 'map already consolidated' endif diff --git a/src/Utils/mmap.f90 b/src/Utils/mmap.f90 index 58def0ae..5a833881 100644 --- a/src/Utils/mmap.f90 +++ b/src/Utils/mmap.f90 @@ -15,7 +15,14 @@ module mmap_module integer(c_int), intent(in), value :: read_only end function - subroutine c_munmap(length, fd, map) bind(c,name='munmap_fortran') + subroutine c_munmap_fortran(length, fd, map) bind(c,name='munmap_fortran') + use iso_c_binding + integer(c_size_t), intent(in), value :: length + integer(c_int), intent(in), value :: fd + type(c_ptr), intent(in), value :: map + end subroutine + + subroutine c_msync_fortran(length, fd, map) bind(c,name='msync_fortran') use iso_c_binding integer(c_size_t), intent(in), value :: length integer(c_int), intent(in), value :: fd @@ -61,7 +68,23 @@ module mmap_module length = PRODUCT( shape(:) ) * bytes fd_ = fd - call c_munmap( length, fd_, map) + call c_munmap_fortran( length, fd_, map) + end subroutine + + subroutine msync(shape, bytes, fd, map) + use iso_c_binding + implicit none + integer*8, intent(in) :: shape(:) ! Shape of the array to map + integer, intent(in) :: bytes ! Number of bytes per element + integer, intent(in) :: fd ! File descriptor + type(c_ptr), intent(in) :: map ! C pointer + + integer(c_size_t) :: length + integer(c_int) :: fd_ + + length = PRODUCT( shape(:) ) * bytes + fd_ = fd + call c_msync_fortran( length, fd_, map) end subroutine end module mmap_module