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
#[macro_export]
macro_rules! make_kv {
($($k:expr => $v:expr;)*) => {
$crate::core::KV { kv: vec![ $(($k, std::rc::Rc::new($v))),* ] }
};
}
#[macro_export]
macro_rules! check_param {
($param:expr, $msg:expr, $error:ident) => {
match $param {
None => {
return Err($crate::core::ArgminError::$error {
text: $msg.to_string(),
}
.into());
}
Some(ref x) => x.clone(),
}
};
($param:expr, $msg:expr) => {
check_param!($param, $msg, NotInitialized)
};
}
#[macro_export]
macro_rules! argmin_error {
($error_type:ident, $msg:expr) => {
$crate::core::ArgminError::$error_type {
text: $msg.to_string(),
}
.into()
};
}
#[macro_export]
macro_rules! argmin_error_closure {
($error_type:ident, $msg:expr) => {
|| -> $crate::core::Error { $crate::argmin_error!($error_type, $msg) }
};
}
#[macro_export]
macro_rules! float {
($t:ident, $val:expr) => {
$t::from_f64($val).unwrap()
};
($val:expr) => {
F::from_f64($val).unwrap()
};
}
#[macro_export]
macro_rules! bulk {
($method_name:tt, $input:ty, $output:ty) => {
paste::item! {
#[doc = concat!(
"Compute `",
stringify!($method_name),
"` in bulk. ",
"If the `rayon` feature is enabled, multiple calls to `",
stringify!($method_name),
"` will be run in parallel using `rayon`, otherwise they will execute ",
"sequentially. If the `rayon` feature is enabled, parallelization can still be ",
"turned off by overwriting `parallelize` to return `false`. This can be useful ",
"in cases where it is preferable to parallelize only certain parts. ",
"Note that even if `parallelize` is set to false, the parameter vectors and the ",
"problem are still required to be `Send` and `Sync`. Those bounds are linked to ",
"the `rayon` feature. This method can be overwritten.",
)]
fn [<bulk_ $method_name>]<'a, P>(&self, params: &'a [P]) -> Result<Vec<$output>, Error>
where
P: std::borrow::Borrow<$input> + SyncAlias,
$output: SendAlias,
Self: SyncAlias,
{
#[cfg(feature = "rayon")]
{
if self.parallelize() {
params.par_iter().map(|p| self.$method_name(p.borrow())).collect()
} else {
params.iter().map(|p| self.$method_name(p.borrow())).collect()
}
}
#[cfg(not(feature = "rayon"))]
{
params.iter().map(|p| self.$method_name(p.borrow())).collect()
}
}
}
#[doc = concat!(
"Indicates whether to parallelize calls to `",
stringify!($method_name),
"` when using `bulk_",
stringify!($method_name),
"`. By default returns true, but can be set manually to `false` if needed. ",
"This allows users to turn off parallelization for certain traits ",
"implemented on their problem. ",
"Note that parallelization requires the `rayon` feature to be enabled, ",
"otherwise calls to `",
stringify!($method_name),
"` will be executed sequentially independent of how `parallelize` is set."
)]
fn parallelize(&self) -> bool {
true
}
};
}
#[cfg(test)]
macro_rules! send_sync_test {
($n:ident, $t:ty) => {
paste::item! {
#[test]
#[allow(non_snake_case)]
fn [<test_send_ $n>]() {
fn assert_send<T: Send>() {}
assert_send::<$t>();
}
}
paste::item! {
#[test]
#[allow(non_snake_case)]
fn [<test_sync_ $n>]() {
fn assert_sync<T: Sync>() {}
assert_sync::<$t>();
}
}
};
}
#[cfg(test)]
#[macro_export]
macro_rules! test_trait_impl {
($n:ident, $t:ty) => {
paste::item! {
#[test]
#[allow(non_snake_case)]
fn [<test_send_ $n>]() {
fn assert_send<T: Send>() {}
assert_send::<$t>();
}
}
paste::item! {
#[test]
#[allow(non_snake_case)]
fn [<test_sync_ $n>]() {
fn assert_sync<T: Sync>() {}
assert_sync::<$t>();
}
}
paste::item! {
#[test]
#[allow(non_snake_case)]
fn [<test_clone_ $n>]() {
fn assert_clone<T: Clone>() {}
assert_clone::<$t>();
}
}
};
}
#[cfg(test)]
#[macro_export]
macro_rules! assert_error {
($n:expr, $t:ty, $s:expr) => {
assert_eq!(
$n.err().unwrap().downcast_ref::<$t>().unwrap().to_string(),
$s
);
};
}