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
use std::{
    fmt,
    marker::PhantomData,
    ops::ControlFlow,
    sync::{
        mpsc::{Receiver, SendError, SyncSender, TrySendError},
        Arc, Barrier,
    },
    task::Poll,
    time::{Duration, Instant},
};

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

use necsim_impls_std::event_log::recorder::EventLogRecorder;
use necsim_partitioning_core::{partition::Partition, LocalPartition, MigrationMode};

use crate::vote::{AsyncVote, Vote};

#[allow(clippy::module_name_repetitions)]
pub struct ThreadsLocalPartition<R: Reporter> {
    partition: Partition,
    vote_any: Vote<bool>,
    vote_min_time: Vote<(PositiveF64, u32)>,
    vote_termination: AsyncVote<ControlFlow<(), ()>>,
    emigration_buffers: Box<[Vec<MigratingLineage>]>,
    emigration_channels: Box<[SyncSender<Vec<MigratingLineage>>]>,
    immigration_buffers: Vec<Vec<MigratingLineage>>,
    immigration_channel: Receiver<Vec<MigratingLineage>>,
    last_migration_times: Box<[Instant]>,
    communicated_since_last_termination_vote: bool,
    migration_interval: Duration,
    recorder: EventLogRecorder,
    local_remaining: u64,
    progress_channel: SyncSender<(u64, u32)>,
    last_report_time: Instant,
    progress_interval: Duration,
    sync_barrier: Arc<Barrier>,
    _marker: PhantomData<R>,
}

impl<R: Reporter> fmt::Debug for ThreadsLocalPartition<R> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct(stringify!(ThreadsLocalPartition)).finish()
    }
}

impl<R: Reporter> ThreadsLocalPartition<R> {
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub(crate) fn new(
        partition: Partition,
        vote_any: &Vote<bool>,
        vote_min_time: &Vote<(PositiveF64, u32)>,
        vote_termination: &AsyncVote<ControlFlow<(), ()>>,
        emigration_channels: &[SyncSender<Vec<MigratingLineage>>],
        immigration_channel: Receiver<Vec<MigratingLineage>>,
        migration_interval: Duration,
        mut recorder: EventLogRecorder,
        progress_channel: SyncSender<(u64, u32)>,
        progress_interval: Duration,
        sync_barrier: &Arc<Barrier>,
    ) -> Self {
        recorder.set_event_filter(R::ReportSpeciation::VALUE, R::ReportDispersal::VALUE);

        let partition_size = partition.size().get() as usize;

        let mut emigration_buffers = Vec::with_capacity(partition_size);
        emigration_buffers.resize_with(partition_size, Vec::new);

        let now = Instant::now();

        Self {
            partition,
            vote_any: vote_any.clone(),
            vote_min_time: vote_min_time.clone(),
            vote_termination: vote_termination.clone(),
            emigration_buffers: emigration_buffers.into_boxed_slice(),
            emigration_channels: Vec::from(emigration_channels).into_boxed_slice(),
            immigration_buffers: Vec::new(),
            immigration_channel,
            last_migration_times: vec![
                now.checked_sub(migration_interval).unwrap_or(now);
                partition_size
            ]
            .into_boxed_slice(),
            communicated_since_last_termination_vote: false,
            migration_interval,
            recorder,
            local_remaining: 0,
            progress_channel,
            last_report_time: now.checked_sub(progress_interval).unwrap_or(now),
            progress_interval,
            sync_barrier: sync_barrier.clone(),
            _marker: PhantomData::<R>,
        }
    }
}

impl<'p, R: Reporter> LocalPartition<'p, R> for ThreadsLocalPartition<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 {
        self.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,
    {
        for (partition, emigrant) in emigrants {
            self.emigration_buffers[partition as usize].push(emigrant);
        }

        let self_rank_index = self.get_partition().rank() as usize;

        let now = Instant::now();

        // Receive incoming immigrating lineages
        if match immigration_mode {
            MigrationMode::Force => true,
            MigrationMode::Default => {
                now.duration_since(self.last_migration_times[self_rank_index])
                    >= self.migration_interval
            },
            MigrationMode::Hold => false,
        } {
            self.last_migration_times[self_rank_index] = now;

            self.immigration_buffers
                .extend(self.immigration_channel.try_iter());
        }

        // Send outgoing emigrating lineages
        for partition in self.partition.size().partitions() {
            let rank_index = partition.rank() as usize;

            if rank_index != self_rank_index
                && match emigration_mode {
                    MigrationMode::Force => true,
                    MigrationMode::Default => {
                        now.duration_since(self.last_migration_times[rank_index])
                            >= self.migration_interval
                    },
                    MigrationMode::Hold => false,
                }
            {
                let emigration_buffer = &mut self.emigration_buffers[rank_index];

                if !emigration_buffer.is_empty() {
                    let emigration_buffer_message = std::mem::take(emigration_buffer);

                    // Send a new non-empty request iff there is capacity
                    match self.emigration_channels[rank_index].try_send(emigration_buffer_message) {
                        Ok(()) => {
                            self.last_migration_times[rank_index] = now;

                            // we cannot terminate in this round since this partition gave up work
                            self.communicated_since_last_termination_vote = true;
                        },
                        Err(TrySendError::Full(emigration_buffer_message)) => {
                            *emigration_buffer = emigration_buffer_message;
                        },
                        Err(TrySendError::Disconnected(_)) => {
                            panic!("threads partitioning migration channel disconnected")
                        },
                    }
                }
            }
        }

        ImmigrantPopIterator::new(&mut self.immigration_buffers)
    }

