源代码:Lamport逻辑钟和矢量逻辑钟的模拟_第1页
源代码:Lamport逻辑钟和矢量逻辑钟的模拟_第2页
源代码:Lamport逻辑钟和矢量逻辑钟的模拟_第3页
源代码:Lamport逻辑钟和矢量逻辑钟的模拟_第4页
源代码:Lamport逻辑钟和矢量逻辑钟的模拟_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/ Lamport and Vector logical clock simulator/numprocs: number of processes#include mpi.h#include #include #include #include #include #include #include class clock(public:long * Vec;/Vector logical clocklong Lampt;/Lamport clockclock(int numprocs, long i1);clock();void Execution( int numprocs, int my

2、id, long &LamptBuffer, long * VecBuffer,class clock& myClock, long count);/update clocks when Executingvoid MsgSend( int numprocs, int myid, int destination,long& LamptBuffer, long * VecBuffer, class clock& myClock,long count); /update clocks when sendingvoid MsgRecv( int numprocs, int myid, int sou

3、rce,long& LamptBuffer ,long * VecBuffer, class clock& myClock, long count); / update clocks when receiving;namespace global(enum EventTypeInitial, Execution,Send, Receive; /parameters for output, corresponding to the four events;void print(global:EventType ThisEvent, int numprocs, int myid, int othe

4、r, class clock& myClock);/print at each event, such as sendinglong string2number (std:string);/convert a string to numbers using namespace std; int main(int argc, char *argv) int myid, numprocs;MPI_Init(&argc,&argv);MPI_Comm_size(MPI_COMM_WORLD,&numprocs);MPI_Comm_rank(MPI_COMM_WORLD,&myid);if (myid

5、=0)printf( there are %d processes in the system n, numprocs);long MsgPlus1=string2number (argv1);/number of times that the initial messages will be sent.long MsgTotal=MsgPlus1-1;class clock myClock(numprocs, 0);/initializinglong Tag=123;long count=1;int isReceiving=0;vector Send(MsgPlus1,-1);/sender

6、 infovector NxtRecv(MsgPlus1,-1);/receiver infovector Send_Tracker(MsgPlus1*numprocs,-1); /for collecting senders infovector NxtRecv_Tracker(MsgPlus1*numprocs,-1);/for collecting receivers infovector TrackerIni(MsgPlus1*numprocs,-1);/vector Report(numprocs*(numprocs+2),-1);/for final report printout

7、 vector:iterator it;long* VecBuffer=new long numprocs; for( int i=0;inumprocs;i+) *(VecBuffer+i)=0;long LamptBuffer=0;for (long i=0;iMsgPlus1;i+)if (i=MsgPlus1-1) count=1;NxtRecv_Tracker=TrackerIni;Send_Tracker=TrackerIni;/step 1: launch a new message; if (myid=0&i=0) (Tag=i;NxtRecvi=rand() % numpro

8、cs;if (NxtRecvi=myid)if (myid+1)!=numprocs)NxtRecvi=myid+1;/ numprocs;/cant send it to the process itself; else NxtRecvi=(myid-1);Send_Trackeri=0;NxtRecv_Trackeri=NxtRecvi;myClock.MsgSend( numprocs, myid, NxtRecvi, LamptBuffer, VecBuffer, myClock,count);MPI_Send(&LamptBuffer, 1,MPI_LONG,NxtRecvi, Ta

9、g,MPI_COMM_WORLD);MPI_Send(VecBuffer, numprocs,MPI_LONG,NxtRecvi, Tag,MPI_COMM_WORLD);for (long ii=0;ii10000;ii+) MPI_Barrier(MPI_COMM_WORLD);/Step 2: send out existing messagesfor (it =find (NxtRecv.begin(), NxtRecv.end(), myid);itNxtRecv.end();it+) it=find(it, NxtRecv.end()-1,myid);if (it!=NxtRecv

10、.end()-1)& (it-NxtRecv.begin()!=i) *it=rand() % numprocs;if (*it=myid )if (myid+1)!=numprocs)*it=(myid+1);/ numprocs;/cant send it to the process itself;else *it=(myid-1);Send_Trackerit-NxtRecv.begin()+myid*MsgPlus1=myid;NxtRecv_Trackerit-NxtRecv.begin()+myid*MsgPlus1=*it;myClock.MsgSend( numprocs,

11、myid, *it,LamptBuffer, VecBuffer, myClock,count);MPI_Send(&LamptBuffer, 1, MPI_LONG, *it, it-NxtRecv.begin(), MPI_COMM_WORLD);MPI_Send(VecBuffer, numprocs, MPI_LONG, *it, it-NxtRecv.begin(), MPI_COMM_WORLD);/coutCheckCheck3-sending myid: myid i is: i target: *it endl; for (long ii=0;ii10000;ii+) MPI

12、_Barrier(MPI_COMM_WORLD);/update sender/receiver infofor (int ii=0;iinumprocs;ii+)MPI_Bcast(&(NxtRecv_Trackerii*MsgPlus1),MsgPlus1, MPI_LONG ii, MPI_COMM_WORLD );for (int ii=0;iinumprocs;ii+) MPI_Bcast(&(Send_Trackerii*MsgPlus1), MsgPlus1, MPI_LONG, ii, MPI_COMM_WORLD );MPI_Barrier(MPI_COMM_WORLD);f

