-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxWeightLib.c
255 lines (245 loc) · 4.72 KB
/
maxWeightLib.c
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
#ifdef DEBUG
# define dbg(x) printf("DEBUG: %s",x);
#else
# define dbg(x) {} //NO DEBUG
#endif
#include <stdio.h>
#include <stdlib.h>
#include "my402list.h"
#include "maxWeight.h"
//Initialise all queues
void InitQueues(My402List *Q)
{
int i,j;
for (i = 0; i < numPort; i++)
{
for (j = 0; j < numPort; j++)
{
if (!My402ListInit( &Q[i * numPort + j] ) )
{
fprintf(stderr,"Error initialisind queue for input port %d and dest port %d\n",i,j);
exit(1);
}
else
{
sprintf(dbgMsg,"Initilised Queue: Input Port:%d Destination Port:%d\n",i,j);
// dbg(dbgMsg);
}
}
}
}
//It is Bernouli function with probability of returning 1 (packet arriving) as load
int packetArrives(double load)
{
double t1 = rand() % 10;
double t2 = 10*load;
if ( t1 <= t2 )
{
return(1);
}
else
{
return(-1);
}
}
//It wil return the destination port equaly likely
//**Works only for 2 port. It will return either 0 or 1 with equal probabilities
int destPort(double p0)
{
//printf("pirobability:%g\n",p0);
if (rand() % 10 < p0*10)
return(0);
else
return(1);
}
//Will fill the 2D array of weights "w" with the queue lengths
void getWeight(int *w,My402List *Q)
{
int i,j;
for (i = 0; i < numPort; i++)
{
for (j =0; j < numPort; j++)
{
*(w + (i*numPort) + j) = My402ListLength(&Q[i * numPort + j]);
}
}
}
//Will Calculate the max weight graph and fill the toDequeue array.
void maxWeight(int *w,int *toDequeue)
{
//int i,j;
//The algorithm for max weight matching graph should come here.
int tmp = 0;
int r = 0;
if ( *w + *(w+3) > *(w+1) + *(w+2) )
{
*toDequeue = 1; //toDequeue[0][0] = 1;
*(toDequeue+1) = 0; //toDequeue[0][1] = 0;
*(toDequeue+2) = 0; //toDequeue[1][0] = 0;
*(toDequeue+3) = 1; //toDequeue[1][1] = 1;
}
else if ( *w + *(w+3) < *(w+1) + *(w+2) )
{
*toDequeue = 0; //toDequeue[0][0] = 0;
*(toDequeue+1) = 1; //toDequeue[0][1] = 1;
*(toDequeue+2) = 1; //toDequeue[1][0] = 1;
*(toDequeue+3) = 0; //toDequeue[1][1] = 0;
}
else if ( *w + *(w+3) == *(w+1) + *(w+2) )
{
r = rand();
tmp = r % 2;
//printf("r:%d tmp:%d\n",r,tmp);
if ( 0 == tmp )
{
printf("##\n");
*toDequeue = 1; //toDequeue[0][0] = 1;
*(toDequeue+1) = 0; //toDequeue[0][1] = 0;
*(toDequeue+2) = 0; //toDequeue[1][0] = 0;
*(toDequeue+3) = 1; //toDequeue[1][1] = 1;
type1++;
}
else
{
printf("$$\n");
*toDequeue = 0; //toDequeue[0][0] = 0;
*(toDequeue+1) = 1; //toDequeue[0][1] = 1;
*(toDequeue+2) = 1; //toDequeue[1][0] = 1;
*(toDequeue+3) = 0; //toDequeue[1][1] = 0;
type2++;
}
}
}
//Parse the config file and populate the load and probabilities of destinations
void parseConfig()
{
FILE *fp;
char buf[1024];
char *ptr,*bufPtr;
if (NULL == (fp = fopen("config","r")))
{
fprintf(stderr,"Could not open config file\n");
exit(1);
}
//Get simTime
if (NULL != fgets(buf, 100,fp))
{
bufPtr = buf;
while (*bufPtr != ':')
{
(bufPtr)++;
}
bufPtr++;
ptr = bufPtr;
while (*ptr != '\n')
ptr++;
*ptr = '\0';
simTime = atof(bufPtr);
sprintf(dbgMsg,"simTime:%d(int)\n",simTime);
dbg(dbgMsg);
}
//Get load[0]
if (NULL != fgets(buf, 100,fp))
{
bufPtr = buf;
while (*bufPtr != ':')
{
(bufPtr)++;
}
bufPtr++;
ptr = bufPtr;
while (*ptr != '\n')
ptr++;
*ptr = '\0';
load[0] = atof(bufPtr);
sprintf(dbgMsg,"Load0:%f (double)\n",load[0]);
dbg(dbgMsg);
}
//Get probability to destination port
if (NULL != fgets(buf, 100,fp))
{
bufPtr = buf;
while (*bufPtr != ':')
{
(bufPtr)++;
}
bufPtr++;
ptr = bufPtr;
while (*ptr != ' ')
{
ptr++;
}
*ptr = '\0';
p[0][0] = atof(bufPtr);
sprintf(dbgMsg,"P00:%g (double)\n",p[0][0]);
dbg(dbgMsg);
ptr++;
bufPtr = ptr;
while (*bufPtr != ':')
{
(bufPtr)++;
}
bufPtr++;
ptr = bufPtr;
while (*ptr != '\n')
{
ptr++;
}
*ptr = '\0';
p[0][1] = atof(bufPtr);
sprintf(dbgMsg,"P01:%g (double)\n",p[0][1]);
dbg(dbgMsg);
}
//Get load[1]
if (NULL != fgets(buf, 100,fp))
{
bufPtr = buf;
while (*bufPtr != ':')
{
(bufPtr)++;
}
bufPtr++;
ptr = bufPtr;
while (*ptr != '\n')
ptr++;
*ptr = '\0';
load[1] = atof(bufPtr);
sprintf(dbgMsg,"Load0:%f (double)\n",load[1]);
dbg(dbgMsg);
}
//Get probability to destination port
if (NULL != fgets(buf, 100,fp))
{
bufPtr = buf;
while (*bufPtr != ':')
{
(bufPtr)++;
}
bufPtr++;
ptr = bufPtr;
while (*ptr != ' ')
{
ptr++;
}
*ptr = '\0';
p[1][0] = atof(bufPtr);
sprintf(dbgMsg,"P10:%g (double)\n",p[1][0]);
dbg(dbgMsg);
ptr++;
bufPtr = ptr;
while (*bufPtr != ':')
{
(bufPtr)++;
}
bufPtr++;
ptr = bufPtr;
while (*ptr != '\n')
{
ptr++;
}
*ptr = '\0';
p[1][1] = atof(bufPtr);
sprintf(dbgMsg,"P11:%g (double)\n",p[1][1]);
dbg(dbgMsg);
}
}