使用Fortran和连续数据类型在MPI中逐列分散矩阵

时间:2017-07-18 12:31:44

标签: parallel-processing fortran mpi

我试图生成一个矩阵为(mysize)x(Np)的随机数矩阵,其中Np是给定的数字(比如说5)。 Mysize来自进程数量。然后每个处理器应采用自己的大小为1xNp的列。

我的第一个想法是创建一个矢量数据类型,但后来我意识到这可以通过使用连续的数据类型来完成。代码运行时没有错误,但它只打印了应该存在的一些数字。我非常确定问题出在指标上(type_columnMPI_REAL等),它们应该如何实现,但我无法解决这个问题。有人在乎帮忙吗?

代码:

program main

    use mpi
    integer :: ierr,myrank,mysize
    integer ::  Np=5, type_column
    integer*8, external :: seedgen
    real*8, dimension(:,:), allocatable :: x_init
    real*8, dimension(:), allocatable :: block_x
    character(4) :: rank
    integer, dimension(1) :: new_seed

    call MPI_INIT(ierr)
    call MPI_COMM_RANK(MPI_COMM_WORLD,myrank,ierr)
    call MPI_COMM_SIZE(MPI_COMM_WORLD,mysize,ierr)

    allocate(block_x(0:Np-1))

    if (myrank==0) then
        allocate(x_init(0:Np-1,0:mysize-1))
        new_seed = seedgen(myrank)
        write(*,*) new_seed !This is to check if the seed changes each time
        call random_seed(put=new_seed)
        call random_number(x_init)
        open(unit=1111, file='./x_tot.txt')
        write(1111,*) x_init
        close(1111) !Up to this point, everything works
    end if

    call MPI_TYPE_CONTIGUOUS(Np,MPI_REAL,type_column,ierr)
    call MPI_TYPE_COMMIT(type_column,ierr)
    call MPI_SCATTER(x_init(:,myrank),Np,MPI_REAL,block_x,Np,type_column,0,MPI_COMM_WORLD,ierr)     

    write(rank,'(I4)') myrank
    write(*,*) 'I, process ',myrank,' received ', block_x(0:Np-1)
    open(unit=myrank, file='./x_teste_'//trim(adjustl(rank))//'.txt')
    write(myrank,*) block_x
    close(myrank)

    call MPI_TYPE_FREE(type_column,ierr)

    call MPI_FINALIZE(ierr)

end program main

!!! You may ignore what is down below, just a function to generate random seeds.

function seedgen(myrank)
    use iso_fortran_env
    implicit none
    integer(kind=int64) :: seedgen
    integer, intent(IN) :: myrank
    integer :: s

    call system_clock(s)
    seedgen = abs( mod((s*181)*((myrank-83)*359), 104729) )
end function seedgen

输出(运行3个处理器,因此mysize = 3)

 I, process            0  received   0.247272870046176     
 0.386432141459887       8.816221414742263E-316  0.000000000000000E+000
 0.000000000000000E+000
 I, process            1  received   2.737792569698344E-008
 I, process            2  received   5.231628354959370E-002
 0.454932876242927       3.330771999738651E-315  0.000000000000000E+000
 0.000000000000000E+000
 3.352416881721526E+270  5.298272496856962E-315  0.000000000000000E+000
 0.000000000000000E+000

矩阵x_tot:

 0.247272870046176       0.386432141459887       0.473788032900533
 0.239586263133600       0.851724848335892       5.231628354959370E-002
 0.454932876242927       0.702720716936168       0.559915585253771
 0.605745282251549       0.253298270763062       0.809867899324171
 0.590174190311136       0.125210425650182       8.138975171285148E-002

所以你可以看到一些数字是好的,剩下的就是垃圾。

1 个答案:

答案 0 :(得分:0)

如上所述使用MPI_DOUBLE并更改下面的书写工作正常。再次感谢

open(unit=myrank, file='./x_teste_'//trim(adjustl(rank))//'.txt')
    do i=0,Np-1
        write(myrank,*) block_x(i)
    end do
close(myrank)