program main USE MPIUTILS implicit none integer rank, num_nodes character(LEN=50) msg,num integer length,anysource !! start MPI call MPISTART() !! obtain the total number of processors num_nodes=MPINUMNODES() !! obtain the rank of this processor rank=MPIRANK() !! if you simply use print* or write(*,*), all processors will write the same message to the standard output (screen). !! MPIWRITE only write the message once. call MPIWRITE("type in any words:") !! MPIREAD read msg from the standard input (keyboard) and broadcast it to all processors. call MPIREAD(msg) !! all processors will print this. print*,trim(msg)," from rank",rank if(rank.gt.0)then num="" write(num,*)rank write(*,*) 'Hello World from rank #'//trim(adjustl(num)) msg="Greeting from processor #"//trim(adjustl(num)) !!send the msg to processor #0, with tag 0 call MPISEND(0,0,msg) !! send the length of the msg to processor #0, with tag 1 call MPISEND(0,1,LEN_TRIM(msg)) else write(*,"(a10,i2,a6)") "There are ",num_nodes," nodes" do rank=1,num_nodes-1 !! recieve a msg with tag 0 (from any source) call MPIRECVANYSOURCE(anysource,0,msg) !! now the # of source who send the msg is written into the variable anysource !! recieve the length of msg from the same source call MPIRECV(anysource,1,length) !! write the msg to screen write(*,*) msg(1:length) enddo endif !! stop MPI call MPIEND() end program