app: buffer_proto5: parallelize the geometry searches
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -163,6 +163,7 @@ dependencies = [
|
|||||||
"bincode",
|
"bincode",
|
||||||
"coremem",
|
"coremem",
|
||||||
"log",
|
"log",
|
||||||
|
"rayon",
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@@ -8,4 +8,5 @@ edition = "2021"
|
|||||||
bincode = "1.3" # MIT
|
bincode = "1.3" # MIT
|
||||||
coremem = { path = "../../coremem" }
|
coremem = { path = "../../coremem" }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
rayon = "1.5" # MIT or Apache 2.0
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
|
@@ -1,75 +1,215 @@
|
|||||||
use log::trace;
|
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
use std::sync::RwLock;
|
||||||
|
|
||||||
pub struct NoSupplier;
|
pub struct NoSupplier;
|
||||||
pub struct DiskCache<K, V, S=NoSupplier> {
|
pub type DiskCache<K, V, S=NoSupplier> = DiskCacheImpl<Entries<K, V>, S>;
|
||||||
|
pub type SyncDiskCache<K, V, S=NoSupplier> = DiskCacheImpl<SyncEntries<K, V>, S>;
|
||||||
|
|
||||||
|
pub struct DiskCacheImpl<E, S=NoSupplier> {
|
||||||
path: String,
|
path: String,
|
||||||
entries: Vec<(K, V)>,
|
entries: E,
|
||||||
supplier: S,
|
supplier: S,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: DeserializeOwned, V: DeserializeOwned> DiskCache<K, V, NoSupplier> {
|
impl<E: EntriesCap> DiskCacheImpl<E, NoSupplier>
|
||||||
|
where
|
||||||
|
E::Key: DeserializeOwned,
|
||||||
|
E::Value: DeserializeOwned,
|
||||||
|
{
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn new(path: &str) -> Self {
|
pub fn new(path: &str) -> Self {
|
||||||
Self::new_with_supplier(path, NoSupplier)
|
Self::new_with_supplier(path, NoSupplier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: DeserializeOwned, V: DeserializeOwned, S> DiskCache<K, V, S> {
|
impl<E: EntriesCap, S> DiskCacheImpl<E, S>
|
||||||
|
where
|
||||||
|
E::Key: DeserializeOwned,
|
||||||
|
E::Value: DeserializeOwned,
|
||||||
|
{
|
||||||
pub fn new_with_supplier(path: &str, supplier: S) -> Self {
|
pub fn new_with_supplier(path: &str, supplier: S) -> Self {
|
||||||
let entries = Self::load_from_disk(path).unwrap_or_default();
|
let entries = Self::load_from_disk(path).unwrap_or_default();
|
||||||
Self {
|
Self {
|
||||||
path: path.into(),
|
path: path.into(),
|
||||||
entries,
|
entries: E::from_vec(entries),
|
||||||
supplier,
|
supplier,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn load_from_disk(path: &str) -> Option<Vec<(K, V)>> {
|
fn load_from_disk(path: &str) -> Option<Vec<(E::Key, E::Value)>> {
|
||||||
let reader = std::io::BufReader::new(std::fs::File::open(path).ok()?);
|
let reader = std::io::BufReader::new(std::fs::File::open(path).ok()?);
|
||||||
bincode::deserialize_from(reader).ok()
|
bincode::deserialize_from(reader).ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: PartialEq, V, S> DiskCache<K, V, S> {
|
impl<E: EntriesCap, S> DiskCacheImpl<E, S>
|
||||||
pub fn get(&self, k: &K) -> Option<&V> {
|
where
|
||||||
self.entries.iter().find(|(comp_k, _v): &&(K, V)| comp_k == k).map(|(_k, v)| v)
|
E::Key: Serialize + Clone,
|
||||||
}
|
E::Value: Serialize + Clone,
|
||||||
}
|
{
|
||||||
|
|
||||||
|
|
||||||
impl<K: Serialize, V: Serialize, S> DiskCache<K, V, S> {
|
|
||||||
pub fn insert(&mut self, k: K, v: V) {
|
|
||||||
self.entries.push((k, v));
|
|
||||||
self.flush();
|
|
||||||
}
|
|
||||||
fn flush(&self) {
|
fn flush(&self) {
|
||||||
let writer = std::io::BufWriter::new(std::fs::File::create(&self.path).unwrap());
|
let writer = std::io::BufWriter::new(std::fs::File::create(&self.path).unwrap());
|
||||||
bincode::serialize_into(writer, &self.entries).unwrap();
|
bincode::serialize_into(writer, &self.entries.to_vec()).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: PartialEq + Serialize, V: Serialize + Clone, S> DiskCache<K, V, S> {
|
impl<E: EntriesCap, S> DiskCacheImpl<E, S>
|
||||||
|
where
|
||||||
|
E::Key: PartialEq,
|
||||||
|
E::Value: Clone,
|
||||||
|
{
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn get(&self, k: &E::Key) -> Option<E::Value> {
|
||||||
|
self.entries.get(k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// non-sync insert is mut, while sync is immute
|
||||||
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S> DiskCache<K, V, S> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
/// insert this k/v ONLY IF NOT PRESENT
|
||||||
|
pub fn insert(&mut self, k: K, v: V) {
|
||||||
|
self.entries.insert(k, v);
|
||||||
|
self.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S> SyncDiskCache<K, V, S> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
/// insert this k/v ONLY IF NOT PRESENT
|
||||||
|
pub fn insert(&self, k: K, v: V) {
|
||||||
|
self.entries.insert(k, v);
|
||||||
|
self.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// non-sync insert is mut, while sync is immute
|
||||||
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S> DiskCache<K, V, S> {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn get_or_insert_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> V {
|
pub fn get_or_insert_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> V {
|
||||||
if let Some(v) = self.get(&k) {
|
self.entries.get_or_insert_with(k, |_| f())
|
||||||
return v.clone();
|
}
|
||||||
}
|
}
|
||||||
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S> SyncDiskCache<K, V, S> {
|
||||||
let v = f();
|
#[allow(dead_code)]
|
||||||
self.insert(k, v.clone());
|
pub fn get_or_insert_with<F: FnOnce() -> V>(&self, k: K, f: F) -> V {
|
||||||
v
|
self.entries.get_or_insert_with(k, |_| f())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: PartialEq + Serialize, V: Serialize + Clone, S: FnMut(&K) -> V> DiskCache<K, V, S> {
|
// non-sync insert is mut, while sync is immute
|
||||||
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S: FnMut(&K) -> V> DiskCache<K, V, S> {
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn get_or_insert_from_supplier(&mut self, k: K) -> V {
|
pub fn get_or_insert_from_supplier(&mut self, k: K) -> V {
|
||||||
|
self.entries.get_or_insert_with(k, |k| (self.supplier)(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S: Fn(&K) -> V> SyncDiskCache<K, V, S> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn get_or_insert_from_supplier(&self, k: K) -> V {
|
||||||
|
self.entries.get_or_insert_with(k, |k| (self.supplier)(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------- disk cache entries ----------
|
||||||
|
// we have the non-sync and the sync K/V collections,
|
||||||
|
// which the DiskCacheImpl wraps.
|
||||||
|
|
||||||
|
pub struct Entries<K, V>(Vec<(K, V)>);
|
||||||
|
pub struct SyncEntries<K, V>(RwLock<Entries<K, V>>);
|
||||||
|
|
||||||
|
pub trait EntriesCap {
|
||||||
|
type Key;
|
||||||
|
type Value;
|
||||||
|
|
||||||
|
fn from_vec(v: Vec<(Self::Key, Self::Value)>) -> Self;
|
||||||
|
fn to_vec(&self) -> Vec<(Self::Key, Self::Value)>
|
||||||
|
where
|
||||||
|
Self::Key: Clone,
|
||||||
|
Self::Value: Clone;
|
||||||
|
fn get(&self, k: &Self::Key) -> Option<Self::Value>
|
||||||
|
where
|
||||||
|
Self::Key: PartialEq,
|
||||||
|
Self::Value: Clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<K, V> EntriesCap for Entries<K, V> {
|
||||||
|
type Key = K;
|
||||||
|
type Value = V;
|
||||||
|
fn from_vec(v: Vec<(K, V)>) -> Self {
|
||||||
|
Self(v)
|
||||||
|
}
|
||||||
|
fn to_vec(&self) -> Vec<(K, V)>
|
||||||
|
where
|
||||||
|
K: Clone,
|
||||||
|
V: Clone,
|
||||||
|
{
|
||||||
|
self.0.clone()
|
||||||
|
}
|
||||||
|
fn get(&self, k: &K) -> Option<V>
|
||||||
|
where
|
||||||
|
K: PartialEq,
|
||||||
|
V: Clone,
|
||||||
|
{
|
||||||
|
self.0.iter().find(|(comp_k, _v): &&(K, V)| comp_k == k).map(|(_k, v)| v.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<K, V> EntriesCap for SyncEntries<K, V> {
|
||||||
|
type Key = K;
|
||||||
|
type Value = V;
|
||||||
|
fn from_vec(v: Vec<(K, V)>) -> Self {
|
||||||
|
Self(RwLock::new(Entries::from_vec(v)))
|
||||||
|
}
|
||||||
|
fn to_vec(&self) -> Vec<(K, V)>
|
||||||
|
where
|
||||||
|
K: Clone,
|
||||||
|
V: Clone,
|
||||||
|
{
|
||||||
|
self.0.read().unwrap().to_vec()
|
||||||
|
}
|
||||||
|
fn get(&self, k: &K) -> Option<V>
|
||||||
|
where
|
||||||
|
K: PartialEq,
|
||||||
|
V: Clone,
|
||||||
|
{
|
||||||
|
self.0.read().unwrap().get(k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<K: PartialEq, V> Entries<K, V> {
|
||||||
|
fn insert(&mut self, k: K, v: V) {
|
||||||
|
|
||||||
|
if !self.0.iter().any(|(comp_k, _v): &(K, V)| comp_k == &k) {
|
||||||
|
self.0.push((k, v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<K: Clone + PartialEq, V: Clone> Entries<K, V> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn get_or_insert_with<F: FnOnce(&K) -> V>(&mut self, k: K, f: F) -> V {
|
||||||
if let Some(v) = self.get(&k) {
|
if let Some(v) = self.get(&k) {
|
||||||
trace!("get_or_insert_from_supplier hit");
|
|
||||||
return v.clone();
|
return v.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
trace!("get_or_insert_from_supplier miss");
|
let v = f(&k);
|
||||||
let v = (self.supplier)(&k);
|
self.insert(k, v.clone());
|
||||||
|
v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<K: PartialEq, V> SyncEntries<K, V> {
|
||||||
|
fn insert(&self, k: K, v: V) {
|
||||||
|
self.0.write().unwrap().insert(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<K: Clone + PartialEq, V: Clone> SyncEntries<K, V> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn get_or_insert_with<F: FnOnce(&K) -> V>(&self, k: K, f: F) -> V {
|
||||||
|
if let Some(v) = self.get(&k) {
|
||||||
|
return v.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
let v = f(&k);
|
||||||
self.insert(k, v.clone());
|
self.insert(k, v.clone());
|
||||||
v
|
v
|
||||||
}
|
}
|
||||||
|
@@ -24,10 +24,11 @@ use coremem::sim::spirv::{SpirvSim, WgpuBackend};
|
|||||||
use coremem::sim::units::{Seconds, Time as _};
|
use coremem::sim::units::{Seconds, Time as _};
|
||||||
use coremem::stim::{CurlVectorField, Exp, ModulatedVectorField, Sinusoid, TimeVaryingExt as _};
|
use coremem::stim::{CurlVectorField, Exp, ModulatedVectorField, Sinusoid, TimeVaryingExt as _};
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
|
use rayon::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
mod cache;
|
mod cache;
|
||||||
use cache::DiskCache;
|
use cache::SyncDiskCache;
|
||||||
|
|
||||||
type Mat = IsoConductorOr<f32, Ferroxcube3R1MH>;
|
type Mat = IsoConductorOr<f32, Ferroxcube3R1MH>;
|
||||||
|
|
||||||
@@ -673,7 +674,7 @@ fn main() {
|
|||||||
variants.len() / post_times.len(),
|
variants.len() / post_times.len(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut geom_cache = DiskCache::new_with_supplier(
|
let mut geom_cache = SyncDiskCache::new_with_supplier(
|
||||||
&format!("{}/.geom_cache", ensure_out_dir(i)),
|
&format!("{}/.geom_cache", ensure_out_dir(i)),
|
||||||
|geom: &GeomParams| derive_geometries(geom.clone())
|
|geom: &GeomParams| derive_geometries(geom.clone())
|
||||||
);
|
);
|
||||||
@@ -719,7 +720,7 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let wraps1_choices: Vec<_> = (-120..120)
|
let wraps1_choices: Vec<_> = (-120..120)
|
||||||
.into_iter()
|
.into_par_iter()
|
||||||
.filter_map(|wraps1| {
|
.filter_map(|wraps1| {
|
||||||
let params = GeomParams {
|
let params = GeomParams {
|
||||||
wraps1: (wraps1 * 4) as f32,
|
wraps1: (wraps1 * 4) as f32,
|
||||||
|
Reference in New Issue
Block a user