feanor_math/
computation.rs

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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use std::{fmt::Arguments, io::Write, sync::atomic::{AtomicBool, Ordering}};

use atomicbox::AtomicOptionBox;

use crate::{seq::VectorFn, unstable_sealed::UnstableSealed};

///
/// Provides an idiomatic way to convert a `Result<T, !>` into `T`, via
/// ```
/// # #![feature(never_type)]
/// # use feanor_math::computation::*;
/// fn some_computation() -> Result<&'static str, !> { Ok("this computation does not fail") }
/// println!("{}", some_computation().unwrap_or_else(no_error));
/// ```
/// 
pub fn no_error<T>(error: !) -> T {
    error
}

///
/// Trait for objects that observe and control a potentially long-running computation.
/// 
/// The idea is that this trait defines multiple functions that can be called during an
/// algorithm, and provide certain functionality. This way, each algorithm can decide which
/// functionality is relevant and how it is used.
/// 
/// This is currently unstable-sealed, since I expect significant additional functionality,
/// potentially including
///  - Early aborts, timeouts
///  - Multithreading
///  - Logging
///  - ...
/// 
/// As a user, this trait should currently be used by passing either [`LogProgress`]
/// or [`DontObserve`] to algorithms.
/// 
/// Also, note that all `description` parameters passed to computation controller
/// functions are only for logging/debugging purposes only. There is no specified format,
/// nor any stability guarantees on those messages.
/// 
/// # Example
/// 
/// Which features of a [`ComputationController`] an algorithm supports is completely up
/// to the algorithm. Elliptic Curve factorization currently supports logging, abortion
/// and multithreading.
/// ```
/// # use feanor_math::ring::*;
/// # use feanor_math::algorithms::ec_factor::*;
/// # use feanor_math::rings::zn::*;
/// # use feanor_math::computation::*;
/// let ring = zn_64::Zn::new(8591966237);
/// // factors 8591966237 while printing progress
/// let factor = lenstra_ec_factor(ring, LogProgress).unwrap_or_else(no_error);
/// assert!(8591966237 % factor == 0);
/// // factor it again, but don't print progress
/// let factor = lenstra_ec_factor(ring, DontObserve).unwrap_or_else(no_error);
/// assert!(8591966237 % factor == 0);
/// ```
/// If the multithreading with rayon is enabled, we can also do
/// ```
/// # use feanor_math::ring::*;
/// # use feanor_math::algorithms::ec_factor::*;
/// # use feanor_math::rings::zn::*;
/// # use feanor_math::computation::*;
/// # let ring = zn_64::Zn::new(8591966237);
/// // factors 8591966237 using multiple threads
/// let factor = lenstra_ec_factor(ring, RunMultithreadedLogProgress).unwrap_or_else(no_error);
/// assert!(8591966237 % factor == 0);
/// ```
///
pub trait ComputationController: Clone + UnstableSealed {

    type Abort: Send;

    ///
    /// Called by algorithms in (more or less) regular time intervals, can provide
    /// e.g. early aborts or tracking progress.
    /// 
    #[stability::unstable(feature = "enable")]
    fn checkpoint(&self, _description: Arguments) -> Result<(), Self::Abort> { 
        Ok(())
    }
    
    ///
    /// Runs the given closure with a clone of this iterator, possibly adding a log
    /// message before and/or after the computation starts/finishes.
    /// 
    /// I am currently not completely sure what the right behavior is when this
    /// function is called multiple times (possibly nested) for clones of the current
    /// controller. We should certainly support nesting of computations, but what should
    /// happen in multithreaded scenarios, if we have clones of controllers, or multiple
    /// different controllers?
    /// 
    #[stability::unstable(feature = "enable")]
    fn run_computation<F, T>(self, _description: Arguments, computation: F) -> T
        where F: FnOnce(Self) -> T
    {
        computation(self)
    }

    #[stability::unstable(feature = "enable")]
    fn log(&self, _description: Arguments) {}

    ///
    /// Inspired by Rayon, and behaves the same as `join()` there.
    /// Concretely, this function runs both closures, possibly in parallel, and
    /// returns their results.
    /// 
    /// 
    #[stability::unstable(feature = "enable")]
    fn join<A, B, RA, RB>(self, oper_a: A, oper_b: B) -> (RA, RB)
        where
            A: FnOnce(Self) -> RA + Send,
            B: FnOnce(Self) -> RB + Send,
            RA: Send,
            RB: Send
    {
        (oper_a(self.clone()), oper_b(self.clone()))
    }
}

///
/// The reason why a (part of a) short-circuiting computation was aborted.
/// 
/// `Finished` means that the computation was aborted, since another part already
/// found a result or aborted. `Abort(e)` means that the controller chose to abort 
/// the computation at a checkpoint, with data `e`.
/// 
pub enum ShortCircuitingComputationAbort<E> {
    Finished,
    Abort(E)
}

