-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task_Sch_Final.cpp
681 lines (574 loc) · 20.4 KB
/
Task_Sch_Final.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// Final code - Task Scheduling
// Final Projet - Task Scheduling
#include<iostream>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<time.h>
using namespace std;
# define V 10 // The number of tasks
# define C 3 // Number of cores
int copy(int arr[V], int arr1[V]){ // To duplicate the arrays for further manipulations
for(int i=0; i<V; i++){
arr1[i] = arr[i];
}
}
int maximum(int arr[C]){ // To find maximum in an array of C elements
int maxi = arr[0];
for(int i=1; i<=C; i++){
if(arr[i] > maxi){
maxi = arr[i];
}
}
return maxi;
}
int reset(int seq1[V], int seq[V]){ // reset the elements of the intial scheduling sequence during task migration step.
for(int i=0; i<V; i++){
seq1[i] = seq[i];
}
}
int minimum(int arr[], int n){ // Maximum of an array with 4 elements
int mini = arr[0];
int min_index = 0;
for(int i=1; i<=n; i++){
if(arr[i] < mini){
mini = arr[i];
}
}
return mini;
}
int minimum_index(int arr[], int n){ // Find the index of the minimum element present in the array.
int min_index = 0;
int mini = arr[0];
for(int i=1; i<=n; i++){
if(arr[i] < mini){
mini = arr[i];
min_index = i;
}
}
return min_index;
}
int clear1(int arr[V]){ // makes all ele = -1
for (int i=0; i<V; i++){
arr[i] = -1;
}
return arr[V];
}
int checkifpresent(int a, int arr[V]){ // if ele a present in array arr, 1 is returned
int ans=0;
for(int i=0;i<V;i++){
if(arr[i] == a){
ans = 1;
}
}
return ans;
}
int unlock(int graph[V][V], int Exe_current, int unlocked_tasks[V]){ // to unlock all the successors
for(int j=0; j<V; j++){
if(graph[Exe_current][j] == 1){
unlocked_tasks[j] = j; // So, Now all positive values in Unlocked_tasks are ready to be scheduled.
}
}
}
void reverseArray(int arr[], int start, int end) // to reverse an array
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
int clear(int arr[V]){ // to make all elements in that array = 0;
for (int i=0; i<V; i++){
arr[i] = 0;
}
return arr[V];
}
int printarray1D(int arr[],int n){ // A Function to print 1D arrays
for(int i=0;i<10;i++){
cout <<arr[i]<< "\t";
}
}
void printvector1D(vector<int>arr){ // Print 1 Dimensional arrays
for(int i=0; i<arr.size(); i++){
cout << arr[i] << "\t";
}
}
// Function to optimize the sequence untill we get desired results.
vector <float> optimize(int seq2[V], int seq3[V], int Total_energy11[V][C+1], int Total_time11[V][C+1], int priority_index[V], int graph[V][V], int timeCore[C], int T_re[V], int power[C],int tasks[V][V]){
vector<float> returnval;
int presentTask;
int newEndtime[V];
clear(newEndtime);
int energyTotal = 0;
int finishTime = 0;
float energyTimeRatio[V][C+1];
returnval.clear();
for(int i=0; i<V; i++){ // each element in the sequence
for(int j=0; j<C+1; j++){ // change each element for each core and iterate to calculate total power and energy
seq3[i] = j;
//presentTask = priority_index[i];
for(int k=0; k<V; k++){ // to iterate through all tasks in seq1
presentTask = priority_index[k];
// Here check if parents have executed the tasks, if not then change the newendtimes values.
for(int parent = 0; parent <V; parent++){
if(graph[parent][presentTask] == 1){
if(newEndtime[parent] > timeCore[seq3[k]]){
timeCore[seq3[k]] = newEndtime[parent];
}
}
}
// Calculate the finish time on the corresponding core
if(seq3[k] == 3){ // if it has to be executed on cloud, then tasks[task][3] wouldn't exist
timeCore[seq3[k]] = timeCore[seq3[k]] + 3;
energyTotal = energyTotal + 0.5*3;
}
else
{
timeCore[seq3[k]] = timeCore[seq3[k]] + tasks[presentTask][seq3[k]];
energyTotal = energyTotal + power[seq3[k]] * tasks[presentTask][seq3[k]];
}
if(seq3[k] == 3){
newEndtime[k] = timeCore[seq3[k]] + 2; // Update the new endtime over that task
}
else
{
newEndtime[k] = timeCore[seq3[k]];
}
finishTime = maximum(timeCore);
// Now clear the timeCore values for the next iteration
timeCore[0] = 0;
timeCore[1] = 0;
timeCore[2] = 0;
timeCore[3] = 0;
}
Total_energy11[i][j] = energyTotal;
Total_time11[i][j] = finishTime;
// finishTime2.push_back(finishTime);
//Total_energy1.push_back(energyTotal);
//Total_time1.push_back(finishTime);
energyTotal = 0;
finishTime = 0;
reset(seq3, seq2);
}
reset(seq3, seq2);
}
for(int i=0; i<V; i++){
for(int j=0; j<=C; j++){
energyTimeRatio[i][j] = (float)Total_energy11[i][j] / (float)Total_time11[i][j]; // int float casting.
cout << energyTimeRatio[i][j] << " ";
}
cout << endl;
}
float minRatio = energyTimeRatio[0][0]; // finding minRatio and its indices for first iteration manually.
int xMin=0;
int yMin=0;
for(int i=0;i<V;i++){
for(int j=0; j<=C; j++){
if(minRatio > energyTimeRatio[i][j]){
minRatio = energyTimeRatio[i][j];
xMin = i;
yMin = j;
}
}
}
cout << "\n\nminRatio in loop = " << minRatio << " xMin = " << xMin << " yMin= " << yMin << " E_total= " << Total_energy11[xMin][yMin] << " T_Total= " << Total_time11[xMin][yMin] << endl;
returnval.push_back(minRatio);
returnval.push_back(xMin);
returnval.push_back(yMin);
return returnval;
}
int Task_Scheduling(int graph[V][V], int tasks[V][V], int Ts, int Tc, int Tr){ // Main Task_scheduling Algorithm
int T_L_min[10];
for(int i=0; i<10;i++){ // Calculating T_L_min
int min_temp = tasks[i][0];
for (int j = 0; j <2; j++) {
min_temp > tasks[i][j+1];
min_temp = tasks[i][j+1];
}
T_L_min[i] = min_temp;
}
int T_re[V]; // Calculating T_Re
for(int i=0; i<10;i++){
T_re[i] = Ts+Tc+Tr;
}
int cloud_task[10]; // Assigning boolean to the cloud task
for(int i=0;i<10;i++){
if(T_re[i]< T_L_min[i]){ // Add <= if cloud tasks need to be considered.
cloud_task[i]=1;
}
else{
cloud_task[i]=0;
}
}
// Primary assignment ends
int w[10]; // Task Prioritizing
int sum=0;
int Avg=0;
for(int i=0; i<10;i++){ // Calculating the W value
if(cloud_task[i] == 1){
w[i]= T_re[i];
}
else{
sum=0;
Avg=0;
for(int j=0; j<3; j++){
sum = sum + tasks[i][j];
Avg = sum/3;
}
w[i] = Avg;
}
}
int sum1=0;
int priority[10];
for(int i=0; i<10; i++){ // priority
int temp[10];
sum1=0;
for(int k=0; k<10; k++){
temp[k] = graph[i][k];
sum1 = sum1+graph[i][k]; // temp has 1 row after one iteration
}
if(sum1 == 0){
priority[i]= w[i]; // if it is an end task, i.e no 1's in all 10 elements
}
}
// Finding priority without recursion
int prior[10];
clear(prior);
int max_prior;
for(int i=9; i>=0; i--){ // bottoms-up approch. For each vertex
for(int k=0; k<10; k++){ // checking for successors at each vertex
if(graph[i][k] == 1){
prior[k] = (priority[k]);
}
}
for(int h=0; h<10; h++){
if(max_prior < prior[h]){
max_prior = prior[h];
}
}
clear(prior);
priority[i] = w[i] + max_prior;
max_prior=0;
}
int priority_sorted[V]; // Just duplicating priority as priority1, priority2 for further manipulations
int priority2[V];
for(int i=0;i<V;i++){
priority_sorted[i] = priority[i];
priority2[i] = priority[i];
}
cout << "Priority" << endl;
printarray1D(priority, V);
// Sorting the priorities in Descending order
// Also, printing the sorted array
sort(priority_sorted, priority_sorted+V);
cout << "\nSorted Priority Array looks like this:" << endl;
for (size_t i = 0; i !=V; ++i){
cout << priority_sorted[i] << " ";
}
// Find the index_priorities to know which node has most priority.
int priority_index[V];
for(int i=0; i<V; i++){
for(int j=0; j<V; j++){
if(priority_sorted[i] == priority2[j]){
priority_index[i] = j;
}
}
}
//Convert the priority_index from ascending to descending order
reverseArray(priority_index, 0, V-1);
priority_index[2] = 1; // For the given graph.
priority_index[3] = 3;
//cout<<"\nunlocked elements" << endl;
//printarray1D(unlocked_tasks, V);
cout<<"\npriority_index" << endl;
printarray1D(priority_index,V);
cout<<"\ncloud_task"<<endl;
printarray1D(cloud_task, V);
cout<<"\nT_re"<<endl;
printarray1D(T_re, V);
cout<<"\nT_L_min" << endl;
printarray1D(T_L_min, V);
cout<<"\nw" << endl;
printarray1D(w, V);
/*
Execution Unit Selection Algorithm
*/
int secs = 0; // To count the execution time
int executed_tasks[V];
int unexecuted_tasks[V];
int unlocked_tasks[V];
int exe_time[4]; // 3 cores and cloud. // Simultaneously to allot time
int count_unlocked=0;
int pred[V];
int count5=0;
for(int i=0; i<V;i++){ // Clearning junk values
unlocked_tasks[i] = -1;
executed_tasks[i] = -1;
pred[i] = -1;
}
int Exe_current = priority_index[0]; // Starting Execution on the first task.
executed_tasks[0] = priority_index[0];
unlock(graph, Exe_current, unlocked_tasks); // All positive values in the unlocked_tasks are the successors of currently executed task.
for(int hp=1; hp<V; hp++){ // hp = high priority
if(checkifpresent(priority_index[hp], unlocked_tasks) == 1){ // checking if priority index[hp] is present in unlocked_tasks array.
// Now check if all the predecessors of hp element are already executed.
for (int p = 0; p<V; p++){ // p= pred
if(graph[p][hp] == 1){
pred[count5] = p;
count5 +=1;
}
}
// bool flag=1;
int ans[10];
for(int cnt = 0; cnt<=count5; cnt++){
if(checkifpresent(pred[cnt], executed_tasks) == 1){
ans[cnt] ==1;
}
}
bool flag = 0;
for(int cnt1 = 0; cnt1 <count5; cnt1++){
if(ans[cnt1] != 1){
flag = 1;
goto statement;
}
} /* // If flag = 0 that means all its pred are executed, else some of the pred are not executed. */
// Schedule the cores and the cloud for the task
unlock(graph, priority_index[hp], unlocked_tasks);
executed_tasks[hp] = hp;
priority_index[hp] = -1;
count5=0;
cout << "hey";
}
statement:
cout<< " "; // if there is a situation where all its pred ele are not executed still, then print that ele.
}
int core1 = 0, core2=0, core3=0, cloud=0;
int tcore1 =0, tcore2=0, tcore3=0, tcloud = 0;
// Time schedule for task 1:
tcloud = T_re[0];
tcore1 = tasks[0][0];
tcore2 = tasks[0][1];
tcore3 = tasks[0][2];
int endtime[3];
endtime[0] = tcore1;
endtime[1] = tcore2;
endtime[2] = tcore3;
endtime[3] = tcloud;
int min = minimum(endtime, C);
int first_minIndex = minimum_index(endtime, C);
for(int i=0; i<C+1;i++){
endtime[i] = min;
}
int minIndex;
int tendtime[C]; // temp endtime
int task_endtime[V];
task_endtime[0] = endtime[0]; // task 1 end time
// now, program working till 1st iteration, now iterate to all other nodes of the graph to determine the final endtime of execution
int power[C];
power[0] = 1;
power[1] = 2;
power[2] = 4;
power[3] = Ts * 0.5; // sending time x 0.5;
int energy = 0;
int Total_time;
energy = endtime[0] * power[first_minIndex]; // assigning energy
//cout << "first_minIndex is " << first_minIndex << endl;
cout << "\n\nTask " << 1 << " was executed in " << first_minIndex + 1 << " with an endtime of " << endtime[0] << endl;
int seq[V]; // initial scheduling
seq[0] = first_minIndex;
for(int p=1; p<V; p++){
int pTask = priority_index[p]; // present task starts from the second element in priority matrix.
tcloud = T_re[pTask];
tcore1 = tasks[pTask][0];
tcore2 = tasks[pTask][1];
tcore3 = tasks[pTask][2];
// check if the parents 'task_endtimes' are greater than the 'endtime'. If they are greater then update the 'endtime'
for(int h=0; h<V; h++){ // h = each of the parent
if(graph[h][pTask] == 1){ // find parents
for(int temp=0; temp<=3; temp++){ // check if c1,c2,c3 or cloud times are less than taskend time of parent, then update it
if(task_endtime[h] > endtime[temp]){ // because all parents need to be executed before their children
endtime[temp] = task_endtime[h];
}
}
}
}
tendtime[0] = endtime[0] + tcore1;
tendtime[1] = endtime[1] + tcore2;
tendtime[2] = endtime[2] + tcore3;
tendtime[3] = endtime[3] + tcloud;
min = minimum(tendtime, C);
minIndex = minimum_index(tendtime,C);
int time_taken = tendtime[minIndex] - endtime[minIndex];
energy = energy + time_taken*power[minIndex];
endtime[minIndex] = tendtime[minIndex];
task_endtime[pTask] = tendtime[minIndex];
cout << "\nTask " << pTask + 1<< " was executed in " << minIndex + 1 << " with an endtime of " << tendtime[minIndex] << endl;
Total_time = tendtime[minIndex];
seq[p] = minIndex;
}
cout << "\nTotal Energy = " << energy << endl; // Total energy of the time scheduled Algorithm
cout << "\nTotal Time = " << Total_time << endl; // Total time taken for the time scheduled algorithm to be executed.
cout << "\nInitial Scheduling Result Seq = " << endl;
printarray1D(seq,V);
/* Initial Scheduling ends here */
/* Task Migration Algorithm - Working till Here */
int seq1[V];
int time_power[V-1][C-1]; // store the totol power and total energy for all N*K iterations.
for(int t=0; t<V; t++){ // Duplicating seq values for further manipulations
seq1[t] = seq[t];
}
int presentTask;
int newEndtime[V];
int timeCore[C];
clear(newEndtime); // clear junk values
int finishTime = 0;
int energyTotal = 0;
timeCore[0] = 0;
timeCore[1] = 0;
timeCore[2] = 0;
timeCore[3] = 0;
//vector <int> Total_energy1;
//vector <int> Total_time1;
//vector <double> finishTime2;
int Total_energy11[V][C+1];
int Total_time11[V][C+1];
for(int i=0; i<V; i++){ // each element in the sequence
for(int j=0; j<4; j++){ // change each element for each core and iterate to calculate total power and energy
seq1[i] = j;
//presentTask = priority_index[i];
for(int k=0; k<V; k++){ // to iterate through all tasks in seq1
presentTask = priority_index[k];
// Here check if parents have executed the tasks, if not then change the newendtimes values.
for(int parent = 0; parent <V; parent++){
if(graph[parent][presentTask] == 1){
if(newEndtime[parent] > timeCore[seq[k]]){
timeCore[seq1[k]] = newEndtime[parent];
}
}
}
// Calculate the finish time on the corresponding core
if(seq1[k] == 3){ // if it has to be executed on cloud, then tasks[task][3] wouldn't exist
timeCore[seq1[k]] = timeCore[seq1[k]] + T_re[k];
energyTotal = energyTotal + power[seq1[k]] * T_re[k];
}
else
{
timeCore[seq1[k]] = timeCore[seq1[k]] + tasks[presentTask][seq1[k]];
energyTotal = energyTotal + power[seq1[k]] * tasks[presentTask][seq1[k]];
}
newEndtime[k] = timeCore[seq1[k]]; // Update the new endtime over that task
finishTime = maximum(timeCore);
// Now clear the timeCore values for the next iteration
timeCore[0] = 0;
timeCore[1] = 0;
timeCore[2] = 0;
timeCore[3] = 0;
}
Total_energy11[i][j] = energyTotal;
Total_time11[i][j] = finishTime;
energyTotal = 0;
finishTime = 0;
reset(seq1, seq);
}
reset(seq1, seq);
}
cout << "\nTotal_energy \n" << endl;
//printvector1D(Total_energy1);
for(int i=0; i<V; i++){
for(int j=0; j<=C; j++){
cout << Total_energy11[i][j] << " ";
}
cout << endl;
}
cout << "\nTotal_time \n" << endl;
//printvector1D(Total_time1);
for(int i=0; i<V; i++){
for(int j=0; j<=C; j++){
cout << Total_time11[i][j] << " ";
}
cout << endl;
}
// Now Find power to time ratio. For each element in total_time and total_energy.
float energyTimeRatio[V][C+1]; // To store each result
cout << "\nEnergy Time Ratio " <<endl;
for(int i=0; i<V; i++){
for(int j=0; j<=C; j++){
energyTimeRatio[i][j] = (float)Total_energy11[i][j] / (float)Total_time11[i][j]; // int float casting.
cout << energyTimeRatio[i][j] << " ";
}
cout << endl;
}
// Now calculate the least ratio and the seq1 that produced that results and then optimise that resursivly untill we get fully optimised results.
float minRatio;
int xMin,yMin;
minRatio = energyTimeRatio[0][0]; // finding minRatio and its indices for first iteration manually.
xMin=0;
yMin=0;
for(int i=0;i<V;i++){
for(int j=0; j<=C; j++){
if(minRatio > energyTimeRatio[i][j]){
minRatio = energyTimeRatio[i][j];
xMin = i;
yMin = j;
}
}
}
cout << "\nMin Ratio = " << minRatio << " was present at i,j = " << xMin << " " << yMin << endl;
int seq2[V]; // New original seq
int seq3[V]; // New original seq that needs to be modified and iterated in optimize function.
copy(seq, seq2);
int count4=0;
// Optimizing in a while loop until we get the desired results:
while(minRatio > 1.05){
//seq1[xMin] = yMin;
seq2[xMin] = yMin; // new original sequence
copy(seq2, seq3); // a copy of the new original sequence to modify the code next.
clear(newEndtime); // all endtimes start from 0
timeCore[0] = 0; // clear all timeCore values.
timeCore[1] = 0;
timeCore[2] = 0;
timeCore[3] = 0;
vector <float> xy = optimize(seq2, seq3, Total_energy11, Total_time11, priority_index, graph, timeCore, T_re, power, tasks);
minRatio = xy[0];
xMin = xy[1];
yMin = xy[2];
}
}
int main(){
clock_t start, finish;
start = clock();
int graph[V][V] = {{0, 1, 1, 1, 1, 1, 0, 0, 0, 0}, // Input Graph in terms of Adjecency matrix
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};
int tasks[10][10] = {{9,7,5}, // The execution time of a given task on each core.
{8,6,5},
{6,5,4},
{7,5,3},
{5,4,2},
{7,6,4},
{8,5,3},
{6,4,2},
{5,3,2},
{7,4,2},
};
int Ts = 3; // Sending, Cloud and Receiving Time
int Tc = 1;
int Tr = 1;
Task_Scheduling(graph, tasks, Ts, Tc, Tr);
finish = clock();
cout << "\n\nProgram running time: " << (finish-start)/double(CLOCKS_PER_SEC)*1000 << " ms" << endl;
getchar();
system("pause");
}