summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs4
-rwxr-xr-xsrc/main.rs8
-rwxr-xr-xsrc/solvers/intuitive.rs4
-rwxr-xr-xsrc/solvers/mod.rs27
-rw-r--r--src/solvers/opencl.rs99
5 files changed, 72 insertions, 70 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 9c73567..8699cc1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,10 +2,8 @@ mod permutations;
pub mod solver;
pub mod solvers;
pub mod structs;
-#[macro_use]
-extern crate lazy_static;
-pub static N: u32 = 8;
+pub static N: u32 = 10;
pub fn solve(n: u32) {
let mut solver = solvers::intuitive::NormalSolver::new(n);
solver.solve();
diff --git a/src/main.rs b/src/main.rs
index 178664f..fdf3fcd 100755
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,17 +2,13 @@ mod permutations;
mod solver;
mod solvers;
mod structs;
-#[macro_use]
-pub extern crate lazy_static;
-
-use crate::solver::{Solver, IteratorSolver};
+use crate::solver::{IteratorSolver, Solver};
pub static N: u32 = 8;
fn main() {
//let mut solver = solvers::intuitive::NormalSolver::new(N);
//solver.solve();
-
- let mut solver = solvers::gpusolver::GpuSolver::new(N);
+ let solver = solvers::gpusolver::GpuSolver::new(N);
println!("solver: {:?}", solver);
for (i, solution) in solver.solve().enumerate() {
println!("{}: {:?}", i, solution);
diff --git a/src/solvers/intuitive.rs b/src/solvers/intuitive.rs
index b8d375a..3db1d33 100755
--- a/src/solvers/intuitive.rs
+++ b/src/solvers/intuitive.rs
@@ -52,8 +52,8 @@ impl NormalSolver {
pub fn solve(&mut self) {
for (n, i) in self.permutations.iter().enumerate() {
let tmp: Vec<u32> = i.iter().map(|x| *x).collect();
- //println!("perm {}: {:?}", n, tmp);
- println!("perm {}: {:?}", n, self.masks[n]);
+ println!("perm {}: {:?}", n, tmp);
+ println!("perm {}: {:b}", n, self.masks[n]);
}
println!("calculate results");
self.permute(
diff --git a/src/solvers/mod.rs b/src/solvers/mod.rs
index 6d26266..1bdc228 100755
--- a/src/solvers/mod.rs
+++ b/src/solvers/mod.rs
@@ -1,28 +1,5 @@
//pub mod incremental_block;
pub mod intuitive;
-#[cfg(feature = "gpu")]
-pub mod opencl;
+//#[cfg(feature = "gpu")]
pub mod gpusolver;
-
-lazy_static! {
- pub static ref PERMUTATIONS: (Vec<Vec<u32>>, Vec<u64>) = {
- let n = crate::N;
- let mut heap = (1..=n).collect::<Vec<u32>>();
- let heap = permutohedron::Heap::new(&mut heap);
- let n_f = permutohedron::factorial(n as usize);
- let mut permutations = Vec::with_capacity(n_f);
-
- let mut masks: Vec<u64> = vec![0; n_f];
- println!("Generating permutations");
- for (j, data) in heap.enumerate() {
- let mut sum = 0;
- permutations.push(data.clone());
- for stone in data.iter().take(n as usize - 1) {
- sum += stone;
- masks[j] |= 1 << sum;
- }
- }
- (permutations, masks)
- };
-}
-
+pub mod opencl;
diff --git a/src/solvers/opencl.rs b/src/solvers/opencl.rs
index 25b8119..676c5cc 100644
--- a/src/solvers/opencl.rs
+++ b/src/solvers/opencl.rs
@@ -1,49 +1,80 @@
-use lazy_static::*;
-use ocl::ProQue;
-#[macro_use]
+use ocl::{Buffer, Context, Device, Platform, Queue};
+use std::sync::mpsc::{Receiver, Sender};
-lazy_static! {
- static ref BUFFER: ocl::Buffer<u64> = {
+pub struct Job {
+ rows: Vec<u32>,
+ bitmask: u64,
+}
+
+pub struct GpuSolver {
+ platform: Platform,
+ device: Device,
+ context: Context,
+ queue: Queue,
+ n: u32,
+ h: u32,
+ w: u32,
+ permutations: Buffer<u64>,
+ rec_queues: Vec<Receiver<Job>>,
+}
+
+impl GpuSolver {
+ pub fn new(permutation_masks: &[u64], n: u32, h: u32, w: u32) -> ocl::Result<Vec<Sender<Job>>> {
let platform = ocl::Platform::default();
- let device = ocl::Device::first(platform).unwrap();
+ let device = ocl::Device::first(platform)?;
let context = ocl::Context::builder()
.platform(platform)
.devices(device.clone())
- .build()
- .unwrap();
- let queue = ocl::Queue::new(&context, device, None).unwrap();
+ .build()?;
+ let queue = ocl::Queue::new(&context, device, None)?;
- println!("created buffer!");
- ocl::Buffer::builder()
- .queue(queue)
- .copy_host_slice(crate::solvers::PERMUTATIONS.1.as_ref())
- .len(crate::solvers::PERMUTATIONS.1.len())
- .build()
- .unwrap()
- };
- static ref QUEUE: ocl::ProQue = {
- let src =
- std::fs::read_to_string("src/solvers/check.cl").expect("failed to open kernel file");
- ProQue::builder()
- .src(src)
- .dims(crate::solvers::PERMUTATIONS.0.len())
- .build()
- .unwrap()
- };
-}
+ let buffer = ocl::Buffer::builder()
+ .queue(queue.clone())
+ .copy_host_slice(permutation_masks)
+ .len(permutation_masks.len())
+ .build()?;
-pub fn get_buffer() -> &'static ocl::Buffer<u64> {
- &BUFFER
-}
+ let mut senders = Vec::with_capacity(h as usize);
+ let mut receivers = Vec::with_capacity(h as usize);
+ for _ in 0..h {
+ let (sx, rx) = std::sync::mpsc::channel();
+ senders.push(sx);
+ receivers.push(rx);
+ }
+ let solver = Self {
+ platform,
+ device,
+ context,
+ queue,
+ n,
+ h,
+ w,
+ permutations: buffer,
+ rec_queues: receivers,
+ };
+ std::thread::spawn(move || {
+ solver.run();
+ });
+ Ok(senders)
+ }
+
+ fn run(self) -> ! {
+ for rec in self.rec_queues.iter().cycle() {
+ //if rec.
+ }
+ loop {}
+ }
+}
+/*
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 = ProQue::builder()
+ println!("offset: {}", offset);
+ println!("length: {}", permutations.len() - offset);
+ let pro_que = ocl::ProQue::builder()
.src(src)
.dims(permutations.len() - offset)
.build()?;
@@ -72,4 +103,4 @@ pub fn check(permutations: &[u64], w: u32, n: u32, mask: u64, offset: usize) ->
println!("The resuts are now '{:?}'!", vec);
}
Ok(())
-}
+}*/