Reputation: 163
I hope someone can help me. I am coding in Fortran 90 with MPI libraries. I am trying to read files in parallel (with function Mpi_File_Read) but am unable to. Here is a simple code which demonstrates the problem:
program Read_And_Write
implicit none
include "mpif.h"
integer, parameter :: N = 16
integer :: i, this_proc, file_handle, error
integer :: read_data(N)
integer :: status(MPI_STATUS_SIZE)
character(len=13) :: file_name = 'one_array.dat'
! Start MPI run
call Mpi_Init(error)
call Mpi_Comm_Rank(MPI_COMM_WORLD, & ! integer comm
this_proc, & ! integer rank
error) ! integer error
! Create a file from one processor, hence "sequentially"
if(this_proc .eq. 0) then
open(9, file=file_name, form='unformatted')
do i = 1, N
write(9) i*N
end do
close(9)
end if
! Open the same file in MPI mode
call Mpi_File_Open(MPI_COMM_WORLD, & ! integer comm
file_name, & ! character filename(*)
MPI_MODE_RDONLY, & ! integer amode
MPI_INFO_NULL, & ! integer info
file_handle, & ! integer file handle
error) ! integer error
! Read the file in MPI mode
call Mpi_File_Read_All(file_handle, & ! integer file handle
read_data, & ! buffer
N, & ! integer count
MPI_INTEGER, & ! integer datatype
status, & ! integer status
error) ! integer error
! Write out what you got
do i = 1, N
print *, 'read: ', read_data(i)
end do
! End MPI run
call Mpi_Finalize(error)
end program
Any hints or ideas what am I doing wrong here?
I am using gfortran 5.4.0 with MPICH 3.2 on Xubuntu 16.04.09
Thanks
Upvotes: 0
Views: 593
Reputation: 59999
Fortran unformatted sequential files are not compatible with MPI I/O. MPI I/O files are compatible with stream access files which work similarly as C language I/O.
Just change
open(9, file=file_name, form='unformatted')
to
open(9, file=file_name, access="stream", form='unformatted')
And the result will be:
read: 16
read: 32
read: 48
read: 64
read: 80
read: 96
read: 112
read: 128
read: 144
read: 160
read: 176
read: 192
read: 208
read: 224
read: 240
read: 256
Sequential Fortran files use additional record markers which MPI I/O read does not expect (e.g., Fortran unformatted file format). You would have to make a compiler specific MPI datatype to avoid the record markers. Not worth doing it if you don't have to.
Upvotes: 2