Skip to content

Commit

Permalink
Rename WithMassive to FromMassive & formatting
Browse files Browse the repository at this point in the history
Better indicates what this struct is used for.
  • Loading branch information
Canleskis committed Apr 28, 2023
1 parent 04d73dc commit c41a5cc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/compute_method/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use glam::Vec3A;

use crate::compute_method::{
wgpu::{setup_wgpu, WgpuData},
WithMassive,
FromMassive,
};

/// A brute-force [`ComputeMethod`](super::ComputeMethod) using the GPU with [wgpu](https://github.com/gfx-rs/wgpu).
Expand All @@ -17,11 +17,11 @@ pub struct BruteForce {
}

impl super::ComputeMethod<Vec3A, f32> for BruteForce {
type Storage = WithMassive<Vec3A, f32>;
type Storage = FromMassive<Vec3A, f32>;

#[inline]
fn compute(&mut self, storage: WithMassive<Vec3A, f32>) -> Vec<Vec3A> {
let particles_len = storage.particles.len() as u64;
fn compute(&mut self, storage: Self::Storage) -> Vec<Vec3A> {
let particles_len = storage.affected.len() as u64;
let massive_len = storage.massive.len() as u64;

if massive_len == 0 {
Expand All @@ -36,7 +36,7 @@ impl super::ComputeMethod<Vec3A, f32> for BruteForce {

self.wgpu_data
.get_or_insert_with(|| WgpuData::init(particles_len, massive_len, &self.device))
.write_particle_data(&storage.particles, &storage.massive, &self.queue)
.write_particle_data(&storage.affected, &storage.massive, &self.queue)
.compute_pass(&self.device, &self.queue)
.read_accelerations(&self.device)
}
Expand Down
21 changes: 13 additions & 8 deletions src/compute_method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ impl<T, S> ParticleStorage<T, S> for Vec<(T, S)> {
}
}

/// Storage for particles with a copy of the massives ones in a second vector.
pub struct WithMassive<T, S> {
/// Particles for which the acceleration is computed.
pub particles: Vec<(T, S)>,

/// Particles used to compute the acceleration of the `particles`.
/// Storage for particles with massive and affected particles in two separate vectors.
pub struct FromMassive<T, S> {
/// Particles used to compute the acceleration of the `affected` particles.
pub massive: Vec<(T, S)>,

/// Particles for which the acceleration is computed.
///
/// This vector and the `massive` vector can share particles.
pub affected: Vec<(T, S)>,
}

impl<T, S> ParticleStorage<T, S> for WithMassive<T, S>
impl<T, S> ParticleStorage<T, S> for FromMassive<T, S>
where
T: Copy,
S: Copy + Default + PartialEq,
Expand All @@ -78,7 +80,10 @@ where
.copied()
.collect();

Self { particles, massive }
Self {
affected: particles,
massive,
}
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/compute_method/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ where
T: InternalVector<Scalar = S>,
S: Scalar,
{
type Storage = super::WithMassive<T, S>;
type Storage = super::FromMassive<T, S>;

#[inline]
fn compute(&mut self, storage: super::WithMassive<T, S>) -> Vec<T> {
fn compute(&mut self, storage: Self::Storage) -> Vec<T> {
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

storage
.particles
.affected
.par_iter()
.map(|&(position1, _)| {
storage
Expand Down Expand Up @@ -56,19 +56,19 @@ where
T: InternalVector<Scalar = S, Array = [S; DIM]>,
BoundingBox<T::Array>: BoundingBoxDivide<(T, S), Output = (Orthant<N>, S)>,
{
type Storage = super::WithMassive<T, S>;
type Storage = super::FromMassive<T, S>;

#[inline]
fn compute(&mut self, storage: super::WithMassive<T, S>) -> Vec<T> {
fn compute(&mut self, storage: Self::Storage) -> Vec<T> {
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

let mut tree = Tree::new();

let bbox = BoundingBox::containing(storage.massive.iter().map(|p| p.0.into()));
let root = tree.build_node(storage.massive, bbox);

storage
.particles
.affected
.par_iter()
.map(|&(position, _)| tree.acceleration_at(root, position, self.theta))
.collect()
Expand Down
16 changes: 8 additions & 8 deletions src/compute_method/sequential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ where
type Storage = Vec<(T, S)>;

#[inline]
fn compute(&mut self, storage: Vec<(T, S)>) -> Vec<T> {
fn compute(&mut self, storage: Self::Storage) -> Vec<T> {
let (massive, massless) = storage
.iter()
.partition::<Vec<_>, _>(|(_, mu)| *mu != S::default());
Expand Down Expand Up @@ -71,12 +71,12 @@ where
S: Scalar,
T: InternalVector<Scalar = S>,
{
type Storage = super::WithMassive<T, S>;
type Storage = super::FromMassive<T, S>;

#[inline]
fn compute(&mut self, storage: super::WithMassive<T, S>) -> Vec<T> {
fn compute(&mut self, storage: Self::Storage) -> Vec<T> {
storage
.particles
.affected
.iter()
.map(|&(position1, _)| {
storage
Expand Down Expand Up @@ -117,17 +117,17 @@ where
T: InternalVector<Scalar = S, Array = [S; DIM]>,
BoundingBox<T::Array>: BoundingBoxDivide<(T, S), Output = (Orthant<N>, S)>,
{
type Storage = super::WithMassive<T, S>;
type Storage = super::FromMassive<T, S>;

#[inline]
fn compute(&mut self, storage: super::WithMassive<T, S>) -> Vec<T> {
fn compute(&mut self, storage: Self::Storage) -> Vec<T> {
let mut tree = Tree::new();

let bbox = BoundingBox::containing(storage.massive.iter().map(|p| p.0.into()));
let root = tree.build_node(storage.massive, bbox);

storage
.particles
.affected
.iter()
.map(|&(position, _)| tree.acceleration_at(root, position, self.theta))
.collect()
Expand Down
2 changes: 1 addition & 1 deletion src/compute_method/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<N, D> Tree<N, D> {
}

/// Inserts a new [`Node`] from the given data and bounding box.
///
///
/// This will recursively insert new nodes into the tree until the initial bounding box stops dividing.
pub fn build_node<B>(&mut self, data: Vec<D>, bbox: B) -> Option<NodeID>
where
Expand Down

0 comments on commit c41a5cc

Please sign in to comment.