From 92bdbf33fae6d3626134284874905f877b3db37a Mon Sep 17 00:00:00 2001 From: mtb0x1 <39337159+mtb0x1@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:03:50 +0200 Subject: [PATCH] first attempt to fix #1, still failling need more digging --- {.cargo => port1/.cargo}/config.toml | 5 +++- port1/Cargo.lock | 45 ++++++++++++++++++++-------- port1/Cargo.toml | 3 +- port1/rust-toolchain.toml | 3 ++ port1/src/lib.rs | 28 +++++++---------- port1/www/index.html | 21 +++++++++---- port1/www/www_server.py | 17 +++++++++++ port2/.cargo/config.toml | 8 +++++ port2/Cargo.lock | 45 ++++++++++++++++++++-------- port2/Cargo.toml | 3 +- port2/rust-toolchain.toml | 3 ++ port2/src/lib.rs | 27 +++++++---------- port2/www/index.html | 12 +++++++- port2/www/www_server.py | 17 +++++++++++ 14 files changed, 168 insertions(+), 69 deletions(-) rename {.cargo => port1/.cargo}/config.toml (58%) create mode 100644 port1/rust-toolchain.toml create mode 100644 port1/www/www_server.py create mode 100644 port2/.cargo/config.toml create mode 100644 port2/rust-toolchain.toml create mode 100644 port2/www/www_server.py diff --git a/.cargo/config.toml b/port1/.cargo/config.toml similarity index 58% rename from .cargo/config.toml rename to port1/.cargo/config.toml index 2d3f8b3..5f3cfed 100644 --- a/.cargo/config.toml +++ b/port1/.cargo/config.toml @@ -2,4 +2,7 @@ target = "wasm32-unknown-unknown" [target.wasm32-unknown-unknown] -rustflags = ["-C","opt-level=3","-C", "target-feature=+relaxed-simd,+simd128"] \ No newline at end of file +rustflags = ["-C","opt-level=3","-C", "target-feature=+relaxed-simd,+simd128,+atomics,+bulk-memory,+mutable-globals"] + +[unstable] +build-std = ["panic_abort", "std"] \ No newline at end of file diff --git a/port1/Cargo.lock b/port1/Cargo.lock index 2c925eb..5e0a7fa 100644 --- a/port1/Cargo.lock +++ b/port1/Cargo.lock @@ -164,8 +164,9 @@ dependencies = [ "getrandom", "log", "rand", - "rayon-wasm", + "rayon", "wasm-bindgen", + "wasm-bindgen-rayon", "web-sys", ] @@ -224,25 +225,25 @@ dependencies = [ ] [[package]] -name = "rayon-core-wasm" -version = "1.10.2" +name = "rayon" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1760cc4f465e594a925a04de9acd1df83fb76cc1dea3cdb5f7131d79bb09d871" +checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" dependencies = [ - "crossbeam-channel", - "crossbeam-deque", - "crossbeam-utils", - "num_cpus", + "either", + "rayon-core", ] [[package]] -name = "rayon-wasm" -version = "1.6.2" +name = "rayon-core" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef9a7616186f65123ac92441fe683b45f738cc0a1125acdf6f76898213c4a43" +checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" dependencies = [ - "either", - "rayon-core-wasm", + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", ] [[package]] @@ -251,6 +252,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "spmc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02a8428da277a8e3a15271d79943e80ccc2ef254e78813a166a08d65e4c3ece5" + [[package]] name = "syn" version = "2.0.29" @@ -322,6 +329,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-rayon" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df87c67450805c305d3ae44a3ac537b0253d029153c25afc3ecd2edc36ccafb1" +dependencies = [ + "js-sys", + "rayon", + "spmc", + "wasm-bindgen", +] + [[package]] name = "wasm-bindgen-shared" version = "0.2.87" diff --git a/port1/Cargo.toml b/port1/Cargo.toml index 481936c..77f65d3 100644 --- a/port1/Cargo.toml +++ b/port1/Cargo.toml @@ -7,7 +7,8 @@ edition = "2021" [dependencies] rand = { version = "0.8.5", features = ["small_rng"] } -rayon-wasm = "1.6.2" #no point if we don't higher throughput +rayon = "1.7.0" +wasm-bindgen-rayon = "1.0" wasm-bindgen = "0.2" web-sys = {version="0.3.64",features=["Window","Performance","Navigator"]} diff --git a/port1/rust-toolchain.toml b/port1/rust-toolchain.toml new file mode 100644 index 0000000..e8d57c1 --- /dev/null +++ b/port1/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "nightly" +profile = "minimal" \ No newline at end of file diff --git a/port1/src/lib.rs b/port1/src/lib.rs index 80e3174..b3f2a74 100644 --- a/port1/src/lib.rs +++ b/port1/src/lib.rs @@ -1,17 +1,14 @@ use std::io::{Read, Seek, SeekFrom}; use std::mem; -use wasm_bindgen::prelude::*; use rand::rngs::SmallRng; use rand::{Rng, SeedableRng}; +use wasm_bindgen::prelude::wasm_bindgen; use std::io::{self, Write}; -use rayon_wasm::current_num_threads; -//use rayon_wasm::ThreadPoolBuilder; -use rayon_wasm::iter::IndexedParallelIterator; -use rayon_wasm::iter::IntoParallelIterator; -use rayon_wasm::iter::IntoParallelRefMutIterator; -use rayon_wasm::iter::ParallelIterator; +use rayon::current_num_threads; +use rayon::prelude::*; + const CONF_VALS: usize = 7; const CONF_SIZE: usize = std::mem::size_of::<[i32; CONF_VALS]>(); @@ -531,6 +528,10 @@ impl RunState { } } +// need to be called before main_wasm +// takes num_threads:usize, returns promise +pub use wasm_bindgen_rayon::init_thread_pool; + #[wasm_bindgen] pub fn main_wasm( model_buffer: Vec, //model_path @@ -547,16 +548,9 @@ pub fn main_wasm( .navigator() .hardware_concurrency() as usize; log::info!("--> [available 'CPUs' = {}]\n\n", cpus); - - //@todo ... - //value: ThreadPoolBuildError - //{ kind: IOError(Error { kind: Unsupported, message: "operation not supported on this platform" }) } - - /*let cpus_in_use = (cpus as f64*0.75) as usize; - ThreadPoolBuilder::new() - .num_threads(cpus_in_use) - .build_global() - .unwrap();*/ + + // we should be able to use 75% if hardware_concurrency is available, + // check init_thread_pool above let active_cpus = current_num_threads(); log::info!("--> [Running Inference on {} 'CPUs']\n\n", active_cpus); diff --git a/port1/www/index.html b/port1/www/index.html index c7164ad..4d8faf6 100644 --- a/port1/www/index.html +++ b/port1/www/index.html @@ -18,11 +18,11 @@ - - - - - + + + + +