From 7f285dbdec9ed5d7f8b6d41541ad13085e56c403 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 26 Mar 2021 20:45:22 +0000 Subject: [PATCH] things --- Cargo.toml | 1 + src/main.rs | 27 ++++++++++++++++++--------- src/model.rs | 12 ++++++------ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index df00865..7a3560c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ itertools = "0.10" rand = "0.8.3" rand_distr = "0.4" rayon = "1.5" +fastapprox = "0.3.0" arrayfire = {git = "https://github.com/arrayfire/arrayfire-rust.git"} #arrayfire = "3.8.0" diff --git a/src/main.rs b/src/main.rs index ea6d165..f16c1a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,13 @@ use rand::Rng; use arrayfire as af; fn main() { - backend_man(); - // af::set_backend(af::Backend::CPU); - af::set_device(0); - af::info(); + let gpu_compute: bool = false; + if gpu_compute { + backend_man(); + // af::set_backend(af::Backend::CPU); + af::set_device(0); + af::info(); + } // let n_iterations = 16384; let n_iterations = 2024; @@ -39,11 +42,17 @@ fn main() { let mut model = model::Model::new(width, height, n_particles, n_populations, diffusivity); model.print_configurations(); - // let dims = af::Dim4::new(&[n_particles as u64, 1, 1, 1]); - for i in 0..n_iterations { - model.step(); - // model.step_cl(dims); - pb.set_position(i); + if gpu_compute { + let dims = af::Dim4::new(&[n_particles as u64, 1, 1, 1]); + for i in 0..n_iterations { + model.step_cl(dims); + pb.set_position(i); + } + } else { + for i in 0..n_iterations { + model.step(); + pb.set_position(i); + } } pb.finish(); diff --git a/src/model.rs b/src/model.rs index aee3385..6d60a05 100644 --- a/src/model.rs +++ b/src/model.rs @@ -188,16 +188,16 @@ impl Model { } = grid.config; let (width, height) = (grid.width, grid.height); - let xc = agent.x + agent.angle.cos() * sensor_distance; - let yc = agent.y + agent.angle.sin() * sensor_distance; + let xc = agent.x + fastapprox::faster::cos(agent.angle) * sensor_distance; + let yc = agent.y + fastapprox::faster::sin(agent.angle) * sensor_distance; let agent_add_sens = agent.angle + sensor_angle; let agent_sub_sens = agent.angle - sensor_angle; - let xl = agent.x + agent_sub_sens.cos() * sensor_distance; - let yl = agent.y + agent_sub_sens.sin() * sensor_distance; - let xr = agent.x + agent_add_sens.cos() * sensor_distance; - let yr = agent.y + agent_add_sens.sin() * sensor_distance; + let xl = agent.x + fastapprox::faster::cos(agent_sub_sens) * sensor_distance; + let yl = agent.y + fastapprox::faster::sin(agent_sub_sens) * sensor_distance; + let xr = agent.x + fastapprox::faster::cos(agent_add_sens) * sensor_distance; + let yr = agent.y + fastapprox::faster::sin(agent_add_sens) * sensor_distance; // Sense. We sense from the buffer because this is where we previously combined data // from all the grid.