ppl 0.1.6

A structured parallel programming library for Rust.
Documentation
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use std::{
    hint,
    sync::{
        atomic::{AtomicBool, AtomicUsize, Ordering},
        Arc, Condvar, Mutex, OnceLock,
    },
    thread::{self},
};

use core_affinity::CoreId;
use log::{debug, error, trace, warn};

use super::configuration::Configuration;

type Func<'a> = Box<dyn FnOnce() + Send + 'a>;

/// Mini Spin Lock
///
/// This can be used to protect critical sections of the code
struct Lock {
    lock: AtomicBool,
}
impl Lock {
    fn new() -> Self {
        Lock {
            lock: AtomicBool::new(false),
        }
    }

    fn lock(&self) {
        while self
            .lock
            .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
            .is_err()
        {
            hint::spin_loop()
        }
    }

    fn unlock(&self) {
        self.lock.store(false, Ordering::Release);
    }
}
impl Drop for Lock {
    fn drop(&mut self) {
        self.unlock();
    }
}
/// Enum representing a Job in the orchestrator.
pub enum Job {
    /// A New job
    NewJob(Func<'static>),
    /// Terminate the executor
    Terminate,
}

/// Struct that reepresent a job in the queue.
///
/// It contains a method to wait till the job is done.
/// It is used to wait for the end of a job.
#[derive(Debug)]
pub(crate) struct JobInfo {
    status: Arc<AtomicBool>,
}
impl JobInfo {
    fn new() -> JobInfo {
        JobInfo {
            status: Arc::new(AtomicBool::new(false)),
        }
    }

    pub(crate) fn wait(&self) {
        while !self.status.load(Ordering::Relaxed) {
            hint::spin_loop();
        }
    }
}

/// An executor.
/// It is a thread that execute jobs.
/// It will fetch jobs from it's local queue.
struct Executor {
    thread: Thread,
    global_lock: Arc<Lock>,
    status: Arc<AtomicBool>,
    available_workers: Arc<AtomicUsize>,
    queue: Arc<Mutex<Vec<Job>>>,
    cvar: Arc<Condvar>,
}
impl Executor {
    /// Create a executor.
    /// It will start executing jobs immediately.
    /// It will terminate when it receives a terminate message.
    fn new(
        core_id: CoreId,
        config: Arc<Configuration>,
        global_lock: Arc<Lock>,
        available_workers: Arc<AtomicUsize>,
    ) -> Executor {
        let status = Arc::new(AtomicBool::new(false));
        let cvar = Arc::new(Condvar::new());
        let queue = Arc::new(Mutex::new(Vec::with_capacity(1)));

        let worker = ExecutorInfo::new(
            status.clone(),
            global_lock.clone(),
            available_workers.clone(),
            queue.clone(),
            cvar.clone(),
        );
        let thread = Thread::new(
            core_id,
            move || {
                worker.run();
            },
            config,
        );
        Executor {
            thread,
            global_lock,
            status,
            available_workers,
            queue,
            cvar,
        }
    }

    /// Get the status of this executor
    fn get_status(&self) -> bool {
        self.global_lock.lock();

        let res = self.status.load(Ordering::Relaxed);

        self.global_lock.unlock();

        res
    }

    /// Push a job in the executor queue
    fn push(&self, job: Job) {
        self.warn_busy();
        let mut queue = self.queue.lock().unwrap();
        assert!(queue.is_empty());
        queue.push(job);
        self.cvar.notify_one();
    }

    // Warn that the executor is busy.
    fn warn_busy(&self) {
        self.global_lock.lock();

        self.status.store(false, Ordering::Relaxed);
        let _ = self.available_workers.fetch_update(
            Ordering::Relaxed,
            Ordering::Relaxed,
            |x| -> Option<usize> { Some(x.saturating_sub(1)) },
        );

        self.global_lock.unlock();
    }

    /// Join the thread running the executor.
    fn join(&mut self) {
        self.thread.join();
    }
}

/// Information about the executor.
/// It contains the queue of jobs and the number of available executors in it's partition.
struct ExecutorInfo {
    status: Arc<AtomicBool>,
    global_lock: Arc<Lock>,
    available_workers: Arc<AtomicUsize>,
    queue: Arc<Mutex<Vec<Job>>>,
    cvar: Arc<Condvar>,
}
impl ExecutorInfo {
    /// Create a new executor info.
    fn new(
        status: Arc<AtomicBool>,
        global_lock: Arc<Lock>,
        available_workers: Arc<AtomicUsize>,
        queue: Arc<Mutex<Vec<Job>>>,
        cvar: Arc<Condvar>,
    ) -> ExecutorInfo {
        ExecutorInfo {
            status,
            global_lock,
            available_workers,
            queue,
            cvar,
        }
    }

