app: buffer_proto5: parallelize the geometry searches

This commit is contained in:
2022-08-31 02:40:47 -07:00
parent a6fe05a993
commit 184997a701
4 changed files with 178 additions and 35 deletions

1
Cargo.lock generated
View File

@@ -163,6 +163,7 @@ dependencies = [
"bincode",
"coremem",
"log",
"rayon",
"serde",
]

View File

@@ -8,4 +8,5 @@ edition = "2021"
bincode = "1.3" # MIT
coremem = { path = "../../coremem" }
log = "0.4"
rayon = "1.5" # MIT or Apache 2.0
serde = "1.0"

View File

@@ -1,75 +1,215 @@
use log::trace;
use serde::{de::DeserializeOwned, Serialize};
use std::sync::RwLock;
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,
entries: Vec<(K, V)>,
entries: E,
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)]
pub fn new(path: &str) -> Self {
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 {
let entries = Self::load_from_disk(path).unwrap_or_default();
Self {
path: path.into(),
entries,
entries: E::from_vec(entries),
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()?);
bincode::deserialize_from(reader).ok()
}
}
impl<K: PartialEq, V, S> DiskCache<K, V, S> {
pub fn get(&self, k: &K) -> Option<&V> {
self.entries.iter().find(|(comp_k, _v): &&(K, V)| comp_k == k).map(|(_k, v)| v)
}
}
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();
}
impl<E: EntriesCap, S> DiskCacheImpl<E, S>
where
E::Key: Serialize + Clone,
E::Value: Serialize + Clone,
{
fn flush(&self) {
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)]
pub fn get_or_insert_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> V {
if let Some(v) = self.get(&k) {
return v.clone();
self.entries.get_or_insert_with(k, |_| f())
}
let v = f();
self.insert(k, v.clone());
v
}
impl<K: Serialize + Clone + PartialEq, V: Serialize + Clone, S> SyncDiskCache<K, V, S> {
#[allow(dead_code)]
pub fn get_or_insert_with<F: FnOnce() -> V>(&self, k: K, f: F) -> 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 {
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) {
trace!("get_or_insert_from_supplier hit");
return v.clone();
}
trace!("get_or_insert_from_supplier miss");
let v = (self.supplier)(&k);
let v = f(&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());
v
}

View File

@@ -24,10 +24,11 @@ use coremem::sim::spirv::{SpirvSim, WgpuBackend};
use coremem::sim::units::{Seconds, Time as _};
use coremem::stim::{CurlVectorField, Exp, ModulatedVectorField, Sinusoid, TimeVaryingExt as _};
use log::{error, info, warn};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
mod cache;
use cache::DiskCache;
use cache::SyncDiskCache;
type Mat = IsoConductorOr<f32, Ferroxcube3R1MH>;
@@ -673,7 +674,7 @@ fn main() {
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)),
|geom: &GeomParams| derive_geometries(geom.clone())
);
@@ -719,7 +720,7 @@ fn main() {
};
let wraps1_choices: Vec<_> = (-120..120)
.into_iter()
.into_par_iter()
.filter_map(|wraps1| {
let params = GeomParams {
wraps1: (wraps1 * 4) as f32,