///
/// Shared data of a short-circuiting computation.
/// 
pub struct ShortCircuitingComputation<T, Controller>
    where T: Send,
        Controller: ComputationController
{
    finished: AtomicBool,
    abort: AtomicOptionBox<Controller::Abort>,
    result: AtomicOptionBox<T>,
}

///
/// Handle to a short-circuiting computation.
/// 
pub struct ShortCircuitingComputationHandle<'a, T, Controller>
    where T: Send,
        Controller: ComputationController
{
    controller: Controller,
    executor: &'a ShortCircuitingComputation<T, Controller>
}

impl<'a, T, Controller> Clone for ShortCircuitingComputationHandle<'a, T, Controller>
    where T: Send,
        Controller: ComputationController
{
    fn clone(&self) -> Self {
        Self {
            controller: self.controller.clone(),
            executor: self.executor
        }
    }
}

impl<'a, T, Controller> ShortCircuitingComputationHandle<'a, T, Controller>
    where T: Send,
        Controller: ComputationController
{
    #[stability::unstable(feature = "enable")]
    pub fn controller(&self) -> &Controller {
        &self.controller
    }

    #[stability::unstable(feature = "enable")]
    pub fn checkpoint(&self, description: Arguments) -> Result<(), ShortCircuitingComputationAbort<Controller::Abort>> { 
        if self.executor.finished.load(Ordering::Relaxed) {
            return Err(ShortCircuitingComputationAbort::Finished);
        } else if let Err(e) = self.controller.checkpoint(description) {
            return Err(ShortCircuitingComputationAbort::Abort(e));
        } else {
            return Ok(());
        }
    }

    #[stability::unstable(feature = "enable")]
    pub fn log(&self, description: Arguments) {
        self.controller.log(description)
    }

    #[stability::unstable(feature = "enable")]
    pub fn join_many<V, F>(self, operations: V)
        where V: VectorFn<F> + Sync,
            F: FnOnce(Self) -> Result<Option<T>, ShortCircuitingComputationAbort<Controller::Abort>>
    {
        fn join_many_internal<'a, T, V, F, Controller>(controller: Controller, executor: &'a ShortCircuitingComputation<T, Controller>, tasks: &V, from: usize, to: usize, batch_tasks: usize)
            where T: Send,
                Controller: ComputationController,
                V: VectorFn<F> + Sync,
                F: FnOnce(ShortCircuitingComputationHandle<'a, T, Controller>) -> Result<Option<T>, ShortCircuitingComputationAbort<Controller::Abort>>
        {
            if executor.finished.load(Ordering::Relaxed) {
                return;
            } else if from == to {
                return;
            } else if from + batch_tasks >= to {
                for i in from..to {
                    match tasks.at(i)(ShortCircuitingComputationHandle {
                        controller: controller.clone(),
                        executor: executor
                    }) {
                        Ok(Some(result)) => {
                            executor.finished.store(true, Ordering::Relaxed);
                            executor.result.store(Some(Box::new(result)), Ordering::AcqRel);
                        },
                        Err(ShortCircuitingComputationAbort::Abort(abort)) => {
                            executor.finished.store(true, Ordering::Relaxed);
                            executor.abort.store(Some(Box::new(abort)), Ordering::AcqRel);
                        },
                        Err(ShortCircuitingComputationAbort::Finished) | Ok(None) => {}
                    }
                }
            } else {
                let mid = (from + to) / 2;
                controller.join(move |controller| join_many_internal(controller, executor, tasks, from, mid, batch_tasks), move |controller| join_many_internal(controller, executor, tasks, mid, to, batch_tasks));
            }
        }
        join_many_internal(self.controller, self.executor, &operations, 0, operations.len(), 1)
    }

    #[stability::unstable(feature = "enable")]
    pub fn join<A, B>(self, oper_a: A, oper_b: B)
        where
            A: FnOnce(Self) -> Result<Option<T>, ShortCircuitingComputationAbort<Controller::Abort>> + Send,
            B: FnOnce(Self) -> Result<Option<T>, ShortCircuitingComputationAbort<Controller::Abort>> + Send
    {
        let success_fn = |value: T| {
            self.executor.finished.store(true, Ordering::Relaxed);
            self.executor.result.store(Some(Box::new(value)), Ordering::AcqRel);
        };
        let abort_fn = |abort: Controller::Abort| {
            self.executor.finished.store(true, Ordering::Relaxed);
            self.executor.abort.store(Some(Box::new(abort)), Ordering::AcqRel);
        };
        self.controller.join(
            |controller| {
                if self.executor.finished.load(Ordering::Relaxed) {
                    return;
                }
                match oper_a(ShortCircuitingComputationHandle {
                    controller,
                    executor: self.executor
                }) {
                    Ok(Some(result)) => success_fn(result),
                    Err(ShortCircuitingComputationAbort::Abort(abort)) => abort_fn(abort),
                    Err(ShortCircuitingComputationAbort::Finished) => {},
                    Ok(None) => {}
                }
            },
            |controller| {
                if self.executor.finished.load(Ordering::Relaxed) {
                    return;
                }
                match oper_b(ShortCircuitingComputationHandle {
                    controller,
                    executor: self.executor
                }) {
                    Ok(Some(result)) => success_fn(result),
                    Err(ShortCircuitingComputationAbort::Abort(abort)) => abort_fn(abort),
                    Err(ShortCircuitingComputationAbort::Finished) => {},
                    Ok(None) => {}
                }
            }
        );
    }
}

