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
use crate::{
configs,
low_level_analysis::types::*,
};
use std::{
ops::Range,
time::{SystemTime, Duration},
};
pub fn run_iterator_pass_verbosely<'a, _IteratorAlgorithmClosure: Fn(u32) -> u32 + Sync,
_OutputClosure: FnMut(&str),
T: TryInto<u64> + Copy> (result_prefix: &str,
result_suffix: &str,
iterator_algorithm: &_IteratorAlgorithmClosure,
algorithm_type: &BigOIteratorAlgorithmType,
range: Range<u32>,
time_unit: &'a TimeUnit<T>,
threads: u32,
mut output: _OutputClosure)
-> (PassResult<'a,T>, u32) {
let (pass_result, r) = run_iterator_pass(iterator_algorithm, algorithm_type, range, time_unit, threads);
output(&format!("{}{}/{}{}", result_prefix, pass_result.time_measurements, pass_result.space_measurements, result_suffix));
(pass_result, r)
}
pub fn run_pass_verbosely<'a, _OutputClosure: FnMut(&str),
T: TryInto<u64> + Copy> (result_prefix: &str,
result_suffix: &str,
algorithm: impl FnMut() -> u32,
time_unit: &'a TimeUnit<T>,
mut output: _OutputClosure)
-> (PassResult<'a,T>, u32) {
let (pass_result, r) = run_pass(algorithm, time_unit);
output(&format!("{}{}/{}{}", result_prefix, pass_result.time_measurements, pass_result.space_measurements, result_suffix));
(pass_result, r)
}
pub(crate) fn run_iterator_pass<'a, _AlgorithmClosure: Fn(u32) -> u32 + Sync,
_ScalarDuration: TryInto<u64> + Copy> (iterator_algorithm: &_AlgorithmClosure,
algorithm_type: &BigOIteratorAlgorithmType,
range: Range<u32>,
time_unit: &'a TimeUnit<_ScalarDuration>,
threads: u32)
-> (PassResult<'a,_ScalarDuration>, u32) {
type ThreadLoopResult = (Duration, u32);
fn thread_loop<_AlgorithmClosure: Fn(u32) -> u32 + Sync>
(iterator_algorithm: &_AlgorithmClosure, algorithm_type: &BigOIteratorAlgorithmType, range: Range<u32>)
-> ThreadLoopResult {
let mut thread_r: u32 = range.end;
let thread_start = SystemTime::now();
match algorithm_type {
BigOIteratorAlgorithmType::ConstantSet => {
if range.end < range.start {
for e in (range.end..range.start).rev() {
thread_r ^= iterator_algorithm(e);
}
} else {
for e in range {
thread_r ^= iterator_algorithm(e);
}
}
},
BigOIteratorAlgorithmType::SetResizing => {
if range.end < range.start {
for e in (range.end..range.start).rev() {
thread_r ^= iterator_algorithm(e);
}
} else {
for e in range {
thread_r ^= iterator_algorithm(e);
}
}
},
}
let thread_end = SystemTime::now();
let thread_duration = thread_end.duration_since(thread_start).unwrap();
(thread_duration, thread_r)
}
crossbeam::scope(|scope| {
let i32_range = range.end as i32 .. range.start as i32;
let chunk_size = (i32_range.end-i32_range.start)/threads as i32;
let mut thread_handlers: Vec<crossbeam::thread::ScopedJoinHandle<ThreadLoopResult>> = Vec::with_capacity(threads as usize);
let allocator_savepoint = configs::ALLOC.save_point();
for n in 0..threads as i32 {
let chunked_range = i32_range.start+chunk_size*n..i32_range.start+chunk_size*(n+1);
thread_handlers.push( scope.spawn(move |_| thread_loop(iterator_algorithm, algorithm_type, chunked_range.start as u32 .. chunked_range.end as u32)) );
}
let mut r = range.start+1;
let mut elapsed_average = 0.0f64;
for handler in thread_handlers {
let joining_result = handler.join();
if joining_result.is_err() {
panic!("Panic! while running provided 'algorithm' closure: algo type: {:?}, range: {:?}: Error: {:?}", algorithm_type, range, joining_result.unwrap_err())
}
let (thread_duration, thread_r) = joining_result.unwrap();
let thread_elapsed = (time_unit.duration_conversion_fn_ptr)(&thread_duration).try_into().unwrap_or_default();
elapsed_average += thread_elapsed as f64 / threads as f64;
r ^= thread_r;
}
let allocator_statistics = configs::ALLOC.delta_statistics(&allocator_savepoint);
(PassResult {
time_measurements: BigOTimePassMeasurements {
elapsed_time: elapsed_average.round() as u64,
time_unit,
},
space_measurements: BigOSpacePassMeasurements {
used_memory_before: allocator_savepoint.metrics.current_used_memory,
used_memory_after: allocator_statistics.current_used_memory,
min_used_memory: allocator_statistics.min_used_memory,
max_used_memory: allocator_statistics.max_used_memory,
},
}, r)
}).unwrap()
}
pub(crate) fn run_pass<'a, _ScalarDuration: TryInto<u64> + Copy> (mut algorithm: impl FnMut() -> u32,
time_unit: &'a TimeUnit<_ScalarDuration>)
-> (PassResult<'a,_ScalarDuration>, u32) {
let allocator_savepoint = configs::ALLOC.save_point();
let start = SystemTime::now();
let r = algorithm();
let duration = start.elapsed().unwrap();
let elapsed = (time_unit.duration_conversion_fn_ptr)(&duration).try_into().unwrap_or_default();
let allocator_statistics = configs::ALLOC.delta_statistics(&allocator_savepoint);
(PassResult {
time_measurements: BigOTimePassMeasurements {
elapsed_time: elapsed,
time_unit,
},
space_measurements: BigOSpacePassMeasurements {
used_memory_before: allocator_savepoint.metrics.current_used_memory,
used_memory_after: allocator_statistics.current_used_memory,
min_used_memory: allocator_statistics.min_used_memory,
max_used_memory: allocator_statistics.max_used_memory,
},
}, r)
}
#[derive(Clone,Copy)]
pub struct PassResult<'a,ScalarTimeUnit: Copy> {
pub time_measurements: BigOTimePassMeasurements<'a,ScalarTimeUnit>,
pub space_measurements: BigOSpacePassMeasurements,
}
impl<ScalarTimeUnit: Copy> Default for PassResult<'_,ScalarTimeUnit> {
fn default() -> Self {
Self {
time_measurements: BigOTimePassMeasurements {
elapsed_time: 0,
time_unit: &TimeUnits::get_const_default(),
},
space_measurements: BigOSpacePassMeasurements {
used_memory_before: 0,
used_memory_after: 0,
min_used_memory: 0,
max_used_memory: 0,
}
}
}
}