-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcred.cpp
More file actions
503 lines (490 loc) · 19 KB
/
Copy pathcred.cpp
File metadata and controls
503 lines (490 loc) · 19 KB
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
/* libraries required to solve the problem */
#include <map>
#include <set>
#include <array>
#include <ctime>
#include <queue>
#include <bitset>
#include <chrono>
#include <random>
#include <vector>
#include <string>
#include <cassert>
#include <climits>
#include <complex>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
struct to represent a data chunk,
since the size is equal, we don't consider the size of chunk
*/
struct data_chunk{
/* an id to identify the data chunk */
int id;
/* time to process the data chunk */
int time;
/* for the purpose of the problem, time = 1 for every chunk */
data_chunk(int id): id(id), time(1){}
};
/* struct to represent a job */
struct job{
/* a job is nothing but consists of C_j data chunks */
vector<data_chunk> C_j;
/* deadline of the job */
int deadline;
};
/* global variables related to the problem */
/* maximum number of VMs that can be spawned on a machine */
int S;
/* maximum number of data chunks that can be hosted on a machine */
int B;
/* number of machines */
int N;
/* number of jobs */
int J;
/* set of all data_chunks */
vector<data_chunk> C;
/* number of active machines */
int N_a;
/*
map for frequency of each chunk on jobs
key: chunk id
value: frequency of the chunk
*/
map<int, int> chunk_map;
/*
map for storing the nodes on which a data chunk is present for a particular deadline
key: {chunk id, deadline}
value: set of nodes
*/
map<pair<int, int>, set<int>> chunk_nodes;
/*
storing the # of slots given to a data chunk on machine id i for a particular deadline
key: {chunk id, deadline}
value: map of machine id to # of slots
*/
map<pair<int, int>, map<int, int>> chunk_machine_slots;
/*
storing the total time slots given on a node
key: node id
value: total time slots left
*/
map<int, int> node_time_slots;
/*
ids of the machines which have been created till now
key: machine id
*/
set<int> machine_ids;
/*
map to store the nodes where a data chunk has been stored
key: data chunk id
value: set of nodes
*/
map<int, set<int>> chunk_node_map;
/*
map to store how many chunks are stored on a node
key: node id
value: number of chunks on map
*/
map<int, set<int>> node_load;
/*
function to give a random number between integers a and b
input: a, b
output: random number between a and b
*/
int between(int a = 1, int b = 100){
return uniform_int_distribution<int>(a, b)(rng);
}
/*
function to schedule the chunks on machines
Idea: by scheduling time slots from the chunks with the smallest number of required time slots,
we can remove more chunks.
Inputs:
c -> vector of chunks in the form of ({chunk freqeuncy, chunk id}),
nts -> number of available time slots,
node -> node number
Output:
vector of chunks that couldn't be scheduled
*/
vector<pair<int, int>> schedule(vector<pair<int, int>> c, int nts, int node, int deadline){
if(nts == 0) return c;
/* cout statement for debugging */
// cout << "Calling schedule for node " << node << " on modified deadline " << deadline << '\n';
/* cout statement for debugging */
sort(c.begin(), c.end());
for(int i = 0; i<c.size(); i++){
if(chunk_nodes[{c[i].second, deadline}].find(node) != chunk_nodes[{c[i].second, deadline}].end()) continue;
/*
in this case, the data chunk can't be scheduled fully on this node
so we schedule the data chunk on this node for the minimum of the available time slots
and the deadline, exhausting all the available resources on this node
*/
if(c[i].first > min(node_time_slots[node], deadline) && (node_load[node].count(node) || node_load[node].size() < B)){
node_load[node].insert(node);
int give = min(node_time_slots[node], deadline);
/* cout statement for debugging */
// cout << "chunk " << c[i].second << " can't be scheduled fully on node " << node << '\n';
// cout << "chunk " << c[i].second << " scheduled on node " << node << " for # of slots " << give << '\n';
/* cout statement for debugging */
/* we are going to schedule the c[i].second chunk on this node for the current deadline */
chunk_nodes[{c[i].second, deadline}].insert(node);
/* we have schedule the c[i].second chunk on this node once */
chunk_node_map[c[i].second].insert(node);
/* total time given to c[i].second chunk on this node at this deadline */
chunk_machine_slots[{c[i].second, deadline}][node] = give;
/* remaining slots for the node and data chunk */
node_time_slots[node] -= give;
chunk_map[c[i].second] -= give;
nts -= give;
c[i].first -= give;
/* cout statement for debugging */
// cout << "chunk " << c[i].second << " remaining slots: " << c[i].first << '\n';
// cout << "node " << node << " remaining slots: " << node_time_slots[node] << '\n';
/* cout statement for debugging */
if(nts == 0) break;
}
/*
in this case, the data chunk can be scheduled fully on this node
so we schedule the data chunk on this node for the required time slots
*/
else if(node_load[node].count(node) || node_load[node].size() < B){
node_load[node].insert(node);
/* cout statement for debugging */
// cout << "chunk " << c[i].second << " can be scheduled fully on node " << node << '\n';
// cout << "chunk " << c[i].second << " scheduled on node " << node << " for # of slots " << c[i].first << '\n';
/* cout statement for debugging */
/* we are going to schedule the c[i].second chunk on this node for the current deadline */
chunk_nodes[{c[i].second, deadline}].insert(node);
/* we have schedule the c[i].second chunk on this node once */
chunk_node_map[c[i].second].insert(node);
/* total time given to c[i].second chunk on this node at this deadline */
chunk_machine_slots[{c[i].second, deadline}][node] = c[i].first;
/* remaining slots for the node and data chunk */
node_time_slots[node] -= c[i].first;
nts -= c[i].first;
chunk_map[c[i].second] = 0;
c[i].first = 0;
/* cout statement for debugging */
// cout << "node " << node << " remaining slots: " << node_time_slots[node] << '\n';
/* cout statement for debugging */
if(nts == 0) break;
}
}
vector<pair<int, int>> res;
for(auto it: c){
if(it.first != 0) res.push_back(it);
}
return res;
}
/* function to solve the problem for same deadlines */
/*
Idea: find the first set of B chunks which require more than s*deadline time slots, make a new
machine and schedule the chunks on it
then schedule the chunks which require less than s*deadline time slots on the other machines.
Inputs:
chunks -> vector of chunks in the form of ({chunk freqeuncy, chunk id}),
virtual_deadline -> virtual deadline of the job
true_deadline -> true deadline of the job
Output:
number of machines required to schedule the chunks
*/
int cred_s(vector<pair<int, int>> chunks, int virtual_deadline, int true_deadline){
if(chunks.size() == 0) return 0;
/* sorting the chunk set in decreasing order of reqd computation */
sort(chunks.begin(), chunks.end(), greater<pair<int, int>>());
/* cout statement for debugging */
// cout << "chunks to be scheduled for deadline " << true_deadline << ": " << '\n';
// for(auto it: chunks){
// cout << it.first << " " << it.second << '\n';
// }
/* cout statement for debugging */
while(chunks.size()){
if(chunks.size() <= B){
/* finding new machine id */
vector<pair<int, int>> res = chunks;
/* try to schedule the chunks on previous machines first */
for(auto jt: machine_ids){
res = schedule(res, node_time_slots[jt], jt, true_deadline);
}
/* try scheduling the chunks on new machine if chunk remain */
if(res.size()){
machine_ids.insert(*machine_ids.rbegin()+1);
node_time_slots[*machine_ids.rbegin()] = S*true_deadline;
res = schedule(res, node_time_slots[*machine_ids.rbegin()], *machine_ids.rbegin(), true_deadline);
}
chunks = res;
}
else{
/* finding new machine id */
vector<pair<int, int>> res, new_chunks;
/* try to schedule the chunks on previous machines first */
for(auto jt: machine_ids){
if(chunks.size() <= B){
res = schedule(chunks, node_time_slots[jt], jt, true_deadline);
if(res.size()) new_chunks.insert(new_chunks.end(), res.begin(), res.end());
}
else{
int sum = 0;
for(int i = chunks.size()-1; i>=chunks.size()-B; i--){
sum += chunks[i].first;
}
int i = chunks.size()-1, j = chunks.size()-B;
while(sum < virtual_deadline && j>=0){
sum -= chunks[i].first;
i--; j--;
sum += chunks[j].first;
}
new_chunks.clear();
vector<pair<int, int>> c;
for(int k = chunks.size()-1; k>=0; k--){
if(j <= k && k <= i){
c.push_back(chunks[k]);
}
else{
new_chunks.push_back(chunks[k]);
}
}
/* cout statement for debugging */
// cout << "chunk set c: " << '\n';
// for(auto it: c){
// cout << it.first << " " << it.second << '\n';
// }
// cout << "new chunks set c: " << '\n';
// for(auto it: new_chunks){
// cout << it.first << " " << it.second << '\n';
// }
/* cout statement for debugging */
res = schedule(c, node_time_slots[jt], jt, true_deadline);
if(res.size()) new_chunks.insert(new_chunks.end(), res.begin(), res.end());
/* cout statement for debugging */
// cout << "new chunks set c after scheduling: " << '\n';
// for(auto it: new_chunks){
// cout << it.first << " " << it.second << '\n';
// }
/* cout statement for debugging */
}
chunks = new_chunks;
/* cout statement for debugging */
// cout << "chunks remaining: " << '\n';
// for(auto it: chunks){
// cout << it.first << " " << it.second << '\n';
// }
/* cout statement for debugging */
}
if(chunks.size()){
new_chunks.clear();
/* cout statement for debugging */
// cout << "new machine created: " << *machine_ids.rbegin()+1 << '\n';
// cout << "chunks to be scheduled on machine " << *machine_ids.rbegin()+1 << " for deadline " << true_deadline << ": " << '\n';
// for(auto it: chunks){
// cout << it.first << " " << it.second << '\n';
// }
/* cout statement for debugging */
machine_ids.insert(*machine_ids.rbegin()+1);
node_time_slots[*machine_ids.rbegin()] = S*true_deadline;
if(chunks.size() <= B){
res = schedule(chunks, node_time_slots[*machine_ids.rbegin()], *machine_ids.rbegin(), true_deadline);
if(res.size()) new_chunks.insert(new_chunks.end(), res.begin(), res.end());
}
else{
int sum = 0;
for(int i = chunks.size()-1; i>=chunks.size()-B; i--){
sum += chunks[i].first;
}
int i = chunks.size()-1, j = chunks.size()-B;
while(sum < virtual_deadline && j>=0){
sum -= chunks[i].first;
i--; j--;
sum += chunks[j].first;
}
new_chunks.clear();
vector<pair<int, int>> c;
for(int k = chunks.size()-1; k>=0; k--){
if(j <= k && k <= i){
c.push_back(chunks[k]);
}
else{
new_chunks.push_back(chunks[k]);
}
}
/* cout statement for debugging */
// cout << "chunk set c: " << '\n';
// for(auto it: c){
// cout << it.first << " " << it.second << '\n';
// }
// cout << "new chunks set c: " << '\n';
// for(auto it: new_chunks){
// cout << it.first << " " << it.second << '\n';
// }
/* cout statement for debugging */
res = schedule(c, node_time_slots[*machine_ids.rbegin()], *machine_ids.rbegin(), true_deadline);
if(res.size()) new_chunks.insert(new_chunks.end(), res.begin(), res.end());
/* cout statement for debugging */
// cout << "new chunks set c after scheduling : " << '\n';
// for(auto it: new_chunks){
// cout << it.first << " " << it.second << '\n';
// }
/* cout statement for debugging */
}
chunks = new_chunks;
/* cout statement for debugging */
// cout << "chunks remaining: " << '\n';
// for(auto it: chunks){
// cout << it.first << " " << it.second << '\n';
// }
/* cout statement for debugging */
}
}
}
return machine_ids.size();
}
/* function to solve the problem for different deadlines */
/*
Idea: first call cred_s for each deadline, then for each deadline
call schedule for each node, to reschedule the existing chunks on it to save time in the future
Inputs:
jobs -> vector of jobs
*/
void cred_m(vector<job> jobs){
/* finding the distinct deadlines of jobs */
set<int> deadlines;
for(auto it: jobs){
deadlines.insert(it.deadline);
}
/* reqd to calculate modified deadline */
int prev = 0;
for(auto it: deadlines){
/* finding the chunks that need to processed for the given deadline */
vector<pair<int, int>> chunks;
map<int, int> temp_chunk_map;
for(auto jt: jobs){
if(jt.deadline == it){
for(auto kt: jt.C_j){
temp_chunk_map[kt.id]++;
}
}
}
for(auto jt: temp_chunk_map){
chunks.push_back({jt.second, jt.first});
}
/* scheduling the given chunks */
/* reusing time left out time slots */
for(auto jt: node_time_slots){
node_time_slots[jt.first] = jt.second + S*(it-prev);
}
int temp = cred_s(chunks, S*(it-prev), it-prev);
chunks.clear();
/* scheduling the already allocated chunks for future on the already allocated machines */
for(int n = 1; n<=temp; n++){
int temp_prev = it;
for(auto jt: deadlines){
if(jt <= it) continue;
temp_chunk_map.clear();
for(auto kt: jobs){
if(kt.deadline == jt){
for(auto lt: kt.C_j){
temp_chunk_map[lt.id]++;
}
}
}
for(auto kt: temp_chunk_map){
if(chunk_node_map[kt.first].count(n) == 0 || kt.second == 0) continue;
chunks.push_back({kt.second, kt.first});
}
chunk_nodes.clear();
schedule(chunks, S*jt-node_time_slots[n], n, jt-temp_prev);
temp_prev = jt;
}
}
N_a = machine_ids.size();
}
}
/*
function for evaluation by simulation
*/
void simulation(){
/* initialization */
N = between(1, 10);
J = between(1, 100);
B = between(1, 20);
S = between(1, 5);
/* creating the data chunks */
int total_chunks = between(1, 20);
for(int i = 0; i < total_chunks; i++){
C.push_back(data_chunk(i));
}
/* creating the jobs */
vector<job> jobs(J);
for(int i = 0; i<J; i++){
total_chunks = between(1, 20);
for(int j = 0; j<total_chunks; j++){
int x = between(0, C.size()-1);
jobs[i].C_j.push_back(C[between(0, C.size()-1)]);
}
jobs[i].deadline = between(1, 10);
}
/* printing the simulation profile */
cout << "---------------------------- SIMULATION PROFILE ------------------------------" << '\n';
cout << "Number of jobs: " << J << '\n';
cout << "Number of data chunks: " << C.size() << '\n';
double avg_chunk = 0, avg_deadline = 0;
cout << "Job profile:" << '\n';
for(int i = 0; i<J; i++){
/* cout statement for debugging */
// cout << "Job " << i << ": " << '\n';
// cout << "Deadline: " << jobs[i].deadline << '\n';
// cout << "Total data chunks required: " << jobs[i].C_j.size() << '\n';
// cout << "Data chunks: " << '\n';
// for(auto it: jobs[i].C_j){
// cout << it.id << " ";
// }
// cout << '\n';
/* cout statement for debugging */
avg_chunk += jobs[i].C_j.size();
avg_deadline += jobs[i].deadline;
}
cout << "Average chunk size: " << avg_chunk/J << "\nAverage deadline of job: " << avg_deadline/J << '\n';
cout << "------------------------------------------------------------------------------" << '\n';
/* solving the problem */
for(auto it: jobs){
for(auto jt: it.C_j){
chunk_map[jt.id]++;
}
}
machine_ids.insert(0);
/* testing cred_m */
cred_m(jobs);
std::cout << "N_a: " << N_a << '\n';
}
/*
function for evaluation by putting manual test cases
*/
void manual(){
cout << "TODO" << '\n';
}
/*
function for evalution using google trace
*/
void trace(){
cout << "TODO" << '\n';
}
int main(){
int number_of_exp = 10;
for(int i = 0; i<number_of_exp; i++){
C.clear();
chunk_map.clear();
chunk_nodes.clear();
chunk_machine_slots.clear();
node_time_slots.clear();
machine_ids.clear();
chunk_node_map.clear();
node_load.clear();
simulation();
}
// manual();
// trace();
}