    fn reduce_vote_any(&mut self, vote: bool) -> bool {
        self.vote_any.vote(|acc| match acc {
            None => vote,
            Some(acc) => *acc || vote,
        })
    }

    fn reduce_vote_min_time(
        &mut self,
        local_time: PositiveF64,
    ) -> Result<PositiveF64, PositiveF64> {
        let vote = (local_time, self.partition.rank());

        let result = self.vote_min_time.vote(|acc| match acc {
            None => vote,
            Some(acc) => vote.min(*acc),
        });

        if result.1 == self.partition.rank() {
            Ok(result.0)
        } else {
            Err(result.0)
        }
    }

    fn wait_for_termination(&mut self) -> ControlFlow<(), ()> {
        let mut local_wait = ControlFlow::Break(());

        // This partition can only terminate once all emigrations have been processed
        for buffer in self.emigration_buffers.iter() {
            if !buffer.is_empty() {
                local_wait = ControlFlow::Continue(());
                break;
            }
        }
        // This partition can only terminate once all immigrations have been processed
        if !self.immigration_buffers.is_empty() {
            local_wait = ControlFlow::Continue(());
        }

        // Continue early if no vote is ongoing
        if local_wait.is_continue() && !self.vote_termination.is_ongoing() {
            return ControlFlow::Continue(());
        }

        // This partition can only terminate if there was no communication since the
        //  last vote
        if self.communicated_since_last_termination_vote {
            local_wait = ControlFlow::Continue(());
        }

        // Participate in an async poll, only blocks on a barrier once all votes are in
        let async_vote = self.vote_termination.vote(
            |global_wait| {
                self.communicated_since_last_termination_vote = false;

                match global_wait {
                    Some(ControlFlow::Continue(())) => ControlFlow::Continue(()),
                    Some(ControlFlow::Break(())) | None => local_wait,
                }
            },
            self.partition.rank(),
        );

        match async_vote {
            Poll::Pending => {
                // This partition doesn't have any work right now but we have
                //  to do another round of voting, so let's yield to not busy
                //  wait - we'll be woken up if more work comes in
                std::thread::yield_now();
                ControlFlow::Continue(())
            },
            Poll::Ready(result) => result,
        }
    }

    fn report_progress_sync(&mut self, remaining: u64) {
        if let Err(SendError(_)) = self
            .progress_channel
            .send((remaining, self.partition.rank()))
        {
            panic!("threads partitioning sync progress channel disconnected");
        }

        self.sync_barrier.wait();
    }
}

impl<R: Reporter> Reporter for ThreadsLocalPartition<R> {
    impl_report!(speciation(&mut self, speciation: MaybeUsed<R::ReportSpeciation>) {
        self.recorder.record_speciation(speciation);
    });

    impl_report!(dispersal(&mut self, dispersal: MaybeUsed<R::ReportDispersal>) {
        self.recorder.record_dispersal(dispersal);
    });

    impl_report!(progress(&mut self, remaining: MaybeUsed<R::ReportProgress>) {
        if self.local_remaining == *remaining {
            return;
        }

        // Only send progress if there is no ongoing termination vote
        if !self.vote_termination.is_ongoing() {
            let now = Instant::now();

            if now.duration_since(self.last_report_time) >= self.progress_interval {
                match self.progress_channel.try_send((*remaining, self.partition.rank())) {
                    Ok(()) => {
                        self.last_report_time = now;
                        self.local_remaining = *remaining;
                    },
                    Err(TrySendError::Full(_)) => (),
                    Err(TrySendError::Disconnected(_)) =>  {
                        panic!("threads partitioning progress channel disconnected")
                    },
                }
            }
        }
    });
}

pub struct ImmigrantPopIterator<'i> {
    immigrants: &'i mut Vec<Vec<MigratingLineage>>,
}

impl<'i> ImmigrantPopIterator<'i> {
    fn new(immigrants: &'i mut Vec<Vec<MigratingLineage>>) -> Self {
        Self { immigrants }
    }
}

impl<'i> Iterator for ImmigrantPopIterator<'i> {
    type Item = MigratingLineage;

    fn next(&mut self) -> Option<Self::Item> {
        let mut next_immigrants = self.immigrants.last_mut()?;

        loop {
            if let Some(next) = next_immigrants.pop() {
                return Some(next);
            }

            self.immigrants.pop();
            next_immigrants = self.immigrants.last_mut()?;
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.immigrants.iter().map(Vec::len).sum();
        (len, Some(len))
    }
}