summaryrefslogtreecommitdiff
path: root/src/solvers/gpusolver.rs
blob: 41de7e7cfaf5339e2cb75fec65e45188a7161c8e (plain)
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
use crate::permutations::PermutationGenerator;
use crate::solvers::{opencl, wall_stats, IteratorSolver, Solver};
use crate::structs::StoneWall;

#[derive(Debug)]
pub struct GpuSolver {
    n: u32,
    h: u32,
    w: u32,
    permutations: Vec<Vec<u32>>,
    masks: Vec<u64>,
}

impl GpuSolver {
    fn solve_to_vec(&mut self) -> Vec<StoneWall> {
        let src =
            std::fs::read_to_string("src/solvers/check.cl").expect("failed to open kernel file");

        let senders =
            opencl::GpuSolver::launch_sevice(&self.masks, self.n, self.h, self.w, 4, src.as_ref())
                .unwrap();
        for i in 0..12 {
            senders[1 - i / 6]
                .send(opencl::Job::new(vec![i as u32], self.masks[i]))
                .unwrap();
        }
        loop {
            std::thread::sleep(std::time::Duration::from_secs(5));
        }
        vec![]
    }
}

fn generate_permutations(n: u32) -> Vec<Vec<u32>> {
    crate::permutations::HeapsPermutations::permutations(n)
}

fn generate_masks(permutations: &[Vec<u32>]) -> Vec<u64> {
    let mut masks = Vec::with_capacity(permutations.len());
    for p in permutations {
        let mut v = 0;
        let mut x = 0u64;
        for i in p.iter().take(p.len() - 1).map(|i| {
            v += i;
            v
        }) {
            x |= 1 << i
        }
        masks.push(x)
    }
    masks
}

impl Solver for GpuSolver {
    fn new(n: u32) -> Self {
        let (h, w) = wall_stats(n);
        let permutations = generate_permutations(n);
        let masks = generate_masks(&permutations);
        Self {
            n,
            h,
            w,
            permutations,
            masks,
        }
    }
    fn n(&self) -> u32 {
        self.n
    }
    fn h(&self) -> u32 {
        self.h
    }
    fn w(&self) -> u32 {
        self.w
    }
}

impl IteratorSolver for GpuSolver {
    type IntoIter = std::vec::IntoIter<StoneWall>;
    fn solve(mut self) -> Self::IntoIter {
        self.solve_to_vec().into_iter()
    }
}