13、or (int ii=0;iinumprocs;ii+)for (int jj=0;jjMsgPlus1;jj+)if (NxtRecv_Trackerii*MsgPlus1+jj!=-1) NxtRecvjj=NxtRecv_Trackerii*MsgPlus1+jj;if(Send_Trackerii*MsgPlus1+jj!=-1)Sendjj=Send_Trackerii*MsgPlus1+jj;/Step 3: receive any messages from any source;for (it =find (NxtRecv.begin(), NxtRecv.end(), myi

14、d);itNxtRecv.end();it+) it=find(it, NxtRecv.end(),myid);if (myid=*it)&(it!=NxtRecv.end() / coutendl;/ coutfrom:Send(it-NxtRecv.begin()myidmyidreceive:ith= i”,NxtRecv: *itendl;MPI_Recv(&LamptBuffer, 1, MPI_LONG Send(it-NxtRecv.begin(), it-NxtRecv.begin(), MPI_COMM_WORLD, MPI_STATUS_IGNORE);MPI_Recv(V

15、ecBuffer,numprocs,MPI_LONG,Send(it-NxtRecv.begin(),it-NxtRecv.begin(),MPI_COMM_WORLD,MPI_STATUS_IGNORE);myClock.MsgRecv( numprocs, myid,Sendit-NxtRecv.begin(), LamptBuffer, VecBuffer, myClock,count);isReceiving=1;/ coutLamptBuffer i=i myid=myidendl;for (long ii=0;ii10000;ii+) MPI_Barrier(MPI_COMM_WO

16、RLD);/Step 4: Excecutionif (isReceiving=1)int tmp=rand()%10;for (int ii=0;iitmp;ii+)myClock.Execution( numprocs, myid, LamptBuffer, VecBuffer, myClock,count);for (long ii=0;ii10000;ii+) MPI_Barrier(MPI_COMM_WORLD);/isReceiving=0;/ collecting info for final reportif (i=MsgTotal)for (int ii=0;ii1) Rep

17、ort(myid)*(numprocs+2)+ii=*(VecBuffer+ii-2);/cout*(VecBuffer+ii);if(myid!=0) MPI_Send(&(Reportmyid*(numprocs+2), numprocs+2, MPI_LONG, 0, 100, MPI_COMM_WORLD);if (myid=0)for (int ii=1;iinumprocs;ii+)MPI_Recv(&(Reportii*(numprocs+2),numprocs+2, MPI_LONG, ii, 100, MPI_COMM_WORLD, MPI_STATUS_IGNORE);MP

18、I_Barrier(MPI_COMM_WORLD);MPI_Barrier(MPI_COMM_WORLD);MPI_Finalize();/ if (myid!=0) coutTransferring control to Process 0endl;/if (myid=0) coutendlendlProcess 0 cleaning upendlEnd of messagesendl;/ output final reportif (myid=0)coutendl=”1initial message and nsteps= n=MsgTotal+1endl;for (int kk=0;kk

19、numprocs;kk+)for (int jj=0;jjnumprocs+2;jj+)if (jj=0) coutProcess: Reportkk*(numprocs+2)+jj;if (jj=1) cout Lamport:Reportkk*(numprocs+2)+jj1) coutvvlvvReportkk*(numprocs+2)+jj;coutendl;coutvv= =vvendl;return 0;/clock:clock( int numprocs, long i1)Vec=new long numprocs;Lampt=i1;for( int i=0;inumprocs;

20、i+)*(Vec+i)=0;/clock:clock( )delete Vec;/void clock:Execution( int numprocs, int myid, long &LamptBuffer, long * VecBuffer, class clock& myClock,long count)/int NumExec = rand() % 10;int NumExec = 1;/Execute several taskes inside the process.*(myClock.Vec+myid)+=NumExec;*(VecBuffer+myid)+=NumExec;my

21、Clock.Lampt+=NumExec;LamptBuffer+=NumExec;if (count=1)print(global:Execution, numprocs, myid, 0, myClock);/void clock:MsgSend( int numprocs, int myid, int destination, long &LamptBuffer, long *VecBuffer, class clock& myClock,long count)*(myClock.Vec+myid)+=1;*(VecBuffer+myid)+=1;myClock.Lampt+=1;Lam

22、ptBuffer+=1;if (count=1)print(global:Send, numprocs, myid, destination, myClock);/void clock:MsgRecv( int numprocs, int myid, int source, long& LamptBuffer, long * VecBuffer, class clock& myClock,long count)/std:coutvvendlvvLamptBuffer:vvLamptBuffervv VecBuffer:*VecBufferendl;*(myClock.Vec+myid)+=1;

23、for( int i=0;inumprocs;i+)*(myClock.Vec+i)=max(*(myClock.Vec+i), *(VecBuffer+i);*(VecBuffer+i)=*(myClock.Vec+i);LamptBuffer+=1;myClock.Lampt+=1;myClock.Lampt=max(LamptBuffer,myClock.Lampt);LamptBuffer=myClock.Lampt;if (count=1)print(global:Receive, numprocs, myid, source, myClock); /void print(globa

24、l:EventType ThisEvent, int numprocs, int myid, int other, class clock& myClock)string x;stringstream ss (stringstream:in | stringstream:out);if (ThisEvent=global:Initial)/coutinitialized-;ssendlmyid:myid /Lampt:myClock.Lampt” /Vec:|;for( int i=0;inumprocs;i+)ss*(myClock.Vec+i)”|”;ssendl;if (ThisEvent=global:Executi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论