-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcapacity_consensus_basic.m
More file actions
288 lines (241 loc) · 8.84 KB
/
capacity_consensus_basic.m
File metadata and controls
288 lines (241 loc) · 8.84 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
%% Basic capacity consensus
%
% This is the basic capacity consensus that is used as a baseline; it
% does not support either delays nor has a finite termination condition -
% meaning: that is guaranteed to terminate as our main algorithm.
%
% Authors:
%
% - Andreas A. Grammenos (ag926@cl.cam.ac.uk)
% - Themistoklis Charalambous (themistoklis.charalambous@aalto.fi)
%
% License: GPLv3
%
%
%% Initialisation
%
clear; clc; close all;
% for reproducibility, fix the seed
rng("default")
%% Configuration
%
% nodes to test for
nodes_to_test = [20, 50, 100, 200, 300, 600];
node_test_len = length(nodes_to_test);
node_len_array = 1:node_test_len;
% Number of iterations
max_iter = 30;
% plotted for trial
plotted_for_trial = 0;
% number of trials
trials = 30;
% enable extensive plots
extensive_plots = 1;
% multiplier from sec to ns & μs
ns_mul = 1e+9;
micro_mul = 1e+6;
% the epsilon to check if we converged
e = 1e-5;
% the minimum workload value
min_workload = 1;
% the maximum workload value
max_workload = 2;
% timers
total_time_global = zeros(trials, node_test_len);
iter_time_global = zeros(trials, node_test_len, max_iter);
% converge statistics
cov_min_global = zeros(node_test_len, trials);
cov_max_global = zeros(node_test_len, trials);
cov_mean_global = zeros(node_test_len, trials);
cov_win_global = zeros(node_test_len, trials);
% setup variables
params.type = "basic";
params.pflag = 0;
params = setup_vars(params);
%% Run the trials
%
for t=1:trials
fprintf("\n** Running trial %d\n", t);
% run for the specified configuration
for i=node_len_array
% number of nodes to consider
nodes = nodes_to_test(i);
% note the converged time for each node
node_converged_at = zeros(nodes, 1);
fprintf("\t-- INFO: Testing for %d nodes in the graph\n", nodes);
% generate the graph
[~, ~, n, P] = gen_graph(nodes);
% generate the workload to be shared among users
x0 = gen_workload(min_workload, max_workload, nodes);
% create the node capacities
y1= 3 * ones(ceil(nodes / 2), 1);
y2= 5 * ones(floor(nodes / 2), 1);
y = [y1; y2];
% generate the "booked" utilization
u = gen_utilisation(nodes);
x = x0 + u;
f = [y1; y2];
archive_z = [0 x'; zeros(max_iter, nodes+1)];
% create the time for the iterations to be saved
iter_run_time = zeros(max_iter, 1);
% -- Run the consensus algorithm -- %
% Perform the basic consensus algorithm (standard)
total_time = tic;
for k=1:max_iter
% start the tag
iter_time = tic;
x = P*x;
y = P*y;
z = (x./y).*f-u;
% finish the tag
iter_run_time(k) = micro_mul * toc(iter_time);
% record the numbers
archive_z(k+1, :) = [k z'];
% check about convergence - leave first row as that's
% the iteration number.
delta = abs(archive_z(k+1, 2:end) - archive_z(k, 2:end))';
% check which converged
converged = find(delta < e);
% check which have not converged yet
non_converged_yet = find(node_converged_at == 0);
% only check for a match if we have an index that has
% been converged but also we have non-converted indices as well
if ~isempty(converged) && ~isempty(non_converged_yet)
% this part is quite tricky - what this does is takes the
% intersection of the converted with the non_converted_yet indices
% and finds which ones are present in both!
%
% However, we are not interested in the actual indices, rather than
% the values that they hold, which are stored in the idcs variable.
[idcs, ~] = intersect(non_converged_yet, converged, 'stable');
% then using logical indexing we update the converge time for these
% nodes to be equal to the current iteration.
node_converged_at(idcs) = k;
end
end
total_time = micro_mul * toc(total_time);
% convergence statistics
cov_min = min(node_converged_at);
cov_max = max(node_converged_at);
cov_mean = mean(node_converged_at);
cov_win = cov_max - cov_min;
fprintf("\t== RUN INFO: mean conv. iter: %.2f, min: %d, max: %d, window: %d\n", ...
cov_mean, cov_min, cov_max, cov_win);
% store the aggregate statistics
cov_min_global(i, t) = cov_min;
cov_max_global(i, t) = cov_max;
cov_mean_global(i, t) = cov_mean;
cov_win_global(i, t) = cov_win;
% -- Plot the results -- %
if (extensive_plots == 1 || nodes <= 10) && plotted_for_trial == 0
fig = figure;
hold on;
plot(0:max_iter, archive_z(:, 2:nodes+1));
hold off
title("Values at each node vs Number of iterations", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
xlabel("Number of iterations", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
ylabel("Value at each node", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
% print the figure
st = sprintf("n_%d_iter_%d_values_for_each_node", nodes, k);
print_fig(fig, st, params);
% plot the execution time
fig = figure;
plot(1:max_iter, iter_run_time);
title(sprintf("Time per iteration with %d nodes", nodes), ...
"Interpreter", "Latex", "FontName", "Times New Roman");
xlabel("Number of iterations", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
ylabel("Time ($\mu s$)", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
% print the figure
st = sprintf("n_%d_iter_%d_iter_execution_time", nodes, k);
print_fig(fig, st, params);
end
% save global stats
iter_time_global(t, i, :) = iter_run_time;
total_time_global(t, i) = total_time;
fprintf("\t-- INFO: Finished %d nodes with exec %.3f μs and mean iter: %.3f μs\n", ...
nodes, total_time, mean(iter_run_time));
end
% raise it to avoid printing for the rest of the trials
plotted_for_trial = 1;
fprintf("** Finished trial %d\n", t);
end
fprintf("\n");
%% Final (aggregate) plots
%
% plot the mean execution time per node batch for all trials
fig = figure;
std_ttg = std(total_time_global) / sqrt(trials);
avg_ttg = mean(total_time_global);
errorbar(node_len_array, avg_ttg, std_ttg, '-*', 'LineWidth', 2);
title(sprintf("Average total time spend with %d iterations over %d trials", ...
max_iter, trials), "Interpreter", "Latex", "FontName", "Times New Roman");
xticks(node_len_array)
xticklabels(num2cell(nodes_to_test))
xlabel("Nodes", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
ylabel("Time ($\mu s$)", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
% print the figure
st = sprintf("n_%d_iter_%d_trials_%d_total_exec_time", nodes, k, trials);
print_fig(fig, st, params);
% plot the mean iteration execution time for node batch for all trials
fig = figure;
hold on;
wlegs = cell(1, node_test_len);
for i=node_len_array
cur_iter = squeeze(iter_time_global(i, :, :));
std_cur = std(cur_iter) / sqrt(trials);
mean_cur = mean(cur_iter);
% plot looks cleaner for this, but leaving errorbar as well.
% errorbar(mean_cur, std_cur, 'LineWidth', 2);
plot(mean_cur, 'LineWidth', 2);
wlegs{i} = sprintf("N=%d", nodes_to_test(i));
end
title("Average iteration time", ...
"Interpreter", "Latex", "FontName", "Times New Roman")
hold off;
xlabel("Iterations", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
ylabel("Time ($\mu s$)", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
legend(wlegs);
% print the figure
st = sprintf("n_%d_iter_%d_trials_%d_avg_iter_exec_time", nodes, k, trials);
print_fig(fig, st, params);
% plot the mean convergence for each node batch
fig = figure;
hold on;
% -- MIN CONVERGENCE -- %
min_avg = mean(cov_min_global, 2);
min_std = std(cov_min_global, 0, 2) / sqrt(trials);
errorbar(node_len_array, min_avg, min_std, '-*', 'LineWidth', 2);
% -- MAX CONVERGENCE -- %
max_avg = mean(cov_max_global, 2);
max_std = std(cov_max_global, 0, 2) / sqrt(trials);
errorbar(node_len_array, max_avg, max_std, '-*', 'LineWidth', 2);
% -- MEAN CONVERGENCE -- %
avg_avg = mean(cov_mean_global, 2);
avg_std = std(cov_mean_global, 0, 2) / sqrt(trials);
errorbar(node_len_array, avg_avg, avg_std, '-*', 'LineWidth', 2);
% -- WINDOW OF CONVERGENCE -- %
win_avg = mean(cov_win_global, 2);
win_std = std(cov_win_global, 0, 2) / sqrt(trials);
errorbar(node_len_array, win_avg, win_std, '-*', 'LineWidth', 2);
hold off;
xticks(node_len_array)
xticklabels(num2cell(nodes_to_test))
xlabel("Nodes", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
ylabel("Iteration", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
title("Convergence statistics", ...
"Interpreter", "Latex", "FontName", "Times New Roman");
legend("min", "max", "mean", "window");
% print the figure
st = sprintf("n_%d_iter_%d_trials_%d_agg_cov_statistics", nodes, k, trials);
print_fig(fig, st, params);