Reputation: 60133
I am modernizing a simple MPI-IO subroutine from https://github.com/LadaF/PoisFFT/blob/master/src/testmpi.f90 (save_vtk()
at the very end) somewhat in the spirit of MPI I/O, mix of single- and multiple-process output. My intention is to use this process in a more important program.
I arrived to this code, which produces the correct output (error checking removed):
This is done only one and is insignificant for the performance:
if (master) then
header = ... !the header from the link above in a long string
end if
call MPI_Bcast(header_len, storage_size(header_len)/8, &
MPI_BYTE, 0, glob_comm, ie)
call MPI_Type_create_subarray(3, int(ng), int(nxyz), int(off), &
MPI_ORDER_FORTRAN, MPI_RP, filetype, ie)
call MPI_type_commit(filetype, ie)
This is done many times:
call MPI_File_open(glob_comm,"out.vtk", &
IOR(IOR(MPI_MODE_CREATE, MPI_MODE_WRONLY), &
MPI_MODE_EXCL), MPI_INFO_NULL, fh, ie)
if (ie/=0) then !should not happen in serious computations
call MPI_Barrier(glob_comm, ie)
if (master) call MPI_File_delete("out.vtk",MPI_INFO_NULL, ie)
call MPI_Barrier(glob_comm, ie)
call MPI_File_open(glob_comm,"out.vtk", &
IOR(MPI_MODE_CREATE, MPI_MODE_WRONLY),&
MPI_INFO_NULL, fh, ie)
end if
call MPI_File_set_view(fh, 0_MPI_OFFSET_KIND, &
MPI_CHARACTER, MPI_CHARACTER, "native", &
MPI_INFO_NULL, ie)
if (master) then
call MPI_File_write(fh, header, header_len, &
MPI_CHARACTER, MPI_STATUS_IGNORE, ie)
end if
call MPI_Barrier(glob_comm, ie)
call MPI_File_set_view(fh, header_len, MPI_RP, &
filetype, "native", MPI_INFO_NULL, ie)
call MPI_File_write_all(fh, buffer, nx*ny*nz, MPI_RP, &
MPI_STATUS_IGNORE, ie)
call MPI_File_close(fh, ie)
The code first test whether the file already existed and if yes it deletes it and opens again. A full testable code is in a new git branch (https://github.com/LadaF/PoisFFT/blob/new_mpi_io/src/testmpi.f90).
I would like to get rid of the repeated MPI_File_set_view()
or at least of the MPI_Barrier()
in between, so to set the view for the root process for the header and the chunk of the array straight away and then let the root process to write both the header and the array chunk into the view.
Is that possible?
Upvotes: 0
Views: 232
Reputation: 2148
I would have the root process just use fortran streamIO with 'replace' to get rid of the MPI_File_delete and multiple view and barrier calls:
if (master) then
open(file="out.vtk", access='stream', status='replace', newunit=out)
write(out) header
close(out)
endif
call MPI_Barrier(...)
call MPI_File_open(...)
call MPI_File_setview(...)
call MPI_File_write_all(...)
call MPI_File_close(...)
There's still one barrier but I don't see a way around it.
Upvotes: 1