2016-07-01 37 views
0

我想使用MPI_SEND()和MPI_RECV()一个孩子,其父进程,通过使用MPI_Comm_spawn创造之间的沟通创造了父子进程之间MPI_RECV如下面可以看到:使用MPI_SEND与MPI_Comm_spawn

Parent.f90

program master 
use mpi 
implicit none 

    integer :: ierr, num_procs, my_id, intercomm, i, array(10), tag 

    CALL MPI_INIT(ierr) 

    CALL MPI_COMM_RANK(MPI_COMM_WORLD, my_id, ierr) 
    CALL MPI_COMM_SIZE(MPI_COMM_WORLD, num_procs, ierr) 

    if (.not. (ierr .eq. 0)) then 
     print*, "S.Unable to initilaize!" 
     stop 
    endif 

    if (my_id .eq. 0) then 
     call MPI_Comm_spawn("./child.out", MPI_ARGV_NULL, 1, MPI_INFO_NULL, my_id, & 
     & MPI_COMM_WORLD, intercomm, MPI_ERRCODES_IGNORE, ierr) 

     call MPI_Send(array, 255, MPI_INTEGER, my_id, tag, intercomm, ierr) 
    endif 

    call MPI_Finalize(ierr) 

end program master 

Child.f90

program name 
use mpi 
implicit none 

    ! type declaration statements 
    integer :: ierr, parent, my_id, n_procs, i, array(10), tag, intercomm 
    logical :: flag, high 

    ! executable statements 
    call MPI_Init(ierr) 
    call MPI_Initialized(flag, ierr) 
    call MPI_Comm_get_parent(parent, ierr) 
    call MPI_Comm_rank(MPI_COMM_WORLD, my_id, ierr) 
    call MPI_Comm_size(MPI_COMM_WORLD, n_procs, ierr) 

    print *, "Initilaized? ", flag 
    print *, "My mommy is: ", parent 
    print *, "My rank is:", my_id 

    tag = 1 

    call MPI_Recv(array, 255, MPI_INTEGER, MPI_ANY_SOURCE, tag, parent, MPI_STATUS_IGNORE, ierr) 
    print *, "Client received array." 

    call MPI_Finalize(ierr) 
end program name 

当上面的程序运行时,家长似乎经过精细跑,但孩子从来首席ts:“客户端收到数组”,导致我相信我已经搞砸了send/recv。

如果不清楚我想实现的目标,我希望父项产生子项,向该子项发送一个数组,子项用于处理数组,子项将数组发送回父项。 (斜体字还没写,我想这基本通信第一个工作日)

此刻,当我运行:mpiexec -np 1 parent.out,孩子打印:

Initilaized? T 
My mommy is:   3 
My rank is:   0 

,而不是“客户端接收阵列。 “

回答

0

我能解决我的问题。以下代码启动一个父项,向其子项发送一个大小为1000000的数组,该子项将该数组平方并将其发送回其父项。

Parent.f90

program master 
use mpi 
implicit none 

    integer :: ierr, num_procs, my_id, intercomm, i, array(1000000), s_tag, s_dest, siffra 

    CALL MPI_INIT(ierr) 

    CALL MPI_COMM_RANK(MPI_COMM_WORLD, my_id, ierr) 
    CALL MPI_COMM_SIZE(MPI_COMM_WORLD, num_procs, ierr) 

    !print *, "S.Rank =", my_id 
    !print *, "S.Size =", num_procs 

    if (.not. (ierr .eq. 0)) then 
     print*, "S.Unable to initilaize bös!" 
     stop 
    endif 

    do i=1,size(array) 
     array(i) = 2 
    enddo 

    if (my_id .eq. 0) then 
     call MPI_Comm_spawn("./client2.out", MPI_ARGV_NULL, 1, MPI_INFO_NULL, my_id, & 
     & MPI_COMM_WORLD, intercomm, MPI_ERRCODES_IGNORE, ierr) 


     s_dest = 0 !rank of destination (integer) 
     s_tag = 1 !message tag (integer) 
     call MPI_Send(array(1), 1000000, MPI_INTEGER, s_dest, s_tag, intercomm, ierr) 

     call MPI_Recv(array(1), 1000000, MPI_INTEGER, s_dest, s_tag, intercomm, MPI_STATUS_IGNORE, ierr) 

     !do i=1,10 
     ! print *, "S.Array(",i,"): ", array(i) 
     !enddo 

    endif 

    call MPI_Finalize(ierr) 

end program master 

Child.f90

program name 
use mpi 
implicit none 

    ! type declaration statements 
    integer :: ierr, parent, my_id, n_procs, i, array(1000000), ctag, csource, intercomm, siffra 
    logical :: flag 

    ! executable statements 
    call MPI_Init(ierr) 
    call MPI_Initialized(flag, ierr) 
    call MPI_Comm_get_parent(parent, ierr) 
    call MPI_Comm_rank(MPI_COMM_WORLD, my_id, ierr) 
    call MPI_Comm_size(MPI_COMM_WORLD, n_procs, ierr) 

    csource = 0 !rank of source 
    ctag = 1 !message tag 

    call MPI_Recv(array(1), 1000000, MPI_INTEGER, csource, ctag, parent, MPI_STATUS_IGNORE, ierr) 

    !do i=1,10 
    ! print *, "C.Array(",i,"): ", array(i) 
    !enddo 

    do i=1,size(array) 
     array(i) = array(i)**2 
    enddo 

    !do i=1,10 
    ! print *, "C.Array(",i,"): ", array(i) 
    !enddo 

    call MPI_Send(array(1), 1000000, MPI_INTEGER, csource, ctag, parent, ierr) 

    call MPI_Finalize(ierr) 
end program name