    // Warn that the executor is available.
    fn warn_available(&self) {
        self.global_lock.lock();

        self.status.store(true, Ordering::Relaxed);
        self.available_workers.fetch_add(1, Ordering::Relaxed);

        self.global_lock.unlock();
    }

    /// Run the executor.
    /// It will fetch jobs from the local queue.
    /// It will terminate when it receives a terminate message.
    fn run(&self) {
        loop {
            if let Some(job) = self.fetch_job() {
                match job {
                    Job::NewJob(f) => {
                        f();
                        self.warn_available();
                    }
                    Job::Terminate => {
                        break;
                    }
                }
            }
        }
    }

    /// Fetch a job from the local queue.
    fn fetch_job(&self) -> Option<Job> {
        let mut queue = self.queue.lock().unwrap();
        let mut job = queue.pop();

        while job.is_none() {
            queue = self.cvar.wait(queue).unwrap();
            job = queue.pop();
        }
        job
    }
}

/// Struct that represent a thread.
/// It contains a thread and a method to join it.
struct Thread {
    thread: Option<thread::JoinHandle<()>>,
}
impl Thread {
    /// Create a new thread.
    /// If pinning is enabled, it will pin the thread to the specified core.
    /// If pinning is disabled, it will not pin the thread.
    fn new<F>(core_id: CoreId, f: F, configuration: Arc<Configuration>) -> Thread
    where
        F: FnOnce() + Send + 'static,
    {
        // Create the thread and pin it if needed
        Thread {
            thread: Some(thread::spawn(move || {
                if configuration.get_pinning() {
                    let err = core_affinity::set_for_current(core_id);
                    if !err {
                        error!("Thread pinning on core {} failed!", core_id.id);
                    } else {
                        trace!("Thread pinned on core {}.", core_id.id);
                    }
                }
                trace!("{:?} started", thread::current().id());
                (f)();
                trace!("{:?} now will end.", thread::current().id());
            })),
        }
    }

    /// Join the thread.
    /// It will wait until the thread is terminated.
    fn join(&mut self) {
        if let Some(thread) = self.thread.take() {
            thread.join().unwrap();
        }
    }
}

/// Struct that represent a partition.
/// It contains the list of workers (executors) and the number of available workers.
/// A partition, if pinning is enabled, will be pinned to a specific core.
/// Basically a partition represent a core, the executors are the threads pinned on that core.
pub struct Partition {
    core_id: CoreId,
    workers: Mutex<Vec<Executor>>,
    global_lock: Arc<Lock>,
    available_workers: Arc<AtomicUsize>,
    configuration: Arc<Configuration>,
}

impl Partition {
    /// Create a new partition.
    /// It will create the list of workers (executors).
    /// If pinning is enabled, it will pin the partition to the specified core.
    fn new(core_id: usize, configuration: Arc<Configuration>) -> Partition {
        let workers = Vec::new();
        let core_id = configuration.get_thread_mapping()[core_id];
        Partition {
            core_id,
            workers: Mutex::new(workers),
            global_lock: Arc::new(Lock::new()),
            available_workers: Arc::new(AtomicUsize::new(0)),
            configuration,
        }
    }

    /// Get the number of executor in the partition.
    fn get_worker_count(&self) -> usize {
        self.workers.lock().unwrap().len()
    }

    /// Get the number of busy executors (in this instant) in the partition.
    fn get_busy_worker_count(&self) -> usize {
        self.get_worker_count() - self.get_free_worker_count()
    }

    /// Get the number of free executors (in this instant) in the partition.
    fn get_free_worker_count(&self) -> usize {
        self.global_lock.lock();

        let res = self.available_workers.load(Ordering::Acquire);

        self.global_lock.unlock();

        res
    }

