This repository has been archived by the owner on Apr 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduleWithReshuffling.m
133 lines (115 loc) · 4.78 KB
/
ScheduleWithReshuffling.m
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
function [DataCenters, jobCompletionTimes] = ScheduleWithReshuffling(K,P,C)
% problem: CPm | K | \sum_{j} C_j
% Inputs:
%
% K = number of servers per datacenter
%
% P = matrix of processing times,
% one job per row
% one datacenter per column (because one datacenter per task)
% *** OR *** P = one-by-three vector
% P(1) = number of jobs,
% P(2) = number of datacenters,
% P(3) = maximum processing time (used in discrete uniform
% distribution)
%
% C = ordering constraint. Jobs will be scheduled in order C(1),
% C(2),..., C(n)
% Output:
%
% DataCenters = a cell array. DataCenters{dc} is the set of servers at
% DataCenter "dc." The set of servers is size 1-x-K(dc). A given server
% is referenced by DataCenters{dc}(svr).
%
% jobCompletionTimes = vector. jobCompletionTimes(j) is the time at
% which job "j" completes.
if (size(P,1) == 1 && size(P,2) == 3)
P = randi([0,P(3)],P(1),P(2));
end
n = size(P,1);
m = size(P,2);
jobCompletionTimes = zeros(1,n);
for dc = 1:m
DataCenters{dc}(K(dc)) = Server;
for j = 1:n
nextFrees = vertcat(DataCenters{dc}.nextFree);
firstAvail = find(nextFrees == min(nextFrees),1);
nextSvr = DataCenters{dc}(firstAvail);
nextJob = C(j);
nextSvr.nextFree = nextSvr.nextFree + P(nextJob, dc);
nextSvr.toDo(end + 1) = nextJob; % append nextJob
nextSvr.completionTimes(end + 1) = nextSvr.nextFree;
if (jobCompletionTimes(j) < nextSvr.completionTimes(end))
jobCompletionTimes(j) = nextSvr.completionTimes(end);
end
end
end % schedule finished
%% Begin reordering
% Note: DCs is an abbreviation for DataCenters
unassigned = 1:n;
while size(unassigned,2) > 0
% Find job j in unassigned with latest completion time C_j in DCs
makespan = zeros(1,m);
for dc = 1:m
nextFrees = vertcat(DataCenters{dc}.nextFree);
makespan(dc) = max(nextFrees);
end
Cj = max(makespan); % C_j is the latest completion time
CjDC = find(makespan == Cj,1); % Datacenter of C_j
j = DataCenters{CjDC}.toDo(end); % Job j completes at C_j
% Make sure j is in unassigned
while sum(unassigned == j) == 0
makespan = makespan(makespan ~= Cj);
Cj = max(makespan);
CjDC = find(makespan == Cj,1);
j = DataCenters{CjDC}.toDo(end);
end
for dc = 1:m
for svr = 1:K(dc)
jobs = DataCenters{dc}(svr).toDo; % for brevity's sake
cTimes = DataCenters{dc}(svr).completionTimes;
if sum(jobs == j) == 1 % j is on that server
jIndex = find(jobs == j,1);
if cTimes(jIndex) ~= Cj % j does not complete at C_j
% Rearrange on server so that j completes at C_j
% job being processed at time C_j
targetJobIdx = find(CTimes >= Cj,1);
% for all jobs inbetween j and this job
for i = jIndex+1:targetJobIdx
Ji = jobs(i);
if sum(unassigned == Ji) ~= 0 % if unassigned
if cTimes(i)+ P(j,dc) <= Cj % swap?
% jobs to be switched
[DataCenters{dc}(svr).toDo,...
DataCenters{dc}(svr).completionTimes]...
= swap(jobs,cTimes,j,Ji);
end
end
end
end
end
end
end
unassigned = unassigned(unassigned~=j); % j is now assigned
end
for dc = 1:m
for svr = 1:K(dc)
[DataCenters{dc}(svr).toDo, DataCenters{dc}(svr).completionTimes]...
= killGaps(dc,DataCenters{dc}(svr).toDo,...
DataCenters{dc}(svr).completionTimes);
end
end
end
function [toDo, completionTimes] = swap(dc,toDo,completionTimes,j1,j2)
index1 = find(toDo == j1,1);
index2 = find(toDo == j2,1);
toDo(index1) = j2;
toDo(index2) = j1;
[toDo, completionTimes] = killGaps(dc,toDo,completionTimes); % might change later
end
function [toDo, completionTimes] = killGaps(dc,toDo,completionTimes)
completionTimes(1) = P(toDo(1),dc);
for j = 2:size(toDo) % schedules purely based on processing times of jobs
completionTimes(j) = completionTimes(j-1)+P(toDo(j),dc);
end
end