【问题标题】:Writing a large matrix in a single file using MPI使用 MPI 在单个文件中写入大矩阵
【发布时间】:2015-01-13 00:55:08
【问题描述】:

我有一个包含实数的大型 N × N 矩阵,它已使用 MPI 分解为块。我现在正在尝试重构这个矩阵并将其写入一个文件中。

这个话题 (writing a matrix into a single txt file with mpi) 涵盖了一个类似的问题,但我对所有“整数到字符串”的转换等感到很困惑(我不是专家!)。我正在为我的代码使用 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 没有特定的子例程。我做错什么了吗?剩下的代码还可以吗?

非常感谢您的帮助!!

约阿希姆

【问题讨论】:

  • 您使用的是哪种 MPI 实现?是否有可能在未启用 MPI I/O 支持的情况下进行编译(如果您自己编译,这是 MPICH 和 Open MPI 中的一个选项)。
  • 有趣的是,编译器抱怨 mpi_file_set_view。如果以某种方式未启用 mpi-io 支持,它也会抱怨 MPI_FILE_OPEN、MPI_FILE_CLOSE 和 MPI_FILE_WRITE_ALL,对吧?

标签: view set fortran mpi


【解决方案1】:

我想说最简单的方法是使用旨在高效执行此类操作的库: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的回答

【讨论】:

  • 非常感谢大家的回答!你是对的,有一个问题,因为偏移量是一个整数而不是一个整数(kind=mpi_offset_kind)!我修改了一些东西,现在代码运行良好。
猜你喜欢
  • 2012-04-04
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
  • 2012-05-07
  • 1970-01-01
  • 2019-07-09
相关资源
最近更新 更多