    /// Find executor
    fn find_executor(workers: &mut Vec<Executor>) -> Option<Executor> {
        for i in 0..workers.len() {
            if workers[i].get_status() {
                return Some(workers.remove(i));
            }
        }
        None
    }
    /// Push a new job to the partition.
    /// This method return a JobInfo that can be used to wait for the Job to finish.
    /// If there aren't executors in the partition or all the existing executors are busy, a new executor is created.
    fn push<F>(&self, f: F) -> JobInfo
    where
        F: FnOnce() + Send + 'static,
    {
        let job_info = JobInfo::new();
        let job_info_clone = Arc::clone(&job_info.status);

        let job = Job::NewJob(Box::new(move || {
            f();
            job_info_clone.store(true, Ordering::Relaxed);
        }));

        let mut workers = self.workers.lock().unwrap();
        let worker = Self::find_executor(&mut workers);
        match worker {
            Some(executor) => {
                executor.push(job);
                workers.push(executor);
            }
            None => {
                let executor = Executor::new(
                    self.core_id,
                    self.configuration.clone(),
                    self.global_lock.clone(),
                    self.available_workers.clone(),
                );
                executor.push(job);
                workers.push(executor);
            }
        }

        job_info
    }
}

impl Drop for Partition {
    /// Drop the partition.
    /// It will terminate all the executors and join them.
    fn drop(&mut self) {
        let mut workers = self.workers.lock().unwrap();

        // Join all the workers.
        for worker in workers.iter_mut() {
            worker.push(Job::Terminate);
        }

        for worker in workers.iter_mut() {
            worker.join();
        }
    }
}

/// The orchestrator is the main structure of the library.
/// Is composed by a list of partitions, each partition, if pinning is enabled, is a core.
/// The orchestrator is responsible to create the partitions and to distribute the jobs to the partitions.
/// The orchestractor is global, implemented as a singleton.
/// The main idea is to have a central point that distribuite evenly the jobs to the partitions, exploiting
/// the CPU topology of the system.
pub struct Orchestrator {
    partitions: Vec<Partition>,
    configuration: Arc<Configuration>,
}

/// OnceLock is a structure that allow to create a safe global singleton.
static mut ORCHESTRATOR: OnceLock<Arc<Orchestrator>> = OnceLock::new();

/// Create a new orchestrator with the default configuration.
pub(crate) fn new() -> Arc<Orchestrator> {
    Arc::new(Orchestrator::new(Arc::new(Configuration::new_default())))
}

/// Get or initialize the global orchestrator.
/// If the global orchestrator is not initialized, a new one is created with the default configuration.
pub fn get_global_orchestrator() -> Arc<Orchestrator> {
    unsafe {
        ORCHESTRATOR
            .get_or_init(|| -> Arc<Orchestrator> { new() })
            .clone()
    }
}

impl Orchestrator {
    // Create a new orchestrator.
    fn new(configuration: Arc<Configuration>) -> Orchestrator {
        let mut partitions = Vec::new();
        let mut max_cores = 1;
        if configuration.get_pinning() {
            max_cores = configuration.get_max_cores();
            if cfg!(target_os = "macos") {
                warn!("Thread pinning is not currently supported for Apple Silicon.");
            }
        }

        for i in 0..max_cores {
            partitions.push(Partition::new(i, Arc::clone(&configuration)));
        }

        Orchestrator {
            partitions,
            configuration,
        }
    }

    /// Find the partition with the less busy executors.
    /// If there are more than one partition with the same number of executors, the first one is returned.
    /// If there are no executors in any partition, the first partition is returned.
    fn find_partition(partitions: &[Partition]) -> Option<&Partition> {
        partitions.iter().min_by_key(|p| p.get_busy_worker_count())
    }

    /// Find the subarray of size count of partitions that minimize the number of busy executors contained in each partition of the subarray.
    /// If there are more than one sequence with the same number of busy executors, the first sequence found is returned.
    /// This algorithm will use the sum of the previous subarray, removing the first element, to compute the sum of the next subarray.
    /// This will reduce the complexity from O(n^2) to O(n).
    /// If there are no executors in any partition, the first sequence of 'count' partitions is returned.
    /// This method is used to find the best sequence of partitions to pin a set of jobs.
    fn find_partitions_sequence(partitions: &[Partition], count: usize) -> Option<&[Partition]> {
        if count > partitions.len() {
            return None;
        }

        let mut min = None;
        let mut min_busy = usize::MAX;
        let mut busy = partitions
            .iter()
            .take(count)
            .map(|p| p.get_busy_worker_count())
            .sum();

        if busy == 0 {
            return Some(&partitions[0..count]);
        }

        if busy < min_busy {
            min = Some(&partitions[0..count]);
            min_busy = busy;
        }

        for i in count..partitions.len() {
            busy -= partitions[i - count].get_busy_worker_count();
            busy += partitions[i].get_busy_worker_count();

            if busy == 0 {
                return Some(&partitions[i - count + 1..=i]);
            }

            if busy < min_busy {
                min = Some(&partitions[i - count + 1..=i]);
                min_busy = busy;
            }
        }

        min
    }
    /// Push a function into the orchestrator.
    /// This method return a JobInfo that can be used to wait for the Job to finish.
    /// If there aren't executors in the partitions of the orchestrator or all the existing executors are busy, a new executor is created.
    fn push_single<F>(partitions: &[Partition], f: F) -> JobInfo
    where
        F: FnOnce() + Send + 'static,
    {
        let partition = Self::find_partition(partitions);
        match partition {
            Some(p) => p.push(f),
            None => panic!("No partition found!"), // This should never happen.
        }
    }

