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
use crate::error;
use crate::scheduler::{Batches, Label};
use crate::world::World;
use alloc::boxed::Box;
impl World {
#[cfg(feature = "parallel")]
#[allow(clippy::type_complexity)]
pub(crate) fn run_batches_parallel(
&self,
systems: &[Box<dyn Fn(&World) -> Result<(), error::Run> + Send + Sync + 'static>],
system_names: &[Box<dyn Label>],
batches: &Batches,
#[cfg_attr(not(feature = "tracing"), allow(unused))] workload_name: &dyn Label,
) -> Result<(), error::RunWorkload> {
#[cfg(feature = "tracing")]
let parent_span = tracing::info_span!("workload", name = ?workload_name);
#[cfg(feature = "tracing")]
let _parent_span = parent_span.enter();
let run_batch = || -> Result<(), error::RunWorkload> {
for (batch, batch_run_if) in batches.parallel.iter().zip(&batches.parallel_run_if) {
let mut result = Ok(());
let run_if = (
if batch_run_if.0 == usize::MAX {
// There is no run_if for this system
true
} else {
match (batches.systems_run_if[batch_run_if.0])(self) {
Ok(should_run) => should_run,
Err(err) => {
return Err(error::RunWorkload::Run((
system_names[batch.0.unwrap()].clone(),
err,
)));
}
}
},
batch
.1
.iter()
.zip(&batch_run_if.1)
.map(|(&index, &run_if_index)| {
if run_if_index == usize::MAX {
// There is no run_if for this system
return Ok(true);
}
match (batches.systems_run_if[run_if_index])(self) {
Ok(should_run) => Ok(should_run),
Err(err) => {
Err(error::RunWorkload::Run((system_names[index].clone(), err)))
}
}
})
.collect::<Result<alloc::vec::Vec<_>, error::RunWorkload>>()?,
);
let mut start = 0;
let single_system = batch.0.filter(|_| run_if.0).or_else(|| {
let system = batch.1.first().copied().filter(|_| run_if.1[0]);
if system.is_some() {
start = 1;
}
system
});
rayon::in_place_scope(|scope| {
// This check exists to avoid spawning a parallel job when possible.
// On wasm it causes a "condvar wait not supported" error.
if start < batch.1.len() {
scope.spawn(|_| {
use rayon::prelude::*;
result = batch.1[start..]
.par_iter()
.zip(&run_if.1[start..])
.try_for_each(|(&index, should_run)| {
if !should_run {
return Ok(());
}
#[cfg(feature = "tracing")]
{
self.run_single_system(
systems,
system_names,
&parent_span,
index,
)
}
#[cfg(not(feature = "tracing"))]
{
self.run_single_system(systems, system_names, index)
}
});
});
}
if let Some(index) = single_system {
#[cfg(feature = "tracing")]
self.run_single_system(systems, system_names, &parent_span, index)?;
#[cfg(not(feature = "tracing"))]
self.run_single_system(systems, system_names, index)?;
}
Ok(())
})?;
result?;
}
Ok(())
};
if let Some(thread_pool) = &self.thread_pool {
thread_pool.scope(|_| run_batch())
} else {
// Use non local ThreadPool
run_batch()
}
}
#[cfg(not(feature = "parallel"))]
#[allow(clippy::type_complexity)]
pub(crate) fn run_batches_sequential(
&self,
systems: &[Box<dyn Fn(&World) -> Result<(), error::Run> + Send + Sync + 'static>],
system_names: &[Box<dyn Label>],
batches: &Batches,
#[cfg_attr(not(feature = "tracing"), allow(unused))] workload_name: &dyn Label,
) -> Result<(), error::RunWorkload> {
#[cfg(feature = "tracing")]
let parent_span = tracing::info_span!("workload", name = ?workload_name);
#[cfg(feature = "tracing")]
let _parent_span = parent_span.enter();
batches
.sequential
.iter()
.zip(&batches.sequential_run_if)
.try_for_each(|(&index, &run_if_index)| {
let should_run = if run_if_index == usize::MAX {
// There is no run_if for this system
true
} else {
match (batches.systems_run_if[run_if_index])(self) {
Ok(should_run) => should_run,
Err(err) => {
return Err(error::RunWorkload::Run((
system_names[index].clone(),
err,
)));
}
}
};
if !should_run {
return Ok(());
}
#[cfg(feature = "tracing")]
{
self.run_single_system(systems, system_names, &parent_span, index)
}
#[cfg(not(feature = "tracing"))]
{
self.run_single_system(systems, system_names, index)
}
})
}
#[allow(clippy::type_complexity)]
fn run_single_system(
&self,
systems: &[Box<dyn Fn(&World) -> Result<(), error::Run> + Send + Sync>],
system_names: &[Box<dyn Label>],
#[cfg(feature = "tracing")] parent_span: &tracing::Span,
index: usize,
) -> Result<(), error::RunWorkload> {
#[cfg(feature = "tracing")]
let system_span =
tracing::info_span!(parent: parent_span.clone(), "system", name = ?system_names[index]);
#[cfg(feature = "tracing")]
let _system_span = system_span.enter();
(systems[index])(self)
.map_err(|err| error::RunWorkload::Run((system_names[index].clone(), err)))
}
}