Skip to content

Commit ab597b6

Browse files
committed
Merge branch 'refs/heads/main' into internal-interface-experiments
# Conflicts: # src/simulation/controller.rs
2 parents c7842d8 + 13eb108 commit ab597b6

35 files changed

+361
-178
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ jobs:
1818
- name: Install other libraries
1919
run: sudo apt -y install libclang-dev llvm-dev libmetis-dev libopenmpi-dev
2020
- name: Build
21-
run: cargo build
21+
run: cargo build --release
2222
- name: Install mpirun
2323
run: cargo install --force cargo-mpirun
2424
- name: Run tests
2525
env:
2626
RUST_BACKTRACE: 1
27-
run: cargo test --verbose -- --test-threads=1
27+
run: cargo test --release --verbose -- --test-threads=1
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<vehicleDefinitions xmlns="http://www.matsim.org/files/dtd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.matsim.org/files/dtd http://www.matsim.org/files/dtd/vehicleDefinitions_v2.0.xsd">
4+
<vehicleType id="car">
5+
<attributes>
6+
</attributes>
7+
<description>abc</description>
8+
<length meter="9.5"/>
9+
<width meter="3.0"/>
10+
<maximumVelocity meterPerSecond="20.0"/>
11+
<passengerCarEquivalents pce="1.0"/>
12+
<networkMode networkMode="car"/>
13+
<flowEfficiencyFactor factor="1.5"/>
14+
</vehicleType>
15+
<vehicleType id="bike">
16+
<attributes>
17+
</attributes>
18+
<description>This is a bike</description>
19+
<length meter="2.0"/>
20+
<width meter="1.0"/>
21+
<maximumVelocity meterPerSecond="5.0"/>
22+
<passengerCarEquivalents pce="0.25"/>
23+
<networkMode networkMode="bike"/>
24+
<flowEfficiencyFactor factor="1.5"/>
25+
</vehicleType>
26+
<vehicleType id="walk">
27+
<attributes>
28+
<attribute name="lod" class="java.lang.String">teleported</attribute>
29+
</attributes>
30+
<description>This is a pair of shoes</description>
31+
<length meter="0.5"/>
32+
<width meter="1.0"/>
33+
<maximumVelocity meterPerSecond="1.2"/>
34+
<passengerCarEquivalents pce="0.1"/>
35+
<networkMode networkMode="walk"/>
36+
<flowEfficiencyFactor factor="10.0"/>
37+
</vehicleType>
38+
</vehicleDefinitions>

src/bin/convert_to_binary.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ fn main() {
2828
rust_q_sim::simulation::logging::init_std_out_logging();
2929
let args = InputArgs::parse();
3030

31-
let mut net = Network::from_file_path(&args.network, 1, PartitionMethod::None);
3231
let mut veh = Garage::from_file(&args.vehicles);
32+
let mut net = Network::from_file_path(&args.network, 1, PartitionMethod::None);
3333
let pop = Population::from_file(&args.population, &mut veh);
3434

3535
let cmp_weights = compute_computational_weights(&pop);
@@ -39,6 +39,7 @@ fn main() {
3939
net.to_file(&create_file_path(&args, "network"));
4040
veh.to_file(&create_file_path(&args, "vehicles"));
4141
pop.to_file(&create_file_path(&args, "plans"));
42+
info!("Finished conversion. Exiting.")
4243
}
4344

4445
fn create_file_path(args: &InputArgs, extension: &str) -> PathBuf {

src/experiments/stack_vs_heap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ fn run() {
4444
);
4545
println!("Heap result: {:?}, Time: {:?}", heap_result, heap_duration);
4646

47+
// divide by at least one
4748
println!(
4849
"Ratio Heap/Stack: {:?}",
49-
heap_duration.as_nanos() / stack_duration.as_nanos()
50+
heap_duration.as_nanos() / stack_duration.as_nanos().max(1)
5051
)
5152
}
5253

src/simulation/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl Config {
4343
config.set_output(Output {
4444
output_dir: out_dir,
4545
profiling: config.output().profiling,
46+
write_events: Default::default(),
4647
});
4748
}
4849
config
@@ -90,6 +91,7 @@ impl Config {
9091
let default = Output {
9192
output_dir: "./".to_string(),
9293
profiling: Profiling::None,
94+
write_events: Default::default(),
9395
};
9496
self.modules
9597
.borrow_mut()
@@ -157,6 +159,8 @@ pub struct Output {
157159
pub output_dir: String,
158160
#[serde(default)]
159161
pub profiling: Profiling,
162+
#[serde(default)]
163+
pub write_events: WriteEvents,
160164
}
161165

162166
#[derive(Serialize, Deserialize, Clone)]
@@ -244,6 +248,13 @@ pub enum Profiling {
244248
CSV(ProfilingLevel),
245249
}
246250

251+
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, Default)]
252+
pub enum WriteEvents {
253+
#[default]
254+
None,
255+
Proto,
256+
}
257+
247258
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, Default)]
248259
pub struct ProfilingLevel {
249260
#[serde(default = "default_profiling_level")]

src/simulation/controller.rs

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ use mpi::traits::{Communicator, CommunicatorCollectives};
1010
use nohash_hasher::IntMap;
1111
use tracing::info;
1212

13-
use crate::simulation::config::{CommandLineArgs, Config, PartitionMethod, RoutingMode};
14-
use crate::simulation::id::Id;
13+
use crate::simulation::config::{
14+
CommandLineArgs, Config, PartitionMethod, RoutingMode, WriteEvents,
15+
};
1516
use crate::simulation::io::proto_events::ProtoEventsWriter;
1617
use crate::simulation::messaging::communication::communicators::{
1718
ChannelSimCommunicator, MpiSimCommunicator, SimCommunicator,
1819
};
1920
use crate::simulation::messaging::communication::message_broker::NetMessageBroker;
2021
use crate::simulation::messaging::events::EventsPublisher;
21-
use crate::simulation::network::global_network::{Link, Network};
22+
use crate::simulation::network::global_network::Network;
2223
use crate::simulation::network::sim_network::SimNetworkPartition;
2324
use crate::simulation::population::population::Population;
2425
use crate::simulation::replanning::replanner::{DummyReplanner, ReRouteTripReplanner, Replanner};
@@ -111,13 +112,11 @@ fn execute_partition<C: SimCommunicator + 'static>(comm: C, args: &CommandLineAr
111112
));
112113
let mut garage = Garage::from_file(&PathBuf::from(config.proto_files().vehicles));
113114

