Tue, 06 Dec 2022 14:12:20 +0200
v1.0.0-pre-arxiv (missing arXiv links)
0 | 1 | // The main documentation is in the README. |
6
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
2 | // We need to uglify it in build.rs because rustdoc is stuck in the past. |
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
3 | #![doc = include_str!(concat!(env!("OUT_DIR"), "/README_uglified.md"))] |
0 | 4 | |
5 | // We use unicode. We would like to use much more of it than Rust allows. | |
6 | // Live with it. Embrace it. | |
7 | #![allow(uncommon_codepoints)] | |
8 | #![allow(mixed_script_confusables)] | |
9 | #![allow(confusable_idents)] | |
6
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
10 | // Linear operators may be written e.g. as `opA`, to keep the capital letters of mathematical |
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
11 | // convention while referring to the type (trait) of the operator as `A`. |
0 | 12 | #![allow(non_snake_case)] |
6
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
13 | // We need the drain filter for inertial prune. |
0 | 14 | #![feature(drain_filter)] |
15 | ||
16 | use clap::Parser; | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
17 | use serde::{Serialize, Deserialize}; |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
18 | use serde_json; |
0 | 19 | use itertools::Itertools; |
5
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
20 | use std::num::NonZeroUsize; |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
21 | |
0 | 22 | use alg_tools::parallelism::{ |
23 | set_num_threads, | |
24 | set_max_threads, | |
25 | }; | |
26 | ||
27 | pub mod types; | |
28 | pub mod measures; | |
29 | pub mod fourier; | |
30 | pub mod kernels; | |
31 | pub mod seminorms; | |
32 | pub mod forward_model; | |
33 | pub mod plot; | |
34 | pub mod subproblem; | |
35 | pub mod tolerance; | |
36 | pub mod fb; | |
37 | pub mod frank_wolfe; | |
38 | pub mod pdps; | |
39 | pub mod run; | |
40 | pub mod rand_distr; | |
41 | pub mod experiments; | |
42 | ||
43 | use types::{float, ClapFloat}; | |
44 | use run::{ | |
45 | DefaultAlgorithm, | |
46 | PlotLevel, | |
47 | Named, | |
48 | AlgorithmConfig, | |
49 | }; | |
50 | use experiments::DefaultExperiment; | |
51 | use measures::merging::SpikeMergingMethod; | |
52 | use DefaultExperiment::*; | |
53 | use DefaultAlgorithm::*; | |
54 | ||
55 | /// Command line parameters | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
56 | #[derive(Parser, Debug, Serialize)] |
0 | 57 | #[clap( |
58 | about = env!("CARGO_PKG_DESCRIPTION"), | |
59 | author = env!("CARGO_PKG_AUTHORS"), | |
60 | version = env!("CARGO_PKG_VERSION"), | |
61 | after_help = "Pass --help for longer descriptions.", | |
62 | after_long_help = "", | |
63 | )] | |
64 | pub struct CommandLineArgs { | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
65 | #[arg(long, short = 'm', value_name = "M", default_value_t = 2000)] |
0 | 66 | /// Maximum iteration count |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
67 | max_iter : usize, |
0 | 68 | |
69 | #[arg(long, short = 'n', value_name = "N")] | |
70 | /// Output status every N iterations. Set to 0 to disable. | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
71 | /// |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
72 | /// The default is to output status based on logarithmic increments. |
0 | 73 | verbose_iter : Option<usize>, |
74 | ||
75 | #[arg(long, short = 'q')] | |
76 | /// Don't display iteration progress | |
77 | quiet : bool, | |
78 | ||
79 | /// List of experiments to perform. | |
80 | #[arg(value_enum, value_name = "EXPERIMENT", | |
81 | default_values_t = [Experiment1D, Experiment1DFast, | |
82 | Experiment2D, Experiment2DFast, | |
83 | Experiment1D_L1])] | |
84 | experiments : Vec<DefaultExperiment>, | |
85 | ||
86 | /// Default algorithm configration(s) to use on the experiments. | |
87 | /// | |
88 | /// Not all algorithms are available for all the experiments. | |
89 | /// In particular, only PDPS is available for the experiments with L¹ data term. | |
90 | #[arg(value_enum, value_name = "ALGORITHM", long, short = 'a', | |
91 | default_values_t = [FB, FISTA, PDPS, FW, FWRelax])] | |
92 | algorithm : Vec<DefaultAlgorithm>, | |
93 | ||
94 | /// Saved algorithm configration(s) to use on the experiments | |
95 | #[arg(value_name = "JSON_FILE", long)] | |
96 | saved_algorithm : Vec<String>, | |
97 | ||
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
98 | /// Plot saving scheme |
0 | 99 | #[arg(value_enum, long, short = 'p', default_value_t = PlotLevel::Data)] |
100 | plot : PlotLevel, | |
101 | ||
102 | /// Directory for saving results | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
103 | #[arg(long, short = 'o', required = true, default_value = "out")] |
0 | 104 | outdir : String, |
105 | ||
106 | #[arg(long, help_heading = "Multi-threading", default_value = "4")] | |
107 | /// Maximum number of threads | |
108 | max_threads : usize, | |
109 | ||
110 | #[arg(long, help_heading = "Multi-threading")] | |
111 | /// Number of threads. Overrides the maximum number. | |
112 | num_threads : Option<usize>, | |
113 | ||
114 | #[clap(flatten, next_help_heading = "Experiment overrides")] | |
115 | /// Experiment setup overrides | |
116 | experiment_overrides : ExperimentOverrides<float>, | |
117 | ||
118 | #[clap(flatten, next_help_heading = "Algorithm overrides")] | |
119 | /// Algorithm parametrisation overrides | |
120 | algoritm_overrides : AlgorithmOverrides<float>, | |
121 | } | |
122 | ||
123 | /// Command line experiment setup overrides | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
124 | #[derive(Parser, Debug, Serialize, Deserialize)] |
0 | 125 | pub struct ExperimentOverrides<F : ClapFloat> { |
126 | #[arg(long)] | |
127 | /// Regularisation parameter override. | |
128 | /// | |
129 | /// Only use if running just a single experiment, as different experiments have different | |
130 | /// regularisation parameters. | |
131 | alpha : Option<F>, | |
132 | ||
133 | #[arg(long)] | |
134 | /// Gaussian noise variance override | |
135 | variance : Option<F>, | |
136 | ||
137 | #[arg(long, value_names = &["MAGNITUDE", "PROBABILITY"])] | |
138 | /// Salt and pepper noise override. | |
139 | salt_and_pepper : Option<Vec<F>>, | |
140 | ||
141 | #[arg(long)] | |
142 | /// Noise seed | |
143 | noise_seed : Option<u64>, | |
144 | } | |
145 | ||
146 | /// Command line algorithm parametrisation overrides | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
147 | #[derive(Parser, Debug, Serialize, Deserialize)] |
0 | 148 | pub struct AlgorithmOverrides<F : ClapFloat> { |
149 | #[arg(long, value_names = &["COUNT", "EACH"])] | |
150 | /// Override bootstrap insertion iterations for --algorithm. | |
151 | /// | |
152 | /// The first parameter is the number of bootstrap insertion iterations, and the second | |
153 | /// the maximum number of iterations on each of them. | |
154 | bootstrap_insertions : Option<Vec<usize>>, | |
155 | ||
156 | #[arg(long, requires = "algorithm")] | |
157 | /// Primal step length parameter override for --algorithm. | |
158 | /// | |
159 | /// Only use if running just a single algorithm, as different algorithms have different | |
160 | /// regularisation parameters. Does not affect the algorithms fw and fwrelax. | |
161 | tau0 : Option<F>, | |
162 | ||
163 | #[arg(long, requires = "algorithm")] | |
164 | /// Dual step length parameter override for --algorithm. | |
165 | /// | |
166 | /// Only use if running just a single algorithm, as different algorithms have different | |
167 | /// regularisation parameters. Only affects PDPS. | |
168 | sigma0 : Option<F>, | |
169 | ||
170 | #[arg(value_enum, long)] | |
171 | /// PDPS acceleration, when available. | |
172 | acceleration : Option<pdps::Acceleration>, | |
173 | ||
174 | #[arg(long)] | |
175 | /// Perform postprocess weight optimisation for saved iterations | |
176 | /// | |
177 | /// Only affects FB, FISTA, and PDPS. | |
178 | postprocessing : Option<bool>, | |
179 | ||
180 | #[arg(value_name = "n", long)] | |
181 | /// Merging frequency, if merging enabled (every n iterations) | |
182 | /// | |
183 | /// Only affects FB, FISTA, and PDPS. | |
184 | merge_every : Option<usize>, | |
185 | ||
186 | #[arg(value_enum, long)]//, value_parser = SpikeMergingMethod::<float>::value_parser())] | |
187 | /// Merging strategy | |
188 | /// | |
189 | /// Either the string "none", or a radius value for heuristic merging. | |
190 | merging : Option<SpikeMergingMethod<F>>, | |
191 | ||
192 | #[arg(value_enum, long)]//, value_parser = SpikeMergingMethod::<float>::value_parser())] | |
193 | /// Final merging strategy | |
194 | /// | |
195 | /// Either the string "none", or a radius value for heuristic merging. | |
196 | /// Only affects FB, FISTA, and PDPS. | |
197 | final_merging : Option<SpikeMergingMethod<F>>, | |
198 | } | |
199 | ||
200 | /// The entry point for the program. | |
201 | pub fn main() { | |
202 | let cli = CommandLineArgs::parse(); | |
203 | ||
5
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
204 | #[cfg(debug_assertions)] |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
205 | { |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
206 | use colored::Colorize; |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
207 | println!("{}", format!("\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
208 | ********\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
209 | WARNING: Compiled without optimisations; {}\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
210 | Please recompile with `--release` flag.\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
211 | ********\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
212 | ", "performance will be poor!".blink() |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
213 | ).red()); |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
214 | } |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
215 | |
0 | 216 | if let Some(n_threads) = cli.num_threads { |
217 | let n = NonZeroUsize::new(n_threads).expect("Invalid thread count"); | |
218 | set_num_threads(n); | |
219 | } else { | |
220 | let m = NonZeroUsize::new(cli.max_threads).expect("Invalid maximum thread count"); | |
221 | set_max_threads(m); | |
222 | } | |
223 | ||
224 | for experiment_shorthand in cli.experiments.iter().unique() { | |
225 | let experiment = experiment_shorthand.get_experiment(&cli.experiment_overrides).unwrap(); | |
226 | let mut algs : Vec<Named<AlgorithmConfig<float>>> | |
227 | = cli.algorithm.iter() | |
228 | .map(|alg| experiment.algorithm_defaults(*alg, &cli.algoritm_overrides)) | |
229 | .collect(); | |
230 | for filename in cli.saved_algorithm.iter() { | |
231 | let f = std::fs::File::open(filename).unwrap(); | |
232 | let alg = serde_json::from_reader(f).unwrap(); | |
233 | algs.push(alg); | |
234 | } | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
235 | experiment.runall(&cli, (!algs.is_empty()).then_some(algs)) |
0 | 236 | .unwrap() |
237 | } | |
238 | } |