-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
1963 lines (1635 loc) · 69.4 KB
/
main.rs
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter};
use std::{fs, vec};
use std::time::{Duration, Instant, SystemTime};
use petgraph::{Graph, Undirected};
use petgraph::algo::{bellman_ford};
use petgraph::graph::{NodeIndex, UnGraph};
use petgraph::visit::{Bfs, Dfs, IntoEdges, IntoNeighbors, NodeCount};
use rand::prelude::{IteratorRandom, StdRng};
use rand::{RngCore, SeedableRng};
use rand::distributions::{Distribution, WeightedIndex};
use serde_json::Value;
use rayon::prelude::*;
use rand::prelude::SliceRandom;
//Represents the semantics of recursive headers, not the bit layout.
#[derive(Clone, Debug)]
struct Header {
destination: NodeIndex, //next destination of a header
next_header: Vec<Header>, //sub headers in SEET style
rbs_record: HashSet<NodeIndex>, //nodes that are reached through an RBS header starting from destination
leaves: Vec<NodeIndex>, //all destinations addressed by the header including subheaders
header_size: u64, //current header size in bytes (only SEET+RBS fields)
hops: u64, //hops in the forwarding tree including forwarding of subheaders
broadcast: bool,
rbs_length: u8,
pre_index: usize,
}
impl Display for Header {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?} B:{:?} [{}]",
self.destination,
self.rbs_record,
self.next_header.iter().fold(
"".to_string(),
|mut res, s| {
res.push_str(&s.to_string());
res.push(',');
res
}
)
)
}
}
struct BitstringConfiguration {
leave_subsets: Vec<Vec<HashSet<NodeIndex>>>,
}
type BitstringConfigurations = HashMap<NodeIndex, BitstringConfiguration>;
impl BitstringConfiguration {
fn new(neighbors: Vec<NodeIndex>, max_length: u8) -> BitstringConfiguration {
let mut leave_subsets = Vec::with_capacity(max_length as usize);
leave_subsets.push(vec![HashSet::new()]);
for l in 1 ..= max_length as u32 {
let mut subsets = Vec::with_capacity(16);
let mut start_index = 0usize;
let size = (l*8) as usize;
for _i in 0 .. 16 {
let mut receivers = HashSet::with_capacity(8*l as usize);
for j in start_index .. (start_index + size).min(neighbors.len()) {
receivers.insert(neighbors[j]);
}
start_index += size;
subsets.push(receivers);
if start_index >= neighbors.len() {
break;
}
}
leave_subsets.push(subsets);
if l*8 > neighbors.len() as u32 {
break;
}
}
BitstringConfiguration {
leave_subsets
}
}
fn find_optimal_length(&self, destinations: &HashSet<NodeIndex>, min_length: u8, new_dest: NodeIndex, pre_index: usize) -> Option<(u8, usize)> {
//if self.leave_subsets[min_length as usize][pre_index].contains(&new_dest) {
// return Some((min_length, pre_index))
//}
for l in min_length .. self.leave_subsets.len() as u8 {
for i in 0 .. self.leave_subsets[l as usize].len() {
let subset = &self.leave_subsets[l as usize][i];
if destinations.is_subset(subset) {
return Some((l, i))
}
}
}
None
}
}
//Represents the packet layout, i.e., the length of the SEET and RBS fields.
struct PacketFormat {
bitstring_length_field: u32, //number of bits used to indicate the length of the bitstring in RBS
additional_seet_bits: u32, //deliver bit, broadcast bit, whatever Chef comes up with next
seet_length_field: u32, //seet length field in bits
identifier_bits: u32, //number of bits used for identifier in SEET
padding_bits: u32, //padding bits in SEET header
payload: u64, //bytes
additional_header: u64 //IP header, Ethernet header, whatever in bytes
}
impl PacketFormat {
//Constructs a packet format struct by computing the padding bits and the length of the SEET identifier.
fn new(num_nodes: usize, additional_seet_bits: u32, bitstring_length_field: u32, payload: u64, additional_header: u64) -> PacketFormat {
let identifier_bits = if num_nodes.is_power_of_two() {
(num_nodes as f64).log2() as u32
} else {
(num_nodes as f64).log2() as u32 + 1
};
let padding_bits = if (additional_seet_bits + identifier_bits) % 8 == 0 {
0
} else {
8 - ((additional_seet_bits + identifier_bits) % 8)
};
PacketFormat {
bitstring_length_field,
additional_seet_bits,
seet_length_field: 8,
identifier_bits,
padding_bits,
payload,
additional_header,
}
}
}
impl Header {
//Recursively computes the overall traffic of a message, including payload and other headers.
fn eval_header_traffic(&self, pf: &PacketFormat, paths: &mut GraphPaths) -> u64 {
let path = paths.path(self.destination);
let hops = path.len()-1;
let mut header_bytes = (pf.additional_seet_bits + pf.identifier_bits + pf.padding_bits + pf.seet_length_field) / 8;
if self.rbs_length > 0 {
header_bytes += self.rbs_length as u32 +1;
}
let recursive_header_bytes = self.next_header.iter().fold(0, |sum, s| sum + s.eval_header_traffic(pf, paths));
(hops * header_bytes as usize) as u64 + recursive_header_bytes
}
//Base case header, i.e., one SEET header to the destination.
fn new_base_case(destination: NodeIndex, pf: &PacketFormat, paths: &mut GraphPaths, bitstring_configurations: &BitstringConfigurations) -> Header {
let header_bytes = ((pf.additional_seet_bits + pf.identifier_bits + pf.identifier_bits + pf.padding_bits) / 8) as u64;
let penultimate = paths.penultimate_hop(destination).unwrap();
let mut rbs = HashSet::with_capacity(paths.num_neighbors(penultimate) as usize);
rbs.insert(destination);
let (size, index) = bitstring_configurations[&penultimate].find_optimal_length(&rbs, 0, destination, 0).unwrap();
Header {
destination: penultimate,
next_header: vec![],
rbs_record: rbs,
leaves: vec![destination],
header_size: header_bytes + size as u64,
hops: (paths.path(destination).len() - 1) as u64,
broadcast: false,
rbs_length: size,
pre_index: index,
}
}
//Adds a destination recursively to a header.
//Returns the additional header size and the additional hops that result from the new destination.
fn add_destination(&mut self, destination: NodeIndex, paths: &mut GraphPaths, pf: &PacketFormat, bitstring_configurations: &BitstringConfigurations) -> (u64, i64) {
self.leaves.push(destination);
if destination == self.destination {
return (0,0)
}
let replication_node = paths.replication_node(destination, self.destination);
if replication_node == self.destination {
for child in &mut self.next_header {
let rn = paths.replication_node(destination, child.destination);
if rn != self.destination {
let growth = child.add_destination(destination, paths, pf, bitstring_configurations);
self.header_size = (self.header_size as i64 + growth.1) as u64;
self.hops += growth.0;
return growth;
}
}
match paths.penultimate_hop(destination) {
None => {return (0, 0)}
Some(penultimate_hop) => {
if self.destination == penultimate_hop {
self.rbs_record.insert(destination);
self.hops += 1;
let config = &bitstring_configurations[&self.destination];
return match config.find_optimal_length(&self.rbs_record, self.rbs_length, destination, self.pre_index) {
None => {
self.header_size += 10000000;
(1, 10000000)
}
Some((size, index)) => {
let mut additional_header = if self.rbs_record.len() == 1 {
1
} else {
0
};
additional_header += size - self.rbs_length;
self.rbs_length = size;
self.pre_index = index;
self.header_size += additional_header as u64;
(1, additional_header as i64)
}
}
} else {
let new_header = Header {
destination,
next_header: vec![],
rbs_record: HashSet::with_capacity(paths.num_neighbors(destination) as usize),
leaves: vec![],
header_size: 0,
hops: 0,
broadcast: false,
rbs_length: 0,
pre_index: 0,
};
self.next_header.push(new_header);
self.header_size += ((pf.padding_bits + pf.additional_seet_bits + pf.seet_length_field + pf.identifier_bits) / 8) as u64;
self.hops += (paths.distances[destination.index()] - paths.distances[replication_node.index()]) as u64;
return ((paths.distances[destination.index()] - paths.distances[replication_node.index()]) as u64, ((pf.padding_bits + pf.additional_seet_bits + pf.seet_length_field + pf.identifier_bits) / 8) as i64);
}
}
}
} else {
let penultimate1 = paths.penultimate_hop(self.destination).unwrap();
let penultimate2 = paths.penultimate_hop(destination).unwrap();
if penultimate1 == penultimate2 && self.next_header.is_empty() {
self.rbs_record.insert(destination);
self.rbs_record.insert(self.destination);
self.destination = penultimate1;
self.hops += 1;
let config = &bitstring_configurations[&self.destination];
return match config.find_optimal_length(&self.rbs_record, self.rbs_length, destination, self.pre_index) {
None => {
self.header_size += 10000000;
(1, 10000000)
}
Some((size, index)) => {
let mut additional_header = if self.rbs_record.len() == 1 {
1
} else {
0
};
additional_header += size - self.rbs_length;
self.pre_index = index;
self.rbs_length = size;
self.header_size += additional_header as u64;
(1, additional_header as i64)
}
}
} else {
self.split_at_replication_node(replication_node, destination);
self.header_size += 2*((pf.padding_bits + pf.additional_seet_bits + pf.seet_length_field + pf.identifier_bits) / 8) as u64;
self.hops += (paths.distances[destination.index()] - paths.distances[replication_node.index()]) as u64;
return ((paths.distances[destination.index()] - paths.distances[replication_node.index()]) as u64, 2*((pf.padding_bits + pf.additional_seet_bits + pf.seet_length_field + pf.identifier_bits) / 8) as i64);
}
}
}
fn split_at_replication_node(&mut self, replication_node: NodeIndex, destination: NodeIndex) {
let mut new_header = Header {
destination: self.destination,
next_header: vec![],
rbs_record: HashSet::with_capacity(16),
leaves: vec![],
header_size: 0,
hops: 0,
broadcast: false,
rbs_length: 0,
pre_index: 0,
};
new_header.next_header.append(&mut self.next_header);
new_header.rbs_record.extend(self.rbs_record.iter());
self.rbs_record.clear();
let new_header_dest = Header {
destination,
next_header: vec![],
rbs_record: HashSet::with_capacity(16),
leaves: vec![],
header_size: 0,
hops: 0,
broadcast: false,
rbs_length: 0,
pre_index: 0,
};
self.next_header.push(new_header);
self.next_header.push(new_header_dest);
self.destination = replication_node;
}
}
//Returns bitstring length for a given Bitstring ID. Length is byte aligned.
fn bid_to_bitstring_length(num_neighbors: u32, bid: u8) -> u32 {
let quarter_len = if num_neighbors % 4 == 0 {
num_neighbors / 4
} else {
num_neighbors / 4 + 1
};
let len = bid.count_ones() * quarter_len;
if len % 8 == 0 {
len / 8
} else {
len / 8 + 1
}
}
fn bitstring_configs(graph: &Graph<(), f64, Undirected>) -> BitstringConfigurations {
let mut bitstring_configs = HashMap::with_capacity(graph.node_count());
for node in graph.node_indices() {
let mut neighbors = graph.neighbors(node).collect::<Vec<_>>();
neighbors.sort_by(|n1, n2| n1.index().cmp(&n2.index()));
let bitstring_config = BitstringConfiguration::new(neighbors, 16);
bitstring_configs.insert(node, bitstring_config);
}
bitstring_configs
}
//Abstracts away shortest path algorithm.
//Paths are computed lazily, i.e., only when a path is requested for the first time and paths are cached for future requests.
//Some topologies such as Torus may benefit from Floyd-Warshall instead of Bellman-Ford.
struct GraphPaths<'a> {
source: NodeIndex,
graph: &'a Graph<(), f64, Undirected>,
paths: HashMap<NodeIndex, Vec<NodeIndex>>,
predecessors: Vec<Option<NodeIndex>>,
distances: Vec<f64>,
}
impl<'a> GraphPaths<'a> {
fn new(source: NodeIndex, graph: &Graph<(), f64, Undirected>) -> GraphPaths {
let shortest_paths = bellman_ford(&graph, source).unwrap();
let predecessors = shortest_paths.predecessors;
let distances = shortest_paths.distances;
let mut paths = HashMap::with_capacity(graph.node_count());
GraphPaths {
source,
graph,
paths,
predecessors,
distances,
}
}
//Computes path as node.
fn compute_path(graph: &Graph<(), f64, Undirected>, predecessors: &Vec<Option<NodeIndex>>, source: NodeIndex, destination: NodeIndex) -> Vec<NodeIndex> {
let mut current = destination;
let mut path = Vec::with_capacity(10);
while current != source {
path.push(current);
current = predecessors[current.index()].unwrap();
}
path.push(source);
path.reverse();
path
}
//Returns path as node list.
fn path(&mut self, destination: NodeIndex) -> &Vec<NodeIndex> {
if !self.paths.contains_key(&destination) {
self.paths.insert(destination, GraphPaths::compute_path(self.graph, &self.predecessors, self.source, destination));
}
&self.paths[&destination]
}
//Returns the node where paths to d1 and d2 diverge.
fn replication_node(&mut self, d1: NodeIndex, d2: NodeIndex) -> NodeIndex {
if !self.paths.contains_key(&d1) {
self.paths.insert(d1, GraphPaths::compute_path(self.graph, &self.predecessors, self.source, d1));
}
if !self.paths.contains_key(&d2) {
self.paths.insert(d2, GraphPaths::compute_path(self.graph, &self.predecessors, self.source, d2));
}
let p1 = &self.paths[&d1];
let p2 = &self.paths[&d2];
let shorter_path = if p1.len() < p2.len() {
&p1
} else {
&p2
};
let longer_path = if p1.len() < p2.len() {
&p2
} else {
&p1
};
for (index, node) in shorter_path.iter().enumerate() {
if longer_path[index] != *node {
return longer_path[index-1]
}
}
*shorter_path.last().unwrap()
}
//Returns the penultimate hop of some node relative to the shortest path from self.source.
fn penultimate_hop(&mut self, destination: NodeIndex) -> Option<NodeIndex> {
if self.source == destination {
None
} else {
let path = self.path(destination);
Some(path[path.len()-2])
}
}
//Returns the number of neighbors of some node. Used for RBS bitstring length.
fn num_neighbors(&self, node: NodeIndex) -> u32 {
self.graph.neighbors(node).count() as u32
}
fn neighbor_bid(&self, node: NodeIndex, neighbor: NodeIndex) -> u8 {
let mut neighbors = self.graph.neighbors(node).collect::<Vec<_>>();
neighbors.sort_by(|n1, n2| n1.index().cmp(&n2.index()));
let pos = neighbors.iter().position(|n| *n == neighbor).unwrap();
if pos < neighbors.len()/2 {
if pos < neighbors.len()/4 {
1
} else {
2
}
} else {
if pos < 3*neighbors.len()/4 {
4
} else {
8
}
}
}
//Returns the number of neighbors of some node that are leaves, i.e, have node degree 1.
fn num_leave_neighbors(&self, node: NodeIndex) -> u32 {
let mut num = 0;
self.graph.neighbors(node).for_each(|n| if self.graph.edges(n).count() == 1 {num += 1;} else {});
num
}
fn neighbors(&self, node: NodeIndex) -> Vec<NodeIndex> {
self.graph.neighbors(node).collect::<Vec<_>>()
}
}
//Computes the traffic including header and payload to reach all destinations with IP unicast.
fn traffic_unicast(destinations: &Vec<NodeIndex>, pf: &PacketFormat, paths: &mut GraphPaths) -> EvalResult {
let mut traffic = 0;
let mut hops = 0;
for d in destinations.iter().copied() {
let path = paths.path(d);
traffic += (path.len()-1) as u64 * (pf.payload+pf.additional_header);
hops += (path.len()-1) as u64;
}
let num_destinations = if destinations.contains(&paths.source) {
destinations.len()-1
} else {
destinations.len()
} as f64;
let mc_traffic = traffic_multicast(destinations, pf, paths);
EvalResult {
overall_traffic: traffic as f64,
pkts: hops as f64,
source_traffic: (pf.payload+pf.additional_header) as f64 * destinations.len() as f64,
pkts_from_source: num_destinations,
additional_packets: hops as f64 - mc_traffic.pkts,
denseness: 0.0,
}
}
//Computes the traffic including header and payload to reach all destinations with IPMC.
fn traffic_multicast(destinations: &Vec<NodeIndex>, pf: &PacketFormat, paths: &mut GraphPaths) -> EvalResult {
let mut edges = HashSet::with_capacity(destinations.len());
let mut source_pkts = 0;
for d in destinations.iter().copied() {
let path = paths.path(d);
for i in 1 .. path.len() {
if i == 1 && !edges.contains(&(path[i-1], path[i])) {
source_pkts += 1;
}
edges.insert((path[i-1], path[i]));
}
}
let overall_traffic = edges.len() as u64 * (pf.payload+pf.additional_header);
EvalResult {
overall_traffic: overall_traffic as f64,
pkts: edges.len() as f64,
source_traffic: ((pf.payload+pf.additional_header) * source_pkts) as f64,
pkts_from_source: source_pkts as f64,
additional_packets: 0.0,
denseness: 0.0,
}
}
//Reads graph files used for BIER scalability paper and constructs petgraph instance.
fn read_brite(filename: &String) -> (Graph<(), f64, Undirected>, HashMap<u32, NodeIndex>) {
let input = fs::read_to_string(filename).unwrap();
let lines = input.lines();
let mut result: Graph<(), f64, Undirected> = Graph::new_undirected();
let mut read_nodes = false;
let mut read_edges = false;
let mut map: HashMap<u32, NodeIndex> = HashMap::new();
for line in lines {
if line.starts_with("Nodes") {
read_nodes = true;
} else if line.starts_with("Edges") {
read_edges = true;
} else if line.eq("") {
read_edges = false;
read_nodes = false;
} else {
let cols = line.split("\t").collect::<Vec<_>>();
if read_nodes {
let next_node_id: u32 = cols[0].parse().unwrap();
let next_node = result.add_node(());
map.insert(next_node_id, next_node);
} else if read_edges {
let first_node_id: u32 = cols[1].parse().unwrap();
let second_node_id: u32 = cols[2].parse().unwrap();
result.add_edge(map[&first_node_id], map[&second_node_id], 1.0);
}
}
}
(result, map)
}
//Reads SDs for graphs from BIER scalability.
fn parse_bier_sds(filename: String, map: HashMap<u32, NodeIndex>, bitstring_length: u32) -> Vec<Vec<NodeIndex>> {
let input: Value = serde_json::from_str(&*fs::read_to_string(filename).unwrap()).unwrap();
let bitstring_sol = input.get(bitstring_length.to_string()).unwrap().as_array().unwrap();
let mut sol = Vec::with_capacity(bitstring_sol.len());
for sd in bitstring_sol {
sol.push(sd.as_array().unwrap().iter().map(|i| map[&(i.as_u64().unwrap() as u32)]).collect());
}
sol
}
struct Mesh {
name: String,
graph: Graph<(), f64, Undirected>,
hosts: Vec<NodeIndex>,
map: HashMap<u32, NodeIndex>
}
fn construct_mesh(name: &str) -> Mesh {
let (graph, map) = read_brite(&(name.to_string() + ".brite"));
let mut hosts = Vec::with_capacity(graph.node_count());
hosts.extend(graph.node_indices());
Mesh {
name: name.to_string(),
graph,
hosts,
map,
}
}
impl Topology for Mesh {
fn graph(&self) -> &Graph<(), f64, Undirected> {
&self.graph
}
fn hosts(&self) -> &Vec<NodeIndex> {
&self.hosts
}
fn bier_sds(&self, bitstring_length: u32) -> Vec<Vec<NodeIndex>> {
let input: Value = serde_json::from_str(&*fs::read_to_string(self.name.clone() + ".brite.sol").unwrap()).unwrap();
let bitstring_sol = input.get(bitstring_length.to_string()).unwrap().as_array().unwrap();
let mut sol = Vec::with_capacity(bitstring_sol.len());
for sd in bitstring_sol {
sol.push(sd.as_array().unwrap().iter().map(|i| self.map[&(i.as_u64().unwrap() as u32)]).collect());
}
sol
}
fn sample(&self, num: u32, s: f64, rng: &mut StdRng) -> Vec<NodeIndex> {
let mut destinations = self.hosts.iter().copied().choose_multiple(rng, num as usize);
destinations.sort_by(|n1, n2| n1.index().cmp(&n2.index()));
destinations
}
}
struct Mesh2 {
name: String,
graph: Graph<(), f64, Undirected>,
hosts: Vec<NodeIndex>,
map: HashMap<u32, NodeIndex>,
seed: u64,
random_sds: bool,
}
fn construct_mesh2(name: &str, leaves: u32, seed: u64, random_sds: bool) -> Mesh2 {
let (mut graph, map) = read_brite(&(name.to_string() + ".brite"));
let mut hosts = Vec::with_capacity(graph.node_count());
for node in graph.node_indices().collect::<Vec<_>>() {
for _l in 0 .. leaves {
let n = graph.add_node(());
graph.add_edge(node , n, 1.0);
hosts.push(n);
}
}
//hosts.append(&mut graph.node_indices().collect::<Vec<_>>());
Mesh2 {
name: name.to_string(),
graph,
hosts,
map,
seed,
random_sds,
}
}
impl Topology for Mesh2 {
fn graph(&self) -> &Graph<(), f64, Undirected> {
&self.graph
}
fn hosts(&self) -> &Vec<NodeIndex> {
&self.hosts
}
fn bier_sds(&self, bitstring_length: u32) -> Vec<Vec<NodeIndex>> {
if self.random_sds {
let mut rng = StdRng::seed_from_u64(self.seed);
let num_sds = (self.graph.node_count() as f64 / bitstring_length as f64).ceil() as usize;
let mut sds: Vec<Vec<NodeIndex>> = vec![Vec::with_capacity(bitstring_length as usize); num_sds];
for v in self.graph.node_indices() {
let mut sd: &mut Vec<NodeIndex> = sds.choose_mut(&mut rng).unwrap();
while sd.len() >= bitstring_length as usize {
sd = sds.choose_mut(&mut rng).unwrap();
}
sd.push(v);
}
sds
} else {
self.compute_bier_sds(bitstring_length, self.seed)
}
}
fn sample(&self, num: u32, s: f64, rng: &mut StdRng) -> Vec<NodeIndex> {
let mut destinations = self.hosts.iter().copied().choose_multiple(rng, num as usize);
destinations.sort_by(|n1, n2| n1.index().cmp(&n2.index()));
destinations
}
}
impl Mesh2 {
fn compute_bier_sds(&self, bitstring_length: u32, seed: u64) -> Vec<Vec<NodeIndex>> {
let mut rng = StdRng::seed_from_u64(seed);
let num_sds = (self.graph.node_count() as f64 / bitstring_length as f64).ceil() as usize;
let mut best_sds = vec![Vec::with_capacity(self.graph.node_count()); num_sds];
for iteration in 0 .. 1 {
let mut seeds = self.hosts.choose_multiple(&mut rng, num_sds).copied().collect::<Vec<_>>();
let mut visited: HashSet<NodeIndex> = HashSet::with_capacity(self.graph.node_count());
let mut bfs_searches = seeds.iter().map(|s| Bfs::new(&self.graph, *s)).collect::<Vec<_>>();
let mut current = 0;
let mut sds = vec![Vec::with_capacity(self.graph.node_count()); num_sds];
while (self.graph.node_count() > visited.len()) {
let mut next = bfs_searches[current % num_sds].next(&self.graph).unwrap();
while (visited.contains(&next)) {
next = bfs_searches[current % num_sds].next(&self.graph).unwrap();
}
visited.insert(next);
sds[current % num_sds].push(next);
current += 1;
}
best_sds = sds;
}
best_sds
}
}
//Computes the traffic including header and payload to reach all destinations with BIER.
fn traffic_bier(destinations: &Vec<NodeIndex>, sds: &Vec<Vec<NodeIndex>>, bitstring_length: u32, pf: &PacketFormat, paths: &mut GraphPaths) -> EvalResult {
let mut traffic = 0;
let mut hops = 0;
let mut source_pkts = 0;
let mut source_traffic = 0;
let mut denseness = 0.0;
let mut covered_sds = 0;
for sd in sds {
let mut edges = HashSet::with_capacity(destinations.len());
let mut set_bits = 0;
let mut sd_size = 0;
for d in destinations.iter().copied() {
if !sd.contains(&d) {
continue;
}
sd_size += 1;
let path = paths.path(d);
set_bits += path.len()-1;
for i in 1 .. path.len() {
if i == 1 && !edges.contains(&(path[i-1], path[i])) {
source_pkts += 1;
source_traffic += pf.payload+pf.additional_header+(bitstring_length as u64/8);
}
edges.insert((path[i-1], path[i]));
}
}
if sd_size > 0 && edges.len() > 0 {
denseness += (set_bits as f64) / (edges.len() as f64);
covered_sds += 1;
}
hops += edges.len() as u64;
traffic += edges.len() as u64 * (pf.payload+pf.additional_header+(bitstring_length as u64/8));
}
let mc_traffic = traffic_multicast(destinations, pf, paths);
EvalResult {
overall_traffic: traffic as f64,
pkts: hops as f64,
source_traffic: source_traffic as f64,
pkts_from_source: source_pkts as f64,
additional_packets: hops as f64 - mc_traffic.pkts,
denseness: denseness / (covered_sds as f64),
}
}
fn combined_traffic(destinations: &Vec<NodeIndex>, headers: &Vec<Header>, pf: &PacketFormat, paths: &mut GraphPaths) -> EvalResult {
let mut overall_traffic = 0.0;
let mut hops = 0.0;
let mut source_traffic = 0.0;
for header in headers {
overall_traffic += (header.eval_header_traffic(pf, paths) + header.hops*(pf.payload+pf.additional_header)) as f64;
source_traffic += (header.header_size + pf.payload + pf.additional_header) as f64;
hops += header.hops as f64;
}
let mc_traffic = traffic_multicast(destinations, pf, paths);
EvalResult {
overall_traffic,
pkts: hops,
source_traffic,
pkts_from_source: headers.len() as f64,
additional_packets: hops-mc_traffic.pkts,
denseness: 0.0,
}
}
//Computes partitioning of destinations such that the overall traffic is small when one message is used for every partition.
fn optimize_second_approach(source: NodeIndex, destinations: &Vec<NodeIndex>, max_header_size: u64, pf: &PacketFormat, graph: &Graph<(), f64, Undirected>, mut paths: &mut GraphPaths, bitstring_configurations: &BitstringConfigurations) -> Vec<Header> {
let mut result = Vec::with_capacity(destinations.len());
let mut current_nodes = Vec::with_capacity(destinations.len());
let mut header = None;
let mut destination_set: HashSet<NodeIndex> = HashSet::with_capacity(destinations.len());
destination_set.extend(destinations.iter());
let mut path_links = HashSet::with_capacity(graph.node_count());
for n in destinations {
let path_nodes = paths.path(*n);
for i in 1 .. path_nodes.len() {
path_links.insert((path_nodes[i-1], path_nodes[i]));
}
}
let mut stack = Vec::with_capacity(graph.node_count());
stack.push(source);
let mut visited = HashSet::with_capacity(graph.node_count());
while let Some(node) = stack.pop() {
visited.insert(node);
let mut neighbors = graph.neighbors(node).collect::<Vec<_>>();
neighbors.retain(|n| !visited.contains(n) && path_links.contains(&(node, *n)));
neighbors.sort_by(|n1, n2| n1.index().cmp(&n2.index())); //allows optimization with Bitstring IDs
for n in &neighbors {
stack.insert(0, *n);
}
if !destination_set.contains(&node) || source == node {
continue;
}
if current_nodes.len() == 0 {
header = Some(Header::new_base_case(node, pf, paths, bitstring_configurations));
current_nodes.push(node);
} else {
let n1 = paths.path(current_nodes[0])[1];
let n2 = paths.path(node)[1];
if n1 != n2 {
let mut new_header = Header::new_base_case(current_nodes[0], pf, paths, bitstring_configurations);
for i in 1 .. current_nodes.len() {
new_header.add_destination(current_nodes[i], paths, pf, bitstring_configurations);
}
result.push(new_header.clone());
header = None;
current_nodes.clear();
header = Some(Header::new_base_case(node, pf, paths, bitstring_configurations));
current_nodes.push(node);
} else {
let s = header.as_mut().unwrap();
s.add_destination(node, paths, pf, bitstring_configurations);
if s.header_size > max_header_size {
let mut new_header = Header::new_base_case(current_nodes[0], pf, paths, bitstring_configurations);
for i in 1 .. current_nodes.len() {
new_header.add_destination(current_nodes[i], paths, pf, bitstring_configurations);
}
result.push(new_header.clone());
header = None;
current_nodes.clear();
header = Some(Header::new_base_case(node, pf, paths, bitstring_configurations));
current_nodes.push(node);
} else {
current_nodes.push(node);
}
}
}
}
if current_nodes.len() > 0 {
let mut new_header = Header::new_base_case(current_nodes[0], pf, paths, bitstring_configurations);
for i in 1 .. current_nodes.len() {
new_header.add_destination(current_nodes[i], paths, pf, bitstring_configurations);
}
let traffic = new_header.eval_header_traffic(pf, paths) + new_header.hops*(pf.payload+pf.additional_header);
result.push(new_header.clone());
}
result
}
//Trait for network topologies.
//Allows to implement specific algorithms for BIER SDs and node sampling for special cases like Fat Tree and Torus.
trait Topology {
fn graph(&self) -> &Graph<(), f64, Undirected>;
fn hosts(&self) -> &Vec<NodeIndex>;
fn bier_sds(&self, bitstring_length: u32) -> Vec<Vec<NodeIndex>>;
fn sample(&self, num: u32, s: f64, rng: &mut StdRng) -> Vec<NodeIndex>;
}
//Prof. Menth's preferred topology for highly meshed networks.
//Basically, Grid network where nodes are connected to exactly 4 neighbors, wrap around for first/last line/column.
struct Torus {
graph: Graph<(), f64, Undirected>,
k: u32,
nodes: HashMap<(u32, u32), NodeIndex>,
hosts: Vec<NodeIndex>,
}
fn generate_torus(k: u32) -> Torus {
let mut graph: Graph<(), f64, Undirected> = Graph::with_capacity(k.pow(2) as usize, (4*k.pow(2) as usize));
let mut nodes = HashMap::with_capacity(k.pow(2) as usize);
let mut hosts = Vec::with_capacity(k.pow(2) as usize);
//nodes
for i in 0 .. k { //line
for j in 0 .. k { //column
let node = graph.add_node(());
nodes.insert((i, j), node);
hosts.push(node);
}
}
//edges
for i in 0 .. k { //line
for j in 0 .. k { //column
let node = nodes[&(i, j)];
let right = nodes[&(i, (j+1) % k)];
let bottom = nodes[&((i+1) % k, j)];
graph.add_edge(node, right, 1.0);
graph.add_edge(node, bottom, 1.0);
}
}
Torus {
graph,
k,
nodes,
hosts,
}
}
impl Topology for Torus {
fn graph(&self) -> &Graph<(), f64, Undirected> {
&self.graph
}
fn hosts(&self) -> &Vec<NodeIndex> {
&self.hosts
}
//Torus is symmetric, so it does not matter how SDs are computed as long as they are compact.
fn bier_sds(&self, bitstring_length: u32) -> Vec<Vec<NodeIndex>> {
let mut sds = Vec::with_capacity((self.k / bitstring_length) as usize + 1);
let mut current_sd = Vec::with_capacity(bitstring_length as usize);
for line in 0 .. self.k {
for column in 0 .. self.k {
current_sd.push(self.nodes[&(line, column)]);
if current_sd.len() == bitstring_length as usize {
sds.push(current_sd);
current_sd = Vec::with_capacity((bitstring_length) as usize);
}
}
}
if current_sd.len() > 0 {
sds.push(current_sd);
}
sds
}
//Uniform sampling
fn sample(&self, num: u32, s: f64, rng: &mut StdRng) -> Vec<NodeIndex> {
let mut destinations = self.graph.node_indices().choose_multiple(rng, num as usize);
destinations.sort_by(|n1, n2| n1.index().cmp(&n2.index()));
destinations
}