Skip to content

Commit 868d213

Browse files
committed
Remove dependency to lazy_static
The `lazy_static` crate is no longer necessary as the `Mutex::new()` method is now `const`, i.e. it can be safely declared in static contexts. Also updated the `serialize` function to use the serde `to_string()` function instead of `to_string_pretty()`.
1 parent 4477f8b commit 868d213

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

interpol-rs/Cargo.toml

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
[package]
22
name = "interpol-rs"
3-
version = "0.2.5"
3+
version = "0.3.0"
44
edition = "2021"
55

66
[build-dependencies]
77
cbindgen = "0.20.0"
88

99
[dependencies]
10-
derive_builder = "0.10.2"
11-
lazy_static = "1.4.0"
10+
derive_builder = "0.10"
1211
serde = { version = "1.0", features = ["derive"] }
1312
serde_json = "1.0"
14-
rayon = "1.5.2"
15-
typetag = "0.1.8"
13+
rayon = "1.5"
14+
typetag = "0.1"
1615

1716
[lib]
1817
crate-type = ["cdylib"] # shared library (.so)

interpol-rs/src/interpol.rs

+24-28
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::mpi_events::{
1818
};
1919
use crate::types::{MpiCallType, MpiComm, MpiOp, MpiRank, MpiReq, MpiTag, Tsc, Usecs};
2020
use crate::InterpolError;
21-
use lazy_static::lazy_static;
2221
use rayon::prelude::*;
2322
use std::fs::{self, File};
2423
use std::io::Write;
@@ -61,30 +60,28 @@ macro_rules! impl_register {
6160
};
6261
}
6362

64-
lazy_static! {
65-
/// A vector that keeps track of interposed MPI functions called by a process.
66-
///
67-
/// The `lazy_static` macro creates static objects that are only initialized when needed at
68-
/// runtime. In this case, this implementation is similar to a singleton. It removes the need
69-
/// to pass a constant pointer on the `Vec` back to the C part of the interposition library,
70-
/// therefore avoiding the use of `unsafe` code sections.
71-
///
72-
/// As the MPI standard allows for processes to run code in parallel (e.g. through libraries
73-
/// like OpenMP or pthread), the `Vec` *must* be wrapped in a `Mutex` to prevent concurrent
74-
/// attempts at pushing onto the traces vector. Each time an event is registered, the caller
75-
/// must first take the lock on the `Mutex` before pushing an `Event`. This ensures that
76-
/// `interpol-rs` is thread-safe even in `MPI_THREAD_MULTIPLE` context.
77-
///
78-
/// We have chosen to implement mutual exclusion in the Rust part of the interposition library
79-
/// to reduce the critical section of code to the minimum, i.e. when a MPI call has been
80-
/// registered and *needs* to be saved. This choice theoretically allows for the best
81-
/// safety/performance ratio.
82-
///
83-
/// It should be noted that in a MPI context, it is "rare" that the same process manages a
84-
/// large number of threads. Therefore, the contention on the `Mutex` should not impact the
85-
/// performance of the application and the blocking of threads will be kept to a minimum.
86-
static ref EVENTS: Trace = Trace(Mutex::new(Vec::new()));
87-
}
63+
/// A vector that keeps track of interposed MPI functions called by a process.
64+
///
65+
/// The `lazy_static` macro creates static objects that are only initialized when needed at
66+
/// runtime. In this case, this implementation is similar to a singleton. It removes the need
67+
/// to pass a constant pointer on the `Vec` back to the C part of the interposition library,
68+
/// therefore avoiding the use of `unsafe` code sections.
69+
///
70+
/// As the MPI standard allows for processes to run code in parallel (e.g. through libraries
71+
/// like OpenMP or pthread), the `Vec` *must* be wrapped in a `Mutex` to prevent concurrent
72+
/// attempts at pushing onto the traces vector. Each time an event is registered, the caller
73+
/// must first take the lock on the `Mutex` before pushing an `Event`. This ensures that
74+
/// `interpol-rs` is thread-safe even in `MPI_THREAD_MULTIPLE` context.
75+
///
76+
/// We have chosen to implement mutual exclusion in the Rust part of the interposition library
77+
/// to reduce the critical section of code to the minimum, i.e. when a MPI call has been
78+
/// registered and *needs* to be saved. This choice theoretically allows for the best
79+
/// safety/performance ratio.
80+
///
81+
/// It should be noted that in a MPI context, it is "rare" that the same process manages a
82+
/// large number of threads. Therefore, the contention on the `Mutex` should not impact the
83+
/// performance of the application and the blocking of threads will be kept to a minimum.
84+
static EVENTS: Trace = Trace(Mutex::new(Vec::new()));
8885

8986
#[derive(Debug, PartialEq)]
9087
#[repr(C)]
@@ -112,8 +109,7 @@ fn serialize(
112109
current_rank: MpiRank,
113110
) -> Result<(), InterpolError> {
114111
println!("[interpol]: serializing traces for rank {current_rank}");
115-
let ser_traces = serde_json::to_string_pretty(events)
116-
.expect("failed to serialize vector contents to string");
112+
let traces = serde_json::to_string(events).expect("failed to serialize traces to string");
117113
let filename = format!("{}/rank{}_traces.json", INTERPOL_DIR, current_rank);
118114

119115
fs::create_dir_all(INTERPOL_DIR)?;
@@ -122,7 +118,7 @@ fn serialize(
122118
.truncate(true)
123119
.create(true)
124120
.open(filename)?;
125-
write!(file, "{}", ser_traces)?;
121+
write!(file, "{}", traces)?;
126122
Ok(())
127123
}
128124

0 commit comments

Comments
 (0)