asim.cc

来自「NS2网络仿真软件是目前最为流行的网络仿真模拟软件」· CC 代码 · 共 1,021 行 · 第 1/2 页

CC
1,021
字号
	    if(flows[j].is_sflow){	      links[i].lambda+=flows[j].slambda*flows[j].snopkts;	    }	    links[i].theflows[c++]=j;	    //	    cout << links[i].theflows[c-1] << sp;	  }	}      }      // cout << " slambda = " << links[i].lambda;    }    else links[i].theflows = 0; // no connection passing through this edge    //cout << endl;  }  /*  char c= getchar();  for(int i=0;i<nConnections;i++){    cout << "connection" << sp << i << sp << "-";     for(int j=0;j<nAdj[i];j++){      cout << sp << Adj[i][j];    }    cout << endl;  }  */  }double redFn(double minth, double pmin,	     double maxth, double pmax, double qlength){  assert(qlength>=0 && qlength<=1);  assert(pmax>=0 && pmax<=1);  assert(pmin>=0 && pmin<=1);  assert(minth>=0 && minth<=1);  assert(maxth>=0 && maxth<=1);  assert(maxth>=minth);  assert(pmax>pmin);  //Double t;  if(qlength<minth)    return 0;  if(qlength>maxth)    return 1;  return pmin + (qlength-minth)/(pmax-pmin);}void  CalcLinkDelays(int flag = 0){  // flag = 1 means enable RED  // Calculate Link delays ... basically queuing delays  for(int i=0; i<nLinks; i++){    double rho = links[i].lambda/links[i].mu;    double qlength = Lq(rho,links[i].buffer);    links[i].qdelay = qlength/links[i].mu;     links[i].drop = Pk(rho,links[i].buffer,links[i].buffer);//cout << "rho = " << rho << " drop = " << links[i].drop << endl;    // Special code for RED gateways    if(flag){      if(links[i].red){	double minth, maxth, pmin, pmax, delay,p;	minth = links[i].minth;	maxth = links[i].maxth;	pmin = links[i].pmin;	pmax = links[i].pmax;	/* Debo's RED approx	links[i].drop = redFn(minth,pmin,maxth,pmax,qlength/links[i].buffer);	qlength = (1-links[i].drop)*links[i].buffer;	links[i].qdelay = qlength/links[i].mu; 	*/		// Ashish's RED approx.	p=(links[i].redrouter)->ComputeProbability(rho, delay);	links[i].drop = p;	qlength = Lq(rho*(1-p), links[i].buffer);	links[i].qdelay = delay;      }    }    //cout << "delay = " << links[i].qdelay << " and drop = " << links[i].drop << endl;  }}void CalcPerFlowDelays(){  for(int i=0; i<nConnections; i++){    double d = 0, p = 1 ;    // Calculate drops and delays    for(int j=0;j<nAdj[i];j++){      d += 2*links[Adj[i][j]].prop + links[Adj[i][j]].qdelay;      p *= 1-links[Adj[i][j]].drop;    }    p = 1-p;    flows[i].no = nAdj[i];    flows[i].delay = d;    flows[i].drop = p;    flows[i].t = flows[i].p_tput;        // p is the end2end drop prob    // If its normal flow, calculate Padhye's stuff    // If its short flow, use our approximations    // Nothing more        if(flows[i].is_sflow){      // If k flows come and each each flow has n packets to       // send then       double t = (flows[i].slambda*flows[i].snopkts);      flows[i].p_tput = t/(1-p);    }    else{      // regular bulk TCP connections, Padhye et. al.      if(!p){	// cout << "Oops, something wrong";      }      flows[i].p_tput = padhye(d,p);    }    //    cout << "connection " << sp << i << sp << d << sp << p;     //cout << sp << flows[i].p_tput << endl;     }}void PrintData(){  for(int i=0;i<nLinks;i++){    cout << i << sp << links[i].lambda << sp << links[i].mu;    cout << sp << links[i].buffer << endl;  }}void PrintResults(){  int i;  for(i=0;i<nLinks;i++){    // cout << i << sp << links[i].qdelay << sp << links[i].drop;    cout << sp << "Qdelay = " << links[i].prop << sp << links[i].lambda;    cout << sp << links[i].drop << endl;  }  for(i=0; i<nConnections; i++){    cout << i << sp << flows[i].delay << sp;    cout << flows[i].drop << sp << flows[i].p_tput << sp;    cout << sp <<  endl;  }}void UpdateHelper(int flag=0){  // if flag = 1 then update only when link is unscaled as of now  // if flag = 0 then do the usual update   int i;  for(i=0; i<nLinks; i++){    links[i].tlambda=0;  }  for(i=0; i<nConnections; i++){    if(!flag || !flows[i].scaled)       for(int j=0;j<nAdj[i];j++)	links[Adj[i][j]].tlambda += flows[i].p_tput;    //    cout << flows[i].p_tput << "\n";  }}void Update(int niter){  UpdateHelper();  for(int i=0; i<nLinks; i++){    links[i].plambda = links[i].lambda;    double t;    double tk=links[i].mu*(1.1);        if(niter){      if(links[i].tlambda>tk)	//t = pow((sqrt(links[i].lambda)+sqrt(links[i].mu+5))/2,2);	t = ((links[i].lambda)+tk)/2;      // t = exp((log(links[i].lambda)+log(links[i].mu+5))/2);      else	//t = pow((sqrt(links[i].tlambda)+sqrt(links[i].lambda))/2,2);	t= ((links[i].tlambda)+(links[i].lambda))/2;      // t = exp((log(links[i].tlambda)+log(links[i].lambda))/2);    }    else t = links[i].tlambda;    links[i].lambda = t; // Update the lambda ..........  }}  void Update2(){  UpdateHelper();  for(int i=0; i<nLinks; i++){    links[i].plambda = links[i].lambda;    links[i].lambda = (links[i].lambda + links[i].tlambda)/2;  }}int allscaled(){  //cout << nConnections;  for(int i=0; i<nConnections; i++)    if(!flows[i].is_sflow && !flows[i].scaled){      cout << "Connection " << i << " not scaled as yet\n";      return 0;    }  //cout << "All are scaled\n";  return 1;}  void Update3(int flag = 0){// flag = 1 means dont touch short flows in your scaling algo  double maxtlambda = -1e7;  int bneck = -1;  int i;  // 1st get set scaled var of all flows to 0  for(i=0; i<nConnections; i++)    flows[i].scaled = 0;  // Calculate the tlambdas  UpdateHelper();  // Find out the link with the max throughput  for(i=0; i<nLinks; i++){    //cout << "after updatehelper link #" << i << " " << links[i].tlambda << "\n";    if(links[i].tlambda>maxtlambda){      bneck = i;      maxtlambda = links[i].tlambda;    }  }  cout << "bottleneck = " << bneck << sp << maxtlambda <<endl;  double tk = links[bneck].mu*(1+links[bneck].drop)+5;   // We cant go above this tk ......  while((maxtlambda > tk + 1) && ! allscaled()){    cout << "Maxtlambda = " << maxtlambda << " bneck = " << bneck << endl;    //    cout << "tk =  "<< tk << " maxlambda = " << maxtlambda << endl;    // Now lets reduce this to tk    assert(bneck>=0 && bneck<=nLinks);    int i;    for(i=0; i<links[bneck].nflows; i++){      // For all the connections passing through this link      int t = links[bneck].theflows[i]; // get a connection id      // Now reduce its p_tput iff its not a short flow      // For short flows we dont do scaling      if(flag){	if(!flows[t].is_sflow && !flows[t].scaled){	  flows[t].p_tput *= (tk)/maxtlambda;	  //cout << "Flow " << t << " getting scaled to  << " << flows[t].p_tput <<" \n";	  flows[t].scaled = 1; // we have scaled this flow already	}      }      else	flows[t].p_tput *= (tk)/maxtlambda;      flows[t].scaled = 1; // we have scaled this flow already     }    for (i=0; i<nLinks;i++){      cout << "Link " << i << " tlambda = " << links[i].tlambda << endl;    }    //Char x =getchar();    // Recalculate the flows' stats    UpdateHelper(0);    // Find out the link with the max throughput    bneck = -1;    maxtlambda = -1e7;    for(i=0; i<nLinks; i++){      if(links[i].tlambda>maxtlambda){	bneck = i;	maxtlambda = links[i].tlambda;      }    }        tk = links[bneck].mu*(1+links[bneck].drop)+5;  }  Update(0);  cout << "Out of the converge loop\n";    for (i =0; i<nLinks;i++){      cout << "Link " << i << " tlambda = " << links[i].tlambda << endl;    }}void newupdate(int niter){  int i;  // 1st init all unscaled tputs and cap  for (i=0;i<nLinks;i++){    links[i].uc = links[i].mu*(1.05);    links[i].utput = 0;  }  // calc all the unscaled tputs and C set all short flows   // to be scaled already   for(i=0; i<nConnections; i++){    if(flows[i].is_sflow)      flows[i].scaled = 1;    else       flows[i].scaled = 0;    for(int j=0;j<nAdj[i];j++){      if(flows[i].is_sflow)	links[Adj[i][j]].uc -= flows[i].p_tput;      else	links[Adj[i][j]].utput += flows[i].p_tput;    }  }  //  for(i=0; i<nLinks; i++ ){  //cout << i << sp << links[i].uc << sp << links[i].utput << endl;  //}  double maxgamma; // most congested link  int bneck;  double t;  bneck = -1;  maxgamma = 0;  for(i=0; i<nLinks; i++){    if(links[i].uc){      t=links[i].utput/links[i].uc;      if(t > maxgamma){	bneck = i;	maxgamma = t;      }    }  }      //cout << bneck << endl;  //char c= getchar();  /*  for(i=0;i<nConnections;i++){    cout << "connection" << sp << i << sp << "-";     for(int j=0;j<nAdj[i];j++){      cout << sp << Adj[i][j];    }    cout << endl;  }  */  // c= getchar();  while(bneck+1){    // cout << "bneck = " << bneck << sp << links[bneck].uc << sp << links[bneck].utput << sp << maxgamma << sp << links[bneck].nflows <<endl;    for(i=0; i<links[bneck].nflows; i++){     // For all the connections passing through this link      int t = links[bneck].theflows[i]; // get a connection id      //     cout << i<< sp << t << sp ;      // Now reduce its p_tput iff its not a short flow      // For short flows we dont do scaling      if(!flows[t].is_sflow && !flows[t].scaled){	flows[t].p_tput /= maxgamma;	//cout << "Flow " << t << " getting scaled to  << " << flows[t].p_tput;	flows[t].scaled = 1; // we have scaled this flow already	for(int j=0;j<nAdj[t];j++){	  // subtract this scaled throughout from all teh links that	  // have this flow. 	  links[Adj[t][j]].uc -= flows[t].p_tput;	  links[Adj[t][j]].utput -= flows[t].p_tput*maxgamma;	  // cout << sp << Adj[i][j];	}	//cout << endl;      }    }        // cout << links[bneck].uc << sp << links[bneck].utput << endl;        links[bneck].uc = 0;    bneck = -1;    maxgamma = 0;    for(i=0; i<nLinks; i++){      if(links[i].uc){	t=links[i].utput/links[i].uc;	if(t > maxgamma){	  bneck = i;	  maxgamma = t;	}      }    }     /*    c = getchar();    for(i=0;i<nConnections;i++){      cout << "connection" << sp << i << sp << "-";       for(int j=0;j<nAdj[i];j++){	cout << sp << Adj[i][j];      }      cout << endl;      }*/        // c=getchar();  }  Update(niter);}  asim(){    // cout << "Reached here\n";  }  void recv(Packet *, Handler * = 0){}};static class AsimClass : public TclClass {public:  AsimClass(): TclClass("Asim"){ }  TclObject * create(int, const char*const*) {    return (new asim());  }} class_asim;/*int main(int argc, char **argv) {  int niter = 0;  asim sim;  sim.GetInputs(argc, argv);  //  PrintResults();  cout << "Read the input .... \n";  for(int i=0; i<10; i++){    sim.CalcLinkDelays(1);    cout << "Calculated link delays ... \n";    //    PrintResults();    sim.CalcPerFlowDelays();    cout << "Calculated per flow delays ... \n";    cout << " ------------------------------\n";    sim.newupdate(niter);    //sim.PrintResults();    cout << " ------------------------------\n";   }  sim.PrintResults();}*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?