-
Notifications
You must be signed in to change notification settings - Fork 3
/
OCT.cpp
385 lines (356 loc) · 8.78 KB
/
OCT.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <iostream>
#include <vector>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <map>
#define MAX 1000
using namespace std;
int parent_proc[MAX];
int OUT[MAX][MAX];
float tp[MAX]={0}; //processor availability
//float time=0;
float aft[MAX]={0}; // clock time for processor
int comm_mat[MAX][MAX]={0}; //process time on diff. processor
int adj[MAX][MAX]={0}; //adjancecy matrix
int p_matrix[MAX][MAX],p_temp[MAX][MAX]; //processor matrix
int nodes=0; //number of nodes
int n_proc=0; //number of processors
float rank[MAX][MAX]={0.0}; //oct ranking
float est[MAX][MAX]={0}; // average of all rank on processor
vector<float> ready_list;
vector<int> n_ready;
ifstream ifs;
void SORT();
void remove_duplicate();
inline void set_aft(){for(int i=0;i<n_ready.size();i++)aft[i]=0;}
inline void set_rank_zero(int i){for(int j=0;j<nodes;j++)rank[i][j]=0.0;}
inline double round(double d){ return floor(d + 0.5);} //round off float
void read_adj(); //to read adj. matrix
void read_processor(); //to read processor matrix
void read_weight(); //to read processor time matrix
bool check_end_node(int); //check for end nodes
void set_end_nodes(); //sets rank of end nodes to zero
void identity(const int I[MAX][MAX],int w,int O[MAX][MAX]);
void OCT(int a,int b,int A[MAX][MAX]);
void addMatrix(const int a[MAX][MAX],int b[MAX][MAX]);
void row_wise_min(); // to find row wise min of OUT matrix
void assign_rank(int);
float EFT(int ni,int pj,int previous);
void display(); //To display result
void N_entry(); //To find entry nodes in a graph
void add_mul_mat(float add,float mul,int a[][MAX]);
void Oeft();
void push_children(int i);
int highest_oct();
int g_level(int ni=0,int rank=0){ //find rank of a node in a graph
if(ni<=0) return rank;
else if(ni==1) return 1;
int save=0;
for(int i=0;i<nodes;i++)
if(adj[i][ni]!=-1)
save=i;
return g_level(save,rank+1);
}
int main(int argc,char *argv[])
{
if(argv[1]==NULL){cout<<"\nError No File Supplied!\nUsage: [./a.out file_name.txt]\n";return 1;}
ifs.open(argv[1]); /* read from a file */
ifs>>nodes;
ifs>>n_proc;
read_weight();
read_processor();
read_adj();ifs.close(); /* read endds */
set_end_nodes();
for(int i=nodes-1;i>=0;i--)
{
if(check_end_node(i)==true) set_rank_zero(i); /* If a terminal node set rank to 0.0*/
else{ /* if not a terminal node*/
for(int j=i+1;j<nodes;j++) /*check for all successor nodes*/
{
if(adj[i][j]!=-1) /*if a successor */
{
identity(p_matrix,adj[i][j],p_temp); /* for Cij */
OCT(i,j,OUT); /* OCT(Tj,Pw)+W(Tj,Pw)*/
addMatrix(p_temp,OUT); /* OCT(Tj,Pw) + W(Tj,Pw)+ Cij*/
row_wise_min(); /* min(OCT(Tj,Pw) + W(Tj,Pw)+ Cij) */
assign_rank(i); /* max(min(OCT(Tj,Pw) + W(Tj,Pw)+ Cij)) */
}
}
}
}
/* calculation of RANKoct */
display();
cout<<endl;
for(int i=0;i<nodes;i++){cout<<"Process: "<<i+1;
int temp=0;
for(int j=0;j<n_proc;j++){
cout<<"\t"<<rank[i][j];temp+=rank[i][j];
}
ready_list.push_back((float)temp/n_proc);
cout<<"\t";printf("%.1f \n",(float)temp/n_proc);
}
Oeft(); //PEFT Algorithm
return 0;
}
void Oeft()
{
int parent=-1,comp=10000;
int ni=0,save=0;
int p=0,pro=0;
float oc;int previous=0;
N_entry();set_aft();
while(n_ready.size())
{
int test_case=10000;
cout<<endl;
oc=10000;
//ni=highest_oct();
ni=n_ready[0];
for(int b=0;b<nodes;b++) if(adj[b][ni]!=-1 && parent!=b) parent=b;
for(int j=0;j<n_proc;j++)
{
//int c=n_ready[0];
if((EFT(ni,j,parent_proc[parent])+rank[ni][j])<oc){
oc=EFT(ni,j,parent_proc[parent])+rank[ni][j];
pro=j;
comp=EFT(ni,j,parent_proc[parent]);
}
cout<<"EFT: "<<EFT(ni,j,parent_proc[parent])<<endl;
cout<<"P"<<ni+1<<" RANKu: "<<(EFT(ni,j,parent_proc[parent])+rank[ni][j])<<"\tProcessor: "<<j+1<<endl;
}
previous=pro;
cout<<endl;
int i=n_ready[0];
parent_proc[i]=pro;
tp[pro]=comp;
aft[i]=comp;
cout<<"AFT: "<<i+1<<" "<<aft[i]<<endl;
save=n_ready[0]; //assign AFT
n_ready.erase(n_ready.begin()); //pop first element
for(int l=0;l<n_proc;l++) cout<<"P[ "<<l+1<<" ]: "<<tp[l]<<endl;
push_children(i);
SORT();
int rank=0;
// for(int ml=0;ml<nodes;ml++)
// cout<<"Node: "<<ml<<" RANK:"<<set_level(ml,0)<<endl;
for(int kk=0;kk<n_ready.size();kk++) cout<<"T"<<n_ready[kk]+1<<" ";
printf("Processor Assigned: %d\t Process: %d\t\n",pro+1,i+1);
cout<<"-----__________________________________________-----\n";
getchar();
}
}
void push_children(int i)
{
for(int j=i+1;j<nodes;j++)
if(adj[i][j]!=-1) n_ready.push_back(j);
}
int highest_oct()
{
int save=-1;
int temp=-1;
for(int i=0;i<n_ready.size();i++)
if(ready_list[n_ready[i]]>temp){
temp=ready_list[n_ready[i]];
save=i;
}
n_ready.erase(n_ready.begin()+save);
return save;
}
float EFT(int ni,int pj,int previous)
{
int temp[MAX]={0};
float mt;
for(int i=0;i<=ni;i++)
{
if(adj[i][ni]!=-1)
{
for(int j=0;j<n_proc;j++)
{
mt=aft[i]+p_matrix[parent_proc[i]][j]*adj[i][ni];
if(temp[j]<=mt)
temp[j]=mt;
}
}
}
for(int i=0;i<n_proc;i++){
if(tp[i]>temp[i]) est[ni][i]=tp[i];
else est[ni][i]=temp[i];
}
/*
for(int j=0;j<=ni;j++)
{
if(adj[j][ni]!=-1) //if a pred. //
{
add_mul_mat(aft[j],adj[j][ni],p_temp); /// AFT(Nm)+Cm,i
}
else continue;
for(int z=0;z<n_proc;z++)
{
if(tp[z]>=p_temp[previous][z]) est[ni][z]=tp[z];
else est[ni][z]=p_temp[previous][z]; // EST
}
}
*/
return est[ni][pj]+comm_mat[ni][pj]; /* EFT */
}
void add_mul_mat(float add,float mul,int a[][MAX])
{
float mx=0;
for(int i=0;i<n_proc;i++)
for(int j=0;j<n_proc;j++)
{
a[i][j]=(p_matrix[i][j]*mul)+add;
//if(a[previous][j]>mx) mx=(float)a[previous][j];
}
//return mx;
}
void N_entry()
{
bool flag=true;
for(int i=0;i<nodes;i++)
{
for(int j=0;j<nodes;j++)
if(adj[j][i]!=-1)
{
flag=false;
break;
}
if(flag==true)
{
n_ready.push_back(i);
flag=true;
for(int k=0;k<n_proc;k++)
est[i][k]=0.0;
}
}
}
void assign_rank(int a)
{
for(int i=0;i<n_proc;i++)
if(rank[a][i]<OUT[i][0])
rank[a][i]=OUT[i][0];
}
void row_wise_min()
{
for(int i=0;i<n_proc;i++)
{
int temp=1000;
for(int j=0;j<n_proc;j++)
if(OUT[i][j]<temp) temp=OUT[i][j];
OUT[i][0]=temp;
}
}
void OCT(int a,int b,int A[MAX][MAX])
{
for(int i=0;i<n_proc;i++)
for(int j=0;j<n_proc;j++)
A[i][j]=rank[b][j]+comm_mat[b][j]; /* OCT(Tj,Pw)+W(Tj,Pw)*/
}
void addMatrix(const int a[MAX][MAX],int b[MAX][MAX]) /* adds nXn Matrix */
{
for(int i=0;i<n_proc;i++)
for(int j=0;j<n_proc;j++)
b[i][j]+=a[i][j];
}
void identity(const int I[MAX][MAX],int w,int O[MAX][MAX])
{
for(int i=0;i<n_proc;i++)
for(int j=0;j<n_proc;j++)
O[i][j]=w*I[i][j];
}
void set_end_nodes()
{
for(int i=0;i<nodes;i++)
{
if(check_end_node(i)==true) {
for(int j=0;j<nodes;j++) rank[i][j]=0.0;
//cout<<i<<" "<<rank[i]<<" ";
}
}
}
bool check_end_node(int n)
{ for(int j=0;j<nodes;j++)
if(adj[n][j]!=-1) return false;
return true;
}
void read_adj()
{ for(int i=0;i<nodes;i++)
for(int j=0;j<nodes;j++)
ifs>>adj[i][j];
}
void read_processor()
{ for (int i = 0; i <n_proc; i++)
for(int j=0;j<n_proc;j++)
ifs>>p_matrix[i][j];
}
void read_weight()
{ for(int i=0;i<nodes;i++)
for(int j=0;j<n_proc;j++)
ifs>>comm_mat[i][j];
}
void display()
{
cout<<"Nodes: "<<nodes<<endl<<"Processor: "<<n_proc<<endl<<"Communication Cost Matrix\n";
for(int i=0;i<nodes;i++)
{
for(int j=0;j<n_proc;j++)
cout<<comm_mat[i][j]<<"\t";
cout<<endl;
}
cout<<"\nAdj Matrix\n";
for(int i=0;i<nodes;i++){
for(int j=0;j<nodes;j++)
cout<<adj[i][j]<<" ";
cout<<endl;
}
cout<<"\nProcessor Matrix\n";
for (int i = 0; i <n_proc; i++){
for(int j=0;j<n_proc;j++)
cout<<p_matrix[i][j]<<"\t";
cout<<endl;
}
}
void SORT()
{
for(int i=0;i<n_ready.size();i++)
for(int j=i+1;j<n_ready.size();j++)
if(ready_list[n_ready[i]]<ready_list[n_ready[j]])
{
int temp=n_ready[i];
n_ready[i]=n_ready[j];
n_ready[j]=temp;
}remove_duplicate();
}
void remove_duplicate()
{
for(int i=0;i<n_ready.size();i++)
for(int j=i+1;j<n_ready.size();j++)
if(n_ready[i]==n_ready[j]) n_ready.erase(n_ready.begin()+j);
}
/* sample input
10 3
14 16 9
13 19 18
11 13 19
13 8 17
12 13 10
13 16 9
7 15 11
5 11 14
18 12 20
21 7 16
0 1 1
1 0 1
1 1 0
-1 18 12 9 11 14 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 19 16 -1
-1 -1 -1 -1 -1 -1 23 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 27 23 -1
-1 -1 -1 -1 -1 -1 -1 -1 13 -1
-1 -1 -1 -1 -1 -1 -1 15 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 17
-1 -1 -1 -1 -1 -1 -1 -1 -1 11
-1 -1 -1 -1 -1 -1 -1 -1 -1 13
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
*/