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);
    });
}