SPMD- (SingleProgram MultipleData, ). SPMD
ProgramExample3d
Implicit None
Include 'mpif.h'
Integer Size, Rank, Ierr, I, N
Double Precision Sum, GSum, A, B, time1, time2, Al, Bl, X, F
!
Parameter (A=0.d0, B=1.d0)
!
F(x)=DLog(1/x)
Call MPI_INIT(Ierr)
Call MPI_COMM_SIZE(MPI_COMM_WORLD, Size, Ierr)
Call MPI_COMM_RANK(MPI_COMM_WORLD, Rank, Ierr)
! 0-
If (Rank.eq.0) time1 = MPI_WTime()
!
!
Al = A+(B-A)*Rank/Size
Bl = Al+(B-A)/Size
N = 1000000
!
Sum = 0
Do I = 1,N
X = Al+(Bl-Al)*(I-0.5d0)/N
Sum = Sum + F(X)
End Do
! 0-
Call MPI_REDUCE(Sum, GSum, 1, MPI_DOUBLE_PRECISION, MPI_SUM, 0, MPI_COMM_WORLD, Ierr)
If (Rank.eq.0) Then
time2 = MPI_WTime()
GSum = GSum/(N*Size)
Write (6,*) 'Result=',GSum,' Error=',1-GSum, ' Time=',time2 - time1
End If
Call MPI_FINALIZE(Ierr)
Stop
EndProgram
MPMD
int MPI_Comm_spawn(char *command, char *argv[],
int maxprocs, MPI_Info info, int root, MPI_Comm comm,
MPI_Comm *intercomm,
int array_of_errcodes[])
3. 2*sin(x) [0,1].
double f(double x)
{return 2*sin(x);}
int main(intargc,char **argv)
{ intsize,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(inti=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. ?
, , . , . , . :
1) (CAD ComputerAidedDesign). 2) . 3) . 4) . 5) . 6) -. , , , .