use super::{CheckRequest, Message, RowResult}; use std::sync::mpsc::{channel, Receiver, Sender}; use std::thread::JoinHandle; #[derive(Debug)] struct RequestBuffer { mask_buff: Vec, row_buff: Vec>, pointer: usize, } impl RequestBuffer { pub fn new(size: usize) -> Self { RequestBuffer { mask_buff: vec![0; size], row_buff: vec![Vec::new(); size], pointer: 0, } } pub fn read(&mut self, request: CheckRequest) -> Option<(&[u64], &[Vec])> { self.mask_buff[self.pointer] = request.bitmask; self.row_buff[self.pointer] = request.rows; self.pointer += 1; if self.pointer == self.mask_buff.len() { self.pointer = 0; return Some((self.mask_buff.as_ref(), self.row_buff.as_ref())); } None } } pub struct OclManager { job_id: u64, host_sender: Sender, output_sender: Sender, receiver: Receiver, buffers: Vec, output_handle: JoinHandle<()>, host_handle: Option>, } impl OclManager { pub fn launch_sevice( permutations: &[Vec], permutations_mask: &[u64], n: u32, // Workgroup size, set to 0 for max mut wg_size: usize, result_output: Sender, ) -> (Sender, JoinHandle<()>) { let (h, w) = crate::solvers::wall_stats(n); let src = include_str!("check.cl"); let platform = ocl::Platform::default(); let device = ocl::Device::first(platform).expect("failed to create opencl device"); let max_wg_size = device.max_wg_size().expect("failed to query max_wg_size"); if wg_size == 0 { wg_size = max_wg_size; } else if wg_size > max_wg_size { println!("invalid workgroup size"); } let (output_sender, output_handle) = super::output::Output::launch_sevice(permutations, permutations_mask, result_output); let (host_sender, host_handle) = super::host::Host::launch_sevice( permutations_mask, n, h, w, wg_size, src, output_sender.clone(), ) .unwrap(); let (sender, receiver) = channel(); println!("wg {}", wg_size); let mut buffers = Vec::with_capacity((n - h + 1) as usize); for _ in 0..=(n - h) { buffers.push(RequestBuffer::new(wg_size as usize)); } let manager = Self { job_id: 0, host_sender, output_sender, receiver, buffers, output_handle, host_handle: Some(host_handle), }; ( sender, std::thread::Builder::new() .name("GPU Manager Deamon".into()) .spawn(move || { manager.run(); }) .unwrap(), ) } fn run(mut self) { loop { match self.receiver.recv().expect("Channel to GPU Manager broke") { Message::CheckRequest(request) => { let queue = request.queue; if let Some(buffer) = self.buffers[queue as usize].read(request) { self.host_sender .send(Message::HostMessage((self.job_id, queue, buffer.0.into()))) .unwrap(); self.output_sender .send(Message::OutputMessage((self.job_id, buffer.1.into()))) .unwrap(); self.job_id += 1; } } Message::CpuDone => { //TODO panic!("flush buffers"); self.host_sender.send(Message::CpuDone); self.host_handle.take().unwrap().join(); } Message::Terminate => { self.output_sender.send(Message::Terminate); self.output_handle.join(); return; } _ => println!("Invalid MessageType"), } } } }