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
use std::ops::ControlFlow;

use necsim_core::{
    impl_report,
    lineage::MigratingLineage,
    reporter::{boolean::False, Reporter},
};
use necsim_core_bond::PositiveF64;

use necsim_partitioning_core::{
    iterator::ImmigrantPopIterator, partition::Partition, LocalPartition, MigrationMode,
};

mod common;
mod parallel;
mod root;
mod utils;

#[allow(clippy::module_name_repetitions)]
pub use parallel::MpiParallelPartition;
#[allow(clippy::module_name_repetitions)]
pub use root::MpiRootPartition;

use crate::FinalisableMpiReporter;

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub enum MpiLocalPartition<'p, R: Reporter> {
    Root(Box<MpiRootPartition<'p, R>>),
    Parallel(Box<MpiParallelPartition<'p, R>>),
}

impl<'p, R: Reporter> LocalPartition<'p, R> for MpiLocalPartition<'p, R> {
    type ImmigrantIterator<'a> = ImmigrantPopIterator<'a> where 'p: 'a, R: 'a;
    type IsLive = False;
    type Reporter = Self;

    fn get_reporter(&mut self) -> &mut Self::Reporter {
        self
    }

    fn get_partition(&self) -> Partition {
        match self {
            Self::Root(partition) => partition.get_partition(),
            Self::Parallel(partition) => partition.get_partition(),
        }
    }

    fn migrate_individuals<'a, E: Iterator<Item = (u32, MigratingLineage)>>(
        &'a mut self,
        emigrants: &mut E,
        emigration_mode: MigrationMode,
        immigration_mode: MigrationMode,
    ) -> Self::ImmigrantIterator<'a>
    where
        'p: 'a,
    {
        match self {
            Self::Root(partition) => {
                partition.migrate_individuals(emigrants, emigration_mode, immigration_mode)
            },
            Self::Parallel(partition) => {
                partition.migrate_individuals(emigrants, emigration_mode, immigration_mode)
            },
        }
    }

    fn reduce_vote_any(&mut self, vote: bool) -> bool {
        match self {
            Self::Root(partition) => partition.reduce_vote_any(vote),
            Self::Parallel(partition) => partition.reduce_vote_any(vote),
        }
    }

    fn reduce_vote_min_time(
        &mut self,
        local_time: PositiveF64,
    ) -> Result<PositiveF64, PositiveF64> {
        match self {
            Self::Root(partition) => partition.reduce_vote_min_time(local_time),
            Self::Parallel(partition) => partition.reduce_vote_min_time(local_time),
        }
    }

    fn wait_for_termination(&mut self) -> ControlFlow<(), ()> {
        match self {
            Self::Root(partition) => partition.wait_for_termination(),
            Self::Parallel(partition) => partition.wait_for_termination(),
        }
    }

    fn report_progress_sync(&mut self, remaining: u64) {
        match self {
            Self::Root(partition) => partition.report_progress_sync(remaining),
            Self::Parallel(partition) => partition.report_progress_sync(remaining),
        }
    }
}

impl<'p, R: Reporter> Reporter for MpiLocalPartition<'p, R> {
    impl_report!(speciation(&mut self, speciation: MaybeUsed<R::ReportSpeciation>) {
        match self {
            Self::Root(partition) => partition.get_reporter().report_speciation(
                speciation.into()
            ),
            Self::Parallel(partition) => partition.get_reporter().report_speciation(
                speciation.into()
            ),
        }
    });

    impl_report!(dispersal(&mut self, dispersal: MaybeUsed<R::ReportDispersal>) {
        match self {
            Self::Root(partition) => partition.get_reporter().report_dispersal(
                dispersal.into()
            ),
            Self::Parallel(partition) => partition.get_reporter().report_dispersal(
                dispersal.into()
            ),
        }
    });

    impl_report!(progress(&mut self, progress: MaybeUsed<R::ReportProgress>) {
        match self {
            Self::Root(partition) => partition.get_reporter().report_progress(
                progress.into()
            ),
            Self::Parallel(partition) => partition.get_reporter().report_progress(
                progress.into()
            ),
        }
    });
}

impl<'p, R: Reporter> MpiLocalPartition<'p, R> {
    pub(crate) fn into_reporter(self) -> FinalisableMpiReporter<R> {
        match self {
            Self::Root(partition) => FinalisableMpiReporter::Root(partition.into_reporter().into()),
            Self::Parallel(_) => FinalisableMpiReporter::Parallel,
        }
    }
}