📄 switching.c
字号:
index1=0; index2=0; index=0; for(itter = 0;itter < maxTimeSlot; itter++) { for(i=0;i<noOfPorts;i++) outSend[i]=-1; //perticluar i th output port is not used. /*Packet Generation */ for(inputPort = 0;inputPort < noOfPorts; inputPort++) { /*Every time packet is created but its state is CREATED or NOTCREATED i.e. if created then process otherwise dont and depends on the probability of the generation i.e. random value created..*/ /* IF(CREATED) packet[index1].stateCreated=true ELSE packet[index1].stateCreated=false*/ packetGenerateRandom = (rand()%100)*0.01; if(packetGenerateRandom < 0) packetGenerateRandom = -packetGenerateRandom; if(packetGenerateRandom < packetGenProb) packet[index1].stateCreated = true; else packet[index1].stateCreated = false; //printf("\npacket Generate :%f",packetGenerateRandom); packet[index1].startDelay = 0; packet[index1].endDelay = 0; packet[index1].delay = 0; packet[index1].startTime = itter+((rand())%101)*0.0001; packet[index1].slotNo = itter; packet[index1].inputPort = inputPort; packet[index1].outputPort = rand()%noOfPorts; packet[index1].discarded = false; if(packet[index1].outputPort < 0) packet[index1].outputPort = -packet[index1].outputPort; //printf("\nStart time: %f Output Port:%d Input port:%d\n",packet[index1].startTime,packet[index1].outputPort,packet[index1].inputPort); if(packet[index1].stateCreated == true) { enInputQueue(packet[index1].inputPort,index1,bufferSize); packet[index1].startDelay=nowTime(); } index1++; } for(outputPort = 0;outputPort < noOfPorts; outputPort++) initializeOutputQueue(outputPort); /*Packet select for Output port*/ for(outputPort = 0;outputPort < noOfPorts; outputPort++) { if(inputQ[outputPort].rear != inputQ[outputPort].front) { for(count=inputQ[outputPort].front; count < inputQ[outputPort].rear; count++) { if(count == depth) { break; } tempindex = inputQ[outputPort].packetSlot[count]; if(outSend[packet[tempindex].outputPort] == -1 && packet[tempindex].stateCreated == true) { //printf("Packet at Output port :%d input port :%d",packet[tempindex].outputPort,packet[tempindex].inputPort); retValue=deInputQueue(packet[tempindex].inputPort); outSend[packet[tempindex].outputPort] = packet[tempindex].inputPort; enOutputQueue(packet[tempindex].outputPort,tempindex,1); //bufferLength is 1 as only 1 output buffer. deOutputQueue(packet[tempindex].outputPort); //transmit that packet for that slot.... /*calculating the time delay between recieved and transmitted packet*/ linkUtilization[packet[tempindex].inputPort].activeLink[packet[tempindex].outputPort]+=1; packet[tempindex].endDelay = nowTime(); packet[tempindex].delay = (packet[tempindex].endDelay-packet[tempindex].startDelay); //printf("\nDelay is in microsec:%d\n\n",packet[tempindex].delay); } //if(packet[tempindex].endDelay != 0) if(packet[tempindex].delay > 0 ) { intermediateVal1[index++]=packet[tempindex].delay; tempDelay += packet[tempindex].delay; } } } index2++; } //printf("\n-------------------------------------------------------------------------------\n"); } averageDelay = tempDelay*10/(noOfPorts*maxTimeSlot); for (count=0;count<index;count++) devIntermediate+=(intermediateVal1[count]-averageDelay)*(intermediateVal1[count]-averageDelay); devIntermediate=devIntermediate*1.0/(noOfPorts*maxTimeSlot); stdDevDelay=sqrt(devIntermediate); indexcount=0; /*Average link utilization*/ for(inputPort = 0;inputPort < noOfPorts; inputPort++) { for(outputPort = 0;outputPort < noOfPorts; outputPort++) { linkUtilization[inputPort].averageLink[outputPort] = linkUtilization[inputPort].activeLink[outputPort]*1.0/maxTimeSlot; //printf("link utilization for the link %d %d is %f\n",inputPort,outputPort,linkUtilization[inputPort].averageLink[outputPort]); intermediateVal2[indexcount++]=linkUtilization[inputPort].averageLink[outputPort]; tempLinkUtilization += linkUtilization[inputPort].averageLink[outputPort]; } } tempLinkUtilization *= 100; averageLinkUtilization=tempLinkUtilization/noOfPorts*1.0; for (count=0;count<indexcount;count++) devIntermediate+=(intermediateVal2[count]-averageLinkUtilization)*(intermediateVal2[count]-averageLinkUtilization); devIntermediate=devIntermediate*1.0/(noOfPorts*maxTimeSlot); stdDevLinkUtilization=sqrt(devIntermediate); printf("\n\nAVERAGE LINK UTILIZATION FOR NOQ IS: %f\nSTANDERED DEVIATION: %f \nSWITCH UTILIZATION: %f\n",tempLinkUtilization/(noOfPorts*1.0),stdDevLinkUtilization,tempLinkUtilization); /*Printing in a standered ouput*/ printf("\nN p Q Type Avg PD Std Dev of PD Avg LU Std Dev of LU Switch Util"); printf("\n%d %f %s %fus %f %f %f %f\n",noOfPorts,packetGenProb,queue,averageDelay,stdDevDelay,tempLinkUtilization/(noOfPorts*1.0),stdDevLinkUtilization,tempLinkUtilization); /*Printing in a file*/ fprintf(of,"\n%d %f %s %fus %f %f %f %f\n",noOfPorts,packetGenProb,queue,averageDelay,stdDevDelay,tempLinkUtilization/(noOfPorts*1.0),stdDevLinkUtilization,tempLinkUtilization); fprintf(graphFile5,"%d %f\n",noOfPorts,averageDelay); fprintf(graphFile6,"%d %f\n",noOfPorts,tempLinkUtilization); } break;/****************************************************************************************** Logic for Output Queueing - OUQ *******************************************************************************************/ case 3 : for(noOfPorts=4;noOfPorts<101;noOfPorts++) { for(outputPort = 0;outputPort < noOfPorts; outputPort++) initializeOutputQueue(outputPort); for(N1=0;N1<noOfPorts;N1++) for(N2=0;N2<noOfPorts;N2++) { linkUtilization[N1].activeLink[N2]=0; linkUtilization[N1].averageLink[N2]=0.0; } tempDelay=0; tempLinkUtilization=0; devIntermediate=0; index1=0; index2=0; index=0; for(itter = 0;itter < maxTimeSlot;itter++) { /*Packet Generation */ for(inputPort = 0;inputPort < noOfPorts; inputPort++) { /*Every time packet is created but its state is CREATED or NOTCREATED i.e. if created then process otherwise dont and depends on the probability of the generation i.e. random value created..*/ /* IF(CREATED) packet[index1].stateCreated=true ELSE packet[index1].stateCreated=false*/ packetGenerateRandom = (rand()%100)*0.01; if(packetGenerateRandom < 0) packetGenerateRandom = -packetGenerateRandom; if(packetGenerateRandom < packetGenProb) packet[index1].stateCreated = true; else packet[index1].stateCreated = false; //printf("\npacket Generate :%f",packetGenerateRandom); packet[index1].startDelay = 0.0; packet[index1].endDelay = 0.0; packet[index1].delay = 0.0; packet[index1].startTime = itter+((rand())%101)*0.0001; packet[index1].slotNo = itter; packet[index1].inputPort = inputPort; packet[index1].outputPort = rand()%noOfPorts; packet[index1].discarded = false; if(packet[index1].outputPort < 0) packet[index1].outputPort = -packet[index1].outputPort; //printf("\nStart time: %f Output Port:%d Input port:%d\n",packet[index1].startTime,packet[index1].outputPort,packet [index1].inputPort); packet[index1].startDelay = nowTime(); index1++; } sortAllPackets(index1,noOfPorts); //i.e. to sort all input packets depending on startTime and then enQueue depending on inputport tempindex = index2; /*Packet select for Output port or discard*/ for(outputPort = 0;outputPort < noOfPorts; outputPort++) { if(packet[index2].stateCreated == true) { //printf("\nindex :%d",index2); for(inputPort = 0;inputPort < noOfPorts; inputPort++) { if(indexVec[inputPort] == index2) { enOutputQueue(packet[indexVec[inputPort]].outputPort,indexVec[inputPort],bufferSize); packet[indexVec[inputPort]].endDelay = nowTime(); packet[indexVec[inputPort]].delay = (packet[indexVec[inputPort]].endDelay-packet[indexVec[inputPort]].startDelay)/noOfPorts; //retValue=deOutputQueue(packet[indexVec[inputPort]].outputPort); //printf("\nretValue:%d",retValue); //printf("\nQueing :at %d queue, index: %d:\n",packet[indexVec[inputPort]].outputPort,indexVec[inputPort]%noOfPorts); } } } //linkUtilization[packet[index2].inputPort].activeLink[packet[tempindex].outputPort]+=1; index2++; } for(outputPort = 0;outputPort < noOfPorts; outputPort++) { retValue = deOutputQueue(outputPort); //i.e. only one packet is deleted but other are there in the O/P queue still waiting for transmission if(retValue != -1) { //printf("\nRETVALUE>>>>> %d ",retValue); linkUtilization[packet[retValue].inputPort].activeLink[packet[retValue].outputPort]+=1; //printf("Delay is in microsec:%d\n\n",packet[retValue].delay); } if(packet[retValue].endDelay != 0) if(packet[retValue].delay > 0) { intermediateVal1[index++]=packet[retValue].delay; tempDelay += packet[retValue].delay; } tempindex++; } //printf("\n-------------------------------------------------------------------------------\n"); } averageDelay = tempDelay/(noOfPorts*maxTimeSlot); for (count=0;count<index;count++) devIntermediate+=(intermediateVal1[count]-averageDelay)*(intermediateVal1[count]-averageDelay); devIntermediate=devIntermediate*1.0/(noOfPorts*maxTimeSlot); stdDevDelay=sqrt(devIntermediate); //printf("\n\nAVERAGE DELAY FOR OUQ IS:%f Std deviation: %f us\n",averageDelay,stdDevDelay); indexcount=0; /*Average link utilization*/ for(inputPort = 0;inputPort < noOfPorts; inputPort++) { for(outputPort = 0;outputPort < noOfPorts; outputPort++) { linkUtilization[inputPort].averageLink[outputPort] = linkUtilization[inputPort].activeLink[outputPort]/(maxTimeSlot*1.0); //printf("link utilization for the link %d %d is %f\n",inputPort,outputPort,linkUtilization[inputPort].averageLink[outputPort]); intermediateVal2[indexcount++]=linkUtilization[inputPort].averageLink[outputPort]; tempLinkUtilization += linkUtilization[inputPort].averageLink[outputPort]; } } averageLinkUtilization=tempLinkUtilization/(noOfPorts*1.0); for (count=0;count<indexcount;count++) devIntermediate+=(intermediateVal2[count]-averageLinkUtilization)*(intermediateVal2[count]-averageLinkUtilization); devIntermediate=devIntermediate*1.0/(noOfPorts*maxTimeSlot); stdDevLinkUtilization=sqrt(devIntermediate); //printf("\n\nAVERAGE LINK UTILIZATION FOR NOQ IS: %f\nSTANDERED DEVIATION: %f \nSWITCH UTILIZATION: %f\n",tempLinkUtilization/ (noOfPorts*1.0),stdDevLinkUtilization,tempLinkUtilization); /*Printing in a standered ouput*/ printf("\nN p Q Type Avg PD Std Dev of PD Avg LU Std Dev of LU Switch Util"); printf("\n%d %f %s %fus %f %f %f %f\n",noOfPorts,packetGenProb,queue,averageDelay,stdDevDelay,tempLinkUtilization/(noOfPorts*1.0),stdDevLinkUtilization,tempLinkUtilization); /*Printing in a file*/ fprintf(of,"\n%d %f %s %fus %f %f %f %f\n",noOfPorts,packetGenProb,queue,averageDelay,stdDevDelay,tempLinkUtilization/(noOfPorts*1.0),stdDevLinkUtilization,tempLinkUtilization); fprintf(graphFile7,"%d %f\n",noOfPorts,averageDelay); fprintf(graphFile8,"%d %f\n",noOfPorts,tempLinkUtilization); } break;} /*Closing Files*/ fclose(graphFile1);fclose(graphFile2);fclose(graphFile3);fclose(graphFile4);fclose(graphFile5);fclose(graphFile6);fclose(graphFile7);fclose(graphFile8);fclose(of); return 0;} /*Compiling and running*/// gcc switching.c -lm -o switch//./switch -N 8 -B 1024 -L 2 -p 0.8 -queue NOQ -out outputfile.txt -T 10000 /*For Graph*/// xgraph NOQ-AvgpktdlyVsNoofports.graph INQ-AvgpktdlyVsNoofports.graph INI-AvgpktdlyVsNoofports.graph OUQ-AvgpktdlyVsNoofports.graph// xgraph NOQ-SwitchutilizationVsNoofports.graph INQ-SwitchutilizationVsNoofports.graph INI-SwitchutilizationVsNoofports.graph OUQ-SwitchutilizationVsNoofports.graph
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -