MPI_BARRIER,同步
MPI_BARRIER(COMM, IERROR)
!!!說明!!!
COMM -- 通信域
IERROR -- 返回的錯誤代碼
說明:MPI_BARRIER阻塞所有的調用者直到所有的組成員都調用了它,各個進程中這個調用才可以返回。就是說,有些進程執行得快,有些進程執行得慢,要等待所有的進程都執行到這里,才開時同時執行之后的命令,即“同步”。
例:
1 program main 2 3 use mpi 4 implicit none 5 6 character(len=20) :: message1,message2,message3 7 integer :: myid, ierr, status(mpi_status_size), rc, numprocs 8 9 call MPI_INIT(ierr) 10 call MPI_COMM_RANK( MPI_COMM_WORLD, myid, ierr ) 11 call MPI_COMM_SIZE(MPI_COMM_WORLD,numprocs,ierr) 12 write(*,*) ' process ', myid, ' of ', numprocs, ' is alive' 13 14 call MPI_BARRIER( MPI_COMM_WORLD, ierr ) 15 16 if ( myid .eq. 0) then 17 message1 = 'Hello, process 1' 18 call MPI_SEND(message1,20,MPI_CHAR,1,99,MPI_COMM_WORLD,ierr) 19 message2 = 'Hello, process 2' 20 call MPI_SEND(message2,20,MPI_CHAR,2,99,MPI_COMM_WORLD,ierr) 21 message3 = 'Hello, process 3' 22 call MPI_SEND(message3,20,MPI_CHAR,3,99,MPI_COMM_WORLD,ierr) 23 else if ( myid .eq. 1 ) then 24 call MPI_RECV(message1,20,MPI_CHAR,0,99,MPI_COMM_WORLD,status,ierr) 25 write(*,*) message1 26 else if ( myid .eq. 2 ) then 27 call MPI_RECV(message2,20,MPI_CHAR,0,99,MPI_COMM_WORLD,status,ierr) 28 write(*,*) message2 29 else if ( myid .eq. 3 ) then 30 call MPI_RECV(message3,20,MPI_CHAR,0,99,MPI_COMM_WORLD,status,ierr) 31 write(*,*) message3 32 end if 33 34 call MPI_FINALIZE(rc) 35 36 end
沒有14行MPI_BARRIER的時候,程序執行結果可能是這樣的:
1 process 0 of 4 is alive 2 process 1 of 4 is alive 3 Hello, process 1 4 process 2 of 4 is alive 5 Hello, process 2 6 process 3 of 4 is alive 7 Hello, process 3
可以看到,12行write寫命令並沒有連續輸出,中間被其他的寫命令搶先了,因為23行if做了個判斷,當process=1時,寫出message1,之后的執行同樣如此。
當加入14行MPI_BARRIER的時候,程序強制讓執行快的進程等一下執行慢的,或者不考慮之后的命令,所有的進程全部都執行完12行的write寫命令,之后才開始執行之后的命令。程序執行結果為:
1 process 1 of 4 is alive 2 process 2 of 4 is alive 3 process 3 of 4 is alive 4 process 0 of 4 is alive 5 Hello, process 1 6 Hello, process 2 7 Hello, process 3
可以看到,4個12行的write寫命令都執行完之后,程序才執行之后的命令。