【问题标题】:MPI_Exscan- How to use for parallel File writeMPI_Exscan-如何用于并行文件写入
【发布时间】:2012-11-04 08:23:35
【问题描述】:

我正在为并行 IO 编写一些 MPI 代码。 假设我在文件中写入了一个数组 [2 ,5,6,9,0,4,3,1,8,7 ],并且我有 2 个进程。

我定义文件视图来读取这个文件的一部分。 进程 0 看到:2 ,5,6,9,0(5 个元素) 过程 1 看到:4,3,1,8,7(5 个元素) 两个进程都调用一个排序函数。作为排序功能的结果, 进程 0 有:0,1,2,3(4 个元素) 进程 1 有:4,5,6,7,8,9(6 个元素)

现在我需要将此输出写入一个新的输出文件。 进程 0 在偏移量 0 处写入时会正确写入。但是进程 1 怎么知道写入文件的偏移量?我知道我需要定义一个要写入的文件视图,但是新的位移是什么。我有点认为 MPI_Exscan 可以做到。但我不确定如何...有人可以帮忙吗??

提前致谢

【问题讨论】:

    标签: mpi


    【解决方案1】:

    当然;您在本地元素数量上使用 MPI_ExscanMPI_SUM 以获得您“左侧”的总数,并在您的视图中使用它(作为偏移量,或在您创建的类型中定义您的视图)。

    这是一个小的 Fortran 程序,其中每个等级生成一个“随机”(嗯,2*rank+1)大小的字符数组('0' 表示等级 0,等等),使用 MPI_Exscan 找出它在写入时应该使用的偏移量,然后写道:

    program testexscan
        use mpi
        implicit none
    
        integer :: nelements, nleft, total
        character, allocatable, dimension(:) :: array
    
        integer :: rank, nprocs, ierr, fh
        integer(kind=MPI_OFFSET_KIND) :: disp
    
        call MPI_Init(ierr)
    
        call MPI_Comm_rank(MPI_COMM_WORLD, rank,  ierr)
        call MPI_Comm_rank(MPI_COMM_WORLD, nprocs, ierr)
    
        call generatedata(rank, array)
    
        nelements = size(array)
        call  MPI_Exscan   (nelements, nleft,  1, MPI_INTEGER, MPI_SUM, MPI_COMM_WORLD, ierr)
    
        print *, 'rank = ', rank, ' nelements = ', nelements, ' nleft= ', nleft
    
        call MPI_File_open(MPI_COMM_WORLD, 'output.txt', ior(MPI_MODE_WRONLY,MPI_MODE_CREATE),  &
                           MPI_INFO_NULL, fh, ierr)
    
        disp = nleft * 1    ! offset in bytes
        call MPI_File_set_view(fh, disp, MPI_CHARACTER, MPI_CHARACTER, "native", MPI_INFO_NULL, ierr )
        call MPI_File_write_all(fh, array, nelements, MPI_CHARACTER, MPI_STATUS_IGNORE, ierr)
        call MPI_File_close(fh, ierr)
    
        deallocate(array)
    
        call MPI_Finalize(ierr)
    
        contains
    
            subroutine generatedata(rank, arr)
            character, dimension(:), allocatable, intent(inout) :: arr
            integer, intent(in) :: rank
    
            nelements = rank * 2 + 1
    
            allocate(array(nelements))
            array = char(ichar("0")+rank)
    
            end subroutine generatedata
    
    end program testexscan
    

    运行此命令:

    $ mpif90 -o testexscan testexscan.f90 
    $ mpirun -np 4 ./testexscan
     rank =            0  nelements =            1  nleft=            0
     rank =            1  nelements =            3  nleft=            1
     rank =            2  nelements =            5  nleft=            4
     rank =            3  nelements =            7  nleft=            9
    $ cat output.txt 
    0111222223333333 
    

    【讨论】:

      【解决方案2】:

      您的描述有点模糊,但无论如何,进程 1 可以只向进程 2 发送一条消息,其中包含要使用的偏移量。

      【讨论】:

      • 我不希望进程发送/接收任何消息..我想使用 MPI_Exscan...
      猜你喜欢
      • 1970-01-01
      • 2019-09-17
      • 2020-02-24
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      相关资源
      最近更新 更多