114-
let population: Population = Population::from_file(
115-
&get_numbered_output_filename(
116-
&output_path,
117-
&PathBuf::from(config.proto_files().population),
118-
rank,
119-
),
115+
let population = Population::from_file_filtered_part(
116+
&PathBuf::from(config.proto_files().population),
117+
&network,
120118
&mut garage,
119+
comm.rank(),
121120
);
122121

123122
let network_partition = SimNetworkPartition::from_network(&network, rank, config.simulation());
@@ -130,14 +129,13 @@ fn execute_partition<C: SimCommunicator + 'static>(comm: C, args: &CommandLineAr
130129

131130
let events = Rc::new(RefCell::new(EventsPublisher::new()));
132131

133-
let events_file = format!("events.{rank}.binpb");
134-
let events_path = output_path.join(events_file);
135-
events
136-
.borrow_mut()
137-
.add_subscriber(Box::new(ProtoEventsWriter::new(&events_path)));
132+
if config.output().write_events == WriteEvents::Proto {
133+
let events_file = format!("events.{rank}.binpb");
134+
let events_path = output_path.join(events_file);
135+
events.borrow_mut().add_subscriber(Box::new(ProtoEventsWriter::new(&events_path)));
136+
}
138137
let travel_time_collector = Box::new(TravelTimeCollector::new());
139138
events.borrow_mut().add_subscriber(travel_time_collector);
140-
//events.add_subscriber(Box::new(EventsLogger {}));
141139

142140
let rc = Rc::new(comm);
143141

@@ -186,15 +184,13 @@ fn try_join(mut handles: IntMap<u32, JoinHandle<()>>) {
186184

187185
pub fn partition_input(config: &Config) {
188186
id::load_from_file(&PathBuf::from(config.proto_files().ids));
189-
let net = if let PartitionMethod::Metis(_) = config.partitioning().method {
187+
let _net = if let PartitionMethod::Metis(_) = config.partitioning().method {
190188
info!("Config param Partition method was set to metis. Loading input network, running metis conversion and then store it into output folder");
191189
partition_network(config)
192190
} else {
193191
info!("Config param Partition method was set to none. Loading network from input, assuming it has partitioning information");
194192
copy_network_into_output(config)
195193
};
196-
197-
partition_population(config, &net);
198194
}
199195

200196
fn partition_network(config: &Config) -> Network {
@@ -220,38 +216,6 @@ fn copy_network_into_output(config: &Config) -> Network {
220216
network
221217
}
222218

223-
fn partition_population(config: &Config, network: &Network) {
224-
info!(
225-
"Partition population into {} parts",
226-
config.partitioning().num_parts
227-
);
228-
let pop_in_path = PathBuf::from(config.proto_files().population);
229-
let num_parts = config.partitioning().num_parts;
230-
let pop = Population::from_file(&pop_in_path, &mut Garage::new());
231-
let mut part_pops = vec![];
232-
for _i in 0..num_parts {
233-
part_pops.push(Population::new())
234-
}
235-
236-
let pop_out_path =
237-
create_output_filename(&PathBuf::from(config.output().output_dir), &pop_in_path);
238-
239-
for (id, person) in pop.persons.into_iter() {
240-
let link_id: Id<Link> = Id::get(person.curr_act().link_id);
241-
let partition = network.get_link(&link_id).partition;
242-
part_pops
243-
.get_mut(partition as usize)
244-
.unwrap()
245-
.persons
246-
.insert(id, person);
247-
}
248-
249-
for (i, population) in part_pops.iter().enumerate() {
250-
let part_out_path = insert_number_in_proto_filename(&pop_out_path, i as u32);
251-
population.to_file(&part_out_path);
252-
}
253-
}
254-
255219
pub fn get_numbered_output_filename(output_dir: &Path, input_file: &Path, part: u32) -> PathBuf {
256220
let out = create_output_filename(output_dir, input_file);
257221
insert_number_in_proto_filename(&out, part)

src/simulation/io/proto.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,39 @@ pub fn write_to_file<T: Message>(message: T, path: &Path) {
3737
info!("Finished writing message to file: {path:?}");
3838
}
3939

40+
pub fn read_delimiter<R>(reader: &mut BufReader<R>) -> Option<usize>
41+
where
42+
R: Read + Seek,
43+
{
44+
// read the delimiter of the message. Prost says delimiter is between 1 and 10 bytes
45+
// so, read the first 10 bytes of the buffer
46+
let mut delim_buffer: [u8; 10] = [0; 10];
47+
// this could crash
48+
match reader.read_exact(&mut delim_buffer) {
49+
Ok(_) => {} // go on.
50+
Err(e) => match e.kind() {
51+
ErrorKind::UnexpectedEof => return None,
52+
_ => {
53+
panic!("Error while reading file: {}", e);
54+
}
55+
},
56+
};
57+
58+
let delimiter =
59+
prost::decode_length_delimiter(delim_buffer.as_slice()).expect("error reading delimiter");
60+
61+
// since the delimiter is a varint figure out how many bytes the delimiter was actually taking
62+
// up in the buffer. Set the buffers position to the first byte after the delimiter, which
63+
// should be the start of the TimeStep message
64+
let delim_encoded_len = prost::encoding::encoded_len_varint(delimiter as u64) as i64;
65+
let offset = delim_encoded_len - (delim_buffer.len() as i64);
66+
reader
67+
.seek_relative(offset)
68+
.expect("Seeking relative failed");
69+
70+
Some(delimiter)
71+
}
72+
4073
pub struct MessageIter<T, R>
4174
where
4275
T: Message + Default,
@@ -54,7 +87,7 @@ where
5487
type Item = T;
5588

5689
fn next(&mut self) -> Option<Self::Item> {
57-
if let Some(delimiter) = self.read_delimiter() {
90+
if let Some(delimiter) = read_delimiter(&mut self.internal_reader) {
5891
let mut bytes: Vec<u8> = vec![0; delimiter];
5992
self.internal_reader
6093
.read_exact(&mut bytes)
@@ -78,33 +111,4 @@ where
78111
internal_reader: BufReader::new(reader),
79112
}
80113
}
81-
fn read_delimiter(&mut self) -> Option<usize> {
82-
// read the delimiter of the message. Prost says delimiter is between 1 and 10 bytes
83-
// so, read the first 10 bytes of the buffer
84-
let mut delim_buffer: [u8; 10] = [0; 10];
85-
// this could crash
86-
match self.internal_reader.read_exact(&mut delim_buffer) {
87-
Ok(_) => {} // go on.
88-
Err(e) => match e.kind() {
89-
ErrorKind::UnexpectedEof => return None,
90-
_ => {
91-
panic!("Error while reading file: {}", e);
92-
}
93-
},
94-
};
95-
96-
let delimiter = prost::decode_length_delimiter(delim_buffer.as_slice())
97-
.expect("error reading delimiter");
98-
99-
// since the delimiter is a varint figure out how many bytes the delimiter was actually taking
100-
// up in the buffer. Set the buffers position to the first byte after the delimiter, which
101-
// should be the start of the TimeStep message
102-
let delim_encoded_len = prost::encoding::encoded_len_varint(delimiter as u64) as i64;
103-
let offset = delim_encoded_len - (delim_buffer.len() as i64);
104-
self.internal_reader
105-
.seek_relative(offset)
106-
.expect("Seeking relative failed");
107-
108-
Some(delimiter)
109-
}
110114
}

0 commit comments

Comments
 (0)