summaryrefslogtreecommitdiff
path: root/src/solvers/opencl.rs
blob: 431f4677b40ef137fbf398fc0476cb59dec8b266 (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
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
use ocl::{flags, Buffer, Context, Device, Kernel, Platform, Program, Queue};
use std::sync::mpsc::{Receiver, Sender};

pub struct Job {
    rows: Vec<u32>,
    bitmask: u64,
}

impl Job {
    pub fn new(rows: Vec<u32>, bitmask: u64) -> Self {
        Self { rows, bitmask }
    }
}

#[derive(Debug)]
pub struct GpuSolver {
    #[allow(unused)]
    platform: Platform,
    #[allow(unused)]
    device: Device,
    #[allow(unused)]
    context: Context,
    program: Program,
    queue: Queue,
    n: u32,
    h: u32,
    w: u32,
    /// Workgroup size, set to 0 for max
    wg_size: usize,
    permutations: Buffer<u64>,
    rec_queues: Vec<RequestBuffer>,
    walls: Vec<Vec<u32>>,
}

#[derive(Debug)]
struct RequestBuffer {
    mask_buff: Vec<u64>,
    row_buff: Vec<Vec<u32>>,
    pointer: usize,
    receiver: Receiver<Job>,
}

impl RequestBuffer {
    pub fn new(size: usize, receiver: Receiver<Job>) -> Self {
        RequestBuffer {
            mask_buff: vec![0; size],
            row_buff: vec![Vec::new(); size],
            pointer: 0,
            receiver,
        }
    }
    pub fn read(&mut self) -> Option<&[u64]> {
        for job in self.receiver.try_iter() {
            self.mask_buff[self.pointer] = job.bitmask;
            self.row_buff[self.pointer] = job.rows;
            self.pointer += 1;
            if self.pointer == self.mask_buff.len() {
                self.pointer = 0;
                return Some(self.mask_buff.as_ref());
            }
        }
        None
    }
    #[allow(unused)]
    pub fn get_rows(&self) -> &[Vec<u32>] {
        self.row_buff.as_ref()
    }
}

impl GpuSolver {
    pub fn launch_sevice(
        permutation_masks: &[u64],
        n: u32,
        h: u32,
        w: u32,
        mut wg_size: usize,
        src: &str,
    ) -> ocl::Result<Vec<Sender<Job>>> {
        let platform = ocl::Platform::default();
        let device = ocl::Device::first(platform)?;
        let context = ocl::Context::builder()
            .platform(platform)
            .devices(device.clone())
            .build()?;
        let queue = ocl::Queue::new(&context, device, None)?;

        let program = Program::builder()
            .devices(device)
            .src(src)
            .build(&context)?;
        let buffer = ocl::Buffer::builder()
            .queue(queue.clone())
            .flags(flags::MEM_READ_WRITE)
            .copy_host_slice(permutation_masks)
            .len(permutation_masks.len())
            .build()?;

        let mut senders = Vec::with_capacity((n - h + 1) as usize);
        let mut receivers = Vec::with_capacity((n - h + 1) as usize);
        let max_wg_size = device.max_wg_size()?;
        if wg_size == 0 {
            wg_size = max_wg_size;
        } else if wg_size > max_wg_size {
            return Err(ocl::Error::from("invalid workgroup size"));
        }
        for _ in 0..=(n - h) {
            let (sx, rx) = std::sync::mpsc::channel();
            senders.push(sx);
            receivers.push(RequestBuffer::new(wg_size, rx));
        }

        let solver = Self {
            platform,
            device,
            context,
            program,
            queue,
            n,
            h,
            w,
            wg_size,
            permutations: buffer,
            rec_queues: receivers,
            walls: Vec::new(),
        };
        std::thread::Builder::new()
            .name("GPU Deamon".into())
            .spawn(move || {
                solver.run();
            })
            .unwrap();
        println!("started gpu thread");
        Ok(senders)
    }

    fn get_dim(&self, queue: usize) -> usize {
        let chunk = self.permutations.len() / self.n as usize;
        let dim = (queue + 1) * chunk;
        (dim + self.wg_size - 1) / self.wg_size * self.wg_size
    }
    fn get_off(&self, queue: usize) -> u64 {
        let chunk = self.permutations.len() / self.n as usize;
        let off = self.permutations.len() - chunk - self.get_dim(queue);
        if off > isize::max_value() as usize {
            panic!("workgroup size to big, offset underflow")
        }
        off as u64
    }
    fn get_res(&self, queue: usize) -> usize {
        let dim = self.get_dim(queue);
        dim * self.get_res_save_dim()
    }
    fn get_res_save_dim(&self) -> usize {
        (self.wg_size + 63) / 64
    }

    fn run(mut self) -> ! {
        let queues = self.rec_queues.len();
        let mut instruction_buffer = Vec::with_capacity((self.n - self.h) as usize);
        let mut result_buffer = Vec::with_capacity((self.n - self.h) as usize);

        for i in 0..queues {
            let buffer: Buffer<u64> = Buffer::builder()
                .queue(self.queue.clone())
                .len(self.wg_size)
                .flags(flags::MEM_READ_WRITE)
                .build()
                .unwrap();

            instruction_buffer.push(buffer);
            let results: Buffer<u64> = Buffer::builder()
                .queue(self.queue.clone())
                .len(self.get_res(i))
                .flags(flags::MEM_READ_WRITE)
                .build()
                .unwrap();
            result_buffer.push(results);
        }
        println!("finished gpu setup");
        for i in (0..self.rec_queues.len()).cycle() {
            if let Some(buffer) = self.rec_queues[i].read() {
                instruction_buffer[i].write(buffer).enq().unwrap();
                let dim = self.get_dim(i);

                //println!("dim: {}", dim);
                //println!("off: {}", self.get_off(i));
                //println!("result size: {}", self.get_res_save_dim());
                let kernel = Kernel::builder()
                    .program(&self.program)
                    .name("check")
                    .queue(self.queue.clone())
                    .global_work_size(dim)
                    .arg(&self.permutations)
                    .arg(&result_buffer[i])
                    .arg(&instruction_buffer[i])
                    .arg_local::<u64>(self.wg_size)
                    .arg(self.n)
                    .arg(self.w)
                    .arg(self.get_off(i))
                    .build()
                    .unwrap();

                unsafe {
                    kernel
                        .cmd()
                        .queue(&self.queue)
                        .global_work_offset(kernel.default_global_work_offset())
                        .global_work_size(dim)
                        .local_work_size(self.wg_size)
                        .enq()
                        .unwrap();
                }

                // (5) Read results from the device into a vector (`::block` not shown):
                let mut result = vec![0u64; self.get_res(i)];
                result_buffer[i]
                    .cmd()
                    .queue(&self.queue)
                    .offset(0)
                    .read(&mut result)
                    .enq()
                    .unwrap();
                for (j, r) in result.iter().enumerate() {
                    if j == 0 {
                        continue;
                    }
                    for b in 0..64 {
                        if r & (1 << b) != 0 {
                            let permutation =
                                j / self.get_res_save_dim() + self.get_off(i) as usize;
                            let instruction = (j % self.get_res_save_dim()) * 64 + b;
                            let mut wall = self.rec_queues[i].get_rows()[instruction].clone();
                            wall.push(permutation as u32);
                            println!("{:?}", wall);
                            self.walls.push(wall);
                        }
                    }
                }
            }
        }
        panic!();
    }
}
/*
pub fn check(permutations: &[u64], w: u32, n: u32, mask: u64, offset: usize) -> ocl::Result<()> {
    //println!("read src!");
    let src = std::fs::read_to_string("src/solvers/check.cl").expect("failed to open kernel file");

    //println!("created queue!");
    println!("offset: {}", offset);
    println!("length: {}", permutations.len() - offset);
    let pro_que = ocl::ProQue::builder()
        .src(src)
        .dims(permutations.len() - offset)
        .build()?;

    let results = pro_que.create_buffer::<i32>()?;
    let kernel = pro_que
        .kernel_builder("check")
        .arg(get_buffer())
        .arg(&results)
        .arg(mask)
        .arg(n)
        .arg(w)
        .arg(offset as u64)
        //.global_work_offset(offset)
        .build()?;

    //println!("starting calculation");
    unsafe {
        kernel.enq()?;
    }

    let mut vec = vec![0; results.len()];
    results.read(&mut vec).enq()?;

    if vec.iter().any(|x| *x != 0) {
        println!("The resuts are now '{:?}'!", vec);
    }
    Ok(())
}*/