Skip to content

Commit 095f808

Browse files
committed
Add serde-serialize feature, move code to benchmark
1 parent 931b64a commit 095f808

File tree

7 files changed

+73
-64
lines changed

7 files changed

+73
-64
lines changed

splashsurf_lib/Cargo.toml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ default = []
2929
vtk_extras = ["vtkio"]
3030
profiling = []
3131
io = ["vtk_extras", "vtkio", "ply-rs", "nom", "serde_json", "flate2"]
32+
serde-serialize = ["serde", "serde_derive", "serde_json", "nalgebra/serde-serialize"]
3233

3334
[dependencies]
3435
log = "0.4"
35-
nalgebra = { version = "0.34", features = ["rand", "bytemuck", "serde-serialize"] }
36+
nalgebra = { version = "0.34", features = ["rand", "bytemuck"] }
3637
simba = "0.9.0"
3738
num-traits = "0.2"
3839
num-integer = "0.1"
@@ -51,9 +52,6 @@ bytemuck_derive = "1.3"
5152
numeric_literals = "0.2"
5253
rstar = "0.12"
5354

54-
serde = "1.0.219"
55-
serde_derive = "1.0.219"
56-
5755
# IO
5856
vtkio = { version = "0.6", optional = true }
5957
#vtkio = { git = "https://github.com/elrnv/vtkio", optional = true , rev = "e14c8ff3caf84ae5e21c2ee70aa18275d6e54051" }
@@ -62,6 +60,10 @@ flate2 = { version = "1.0", optional = true }
6260
nom = { version = "8.0", optional = true }
6361
serde_json = { version = "1.0", optional = true }
6462

63+
# Serialization
64+
serde = { version = "1.0", optional = true }
65+
serde_derive = { version = "1.0", optional = true }
66+
6567
[dev-dependencies]
6668
criterion = "0.7"
6769
ultraviolet = "0.10"
@@ -86,4 +88,4 @@ required-features = ["profiling", "io"]
8688
name = "splashsurf_lib_benches"
8789
path = "benches/splashsurf_lib_benches.rs"
8890
harness = false
89-
required-features = ["io"]
91+
required-features = ["io", "serde-serialize"]

splashsurf_lib/benches/benches/bench_grid_loop.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,55 @@
11
use criterion::{Criterion, criterion_group};
2+
use nalgebra::{Scalar, Vector3};
3+
use serde_derive::{Deserialize, Serialize};
24
use splashsurf_lib::dense_subdomains;
35
use splashsurf_lib::kernel::CubicSplineKernel;
6+
use splashsurf_lib::uniform_grid::UniformCartesianCubeGrid3d;
47
use std::time::Duration;
58

