-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.cc
291 lines (224 loc) · 8.23 KB
/
processor.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* processor.cc
*
* Created on: 2017年1月16日
* Author: Vincent
*
* 功能:
* 1. 发送和接收packet,一个packet对应一个flit
* 2. 不采用流控模型,每间隔相应时间直接发送数据
*
*/
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
#include "fat_tree_m.h"
#include "fat_tree.h"
using namespace omnetpp;
// Processor
class Processor : public cSimpleModule
{
private:
cMessage *selfMsgGenMsg; //产生flit定时,package产生按泊松分布或均匀分布
cMessage *selfMsgSendMsg; //发送flit定时,每周期都检查buffer,再发送
long numFlitSent;
long numFlitReceived;
//long numDropped;
//long flitByHop; //用于计算链路利用率
cOutVector hopCountVector;
cOutVector flitDelayTime;
FatTreeMsg* OutBuffer[ProcessorBufferDepth]; //用于存放flit,输出flit的缓存
public:
Processor();
virtual ~Processor();
protected:
virtual FatTreeMsg *generateMessage();
virtual void forwardMessage(FatTreeMsg *msg);
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
virtual int ppid2plid(int ppid);
virtual int plid2ppid(int plid);
virtual int getNextRouterPortP(); //计算与processor相连的router的端口
virtual double Poisson();
// The finish() function is called by OMNeT++ at the end of the simulation:
virtual void finish() override;
};
Define_Module(Processor);
Processor::Processor(){
selfMsgGenMsg=nullptr;
selfMsgSendMsg=nullptr;
}
Processor::~Processor(){
cancelAndDelete(selfMsgGenMsg);
cancelAndDelete(selfMsgSendMsg);
}
void Processor::initialize()
{
// Initialize variables
numFlitSent = 0;
numFlitReceived = 0;
//numDropped = 0;
//flitByHop = 0;
hopCountVector.setName("hopCount");
flitDelayTime.setName("flitDelayTime");
selfMsgSendMsg = new cMessage("selfMsgSendMsg");//注意顺序,先发送buffer里面的msg,再产生新的msg,这样一个flit需要2个周期才会发出去
scheduleAt(Sim_Start_Time, selfMsgSendMsg);
selfMsgGenMsg = new cMessage("selfMsgGenMsg");
scheduleAt(Sim_Start_Time, selfMsgGenMsg);
for(int i = 0; i < ProcessorBufferDepth; i++)
OutBuffer[i] = nullptr;
}
void Processor::handleMessage(cMessage *msg)
{
if (msg->isSelfMessage()) {
//********************发送新数据的自定时消息********************
if(msg == selfMsgSendMsg){
//****************************转发flit**************************
if(OutBuffer[0] != nullptr){
FatTreeMsg* current_forward_msg = OutBuffer[0];
forwardMessage(current_forward_msg);
numFlitSent++;
for(int i=0; i < ProcessorBufferDepth - 1; i++){
OutBuffer[i] = OutBuffer[i+1];
}
OutBuffer[ProcessorBufferDepth-1] = nullptr;
}
scheduleAt(simTime() + ProcessorForwardGap * CLK_CYCLE, selfMsgSendMsg);
}else if(msg == selfMsgGenMsg){
//if(getIndex() == 0){ //processor产生msg的模式,需要改进
if (true) {
//**********************产生flit*****************************
for(int i = 0; i < ProcessorBufferDepth; i++){
if(OutBuffer[i] == nullptr){
FatTreeMsg *newmsg = generateMessage();
OutBuffer[i] = newmsg;
break;
}
}
//**********************产生定时消息*****************************
//package之间的时间间隔为泊松分布或自相似分布,同一个package的flit之间间隔为CLK_CYCLE
#if defined POISSON_DIST //泊松分布
double expTime = Poisson();
if (Verbose >= VERBOSE_DETAIL_DEBUG_MESSAGES) {
EV << "Poisson interval: "<<expTime<<"\n";
}
scheduleAt(simTime() + expTime, selfMsgGenMsg);
#else //均匀分布
int gap = (int) 1.0 / UniformInjectionRate;
scheduleAt(simTime() + gap * CLK_CYCLE, selfMsgGenMsg);
#endif
}
}
}else{
//************************非self message*********************
//***********************收到FatTreeMsg消息*******************
FatTreeMsg *ftmsg = check_and_cast<FatTreeMsg *>(msg);
// Message arrived
int current_ppid = getIndex();
int hopcount = ftmsg->getHopCount();
if (Verbose >= VERBOSE_DEBUG_MESSAGES) {
EV << ">>>>>>>>>>Message {" << ftmsg << " } arrived after " << hopcount <<
" hops at node "<<current_ppid<<"("<<ppid2plid(current_ppid)<<")<<<<<<<<<<\n";
}
// update statistics.
numFlitReceived++;
//flitByHop += hopcount + 1; //包含最后一跳路由器到processor
flitDelayTime.record(simTime().dbl() - ftmsg->getFlitGenTime());
hopCountVector.record(hopcount);
delete ftmsg;
}
}
FatTreeMsg* Processor::generateMessage()
{
// Produce source and destination address
int current_ppid = getIndex();
int n = getVectorSize();//processor的数量
#ifdef UNIFORM //均匀分布
int dst_ppid = intuniform(0, n-2); //均匀流量模型
if (dst_ppid >= current_ppid)
dst_ppid++;//保证不取到current_ppid
#endif
int current_plid = ppid2plid(current_ppid);
int dst_plid = ppid2plid(dst_ppid);
char msgname[200];//初始分配的空间太小导致数据被改变!!!!!!!
sprintf(msgname, "Flit, From processor node %d(%d) to node %d(%d)", current_ppid,current_plid,dst_ppid,dst_plid);
// Create message object and set source and destination field.
FatTreeMsg *msg = new FatTreeMsg(msgname);
msg->setSrc_ppid(current_ppid); //设置发出的processor编号
msg->setDst_ppid(dst_ppid); //设置接收的processor编号
msg->setFrom_router_port(getNextRouterPortP()); //设置收到该msg的Router端口
msg->setFlitGenTime(simTime().dbl());
if (Verbose >= VERBOSE_DEBUG_MESSAGES) {
EV << "<<<<<<<<<<Processor: "<<getIndex()<<"("<<ppid2plid(getIndex())<<") is generating flit>>>>>>>>>>\n";
EV << msg << endl;
}
return msg;
}
//processor转发的路由算法,processor只有一个port,直接转发出去即可
void Processor::forwardMessage(FatTreeMsg *msg)
{
msg->setFrom_router_port(getNextRouterPortP());// 设置收到该信息路由器的端口号
send(msg,"port$o");
if (Verbose >= VERBOSE_DEBUG_MESSAGES) {
EV << "PROCESSOR: "<<getIndex()<<"("<<ppid2plid(getIndex())<<") << Forwarding message >> { " << msg << " } from processor to router\n";
}
}
int Processor::getNextRouterPortP(){
int current_ppid=getIndex();
int plid=ppid2plid(current_ppid);
int port=plid%100;
return port; //返回和processor相连的router的端口
}
//从ppid计算plid
int Processor::ppid2plid(int ppid){
int idtmp = ppid;
int idfinal = 0;
int mul = 1;
for(int i = 0; i < LevelNum - 1; i++){
idfinal = idfinal + idtmp % (PortNum / 2)*mul;
mul = mul * 100;
idtmp = (int) (idtmp / (PortNum / 2));
}
idfinal = idfinal + idtmp * mul;
return idfinal;
}
//从plid计算ppid
int Processor::plid2ppid(int plid){
int tmp = plid;
int mul = 1;
int IDtmp = 0;
for(int i = 0; i < LevelNum; i++){
IDtmp = IDtmp + mul * (tmp % 100);
mul = mul * (PortNum / 2);
tmp = tmp / 100;
}
return IDtmp;
}
double Processor::Poisson() {
//E(x) = 1 / lambda
double exp_time = exponential((double)1/LAMBDA);
exp_time = round(exp_time / TimeScale) * CLK_CYCLE;
if(exp_time < CLK_CYCLE) {
exp_time = CLK_CYCLE;
}
return exp_time;
}
void Processor::finish()
{
// This function is called by OMNeT++ at the end of the simulation.
EV << "Flit Sent: " << numFlitSent << endl;
EV << "Flit Received: " << numFlitReceived << endl;
//EV << "Dropped: " << numDropped << endl;
recordScalar("flitSent", numFlitSent);
recordScalar("flitReceived", numFlitReceived);
//recordScalar("#flit dropped", numDropped);
//recordScalar("#flitByHop", flitByHop);
if(getIndex() == 0) {
double timeSpend = (simTime().dbl() - Sim_Start_Time);
double clockCycle = CLK_CYCLE;
double timeCount = (simTime().dbl() - Sim_Start_Time) / (CLK_CYCLE); //注意,文本替换
recordScalar("timeSpend", timeSpend);
recordScalar("timeCount", timeCount);
recordScalar("clockCycle", CLK_CYCLE);
}
}