mpi理解isend和irecv

时间:2015-06-29 14:46:47

标签: mpi

我有2个进程(0和1),0得到一个随机数,如果不是MOD(数字,2)== 0,如果它比前面的那个大,那么它发送(ISEND)到进程1。

过程1接收数字(RECV)并且必须计算它是否是素数。如果它是素数,则过程1将其发送(ISEND)到过程0.过程0将其保存为最高素数(不是最终素数,他生成一定数量的必须检查的随机数)。

因此,同时进程1检查收到的号码是否为素数,过程0已经在另一个随机数上。 关键是进程0可能会覆盖ISEND中的数据,因为进程1是自己的工作而不是接收。我想要这个。

PROGRAM mpi_NonBlocking
USE MPI
IMPLICIT NONE

INTEGER :: ierr, myid ,npe, istatus(MPI_STATUS_SIZE), num, num_copy, best_prime=1, resto, i, n, now(3), req11
integer,parameter :: seed = 86456, numbers=200 
INTEGER :: req1(numbers), req2(numbers)
LOGICAL :: flag, res


CALL MPI_INIT(ierr)

CALL MPI_COMM_RANK(MPI_COMM_WORLD,myid,ierr)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD,npe,ierr)

IF (npe/=2) THEN
    WRITE(*,*)'avviare il programma con 2 processi (-np 2 ./mpi_NonBlocking)'

ELSE

    IF (myid == 0) THEN

        OPEN(22,FILE='uomo_0.txt', STATUS="UNKNOWN", FORM= "FORMATTED")
        OPEN(24,FILE='uomo_00.txt', STATUS="UNKNOWN", FORM= "FORMATTED")
        CALL ITIME(now)
        WRITE(*,*)
        CALL SRAND(now(3))

        DO n=1, numbers

            req1(n)=0
            req2(n)=0
            num=IRAND()
            WRITE(24,*)'random number',num
            IF (num>best_prime) THEN
                resto=MOD(num,2)
                IF (resto==0 .and. num/=2) THEN
                    WRITE(24,*)'CYCLE'
                    CYCLE
                END IF
            END IF

            WRITE(22,*)'uomo 0 SEND',num,'non-blocking'
            WRITE(24,*)'uomo 0 SEND',num,'non-blocking'
            CALL MPI_ISEND(num,1,MPI_INTEGER,1,1,MPI_COMM_WORLD,req1(n),ierr)
            WRITE(24,*)num,req1(n)
            CALL MPI_IRECV(best_prime,1,MPI_INTEGER,1,0,MPI_COMM_WORLD,req2(n),ierr)

        END DO

        CLOSE(22)


        CALL MPI_SEND(-1,1,MPI_INTEGER,1,1,MPI_COMM_WORLD,ierr)
        PRINT *,'best_prime is',best_prime

        !DO i=1, numbers
            !WRITE(24,*)'TEST isend',i,'flag value',req2(i)
            !CALL MPI_TEST(req2(i),flag,istatus,ierr)
            !IF (flag .eqv. .false.) THEN
                !WRITE(24,*)'uomo 0 RECV',i,'non-blocking FAIL'
            !ELSE IF    (flag .eqv. .true.) THEN
                !WRITE(24,*)'uomo 0 RECV',i,'non-blocking SUCCESS'
            !END IF
        !END DO

        CLOSE(24)


    ELSE


        OPEN(23,FILE='uomo_1.txt', STATUS="UNKNOWN", FORM= "FORMATTED")
        OPEN(25,FILE='uomo_11.txt', STATUS="UNKNOWN", FORM= "FORMATTED")
        DO n=1, numbers

            CALL MPI_RECV(num,1,MPI_INTEGER,0,myid,MPI_COMM_WORLD,istatus,ierr)
            WRITE(23,*)'uomo 1 RECV',num
            WRITE(25,*)'uomo 1 RECV',num
            IF (num==-1) THEN
                WRITE(23,*)'ABORT'
                WRITE(25,*)'ABORT'
                CALL EXIT(0)
            END IF

            res=.true.
            num_copy=NINT(SQRT(REAL(num)))
            WRITE(25,*)'sqrt',num_copy,'di num',num
            DO i=3, num_copy, +2

                resto=MOD(num,i)
                IF (resto==0) THEN
                    res= .false.
                    WRITE(25,*)'num',num,'no numero primo'
                    EXIT     !not numero primo
                END IF
            END DO

            IF (res .eqv. .true.) THEN
                WRITE(25,*)'num',num,'PRIMO'
                WRITE(25,*)'uomo 1 SEND',num,'non-blocking' 
                CALL MPI_ISEND(num,1,MPI_INTEGER,0,0,MPI_COMM_WORLD,req11,ierr)
            END IF

        END DO
        CLOSE(23)
        CLOSE(25)

    END IF

END IF

CALL MPI_FINALIZE(ierr)
END PROGRAM

问题是,发生了重叠,但是进程1继续接收从进程0多次发送的最后一个数字:它继续正好接收,直到他达到进程0的isend调用的数量。

 105) uomo 0 SEND  1310474405 non-blocking
 106) uomo 0 SEND   551041203 non-blocking
 107) uomo 0 SEND  1400012957 non-blocking
 108) uomo 0 SEND   907501537 non-blocking
 109) uomo 0 SEND   949471365 non-blocking

 61) uomo 1 RECV   792320847
 62) uomo 1 RECV  2137864129
 63) uomo 1 RECV  1888232175
 64) uomo 1 RECV     4829859
 65) uomo 1 RECV  2082851615
 66) uomo 1 RECV   949471365
 67) uomo 1 RECV   949471365
 68-109) uomo 1 RECV   949471365
 110) uomo1 RECV   -1
 111) ABORT

0 个答案:

没有答案