refixed previous commitprintf
[charm.git] / examples / pose / HiSim / Main / Nic.C
1 #include "BgSim_sim.h"
2 #include "TCsim_sim.h"
3 #include "math.h" 
4
5 extern void initializeNetwork(Topology **,RoutingAlgorithm **,InputVcSelection **,OutputVcSelection **); 
6 extern void initializeNetwork(Topology **,RoutingAlgorithm **); 
7
8 NetInterface::NetInterface(NetInterfaceMsg *niMsg) { 
9    Packet *p; 
10    nicConsts = new NicConsts;
11    nicConsts->id = niMsg->id;  nicConsts->numP = niMsg->numP;
12    nicConsts->startId = niMsg->startId;
13
14    numRecvd = 0; prevIntervalStart = 0; counter = 0; roundRobin = 0;
15    initializeNetwork(&topology,&routingAlgorithm);
16    int delay = 0,size;
17    if(config.loadRoutingTable) { 
18         if((nicConsts->id-config.nicStart) == SUBNET_MANAGER) {
19         for(int i=0;i<config.numSwitches;i++) {
20                 p = new Packet; CkAssert(p != NULL);
21                 p->hdr.controlInfo = CONTROL_PACKET;
22                 p->hdr.routeInfo.dst = routingAlgorithm->getNextSwitch(i); 
23                 p->hdr.routeInfo.datalen = 0;
24                 p->hdr.portId = topology->getStartPort(nicConsts->id-config.nicStart,nicConsts->numP);
25                 p->hdr.vcid = roundRobin; roundRobin = (roundRobin+1)%config.switchVc;
26                 p->hdr.prevId = nicConsts->id; p->hdr.hop = 0;
27                 p->hdr.prev_vcid = -1; p->hdr.src = p->hdr.prev_src = SUBNET_MANAGER;
28                 p->hdr.nextId = topology->getStartSwitch(nicConsts->id-config.nicStart);
29
30                 routingAlgorithm->sourceToSwitchRoutes(p,nicConsts->numP);
31                 routingAlgorithm->loadTable(p,nicConsts->numP);
32                 POSE_invoke(recvPacket(p),Switch,nicConsts->startId,delay);
33                 elapse(sizeof(char)*config.numNodes);
34         }
35         } else
36         elapse((config.numSwitches+1)*sizeof(char)*config.numNodes);
37    }    
38 }
39
40 // Packetize message and pump it out
41 void NetInterface::recvMsg(NicMsg *nic) {
42         POSE_TimeType delay=0,inputPort,msgLenRest = nic->totalLen,packetnum  = 0,curlen,initPort,initVc;
43         Packet *p;
44         NicMsg *newNic = new NicMsg; *newNic = *nic;
45
46         POSE_invoke(storeMsgInAdvance(newNic),NetInterface,nic->routeInfo.dst+config.nicStart,0);
47
48 //      parent->CommitPrintf("-%d %d %d %d\n",nic->src,nic->msgId,nic->routeInfo.dst,nic->totalLen);
49         while(msgLenRest > 0) {
50                 p = new Packet;
51                 p->hdr = *nic;
52                 curlen  = minP(config.maxpacksize,msgLenRest);
53                 p->hdr.routeInfo.datalen = curlen;
54                 p->hdr.pktId = packetnum ++;
55                 p->hdr.portId = topology->getStartPort(nicConsts->id-config.nicStart,nicConsts->numP);
56                 p->hdr.vcid = roundRobin; roundRobin = (roundRobin+1)%config.switchVc;
57                 p->hdr.prevId = nicConsts->id; p->hdr.hop = 0;
58                 p->hdr.prev_vcid = -1; p->hdr.prev_src = p->hdr.src;
59                 p->hdr.nextId = topology->getStartSwitch(nicConsts->id-config.nicStart);
60                 p->hdr.controlInfo = DATA_PACKET;
61                 if(config.sourceRouting) {
62                 routingAlgorithm->populateRoutes(p,nicConsts->numP); // Should do it just once later per message
63                 } 
64
65                 POSE_invoke(recvPacket(p),Switch,nicConsts->startId,delay);
66                 delay += ((POSE_TimeType)(curlen/config.switchC_BW));  msgLenRest -= curlen; 
67         }
68 elapse((POSE_TimeType)(nic->totalLen/config.switchC_BW));
69 }
70
71
72 // Store part of message having higher level protocol directly to destination
73 void NetInterface::storeMsgInAdvance(NicMsg *m) {
74         MsgStore ms; ms = *m;
75         remoteMsgId rmid(m->msgId,m->src);
76         storeBuf[rmid] = ms;
77 //      CkPrintf("%d Stored src %d msgid %d\n",ovt,m->src,m->msgId);
78 }
79
80 // Receive packet by packet and finally send message to node
81
82 void NetInterface::recvPacket(Packet *p) {
83         POSE_TimeType tmp,expected,extra,remlen; TaskMsg *tm; TransMsg *tr;Position src; MsgStore ms;
84         int hops;
85         remoteMsgId rmid(p->hdr.msgId,p->hdr.src);
86         map<remoteMsgId,int>::iterator it2 = pktMap.find(rmid);
87
88         if(p->hdr.routeInfo.dst != (nicConsts->id-config.nicStart)) {
89 //              CkPrintf("Current node %d \n",nicConsts->id-config.nicStart); p->hdr.dump();
90         }
91
92 //      CkPrintf("NIC id is %d dst is %d \n",nicConsts->id,p->hdr.routeInfo.dst);
93         if(p->hdr.routeInfo.dst != (nicConsts->id-config.nicStart)) {
94                 parent->CommitPrintf("%d I am %d But src %d dst %d\n",
95                 ovt,nicConsts->id-config.nicStart,p->hdr.src,p->hdr.routeInfo.dst);
96                 parent->CommitError("Packet misrouted to destination\n");
97                 return;
98         }
99
100         numRecvd+= p->hdr.routeInfo.datalen;
101         if(it2 == pktMap.end())  {
102                 remlen = p->hdr.totalLen - (p->hdr.routeInfo.datalen); pktMap[rmid] = remlen;
103         } else {
104                 pktMap[rmid] -= p->hdr.routeInfo.datalen;
105         }
106
107         if(!pktMap[rmid])  {
108         map<remoteMsgId,MsgStore>::iterator it1 = storeBuf.find(rmid);
109         if(it1 == storeBuf.end()) {CkPrintf("%d Something wrong src %d dst %d msgid %d \n",
110         ovt,p->hdr.src,p->hdr.routeInfo.dst,p->hdr.msgId);
111         parent->CommitError("message was not stored in advance");
112         return;}
113
114         ms = storeBuf[rmid];
115 //      CkPrintf("id %d size %d time %d src %d dst %d msgid %d index %d recvTime %d totalLen %d
116 //      destNodecode %d destTID %d\n", nicConsts->id-config.nicStart,storeBuf.size(),ovt,
117 //      ms.src,p->hdr.routeInfo.dst,ms.msgId,ms.index,ms.recvTime,ms.totalLen,ms.destNodeCode,ms.destTID);
118
119         if(config.msgstats_on) {
120         extra =
121         routingAlgorithm->expectedTime(p->hdr.src,nicConsts->id-config.nicStart,ovt,ms.origovt,p->hdr.totalLen,&hops);
122         POSE_TimeType curInterval,tmp=prevIntervalStart;
123         if(config.collection_interval != 0) {
124         curInterval = ovt/config.collection_interval;
125         if((curInterval > prevIntervalStart) && (numRecvd)) {
126         prevIntervalStart = curInterval;
127         parent->CommitPrintf("%d*%d %d %lld %.2f\n",nicConsts->id-config.nicStart,numRecvd, hops,prevIntervalStart,(float)counter/numRecvd);
128         counter=0; numRecvd = 0;
129         }
130         }
131         counter += ((POSE_TimeType)(100.0 * extra/(ovt-ms.origovt))) * (p->hdr.totalLen); 
132         }
133
134         pktMap.erase(rmid);
135         storeBuf.erase(rmid);
136
137 //      parent->CommitPrintf("%d %d %d %d\n",ms.src,ms.msgId,nicConsts->id-config.nicStart,ovt);
138         if(config.use_transceiver) {
139                 tr = new TransMsg(ms.src,ms.msgId,(nicConsts->id-config.nicStart)); 
140                 // Be careful. Making assumption that nodeStart == 0
141                 POSE_invoke(recvMessage(tr),Transceiver,nicConsts->id-config.nicStart,0); 
142         } else {        
143                 tm = new TaskMsg(ms.src,ms.msgId,ms.index,ms.recvTime,ms.totalLen,
144                (nicConsts->id-config.nicStart),ms.destNodeCode,ms.destTID);
145                 POSE_invoke(recvIncomingMsg(tm), BGnode, config.origNodes + p->hdr.routeInfo.dst, 0);
146         }
147         }
148 }