首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPI: MPI_File_Set_View与MPI_File_Seek

MPI: MPI_File_Set_View与MPI_File_Seek
EN

Stack Overflow用户
提问于 2013-08-27 09:45:30
回答 1查看 1.4K关注 0票数 1

在Fortran 90中使用MPI编写文件时,我遇到了一个问题。如果我执行以下操作,使用MPI_File_Set_View

代码语言:javascript
复制
program test
  implicit none

  include "mpif.h"

  integer :: myrank, nproc, fhandle, ierr
  integer :: xpos, ypos
  integer, parameter :: loc_x=10, loc_y=10
  integer :: loc_dim
  integer :: nx=2, ny=2
  real(8), dimension(loc_x, loc_y) :: data, data_read
  integer :: written_arr
  integer, dimension(2) :: wa_size, wa_subsize, wa_start
  integer :: int_size, double_size
  integer(kind=MPI_OFFSET_KIND) :: offset

  call MPI_Init(ierr)
  call MPI_Comm_Rank(MPI_COMM_WORLD, myrank, ierr)
  call MPI_Comm_Size(MPI_COMM_WORLD, nproc, ierr)

  xpos = mod(myrank, nx)
  ypos = mod(myrank/nx, ny)

  data = myrank

  loc_dim    = loc_x*loc_y

  ! Write using MPI_File_Set_View
  wa_size    = (/ nx*loc_x, ny*loc_y /)
  wa_subsize = (/ loc_x, loc_y /)
  wa_start   = (/ xpos, ypos /)*wa_subsize
  call MPI_Type_Create_Subarray(2, wa_size, wa_subsize, wa_start &
       , MPI_ORDER_FORTRAN, MPI_DOUBLE_PRECISION, written_arr, ierr)
  call MPI_Type_Commit(written_arr, ierr)

  call MPI_Type_Size(MPI_INTEGER, int_size, ierr)
  call MPI_Type_Size(MPI_DOUBLE_PRECISION, double_size, ierr)

  call MPI_File_Open(MPI_COMM_WORLD, "file_set_view.dat" &
       , MPI_MODE_WRONLY + MPI_MODE_CREATE, MPI_INFO_NULL, fhandle, ierr)
  call MPI_File_Set_View(fhandle, 0, MPI_DOUBLE_PRECISION, written_arr &
       , "native", MPI_INFO_NULL, ierr)
  call MPI_File_Write_All(fhandle, data, loc_dim, MPI_DOUBLE_PRECISION &
       , MPI_STATUS_IGNORE, ierr)
  call MPI_File_Close(fhandle, ierr)

  call MPI_Finalize(ierr)

end program test

我得到了一个69 it文件,考虑到我在其中写的内容,这个文件太大了。顺便说一句,如果增加loc_xloc_y,文件的大小不会改变。

但是,如果我使用MPI_File_Seek,它的工作效率要好得多;创建了一个大小合理的文件,其中包含了我想要写入的数据。

代码语言:javascript
复制
program test
  implicit none

  include "mpif.h"

  integer :: myrank, nproc, fhandle, ierr
  integer :: xpos, ypos
  integer, parameter :: loc_x=10, loc_y=10
  integer :: loc_dim
  integer :: nx=2, ny=2
  real(8), dimension(loc_x, loc_y) :: data, data_read
  integer :: written_arr
  integer, dimension(2) :: wa_size, wa_subsize, wa_start
  integer :: int_size, double_size
  integer(kind=MPI_OFFSET_KIND) :: offset

  call MPI_Init(ierr)
  call MPI_Comm_Rank(MPI_COMM_WORLD, myrank, ierr)
  call MPI_Comm_Size(MPI_COMM_WORLD, nproc, ierr)

  xpos = mod(myrank, nx)
  ypos = mod(myrank/nx, ny)

  data = myrank

  loc_dim    = loc_x*loc_y

  ! Write using MPI_File_Seek
  call MPI_File_Open(MPI_COMM_WORLD, "file_seek.dat" &
       , MPI_MODE_WRONLY + MPI_MODE_CREATE, MPI_INFO_NULL, fhandle, ierr)
  offset = loc_x*loc_y*myrank
  print*, 'myrank, offset, data: ', myrank, offset, data(1,:2)
  call MPI_File_Seek(fhandle, offset, MPI_SEEK_SET)
  call MPI_File_Write_All(fhandle, data, loc_dim, MPI_DOUBLE_PRECISION &
       , MPI_STATUS_IGNORE, ierr)
  call MPI_File_Close(fhandle, ierr)

  call MPI_Finalize(ierr)

end program test

在我看来,这两种方法应该产生相同的东西,特别是第一种方法应该创建这么大的文件。

我用gfortran 4.6.3和OpenMPI 1.6.2编译我的代码。

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-27 10:28:14

答案实际上是在格里斯托·伊里耶夫对this question的回答中给出的

MPI_FILE_SET_VIEW调用中的MPI_FILE_SET_VIEW替换为0_MPI_OFFSET_KIND,或声明一个类型为INTEGER(KIND=MPI_OFFSET_KIND)的常量,值为零,然后传递它。 呼叫MPI_File_Set_View(fhandle,0_MPI_OFFSET_KIND,MPI_DOUBLE_PRECISION,. 或 整数(kind=MPI_OFFSET_KIND),参数::zero_off =0.呼叫MPI_File_Set_View(fhandle,zero_off,MPI_DOUBLE_PRECISION,. 这两种方法都会导致大小为3200字节的输出文件(如预期的那样)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18462088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档