2013-12-17 48 views
-1

我有以下代碼來計算總理的數量,我已經將循環中的工作劃分爲跨處理器。問題是,當子例程將循環的子部分分配給按等級分配的處理器,並且我似乎無法控制它們到達的順序如何在MPI中的進程之間劃分工作

即,我想隨後來到0,1,2 ,3 ...而不是像2,1,0,3 ..

因此,如果在循環和5個處理器中有500次迭代。

  • 序號爲0的執行[1 - 100]
  • 等級1個執行[101-200]等...

     program main 
         implicit none 
         include 'mpif.h' 
         integer(4), parameter :: n = 36500 
         integer(4) :: a(n) 
         integer(4) :: i 
         integer(4) :: j 
         integer(4) :: ista 
         integer(4) :: iend 
         integer(4) :: sum 
         integer(4) :: f=0 
         integer(4) :: ssum 
         integer(4) :: ierr 
         integer(4) :: iproc 
         integer(4) :: nproc 
         call MPI_INIT(ierr) 
         call MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr) 
         call MPI_COMM_RANK(MPI_COMM_WORLD, iproc, ierr) 
         call loop_range(2, n, nproc, iproc, ista, iend) 
         sum = 0.0 
         print *,ista,"-",iend,">",iproc 
         do i = ista, iend 
         f=0 
         do j=2,INT(SQRT(REAL(i))) 
         if(MOD(i,j)==0) then 
         f=1 
         end if 
         end do 
         if(f==0) then 
         sum = sum + 1 
         end if 
         end do 
         call MPI_REDUCE(sum, ssum, 1, MPI_INTEGER,MPI_SUM, 0,MPI_COMM_WORLD, ierr) 
         if (iproc == 0) write(6,*)'Total No of primes=', ssum 
         call MPI_FINALIZE(ierr) 
        end program main 
    
        subroutine para_range(n1, n2, nprocs, irank, ista, iend) 
         integer(4) :: n1  ! Lowest value of iteration variable 
         integer(4) :: n2  ! Highest value of iteration variable 
         integer(4) :: nprocs ! No of Cores/Processors you want to use 
         integer(4) :: irank  ! Process rank 
         integer(4) :: ista  ! Start of iterations for rank iproc 
         integer(4) :: iend  ! End of iterations for rank iproc 
         integer(4) :: iwork1, iwork2 
    
         print *,irank 
         iwork1 = (n2 - n1 + 1)/nprocs 
         iwork2 = MOD(n2 - n1 + 1, nprocs) 
         ista = irank * iwork1 + n1 + MIN(irank, iwork2) 
         iend = ista + iwork1 - 1 
         if (iwork2 > irank) then 
          iend = iend + 1 
         end if 
        end subroutine para_range 
    

我使用開放MPI。

+0

可能重複[開放MPI的隊伍是不是爲了(http://stackoverflow.com/questions/20633008/open-mpi-ranks-是 - 不按順序) –

回答

1

這個問題與您的其他問題(Open MPI ranks are not in order)幾乎相同,答案也是一樣的。你誤解了這個問題。

您的案例中的排序「排序」可以被視爲任意和不重要。您分配問題的級別將完成您分配給他們的工作。您遇到的問題是您希望它們全部以排序順序打印出來。在MPI中這是不可能的。不能保證,如果您完全同時打印來自所有級別的消息,則會以任何特定順序打印。原因是所有輸出必須首先發送到啓動應用程序mpiexecmpirun的進程,然後打印到屏幕上。對於某些流程,這種轉移可能比其他流程更快。如果輸出所有結果至關重要,則必須先將它們全部發送到同一個進程,然後從那裏打印出來。保證如果你打印的文本全部在同一等級上,它們將以正確的順序出來。

所以,你的代碼看起來大致是這樣的:

...initialize variables... 
...divide up work... 
...each work does the work assigned to it... 
...MPI_SEND result to rank 0... 
if (rank == 0) 
    MPI_RECV results from all processes 
    print results in order 
相關問題