c examples/int_pt2pt c include "mpif.h" integer my_rank,nproc,ierr double precision value,sum c c Initialize MPI and determine individual processor and c the size of the world c call init(nproc,my_rank) if(my_rank.eq.0) then call master(nproc) else call slave(my_rank,value) endif ihost=0 call MPI_Reduce(value,sum,1,MPI_DOUBLE_PRECISION,MPI_SUM, 1 ihost,MPI_COMM_WORLD,ierr) if(my_rank.eq.0) then write(*,*) 'area=',sum endif call MPI_Finalize(ierr) stop end c c subroutine init(nproc,my_rank) include "mpif.h" c c initialize the MPI c integer my_rank,nproc character*30 nodename integer nchar call MPI_init(ierr) call MPI_Comm_rank(MPI_COMM_WORLD,my_rank,ierr) call MPI_Comm_size(MPI_COMM_WORLD,nproc,ierr) call MPI_Get_processor_name(nodename,nchar,ierr) write(*,*) 'my_rank=',my_rank,' processor=',nodename return end c c subroutine slave(my_rank,value) include "mpif.h" c c 1. the slave process receives 'a' and 'b' from master c 2. the slave sends `my_rank' and 'value' to master (proc 0) c integer my_rank,ierr,itag1 integer status(MPI_STATUS_SIZE),ipos double precision a,b,value,trap parameter (BUFSIZE=100) character buf(100) idest=0 itag1=10 call MPI_Recv(buf,BUFSIZE,MPI_PACKED,idest,itag1, 1 MPI_COMM_WORLD,status,ierr) ipos=0 call MPI_Unpack(buf,BUFSIZE,ipos,a,1,MPI_DOUBLE_PRECISION, 1 MPI_COMM_WORLD,ierr) call MPI_Unpack(buf,BUFSIZE,ipos,b,1,MPI_DOUBLE_PRECISION, 1 MPI_COMM_WORLD,ierr) write(*,*) 'my_rank=',my_rank,' a=',a,' b=',b value=trap(a,b) return end c c subroutine master(nproc) include "mpif.h" integer i,nproc,itag1,ierr double precision xl,a(10),b(10),dx parameter (BUFSIZE=100) character buf(100) n1=nproc-1 xl=1.0 dx=xl/dfloat(n1) do i=1,n1 a(i)=dx*(i-1) b(i)=a(i)+dx enddo itag1=10 do i=1,n1 idest=i ipos=0 call MPI_Pack(a(i),1,MPI_DOUBLE_PRECISION,buf,BUFSIZE,ipos, * MPI_COMM_WORLD,ierr) call MPI_Pack(b(i),1,MPI_DOUBLE_PRECISION,buf,BUFSIZE,ipos, * MPI_COMM_WORLD,ierr) call MPI_Send(buf,ipos,MPI_PACKED,idest,itag1, * MPI_COMM_WORLD,ierr) enddo return end c double precision function trap(a,b) double precision a,b,calf,x,y integer n n=100 dx=(b-a)/dfloat(n) trap=calf(a) do i=2,n x=a+dx*(i-1) y=calf(x) trap=trap+2.*y enddo trap=0.5*(trap+calf(b))*dx return end c c double precision function calf(x) double precision x calf=x*x return end