使用 MPI 在单个文件中写入大型矩阵
Writing a large matrix in a single file using MPI
我有一个包含实数的 N 乘 N 大矩阵,已使用 MPI 将其分解为块。我现在正在尝试重组这个矩阵并将其写入一个文件中。
本主题 (writing a matrix into a single txt file with mpi) 涵盖了类似的问题,但我对所有 'integer-to-string' 转换等感到非常困惑(我不是专家!)。我在我的代码中使用 Fortran,但我想即使是 C 解释也应该有所帮助。我一直在阅读关于 MPI-IO 的教程,但还有一些我不明白的地方。这是我一直在研究的代码:
use mpi
implicit none
! matrix dimensions
integer, parameter :: imax = 200
integer, parameter :: jmax = 100
! domain decomposition in each direction
integer, parameter :: iprocs = 3
integer, parameter :: jprocs = 3
! variables
integer :: i, j
integer, dimension(mpi_status_size) :: wstatus
integer :: ierr, proc_num, numprocs, fileno, localarray
integer :: loc_i, loc_j, ppp
integer :: istart, iend, jstart, jend
real, dimension(:,:), allocatable :: x
! initialize MPI
call mpi_init(ierr)
call mpi_comm_size(mpi_comm_world, numprocs, ierr)
call mpi_comm_rank(mpi_comm_world, proc_num, ierr)
! define the beginning and end of blocks
loc_j = proc_num/iprocs
loc_i = proc_num-loc_j*iprocs
ppp = (imax+iprocs-1)/iprocs
istart = loc_i*ppp + 1
iend = min((loc_i+1)*ppp, imax)
ppp = (jmax+jprocs-1)/jprocs
jstart = loc_j*ppp + 1
jend = min((loc_j+1)*ppp, jmax)
! write random data in each block
allocate(x(istart:iend,jstart:jend))
do j = jstart, jend
do i = istart, iend
x(i,j) = real(i + j)
enddo
enddo
! create subarrays
call mpi_type_create_subarray( 2, [imax,jmax], [iend-istart+1,jend-jstart+1], &
[istart,jstart], mpi_order_fortran, mpi_real, localarray, ierr )
call mpi_type_commit( localarray, ierr )
! write to file
call mpi_file_open( mpi_comm_world, 'test.dat', IOR(MPI_mode_create,MPI_mode_wronly), &
mpi_info_null, fileno, ierr )
call mpi_file_set_view( fileno, 0, mpi_real, localarray, "native", mpi_info_null, ierr )
call mpi_file_write_all( fileno, x, (jend-jstart+1)*(iend-istart+1), MPI_real, wstatus, ierr )
call mpi_file_close( fileno, ierr )
! deallocate data
deallocate(x)
! finalize MPI
call mpi_finalize(ierr)
我一直在关注 this tutorial (PDF),但我的编译器抱怨没有针对通用 mpi_file_set_view
的特定子例程。我做错什么了吗?剩下的代码可以吗?
非常感谢您的帮助!!
约阿希姆
我想说最简单的方法是使用旨在有效执行此类操作的库:http://2decomp.org/mpiio.html
您还可以查看它们的源代码(文件 io.f90 和 io_write_one.f90)。
在源代码中,您会看到对 MPI_FILE_SET_SIZE 的调用,这可能与您的案例相关。
编辑:考虑使用 "call MPI_File_Set_View(fhandle, 0_MPI_OFFSET_KIND,..."。来自 MPI-IO: MPI_File_Set_View vs. MPI_File_Seek
的回答
我有一个包含实数的 N 乘 N 大矩阵,已使用 MPI 将其分解为块。我现在正在尝试重组这个矩阵并将其写入一个文件中。
本主题 (writing a matrix into a single txt file with mpi) 涵盖了类似的问题,但我对所有 'integer-to-string' 转换等感到非常困惑(我不是专家!)。我在我的代码中使用 Fortran,但我想即使是 C 解释也应该有所帮助。我一直在阅读关于 MPI-IO 的教程,但还有一些我不明白的地方。这是我一直在研究的代码:
use mpi
implicit none
! matrix dimensions
integer, parameter :: imax = 200
integer, parameter :: jmax = 100
! domain decomposition in each direction
integer, parameter :: iprocs = 3
integer, parameter :: jprocs = 3
! variables
integer :: i, j
integer, dimension(mpi_status_size) :: wstatus
integer :: ierr, proc_num, numprocs, fileno, localarray
integer :: loc_i, loc_j, ppp
integer :: istart, iend, jstart, jend
real, dimension(:,:), allocatable :: x
! initialize MPI
call mpi_init(ierr)
call mpi_comm_size(mpi_comm_world, numprocs, ierr)
call mpi_comm_rank(mpi_comm_world, proc_num, ierr)
! define the beginning and end of blocks
loc_j = proc_num/iprocs
loc_i = proc_num-loc_j*iprocs
ppp = (imax+iprocs-1)/iprocs
istart = loc_i*ppp + 1
iend = min((loc_i+1)*ppp, imax)
ppp = (jmax+jprocs-1)/jprocs
jstart = loc_j*ppp + 1
jend = min((loc_j+1)*ppp, jmax)
! write random data in each block
allocate(x(istart:iend,jstart:jend))
do j = jstart, jend
do i = istart, iend
x(i,j) = real(i + j)
enddo
enddo
! create subarrays
call mpi_type_create_subarray( 2, [imax,jmax], [iend-istart+1,jend-jstart+1], &
[istart,jstart], mpi_order_fortran, mpi_real, localarray, ierr )
call mpi_type_commit( localarray, ierr )
! write to file
call mpi_file_open( mpi_comm_world, 'test.dat', IOR(MPI_mode_create,MPI_mode_wronly), &
mpi_info_null, fileno, ierr )
call mpi_file_set_view( fileno, 0, mpi_real, localarray, "native", mpi_info_null, ierr )
call mpi_file_write_all( fileno, x, (jend-jstart+1)*(iend-istart+1), MPI_real, wstatus, ierr )
call mpi_file_close( fileno, ierr )
! deallocate data
deallocate(x)
! finalize MPI
call mpi_finalize(ierr)
我一直在关注 this tutorial (PDF),但我的编译器抱怨没有针对通用 mpi_file_set_view
的特定子例程。我做错什么了吗?剩下的代码可以吗?
非常感谢您的帮助!!
约阿希姆
我想说最简单的方法是使用旨在有效执行此类操作的库:http://2decomp.org/mpiio.html
您还可以查看它们的源代码(文件 io.f90 和 io_write_one.f90)。
在源代码中,您会看到对 MPI_FILE_SET_SIZE 的调用,这可能与您的案例相关。
编辑:考虑使用 "call MPI_File_Set_View(fhandle, 0_MPI_OFFSET_KIND,..."。来自 MPI-IO: MPI_File_Set_View vs. MPI_File_Seek
的回答