MPI ( ) snozawa@env.sci.ibaraki.ac.jp 1 ( ) MPI MPI Message Passing Interface[2] MPI MPICH[3],LAM/MPI[4] (MIMDMultiple Instruction Multipule Data) Message Passing ( ) (MPI (rank) PE(Processing Element) ) 1 (AMachine(CPU,OS,,) ) (amemory x(i) ) (x(i)+α) (BMachine(CPU,OS,,) ) (network) (bmemory) (amemory) (bmemory) amemory x(i) x(i) bmemory x(i) +α x(i) +α AMachine(CPU,OS,,) network BMachine(CPU,OS,,) 1 CPU ( OS) ( ) MPI 1
C/C++ FORTRAN FORTRAN MPI MPI MPI UNIX Windows (SIMD Single Instruction Multipule Data) SMP(Symmetric Multi Processor) MPI (thread) OpenMP[5] 2 2 2 3 3 2
3 CANS MPI FORTRAN i-n 3 CANS1D 3.1 main.f cans(or cans-current)/cans1d/mdp_shktb main.f MPI include "mpif.h" c------ c for MPI call mpi_init(merr) call mpi_comm_size(mpi_comm_world,npe,merr) call mpi_comm_rank(mpi_comm_world,myrank,merr) call mpi_allreduce(dt,dtg,1,mpi_double_precision,mpi_min &,mpi_comm_world,merr) call mpi_finalize(merr) main.f (CANS ) include "mpif.h" call mpi_init(merr) include "mpif.h" mpif.h ( ) 3
MPI 1 mpi_init MPI merr call mpi_finalize(merr) mpi_init merr MPI MPI call mpi_comm_size(mpi_comm_world,npe,merr) call mpi_comm_rank(mpi_comm_world,myrank,merr) call mpi_allreduce(dt,dtg,1,mpi_double_precision,mpi_min &,mpi_comm_world,merr) mpi_comm_size npe mpi_comm_rank myrank mpi_comm_world npe,myrank myrank 0,1,2,,, 0 mpi_allreduce 4 dt (mip_min) dtg mpi_min 4 mpi allreduce dt dtg (mpi min) 1 include "/opt/usr/local/mpi/mpif.h" 4
CFL 3.2 exc h.f cans(or cans-current)/cans1d/commonmpi /exc_h.f MPI c========= subroutine exc_h(margin,ro,pr,vx,ix,myrank,npe) c========= c--------- c from PE(myrank) to PE(myrank+1) for new da(1) c--------- mright= myrank+1 mleft = myrank-1 if (myrank.eq.npe-1) mright = mpi_proc_null if (myrank.eq.0 ) mleft = mpi_proc_null do i=1,margin bufsnd(i,1)=ro(ix-2*margin+i) bufsnd(i,2)=pr(ix-2*margin+i) bufsnd(i,3)=vx(ix-2*margin+i) enddo call mpi_sendrecv & (bufsnd,mmx,mpi_double_precision,mright,1 &,bufrcv,mmx,mpi_double_precision,mleft,1 &,mpi_comm_world,mstatus,merr) if (myrank.ne.0) then do i=1,margin ro(i)=bufrcv(i,1) pr(i)=bufrcv(i,2) vx(i)=bufrcv(i,3) enddo endif 5 mpi sendrecv 5
5 mright= myrank+1 mleft = myrank-1 if (myrank.eq.npe-1) mright = mpi_proc_null if (myrank.eq.0 ) mleft = mpi_proc_null mright,mleft +1,-1 myrank,h=0,npe-1 ( ) mpi_proc_null call mpi_sendrecv & (bufsnd,mmx,mpi_double_precision,mright,1 &,bufrcv,mmx,mpi_double_precision,mleft,1 &,mpi_comm_world,mstatus,merr) bufsnd,bufrcv 6 mpi_sendrecv ro pr vx ro pr vx 6 4 MPI MPI 6
4.1 mpi comm size mpi comm size(communicator,size,ierr) communicator integer mpi_comm_world size integer merr integer ex. call mpi_comm_size(mpi_comm_world,npe,merr) 4.2 mpi comm rank mpi comm rank(communicator,rank,ierr) 0 rank size-1 communicator integer mpi_comm_world rank integer merr integer ex.call mpi_comm_rank(mpi_comm_world,myrank,merr) 4.3 mpi send mpi send(buf,count,datatype,dest,tag,comm,ierr) buf dest integer tag integer ex. call mpi_send( x,1,mpi_real,0,itag,mpi_comm_world,merr) 4.4 mpi recv mpi recv(buf,count,datatype,dest,tag,comm,status,ierr) mpi send status buf dest integer tag integer status integer ex. call mpi_recv( x,1,mpi_real,i,itag,mpi_comm_world,mstatus,merr) 7
4.5 mpi isend mpi_send mpi_isend mpi_wait 2 mpi isend(buf,count,datatype,dest,tag,comm,ireq,ierr) mpi send ireq buf dest integer tag integer ireq integer ex. call mpi_isend( x,1,mpi_real,0,itag,mpi_comm_world,ireq,merr) 4.6 mpi irecv mpi_recv mpi_irecv mpi_wait 3 mpi irecv(buf,count,datatype,dest,tag,comm,ireq,ierr) mpi recv status ireq buf dest integer tag integer ireq integer ex. call mpi_irecv( x,1,mpi_real,i,itag, mpi_comm_world,ireq,merr) 4.7 mpi wait mpi wait(ireq,status,ierr) ireq,status ireq integer status integer ex. call mpi_wait(ireq, mstatus, merr) 2 isend irecv recv mpi wait 3 isend irecv isend send mpi wait 8
4.8 mpi sendrecv mpi sendrecv(buf1,count1,datatype1,dest1,tag1,buf2,count2,datatype2,dest2,tag2,comm,status,ierr) buf1 count1 integer datatype1 integer dest1 integer tag1 integer buf2 count2 integer datatype2 integer dest2 integer tag2 integer status integer ex. call mpi_sendrecv(x,1,mpi_real,0,1,x0,1,mpi_real,i,1,mpi_comm_world,mstatus,merr) 4.9 mpi reduce mpi reduce(buf1,buf2,count,datatype,op,dest,comm,ierr) op buf1 buf2 op integer dest integer ex. call mpi_reduce(dt,dtg,1,mpi_double_precision,mpi_min,0,mpi_comm_world,merr) 4.10 mpi allreduce mpi allreduce(buf1,buf2,count,datatype,op,comm,ierr) op mpi reduce dest buf1 buf2 op integer ex. call mpi_allreduce(dt,dtg,1,mpi_double_precision,mpi_min,mpi_comm_world,merr) 9
4.11 mpi bcast mpi bcast(buf,count,datatype,dest,comm,ierr) buf dest integer ex. call mpi_bcast(x,1,mpi_real,0,mpi_comm_world,merr) 4.12 mpi gather mpi gatherv,mpi allgather,mpi allgatherv mpi gather(buf1,count1,datatype1,buf2,count2,datatype2,dest,comm,ierr) buf1 count1 integer datatype1 integer buf2 count2 integer datatype2 integer dest integer ex. call mpi_gather(x,1,mpi_real,x0,1,0,mpi_comm_world,merr) 4.13 mpi scatter mpi bcast mpi scatterv mpi scatter(buf1,count1,datatype1,buf2,count2,datatype2,dest,comm,ierr) buf1 count1 integer datatype1 integer buf2 count2 integer datatype2 integer dest integer ex. call mpi_scatter(x,1,mpi_real,x0,1,0,mpi_comm_world,merr) 10
mpi_bcast x2 mpi_gather x2 x3 x4 x5 x3 x4 x5 mpi_scatter x2 x3 x4 x5 x2 x3 mpi_allgather x2 x3 x4 x5 x2 x3 x4 x5 x4 x2 x3 x4 x5 x5 x2 x3 x4 x5 7 mpi bcast,mpi gather,mpi scatter,mpi allgather 4.14 datatype byte MPI integer,integer*4 4 mpi_integer real,real*4 4 mpi_real double precision,real*8 8 mpi_real8, mpi_double_precision complex 8 mpi_complex double complex,complex*16 16 mpi_comple6 character 1 mpi_character byte 1 mpi_byte logical,logical*4 4 mpi_logical 11
4.15 MPI (op) mpi reduce,mpi allreduce mpi op create mpi sum mpi_integer,mpi_real,mpi_real8,mpi_complex mpi pro mpi_integer,mpi_real,mpi_real8,mpi_complex mpi max mpi_integer,mpi_real,mpi_real8 mpi min mpi_integer,mpi_real,mpi_real8 mpi maxloc mpi_2integer,mpi_2real,mpi_2double_precision mpi minloc mpi_2integer,mpi_2real,mpi_2double_precision mpi land mpi_logical mpi lor mpi_logical mpi lxor xor( ) mpi_logical mpi band mpi_integer,mpi_byte mpi bor mpi_integer,mpi_byte mpi bxor xor mpi_integer,mpi_byte 5 Unix MPI mpif77 (or mpif90) test.f mpirun -np n( ) a.out (ex. mpirun -np 2./a.out) C-c ps kill -9 ( ) or killall -9 a.out http//www.env.sci.ibaraki.ac.jp/ snozawa/mpi/ 6 [1] http//www.env.sci.ibaraki.ac.jp/ snozawa/mpi/summer03/ [2] http//www.mpi-forum.org/ [3] http//www-unix.mcs.anl.gov/mpi/mpich/ [4] http//www.lam-mpi.org/ [5] http//www.openmp.org/ 12