Skip to content

Commit 8db1031

Browse files
committed
compile-ready wasm
1 parent edb0c20 commit 8db1031

17 files changed

+404
-134
lines changed

Cargo.lock

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+38-15
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,57 @@ license = "MIT"
1111
edition = "2021"
1212

1313
[dependencies]
14-
colored = "2.0"
1514
petgraph = "0.6.5"
16-
dialoguer = "0.11.0"
17-
rand = { version = "0.8.5", features = ["small_rng"] }
1815
bytes = "1.0"
19-
num_cpus = "1.16.0"
2016
log = "0.4.22"
21-
rayon = "1.10.0"
2217
byteorder = "1.5.0"
23-
indicatif = "0.17.8"
24-
simplelog = "0.12.2"
25-
tokio = { version = "1.0", features = ["full"] }
26-
tokio-postgres = "0.7"
2718
futures = "0.3"
28-
clap = { version = "4.0", features = ["derive"] }
29-
actix-web = "4.4"
30-
actix-cors = "0.6"
31-
serde = { version = "1.0", features = ["derive"] }
3219
serde_json = "1.0"
33-
env_logger = "0.11.6"
20+
rand = { version = "0.8.5", features = ["small_rng"] }
21+
colored = { version = "2.0", optional = true }
22+
dialoguer = { version = "0.11.0", optional = true }
23+
num_cpus = { version = "1.16.0", optional = true }
24+
rayon = { version = "1.10.0", optional = true }
25+
indicatif = { version = "0.17.8", optional = true }
26+
simplelog = { version = "0.12.2", optional = true }
27+
tokio = { version = "1.0", features = ["full"], optional = true }
28+
tokio-postgres = { version = "0.7", optional = true }
29+
clap = { version = "4.0", features = ["derive"], optional = true }
30+
actix-web = { version = "4.4", optional = true }
31+
actix-cors = { version = "0.6", optional = true }
32+
serde = { version = "1.0", features = ["derive"] }
33+
env_logger = { version = "0.11.6", optional = true }
34+
js-sys = "0.3"
35+
wasm-bindgen = "0.2"
36+
wasm-bindgen-futures = "0.4"
37+
console_error_panic_hook = "0.1.7"
38+
getrandom = { version = "0.2", features = ["js"] }
39+
40+
[lib]
41+
crate-type = ["cdylib", "rlib"]
3442

3543
[dev-dependencies]
3644
criterion = { version = "0.3", features = ["html_reports"] }
45+
wasm-bindgen-test = "0.3"
3746

3847
[[bench]]
3948
name = "benchmarks"
4049
harness = false
4150

4251
[features]
43-
default = []
52+
default = ["native"]
4453
shortdeck = []
54+
native = [
55+
"colored",
56+
"dialoguer",
57+
"num_cpus",
58+
"rayon",
59+
"indicatif",
60+
"simplelog",
61+
"tokio",
62+
"tokio-postgres",
63+
"clap",
64+
"actix-web",
65+
"actix-cors",
66+
"env_logger"
67+
]

src/clustering/layer.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use super::transitions::Decomp;
77
use crate::cards::isomorphism::Isomorphism;
88
use crate::cards::isomorphisms::IsomorphismIterator;
99
use crate::cards::street::Street;
10-
use crate::save::upload::Table;
1110
use crate::Energy;
1211
use rand::distributions::Distribution;
1312
use rand::distributions::WeightedIndex;
@@ -23,9 +22,11 @@ pub struct Layer {
2322
}
2423