impl<T, Controller> ShortCircuitingComputation<T, Controller>
    where T: Send,
        Controller: ComputationController
{
    #[stability::unstable(feature = "enable")]
    pub fn new() -> Self {
        Self {
            finished: AtomicBool::new(false),
            abort: AtomicOptionBox::none(),
            result: AtomicOptionBox::none()
        }
    }

    #[stability::unstable(feature = "enable")]
    pub fn handle<'a>(&'a self, controller: Controller) -> ShortCircuitingComputationHandle<'a, T, Controller> {
        ShortCircuitingComputationHandle {
            controller: controller,
            executor: self
        }
    }

    #[stability::unstable(feature = "enable")]
    pub fn finish(self) -> Result<Option<T>, Controller::Abort> {
        if let Some(abort) = self.abort.swap(None, Ordering::AcqRel) {
            return Err(*abort);
        } else if let Some(result) = self.result.swap(None, Ordering::AcqRel) {
            return Ok(Some(*result));
        } else {
            return Ok(None);
        }
    }
}

#[macro_export]
macro_rules! checkpoint {
    ($controller:expr) => {
        ($controller).checkpoint(std::format_args!(""))?
    };
    ($controller:expr, $($args:tt)*) => {
        ($controller).checkpoint(std::format_args!($($args)*))?
    };
}

#[macro_export]
macro_rules! log_progress {
    ($controller:expr, $($args:tt)*) => {
        ($controller).log(std::format_args!($($args)*))
    };
}

#[derive(Clone, Copy)]
pub struct LogProgress;

impl UnstableSealed for LogProgress {}

impl ComputationController for LogProgress {

    type Abort = !;

    #[stability::unstable(feature = "enable")]
    fn log(&self, description: Arguments) {
        print!("{}", description);
        std::io::stdout().flush().unwrap();
    }

    #[stability::unstable(feature = "enable")]
    fn run_computation<F, T>(self, description: Arguments, computation: F) -> T
        where F: FnOnce(Self) -> T
    {
        self.log(description);
        let result = computation(self);
        self.log(format_args!("\n"));
        return result;
    }

    #[stability::unstable(feature = "enable")]
    fn checkpoint(&self, description: Arguments) -> Result<(), Self::Abort> {
        self.log(description);
        Ok(())
    }
}

#[derive(Clone, Copy)]
pub struct DontObserve;

impl UnstableSealed for DontObserve {}

impl ComputationController for DontObserve {

    type Abort = !;
}

#[cfg(feature = "parallel")]
mod parallel_controller {

    use super::*;

    #[stability::unstable(feature = "enable")]
    pub struct ExecuteMultithreaded<Rest: ComputationController + Send> {
        rest: Rest
    }

    impl<Rest: ComputationController + Send + Copy> Copy for ExecuteMultithreaded<Rest> {}

    impl<Rest: ComputationController + Send> Clone for ExecuteMultithreaded<Rest> {
        fn clone(&self) -> Self {
            Self { rest: self.rest.clone() }
        }
    }
    
    impl<Rest: ComputationController + Send> UnstableSealed for ExecuteMultithreaded<Rest> {}

    impl<Rest: ComputationController + Send> ComputationController for ExecuteMultithreaded<Rest> {
        type Abort = Rest::Abort;

        #[stability::unstable(feature = "enable")]
        fn checkpoint(&self, description: Arguments) -> Result<(), Self::Abort> { 
            self.rest.checkpoint(description)
        }
    
        #[stability::unstable(feature = "enable")]
        fn run_computation<F, T>(self, description: Arguments, computation: F) -> T
            where F: FnOnce(Self) -> T
        {
            self.rest.run_computation(description, |rest| computation(ExecuteMultithreaded { rest }))
        }

        #[stability::unstable(feature = "enable")]
        fn join<A, B, RA, RB>(self, oper_a: A, oper_b: B) -> (RA, RB)
            where
                A: FnOnce(Self) -> RA + Send,
                B: FnOnce(Self) -> RB + Send,
                RA: Send,
                RB: Send
        {
            let self1 = self.clone();
            let self2 = self;
            rayon::join(|| oper_a(self1), || oper_b(self2))
        }
    }

    #[stability::unstable(feature = "enable")]
    #[allow(non_upper_case_globals)]
    pub static RunMultithreadedLogProgress: ExecuteMultithreaded<LogProgress> = ExecuteMultithreaded { rest: LogProgress };
    #[stability::unstable(feature = "enable")]
    #[allow(non_upper_case_globals)]
    pub static RunMultithreaded: ExecuteMultithreaded<DontObserve> = ExecuteMultithreaded { rest: DontObserve };
}

#[cfg(feature = "parallel")]
pub use parallel_controller::*;