use ocl::{flags, Buffer, Context, Device, Kernel, Platform, Program, Queue}; use std::sync::mpsc::{Receiver, Sender}; pub struct Job { rows: Vec, bitmask: u64, } 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, wg_size: usize, permutations: Buffer, rec_queues: Vec, } struct RequestBuffer { mask_buff: Vec, row_buff: Vec>, pointer: usize, receiver: Receiver, } impl RequestBuffer { pub fn new(size: usize, receiver: Receiver) -> Self { RequestBuffer { mask_buff: Vec::with_capacity(size), row_buff: Vec::with_capacity(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] { self.row_buff.as_ref() } } impl GpuSolver { pub fn launch_sevice( permutation_masks: &[u64], n: u32, h: u32, w: u32, src: &str, ) -> ocl::Result>> { 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(h as usize); let mut receivers = Vec::with_capacity(h as usize); let wg_size = device.max_wg_size()?; for _ in 0..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, }; std::thread::spawn(move || { solver.run(); }); Ok(senders) } fn get_dim(&self, queue: usize) -> usize { let chunk = self.permutations.len() / self.n as usize; let dim = (queue + 1) * chunk; if dim % self.wg_size == 0 { dim } else { let dim_small = dim / self.wg_size; ((dim_small + 1) * chunk) } } fn get_off(&self, queue: usize) -> u64 { let chunk = self.permutations.len() / self.n as usize; let dim = (queue + 1) * chunk; ((self.n as usize - queue - 1) * chunk - self.wg_size + dim % self.wg_size) as u64 } 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); let chunk = self.permutations.len() / self.n as usize; for i in 0..queues { let buffer: Buffer = Buffer::builder() .queue(self.queue.clone()) .len(self.wg_size) .flags(flags::MEM_READ_WRITE) .build() .unwrap(); instruction_buffer.push(buffer); let results: Buffer = Buffer::builder() .queue(self.queue.clone()) .len(self.wg_size * self.get_dim(i) / 64) .flags(flags::MEM_READ_WRITE) .build() .unwrap(); result_buffer.push(results); } 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); 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::(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; dim * self.wg_size / 64]; result_buffer[i] .cmd() .queue(&self.queue) .offset(0) .read(&mut result) .enq() .unwrap(); println!("{:?}", result); } } 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::()?; 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(()) }*/