File size: 1,380 Bytes
be99550 | 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 | use rayon::prelude::*;
pub trait SyncStrategy: Sync + Send {
fn apply_sync(packed_phase: u64, sync_pull: u64, mask_2bit: u64) -> u64;
}
pub struct PaletteAStrategy;
impl SyncStrategy for PaletteAStrategy {
#[inline(always)]
fn apply_sync(packed_phase: u64, sync_pull: u64, _mask: u64) -> u64 {
// `--1` ์ํ(0b11)๋ฅผ ์ ์ฒด 32๊ฐ ๋ด๋ฐ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ ์ฉํ๊ธฐ ์ํ ๋นํธ ์ฐ์ฐ ์์
let pull_11 = sync_pull | (sync_pull << 1);
(packed_phase & !pull_11) | pull_11
}
}
/// ์ฑ๊ธ ์ค๋ ๋ ๋๊ธฐํ (๊ธฐ์กด)
pub fn synchronize_phases_fast<T: SyncStrategy>(packed_phases: &mut [u64], context_signal: u64) {
let mask_2bit = 0x5555555555555555;
for phase in packed_phases.iter_mut() {
let phase_difference = *phase ^ context_signal;
let sync_pull = (phase_difference & mask_2bit) >> 1;
*phase = T::apply_sync(*phase, sync_pull, mask_2bit);
}
}
/// [์ ๊ท] ๋ฉํฐ์ฝ์ด CPU 100% ์ ์ ๋๊ธฐํ (Rayon ์ ์ฉ)
pub fn synchronize_phases_par<T: SyncStrategy>(packed_phases: &mut [u64], context_signal: u64) {
let mask_2bit = 0x5555555555555555;
packed_phases.par_iter_mut().for_each(|phase| {
let phase_difference = *phase ^ context_signal;
let sync_pull = (phase_difference & mask_2bit) >> 1;
*phase = T::apply_sync(*phase, sync_pull, mask_2bit);
});
}
|