9+
// Code to generate the input data for the benchmark from dense_subdomains.rs
10+
/*
11+
if subdomain_particles.len() > 6800 {
12+
println!("{}", subdomain_particles.len());
13+
let arguments = DensityGridLoopParameters {
14+
levelset_grid: levelset_grid.clone(),
15+
subdomain_particles: subdomain_particles.clone(),
16+
subdomain_particle_densities: subdomain_particle_densities.clone(),
17+
subdomain_mc_grid: mc_grid.clone(),
18+
subdomain_ijk: *subdomain_idx.index(),
19+
global_mc_grid: parameters.global_marching_cubes_grid.clone(),
20+
cube_radius,
21+
squared_support_with_margin,
22+
particle_rest_mass: parameters.particle_rest_mass,
23+
compact_support_radius: parameters.compact_support_radius,
24+
};
25+
serde_json::to_writer(
26+
std::fs::File::create(format!(
27+
"density_grid_loop_subdomain_{}.json",
28+
flat_subdomain_idx
29+
))
30+
.unwrap(),
31+
&arguments,
32+
)
33+
.unwrap();
34+
}
35+
*/
36+
37+
#[derive(Clone, Serialize, Deserialize)]
38+
pub struct DensityGridLoopParameters<I: Scalar, R: Scalar> {
39+
pub levelset_grid: Vec<R>,
40+
pub subdomain_particles: Vec<Vector3<R>>,
41+
pub subdomain_particle_densities: Vec<R>,
42+
pub subdomain_mc_grid: UniformCartesianCubeGrid3d<I, R>,
43+
pub subdomain_ijk: [I; 3],
44+
pub global_mc_grid: UniformCartesianCubeGrid3d<i64, R>,
45+
pub cube_radius: I,
46+
pub squared_support_with_margin: R,
47+
pub particle_rest_mass: R,
48+
pub compact_support_radius: R,
49+
}
50+
651
pub fn grid_loop_no_simd(c: &mut Criterion) {
7-
let params: dense_subdomains::DensityGridLoopParameters<i64, f32> = serde_json::from_reader(
52+
let params: DensityGridLoopParameters<i64, f32> = serde_json::from_reader(
853
std::fs::File::open("../data/density_grid_loop_subdomain_33.json").unwrap(),
954
)
1055
.unwrap();
@@ -45,7 +90,7 @@ pub fn grid_loop_neon(c: &mut Criterion) {
4590
return;
4691
}
4792

48-
let params: dense_subdomains::DensityGridLoopParameters<i64, f32> = serde_json::from_reader(
93+
let params: DensityGridLoopParameters<i64, f32> = serde_json::from_reader(
4994
std::fs::File::open("../data/density_grid_loop_subdomain_33.json").unwrap(),
5095
)
5196
.unwrap();
@@ -160,7 +205,7 @@ pub fn grid_loop_avx2(c: &mut Criterion) {
160205
return;
161206
}
162207

163-
let params: dense_subdomains::DensityGridLoopParameters<i64, f32> = serde_json::from_reader(
208+
let params: DensityGridLoopParameters<i64, f32> = serde_json::from_reader(
164209
std::fs::File::open("../data/density_grid_loop_subdomain_33.json").unwrap(),
165210
)
166211
.unwrap();

splashsurf_lib/src/aabb.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
use crate::{Real, RealConvert, ThreadSafe};
44
use nalgebra::{SVector, Scalar};
55
use rayon::prelude::*;
6+
#[cfg(feature = "serde-serialize")]
67
use serde_derive::{Deserialize, Serialize};
78

89
/// Type representing an axis aligned bounding box in arbitrary dimensions
9-
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
10+
#[derive(Clone, Debug, Eq, PartialEq)]
11+
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
1012
pub struct AxisAlignedBoundingBox<R: Scalar, const D: usize> {
1113
min: SVector<R, D>,
1214
max: SVector<R, D>,

splashsurf_lib/src/dense_subdomains.rs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use anyhow::{Context, anyhow};
22
use arrayvec::ArrayVec;
33
use itertools::Itertools;
44
use log::{info, trace};
5-
use nalgebra::{Scalar, Vector3};
5+
use nalgebra::Vector3;
66
use num_integer::Integer;
77
use num_traits::{FromPrimitive, NumCast};
88
use parking_lot::Mutex;
99
use rayon::prelude::*;
10-
use serde_derive::{Deserialize, Serialize};
1110
use std::cell::RefCell;
1211
use std::sync::atomic::{AtomicUsize, Ordering};
1312
use thread_local::ThreadLocal;
@@ -658,20 +657,6 @@ pub(crate) struct SurfacePatch<I: Index, R: Real> {
658657
pub exterior_vertex_edge_indices: Vec<(I, EdgeIndex<I>)>,
659658
}
660659

661-
#[derive(Clone, Serialize, Deserialize)]
662-
pub struct DensityGridLoopParameters<I: Scalar, R: Scalar> {
663-
pub levelset_grid: Vec<R>,
664-
pub subdomain_particles: Vec<Vector3<R>>,
665-
pub subdomain_particle_densities: Vec<R>,
666-
pub subdomain_mc_grid: UniformCartesianCubeGrid3d<I, R>,
667-
pub subdomain_ijk: [I; 3],
668-
pub global_mc_grid: UniformCartesianCubeGrid3d<GlobalIndex, R>,
669-
pub cube_radius: I,
670-
pub squared_support_with_margin: R,
671-
pub particle_rest_mass: R,
672-
pub compact_support_radius: R,
673-
}
674-
675660
/// Auto-dispatching density grid loop for f32/i64: chooses AVX2+FMA on x86, NEON on aarch64, otherwise scalar fallback
676661
pub fn density_grid_loop_auto<K: SymmetricKernel3d<f32>>(
677662
levelset_grid: &mut [f32],
@@ -1359,33 +1344,6 @@ pub(crate) fn reconstruction<I: Index, R: Real>(
13591344
levelset_grid.fill(R::zero());
13601345
levelset_grid.resize(mc_total_points.to_usize().unwrap(), R::zero());
13611346

1362-
/*
1363-
if subdomain_particles.len() > 6800 {
1364-
println!("{}", subdomain_particles.len());
1365-
let arguments = DensityGridLoopParameters {
1366-
levelset_grid: levelset_grid.clone(),
1367-
subdomain_particles: subdomain_particles.clone(),
1368-
subdomain_particle_densities: subdomain_particle_densities.clone(),
1369-
subdomain_mc_grid: mc_grid.clone(),
1370-
subdomain_ijk: *subdomain_idx.index(),
1371-
global_mc_grid: parameters.global_marching_cubes_grid.clone(),
1372-
cube_radius,
1373-
squared_support_with_margin,
1374-
particle_rest_mass: parameters.particle_rest_mass,
1375-
compact_support_radius: parameters.compact_support_radius,
1376-
};
1377-
serde_json::to_writer(
1378-
std::fs::File::create(format!(
1379-
"density_grid_loop_subdomain_{}.json",
1380-
flat_subdomain_idx
1381-
))
1382-
.unwrap(),
1383-
&arguments,
1384-
)
1385-
.unwrap();
1386-
}
1387-
*/
1388-
13891347
use std::any::TypeId;
13901348
use std::mem::transmute;
13911349
if enable_simd

splashsurf_lib/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//! by binary crates calling into this library to add their own profiling scopes to the measurements.
2222
//! If this features is not enabled, the macro will just expend to a no-op and remove the (small)
2323
//! performance overhead of the profiling.
24+
//! - **`serde-serialize`**: Enables `Serialize` and `Deserialize` impls for some types using `serde`.
2425
//!
2526
2627
use log::{info, warn};
@@ -74,7 +75,6 @@ pub mod uniform_grid;
7475
mod utils;
7576
pub(crate) mod workspace;
7677

77-
// TODO: Add documentation of feature flags
7878
// TODO: Feature flag for multi threading
7979
// TODO: Feature flag to disable (debug level) logging?
8080

@@ -176,7 +176,7 @@ pub struct Parameters<R: Scalar> {
176176
/// Whether to allow multi threading within the surface reconstruction procedure
177177
pub enable_multi_threading: bool,
178178
/// Whether to enable SIMD vectorization for some computations if supported by the target architecture
179-
///
179+
///
180180
/// Currently only supported on x86/x86_64 (AVX2 + FMA) and aarch64 (NEON) for single precision (f32) reconstructions.
181181
pub enable_simd: bool,
182182
/// Parameters for the spatial decomposition of the surface reconstruction

splashsurf_lib/src/traits.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use num_traits::{
55
Bounded, CheckedAdd, CheckedMul, CheckedSub, FromPrimitive, NumCast, SaturatingSub, Signed,
66
ToPrimitive,
77
};
8-
use serde::{Serialize, de::DeserializeOwned};
8+
//use serde::{Serialize, de::DeserializeOwned};
99
use simba::scalar::SupersetOf;
1010
use std::fmt::{Debug, Display};
1111
use std::hash::Hash;
@@ -63,8 +63,8 @@ pub trait Index:
6363
+ Display
6464
+ Pod
6565
+ ThreadSafe
66-
+ Serialize
67-
+ DeserializeOwned
66+
// + Serialize
67+
// + DeserializeOwned
6868
+ 'static
6969
{
7070
#[inline]
@@ -127,8 +127,8 @@ pub trait Real:
127127
+ Default
128128
+ Pod
129129
+ ThreadSafe
130-
+ Serialize
131-
+ DeserializeOwned
130+
// + Serialize
131+
// + DeserializeOwned
132132
{
133133
/// Converts the given float value to this Real type
134134
#[inline(always)]
@@ -177,8 +177,8 @@ impl<I> Index for I where
177177
+ Display
178178
+ Pod
179179
+ ThreadSafe
180-
+ Serialize
181-
+ DeserializeOwned
180+
// + Serialize
181+
// + DeserializeOwned
182182
+ 'static
183183
{
184184
}
@@ -194,8 +194,8 @@ impl<R> Real for R where
194194
+ Default
195195
+ Pod
196196
+ ThreadSafe
197-
+ Serialize
198-
+ DeserializeOwned
197+
// + Serialize
198+
// + DeserializeOwned
199199
+ 'static
200200
{
201201
}

splashsurf_lib/src/uniform_grid.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use itertools::iproduct;
77
use log::trace;
88
use nalgebra::{Scalar, Vector3};
99
use num_traits::Bounded;
10+
#[cfg(feature = "serde-serialize")]
1011
use serde_derive::{Deserialize, Serialize};
1112
use std::iter::Iterator;
1213
use thiserror::Error as ThisError;
@@ -126,7 +127,8 @@ pub type UniformGrid<I, R> = UniformCartesianCubeGrid3d<I, R>;
126127
/// obtained using the [`get_point`](UniformCartesianCubeGrid3d::get_point) and [`get_cell`](UniformCartesianCubeGrid3d::get_cell)
127128
/// functions respectively. These functions check if the specified indices are in the valid index range
128129
/// of the grid (as computed during construction based on the extents of the grid).
129-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
130+
#[derive(Clone, Debug, PartialEq)]
131+
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
130132
pub struct UniformCartesianCubeGrid3d<I: Scalar, R: Scalar> {
131133
/// AABB of the grid. Note that the grid may extend beyond the max coordinate of the AABB by less than the `cell_size`.
132134
aabb: Aabb3d<R>,

0 commit comments

Comments
 (0)