0

When I try to construct a simple array and write to a output file using MPIIO, the output file is very large and not correct. The simple array 'buff' is 2 * 3 and every element equals to 1. I first create the subarray filetype which is 1 * 1, and then create a new output file 'out.dat'. I use set_view function and write_all to write this buff array. I compile this and run it use 6 cores so that one core write one element. I expect that I can get a 'out.dat' and a 2*3 array with value 1.

I successfully compile and run this code, and the results are all 0 and the size is very large.

program test
use mpi
implicit none
integer::rank,nproc,ierr,buffsize,status(MPI_STATUS_SIZE),intsize,i,j,filetype,cart_comm
integer::fh
integer(kind=mpi_offset_kind):: offset=0
integer::buff(2,3)
character:: filename*50
integer::sizes(2)
integer::gsize(2)
integer::start(2)
integer::subsize(2)
integer::coords(2)
integer:: nprocs_cart(2)=(/2,3/)
logical::periods(2)
intsize=sizeof(rank)
call MPI_init(ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
CALL MPI_Dims_create(nproc, 2, nprocs_cart, ierr)
CALL MPI_Cart_create(MPI_COMM_WORLD, 2, nprocs_cart, periods, .TRUE., &
cart_comm, ierr)
CALL MPI_Comm_rank(cart_comm, rank, ierr)
CALL MPI_Cart_coords(cart_comm, rank, 2, coords, ierr)


start=coords
gsize=(/2,3/)
subsize=(/1,1/)
buff=1


call MPI_TYPE_CREATE_SUBARRAY(2,gsize,subsize,start,MPI_ORDER_FORTRAN,&
MPI_int,filetype,ierr)
call MPI_TYPE_COMMIT(filetype,ierr)

call MPI_File_open(MPI_COMM_WORLD,'out.dat',&
        MPI_MODE_WRONLY + MPI_MODE_CREATE, MPI_INFO_NULL, fh,ierr)


        call MPI_File_set_view(fh,0,MPI_int,filetype,&
        "native",MPI_INFO_NULL,ierr)
  
        CALL MPI_FILE_WRITE_all(fh, buff,6, filetype, MPI_STATUS_ignore, ierr)


call MPI_File_close(fh,ierr)
call MPI_FINALIZE(ierr)

end program test

  • When asking a question, please describe clearly what the issue is. In this case, the best way is to add a self test to this program to show the discrepancy between the expected and actual result. – Gilles Gouaillardet Jul 08 '21 at 03:13
  • what if you write `1` element instead of `6`? – Gilles Gouaillardet Jul 08 '21 at 03:36
  • I tried to write_all(..,.., 6,) and write_all(..,.., 1,) neighter works – Mac cchiatooo Jul 08 '21 at 03:41
  • 1
    I need to replace `0` with `offset` in `MPI_File_set_view()` in order to make it compile, and then write `1` element to make it work. Do not forget to remove `out.dat` before `mpiexec` since `MPI_File_open()` won't truncate it. – Gilles Gouaillardet Jul 08 '21 at 05:09
  • 1
    To expand on @GillesGouaillardet comment slightly you (almost certainly) have the kind of the second argument to MPI_File_set_view wrong. It should be of kind MPI_OFFSET_KIND - see e.g. https://www.open-mpi.org/doc/v4.0/man3/MPI_File_set_view.3.php – Ian Bush Jul 08 '21 at 06:58
  • 1
    Also note MPI_int is not part of the standard MPI binding for Fortran. That is for C. It should be MPI_Integer – Ian Bush Jul 08 '21 at 06:59
  • Thank you so much, and I think the MPI_INTEGER is right command for FORTRAN. Thank you! – Mac cchiatooo Jul 08 '21 at 14:03

0 Answers0