1. ? ғ ? ғ ғ. MPI , , MIMD. , , . MPI. , MPI . MPI . . `` '' (`` thread-safe''). MPI , MPI, ; MPI , . MPI . , . ; , ; . , MPI ( ), . , , . . 0,..., groupsize-1.
2. . қ ө қ. () MPI : int MPI_Cart_create(MPI_Comm oldcomm, int ndims, int *dims, int *periods, int reorder, MPI_Comm *cartcomm). , . , , , . , , , , . periods - ndims, , . int dims[2]={0,0}, periods[2]={1,0},coords[2], ndims=2, reorder=0; - MPI_Cart_create(MPI_COMM_WORLD,ndims,dims,periods,reorder,&cartcomm) . periods[2]={1,1} .
3. 3 ө ң 2-ө ә .
MPI_Comm GridComm;
int size,rank,i;
int ix,iy,iz;
int x,y,z,X=10,Y=10,Z=10;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
|
|
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Status status;
y=Y/size+2;
double matrix[X][y][Z],vecUP[X][Z],vecDOWN[X][Z];
if(rank==0)
{ for(ix=1;ix<X-1;ix++)
{ for(iz=1;iz<Z-1;iz++)
{ matrix[ix][1][iz]=1;} }
for(ix=1;ix<X-1;ix++) {
for(iy=2;iy<y-1;iy++){
for(iz=1;iz<Z-1;iz++) {
matrix[ix][iy][iz]=0; } } } }
else {
for(ix=1;ix<X-1;ix++) {
for(iy=1;iy<y-1;iy++) {
for(iz=1;iz<Z-1;iz++) {
matrix[ix][iy][iz]=0;}} } }
for(int i=0;i<10;i++) {
if(rank<size-1) {
for(ix=0;ix<X;ix++){
for(iz=0;iz<Z;iz++) {
vecUP[ix][iz]=matrix[ix][y-2][iz];}} MPI_Send(vecUP,X*Z,MPI_FLOAT,rank+1,rank,MPI_COMM_WORLD); MPI_Recv(vecUP,X*Z,MPI_FLOAT,rank+1,rank+1,MPI_COMM_WORLD,&status);
for(ix=0;ix<X;ix++) {
for(iz=0;iz<Z;iz++) {
matrix[ix][y-1][iz]=vecUP[ix][iz]; } } }
if(rank>0) {
MPI_Recv(vecDOWN,X*Z,MPI_FLOAT,rank-1,rank-1,MPI_COMM_WORLD,&status);
for(ix=0;ix<X;ix++) {
for(iz=0;iz<Z;iz++) {
matrix0[ix][0][iz]=vecDOWN[ix][iz];
vecDOWN[ix][iz]=matrix[ix][1][iz]; } }
MPI_Send(vecDOWN,X*Z,MPI_FLOAT,rank-1,rank,MPI_COMM_WORLD); }
for(iz=1;iz<Z-1;iz++) {
for(iy=1;iy<y-1;iy++) {
if(rank==0&&iy==1){continue;}
if(rank==size-1&&iy==y-2){break;}
for(ix=1;ix<X-1;ix++) {
matrix[ix][iy][iz]=matrix[ix+1][iy][iz]+matrix[ix-1][iy][iz]+matrix[ix][iy+1][iz];
matrix[ix][iy][iz]+=matrix[ix][iy-1][iz]+matrix[ix][iy][iz+1]+matrix[ix][iy][iz-1];
matrix[ix][iy][iz]/=6; } } } }
while(stop==0); MPI_Finalize(); return 0;}
1. . ISD, IMD . . . :
SISD (Single Instruction Stream - Single Data Stream) ;
SIMD (Single Instruction Stream Multiple Data Stream) ;
MISD (Multiple Instruction Stream Single Data Stream) ;
MIMD (Multiple Instruction Stream Multiple Data Stream)
MISD
. , , . . ,
MIMD
, . , , .. , , .
|
|
2. MPI , ә .
MPI , . mpicc( ), mpic++( ++), mpif77/mpif90( 77/90). -o name name , a.out, : mpif77 o program program.f
. MPI mpirun,mpiexec, :
mpirun np N < >
mpiexec np N < >
N- , . , .
3. 2*sin(x) ң [0,1] қ ң.
double f(double x)
{return 2*sin(x);}
int main(int argc,char **argv)
{
int size,rank;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
float h=0.05,a=0,b=1,s=0.0;
for(int i=rank; a+h*(i+1)<b+h; i+=size)
{ s+=h*f(a+h*(i+1)); }
if(rank!=0)
{ MPI_Send(&s,1,MPI_FLOAT,0,1,MPI_COMM_WORLD); }
if(rank==0)
{ float r;
MPI_Recv(&r,1,MPI_FLOAT,1,1,MPI_COMM_WORLD,&status);
s+=r;
printf("s=%f\n ",s);
}
}
1. ү. . .
, , , .
, . , , . , . . . , . , . , . , , . , . . , , , , , . , . , , . , : , . , . .
|
|
//
Semaphore fork[5] = { 1, 1, 1, 1, 1 };
// - ( )
Prilosopher(){ // i
while (1) { P(fork[i]); // P(fork[(i+1)%5]); // <>
//
V(fork[i]); V(fork[(i+1)%5])
<> } }
( (i +1)%5 , % ).
2. ә . . : - (MPI_Barrier); - , : (MPI_Bcast); (gather) (root) (MPI_Gather, PI_Gatherv); (gather) (MPI_Allgather, MPI_Allgatherv); (scatter) (MPI_Scatter, MPI_Scatterv); Scatter/Gather (All-to-All), , , (MPI_Alltoall, MPI_Alltoallv). - (sum, min, max .) , : (MPI_Reduce); (MPI_Allreduce); Reduce/Scatter (MPI_Reduce_scatter); (MPI_Scan
3. ө ң
int main(int argc,char **argv)
{ int size,rank,i,n=6;
float *a,*b;
a=new float[n]; b=new float[n];
for(i=0;i<n;i++) {
a[i]=i+1; b[i]=i+1; }
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
float f=0,s=0,s1=0;
int nachalo,konec,shag;
shag=n/(size-1);
if(rank!=size-1) {
nachalo=rank*shag;
konec=rank*shag+shag;
for(i=nachalo;i<konec;i++)
s=s+a[i]*b[i];
MPI_Send(&s,1,MPI_FLOAT,size-1,1,MPI_COMM_WORLD);}
if(rank==size-1){
for(i=0;i<size-1;i++){ MPI_Recv(&s,1,MPI_FLOAT,i,1,MPI_COMM_WORLD,&status);
f=f+s; }
printf("%f\n",f);} MPI_Finalize();}
1. Қ . қ ө. (SMP) (MPP). SMP MPP NUMA-, , . MPP. - , , PVP- . :
1. SISD (Single Instruction Single Data) . . .
2. SIMD (Single Instruction Multiple Data) . , , .
|
|
3. MISD (Multiple Instruction Single Date) . . ,
. - , .
4. MIMD (Multiple Instruction Multiple Date) . .
2. MPI_Cart_create() қ .
() MPI : int MPI_Cart_create(MPI_Comm oldcomm, int ndims, int *dims, int *periods, int reorder, MPI_Comm *cartcomm). 1. oldcomm - . 2. ndims - 3. dims - ndims, . 4. periods - ndims, , . 5.reorder - 6. cartcomm .
int dims[3]={0,0,0}, periods[3]={0,0,0},coords[3], ndims=3, reorder=0;; MPI_Cart_create(MPI_COMM_WORLD,ndims,dims,periods,reorder,&cartcomm);
3. 2 ө ң 2-ө ә .
1. Қ . қ ө. (SMP) (MPP). SMP MPP NUMA-, , . MPP. - , , PVP- . :
1. SISD (Single Instruction Single Data) . . .
2. SIMD (Single Instruction Multiple Data) . , , .
3. MISD (Multiple Instruction Single Date) . . ,
. - , .
4. MIMD (Multiple Instruction Multiple Date) . .
2. MPI_Dims_create() қ .
MPI_DIMS_CREATE , . , ( MPI_COMM_WORLD) n- . int MPI_Dims_create(int nnodes, int ndims, int *dims)nnodes (), ndims (), dims - ndims, .
int dims[2]={0,0},ndims=2;
MPI_Dims_create(size, ndims, dims);
3. 3 ө ң 2-ө ә .
1. Shared & distributed memory . (DSM - Distributed Shared Memory) , . ( ). DSM - , () . DSM , . DSM , . , . , , SMP, . , . , , . NUMA (non-uniform memory access). NUMA (Non-Uniform Memory Access) , . , . , , NUMA- . , , API .
|
|
2. ү-ү . MPI_Bsend(), MPI_Brecv қ .
. - : . MPI , . . .
, .. , .
B (buffered) - . , . , . .
MPI_Bsend . -, , . . , , . .
int MPI_Bsend(void *buf, int count, MPI_Datatype datatype,int dest, int tag, MPI_Comm comm)
buf- ; count ; datatype ; dest - , comm; tag ( nread nwrite PSE nCUBE2); comm .
MPI_Bsend(&buffer, buffsize, MPI_INT, 1, TAG, MPI_COMM_WORLD);
3. ққ ң.
. SISD, SIMD.
. . :
SISD (Single Instruction Stream Single Data Stream) ;
SIMD (Single Instruction Stream Multiple Data Stream) ;
MISD (Multiple Instruction Stream Single Data Stream) ;
MIMD (MultipleInstructionStream MultipleDataStream) .
SISD , "" , ( - ). , , . , , .
SIMD ( ), , , . , . , , . . , . , . SIMD , "" . SIMD.
2 MPI? .
Message Passing Interface (MPI, ) (API) , , . , . MPI , . . MPI. 77/90,Java, ++.
MPI , . MPI . , : ( ) ; ; ; .
MPI : , , ++; ; , ; ; , .
MPI , . .