    /// Push multiple functions into the orchestrator.
    /// This method return a vector of JobInfo that can be used to wait for the Jobs to finish.
    /// If the number of functions is lower than the number of partitions, this method will try to push all the functions in partitions that are contiguous in the vector.
    /// If the number of functions is greater than the number of partitions, this method will distribute evenly the functions in the sequence of partitions found.
    /// If there aren't executors in the partitions of the orchestrator or all the existing executors are busy, new executor are created.
    pub(crate) fn push_jobs<F>(&self, mut f: Vec<F>) -> Vec<JobInfo>
    where
        F: FnOnce() + Send + 'static,
    {
        let mut job_info = Vec::with_capacity(f.len());

        let mut partitions = None;

        if f.len() > 1 {
            partitions = Self::find_partitions_sequence(&self.partitions, f.len());
        }
        match partitions {
            Some(p) => {
                for partition in p {
                    let func = f.remove(0);
                    job_info.push(partition.push(move || {
                        func();
                    }));
                }
            }
            None => {
                for _i in 0..f.len() {
                    let func = f.remove(0);
                    job_info.push(Self::push_single(&self.partitions, move || {
                        func();
                    }));
                }
            }
        }
        job_info
    }

    pub(crate) fn get_configuration(&self) -> Arc<Configuration> {
        Arc::clone(&self.configuration)
    }

    /// Delete the global orchestrator.
    ///
    /// # Safety
    /// This method should be used only when writing tests or benchmarks, or in
    /// the few cases where we want destroy the global orchestrator long before the end
    /// the application.
    pub unsafe fn delete_global_orchestrator() {
        unsafe {
            drop(ORCHESTRATOR.take());
        }
    }
}

impl Drop for Orchestrator {
    fn drop(&mut self) {
        while !self.partitions.is_empty() {
            let partition = self.partitions.remove(0);
            debug!(
                "Total worker for Partition[{}]: {}",
                partition.core_id.id,
                partition.get_worker_count()
            )
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;
    use std::sync::{atomic::AtomicUsize, Arc};

    #[test]
    #[serial]
    fn test_orchestrator() {
        let configuration = Arc::new(Configuration::new_default());
        let orchestrator = Orchestrator::new(Arc::clone(&configuration));
        let counter = Arc::new(AtomicUsize::new(0));

        let mut jobs_info = Vec::with_capacity(1000);

        (0..1000).for_each(|_| {
            let counter_clone = counter.clone();
            let mut job_info = orchestrator.push_jobs(vec![move || {
                counter_clone.fetch_add(1, Ordering::AcqRel);
            }]);
            jobs_info.push(job_info.remove(0));
        });

        for job_info in jobs_info {
            job_info.wait();
        }

        assert_eq!(counter.load(Ordering::Acquire), 1000);
    }

    #[test]
    #[serial]
    fn test_global_orchestrator() {
        let orchestrator = get_global_orchestrator();
        let counter = Arc::new(AtomicUsize::new(0));

        let mut jobs_info = Vec::with_capacity(1000);

        (0..1000).for_each(|_| {
            let counter_clone = counter.clone();
            let mut job_info = orchestrator.push_jobs(vec![move || {
                counter_clone.fetch_add(1, Ordering::AcqRel);
            }]);
            jobs_info.push(job_info.remove(0));
        });

        for job_info in jobs_info {
            job_info.wait();
        }

        assert_eq!(counter.load(Ordering::Acquire), 1000);
        unsafe {
            Orchestrator::delete_global_orchestrator();
        }
    }
}