2524
impl Layer {
25+
#[cfg(feature = "native")]
2626
/// all-in-one entry point for learning the kmeans abstraction and
2727
/// writing to disk in pgcopy
2828
pub fn learn() {
29+
use crate::save::upload::Table;
2930
Street::all()
3031
.into_iter()
3132
.rev()
@@ -43,6 +44,7 @@ impl Layer {
4344
&self.kmeans
4445
}
4546

47+
#[cfg(feature = "native")]
4648
/// primary clustering algorithm loop
4749
fn cluster(mut self) -> Self {
4850
log::info!("{:<32}{:<32}", "initialize kmeans", self.street());
@@ -63,6 +65,7 @@ impl Layer {
6365
self
6466
}
6567

68+
#[cfg(feature = "native")]
6669
/// initializes the centroids for k-means clustering using the k-means++ algorithm
6770
/// 1. choose 1st centroid randomly from the dataset
6871
/// 2. choose nth centroid with probability proportional to squared distance of nearest neighbors
@@ -75,17 +78,17 @@ impl Layer {
7578
use std::hash::DefaultHasher;
7679
use std::hash::Hash;
7780
use std::hash::Hasher;
78-
// deterministic pseudo-random clustering
79-
let ref mut hasher = DefaultHasher::default();
80-
self.street().hash(hasher);
81-
let ref mut rng = SmallRng::seed_from_u64(hasher.finish());
8281
// don't do any abstraction on preflop
8382
let k = self.street().k();
8483
let n = self.points().len();
8584
if self.street() == Street::Pref {
8685
assert!(n == k);
8786
return self.points().clone();
8887
}
88+
// deterministic pseudo-random clustering
89+
let ref mut hasher = DefaultHasher::default();
90+
self.street().hash(hasher);
91+
let ref mut rng = SmallRng::seed_from_u64(hasher.finish());
8992
// kmeans++ initialization
9093
let progress = crate::progress(k * n);
9194
let mut potentials = vec![1.; n];
@@ -116,6 +119,8 @@ impl Layer {
116119
println!();
117120
histograms
118121
}
122+
123+
#[cfg(feature = "native")]
119124
/// calculates the next step of the kmeans iteration by
120125
/// determining K * N optimal transport calculations and
121126
/// taking the nearest neighbor
@@ -192,8 +197,10 @@ impl Layer {
192197
}
193198
/// in ObsIterator order, get a mapping of
194199
/// Isomorphism -> Abstraction
200+
#[cfg(feature = "native")]
195201
fn lookup(&self) -> Lookup {
196202
log::info!("{:<32}{:<32}", "calculating lookup", self.street());
203+
use crate::save::upload::Table;
197204
use rayon::iter::IntoParallelRefIterator;
198205
use rayon::iter::ParallelIterator;
199206
let street = self.street();
@@ -226,7 +233,9 @@ impl Layer {
226233
.into()
227234
}
228235
}
229-
impl Table for Layer {
236+
237+
#[cfg(feature = "native")]
238+
impl crate::save::upload::Table for Layer {
230239
fn done(street: Street) -> bool {
231240
Lookup::done(street) && Decomp::done(street) && Metric::done(street)
232241
}

src/clustering/lookup.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use crate::cards::observation::Observation;
44
use crate::cards::street::Street;
55
use crate::clustering::abstraction::Abstraction;
66
use crate::clustering::histogram::Histogram;
7-
use crate::save::upload::Table;
8-
use rayon::iter::ParallelIterator;
97
use std::collections::BTreeMap;
108

119
#[derive(Default)]
@@ -14,6 +12,17 @@ use std::collections::BTreeMap;
1412
/// to precompute this mapping.
1513
pub struct Lookup(BTreeMap<Isomorphism, Abstraction>);
1614

15+
impl From<Lookup> for BTreeMap<Isomorphism, Abstraction> {
16+
fn from(lookup: Lookup) -> BTreeMap<Isomorphism, Abstraction> {
17+
lookup.0
18+
}
19+
}
20+
impl From<BTreeMap<Isomorphism, Abstraction>> for Lookup {
21+
fn from(map: BTreeMap<Isomorphism, Abstraction>) -> Self {
22+
Self(map)
23+
}
24+
}
25+
1726
impl Lookup {
1827
/// lookup the pre-computed abstraction for the outer observation
1928
pub fn lookup(&self, obs: &Observation) -> Abstraction {
@@ -22,9 +31,11 @@ impl Lookup {
2231
.cloned()
2332
.expect(&format!("precomputed abstraction missing for {obs}"))
2433
}
34+
#[cfg(feature = "native")]
2535
/// generate the entire space of inner layers
2636
pub fn projections(&self) -> Vec<Histogram> {
2737
use rayon::iter::IntoParallelIterator;
38+
use rayon::iter::ParallelIterator;
2839
IsomorphismIterator::from(self.street().prev())
2940
.collect::<Vec<Isomorphism>>()
3041
.into_par_iter()
@@ -45,13 +56,15 @@ impl Lookup {
4556
self.0.keys().next().expect("non empty").0.street()
4657
}
4758
}
59+
4860
#[cfg(test)]
4961
mod tests {
5062
use super::*;
5163

5264
#[ignore]
5365
#[test]
5466
fn persistence() {
67+
use crate::save::upload::Table;
5568
let street = Street::Pref;
5669
let lookup = Lookup::grow(street);
5770
lookup.save();
@@ -63,18 +76,8 @@ mod tests {
6376
}
6477
}
6578

66-
impl From<Lookup> for BTreeMap<Isomorphism, Abstraction> {
67-
fn from(lookup: Lookup) -> BTreeMap<Isomorphism, Abstraction> {
68-
lookup.0
69-
}
70-
}
71-
impl From<BTreeMap<Isomorphism, Abstraction>> for Lookup {
72-
fn from(map: BTreeMap<Isomorphism, Abstraction>) -> Self {
73-
Self(map)
74-
}
75-
}
76-
77-
impl Table for Lookup {
79+
#[cfg(feature = "native")]
80+
impl crate::save::upload::Table for Lookup {
7881
fn name() -> String {
7982
"isomorphism".to_string()
8083
}
@@ -135,6 +138,7 @@ impl Table for Lookup {
135138
/// abstractions for Preflop are cequivalent to just enumerating isomorphisms
136139
fn grow(street: Street) -> Self {
137140
use rayon::iter::IntoParallelIterator;
141+
use rayon::iter::ParallelIterator;
138142
match street {
139143
Street::Rive => IsomorphismIterator::from(Street::Rive)
140144
.collect::<Vec<_>>()

0 commit comments

Comments
 (0)